/// <summary> /// Gets the <see cref="GostECDsa"/> public key from the <see cref="X509Certificate2"/> /// certificate. /// </summary> /// <param name="certificate"> /// The certificate. /// </param> /// <returns> /// The public key, or <see langword="null"/> if the certificate does not have a /// <see cref="GostECDsa"/> public key. /// </returns> /// <exception cref="ArgumentNullException"> /// The <paramref name="certificate"/> parameter is <see langword="null"/>. /// </exception> /// <exception cref="CryptographicException"> /// The handle is invalid. /// </exception> public static GostECDsa?GetGostECDsaPublicKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException(nameof(certificate)); } if (!IsGostECDsa(certificate)) { return(null); } var publicKey = certificate.PublicKey; GostECDsa?result = publicKey.EncodedKeyValue.Oid.Value switch { CryptoConstants.GostECDsa256OidValue => GostECDsa256.Create(), CryptoConstants.GostECDsa512OidValue => GostECDsa512.Create(), _ => null }; if (result is not null) { try { var parameters = ReadParameters(publicKey); result.ImportParameters(parameters); } catch { result.Dispose(); throw; } } return(result); }
/// <summary> /// Gets the <see cref="GostECDsa"/> public key from the <see cref="X509Certificate2"/> /// certificate. /// </summary> /// <param name="certificate"> /// The certificate. /// </param> /// <returns> /// The public key, or <c>null</c> if the certificate does not have a /// <see cref="GostECDsa"/> public key. /// </returns> /// <exception cref="ArgumentNullException"> /// The <paramref name="certificate"/> parameter is <c>null</c>. /// </exception> /// <exception cref="CryptographicException"> /// The handle is invalid. /// </exception> public static GostECDsa GetECDsaPublicKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException(nameof(certificate)); } if (!IsGostECDsa(certificate)) { return(null); } PublicKey publicKey = certificate.PublicKey; GostECDsa result; switch (publicKey.EncodedKeyValue.Oid.Value) { case GostECDsa256OidValue: #if NET45 result = GostECDsa256.Create(); #elif NETCOREAPP1_0 result = new GostECDsa256Managed(); #endif break; case GostECDsa512OidValue: #if NET45 result = GostECDsa512.Create(); #elif NETCOREAPP1_0 result = new GostECDsa512Managed(); #endif break; default: return(null); } try { ECParameters parameters = ReadParameters(publicKey); result.ImportParameters(parameters); } catch { result.Dispose(); throw; } return(result); }