/// <summary> /// Exports the current key in the X.509 SubjectPublicKeyInfo format. /// </summary> /// <returns> /// A byte array containing the X.509 SubjectPublicKeyInfo representation of this key. /// </returns> /// <exception cref="NotSupportedException"> /// The member <see cref="ExportParameters" /> has not been overridden in a derived class. /// </exception> /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception> /// <exception cref="CryptographicException">The key is invalid and could not be exported.</exception> public virtual byte[] ExportSubjectPublicKeyInfo() { ECParameters ecParameters = ExportParameters(); AsnWriter writer = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(ecParameters); return(writer.Encode()); }
/// <summary> /// Attempts to export the current key in the X.509 SubjectPublicKeyInfo format. /// </summary> /// <param name="destination">The byte span to receive the X.509 SubjectPublicKeyInfo data.</param> /// <param name="bytesWritten"> /// When this method returns, contains a value that indicates the number of bytes written to <paramref name="destination" />. /// This parameter is treated as uninitialized. /// </param> /// <returns> /// <see langword="true"/> if <paramref name="destination"/> is big enough to receive the output; /// otherwise, <see langword="false"/>. /// </returns> /// <exception cref="NotSupportedException"> /// The member <see cref="ExportParameters" /> has not been overridden in a derived class. /// </exception> /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception> /// <exception cref="CryptographicException">The key is invalid and could not be exported.</exception> public virtual bool TryExportSubjectPublicKeyInfo(Span <byte> destination, out int bytesWritten) { ECParameters ecParameters = ExportParameters(); AsnWriter writer = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(ecParameters); return(writer.TryEncode(destination, out bytesWritten)); }
public override bool TryExportSubjectPublicKeyInfo( Span <byte> destination, out int bytesWritten) { ECParameters ecParameters = ExportParameters(false); using (AsnWriter writer = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(ecParameters)) { return(writer.TryEncode(destination, out bytesWritten)); } }
private static SafeSecKeyRefHandle ImportKey(ECParameters parameters) { if (parameters.D != null) { using (AsnWriter privateKey = EccKeyFormatHelper.WriteECPrivateKey(parameters)) { return(Interop.AppleCrypto.ImportEphemeralKey(privateKey.EncodeAsSpan(), true)); } } else { using (AsnWriter publicKey = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters)) { return(Interop.AppleCrypto.ImportEphemeralKey(publicKey.EncodeAsSpan(), false)); } } }
private static SafeSecKeyRefHandle ImportKey(ECParameters parameters) { bool isPrivateKey = parameters.D != null; byte[] blob; if (isPrivateKey) { using (AsnWriter privateKey = EccKeyFormatHelper.WriteECPrivateKey(parameters)) { blob = privateKey.Encode(); } } else { using (AsnWriter publicKey = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters)) { blob = publicKey.Encode(); } } return(Interop.AppleCrypto.ImportEphemeralKey(blob, isPrivateKey)); }
private static SafeSecKeyRefHandle ImportKey(ECParameters parameters) { AsnWriter keyWriter; bool hasPrivateKey; if (parameters.D != null) { keyWriter = EccKeyFormatHelper.WriteECPrivateKey(parameters); hasPrivateKey = true; } else { keyWriter = EccKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters); hasPrivateKey = false; } byte[] rented = CryptoPool.Rent(keyWriter.GetEncodedLength()); if (!keyWriter.TryEncode(rented, out int written)) { Debug.Fail("TryEncode failed with a pre-allocated buffer"); throw new InvalidOperationException(); } // Explicitly clear the inner buffer keyWriter.Reset(); try { return(Interop.AppleCrypto.ImportEphemeralKey(rented.AsSpan(0, written), hasPrivateKey)); } finally { CryptoPool.Return(rented, written); } }