/// <summary> /// Attempts to export the current key in the ECPrivateKey format into a provided buffer. /// </summary> /// <param name="destination">The byte span to receive the ECPrivateKey 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="CryptographicException"> /// The key could not be exported. /// </exception> /// <exception cref="NotSupportedException"> /// A derived class has not provided an implementation for <see cref="ExportParameters" />. /// </exception> public virtual unsafe bool TryExportECPrivateKey(Span <byte> destination, out int bytesWritten) { ECParameters ecParameters = ExportParameters(true); fixed(byte *privPin = ecParameters.D) { try { AsnWriter writer = EccKeyFormatHelper.WriteECPrivateKey(ecParameters); return(writer.TryEncode(destination, out bytesWritten)); } finally { CryptographicOperations.ZeroMemory(ecParameters.D); } } }
/// <summary>Exports the current key in the ECPrivateKey format.</summary> /// <returns>A byte array containing the ECPrivateKey representation of this key.</returns> /// <exception cref="CryptographicException">The key could not be exported.</exception> public virtual unsafe byte[] ExportECPrivateKey() { ECParameters ecParameters = ExportParameters(true); fixed(byte *privPin = ecParameters.D) { try { AsnWriter writer = EccKeyFormatHelper.WriteECPrivateKey(ecParameters); return(writer.Encode()); } finally { CryptographicOperations.ZeroMemory(ecParameters.D); } } }
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 ImportLegacyPrivateKey(ref ECParameters parameters) { AsnWriter keyWriter = EccKeyFormatHelper.WriteECPrivateKey(parameters); 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), true)); } finally { CryptoPool.Return(rented, written); } }
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); } }