Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ protected ECDiffieHellman() { }
public virtual byte[] DeriveKeyMaterial(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey) { throw null; }
public virtual byte[] DeriveKeyTls(System.Security.Cryptography.ECDiffieHellmanPublicKey otherPartyPublicKey, byte[] prfLabel, byte[] prfSeed) { throw null; }
public virtual byte[] ExportECPrivateKey() { throw null; }
public string ExportECPrivateKeyPem() { throw null; }
public virtual System.Security.Cryptography.ECParameters ExportExplicitParameters(bool includePrivateParameters) { throw null; }
public virtual System.Security.Cryptography.ECParameters ExportParameters(bool includePrivateParameters) { throw null; }
public override void FromXmlString(string xmlString) { }
Expand All @@ -337,6 +338,7 @@ public virtual void ImportParameters(System.Security.Cryptography.ECParameters p
public override void ImportSubjectPublicKeyInfo(System.ReadOnlySpan<byte> source, out int bytesRead) { throw null; }
public override string ToXmlString(bool includePrivateParameters) { throw null; }
public virtual bool TryExportECPrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
public bool TryExportECPrivateKeyPem(System.Span<char> destination, out int charsWritten) { throw null; }
public override bool TryExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<byte> passwordBytes, System.Security.Cryptography.PbeParameters pbeParameters, System.Span<byte> destination, out int bytesWritten) { throw null; }
public override bool TryExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.Security.Cryptography.PbeParameters pbeParameters, System.Span<byte> destination, out int bytesWritten) { throw null; }
public override bool TryExportPkcs8PrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
Expand Down Expand Up @@ -367,6 +369,7 @@ protected ECDsa() { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static new System.Security.Cryptography.ECDsa? Create(string algorithm) { throw null; }
public virtual byte[] ExportECPrivateKey() { throw null; }
public string ExportECPrivateKeyPem() { throw null; }
public virtual System.Security.Cryptography.ECParameters ExportExplicitParameters(bool includePrivateParameters) { throw null; }
public virtual System.Security.Cryptography.ECParameters ExportParameters(bool includePrivateParameters) { throw null; }
public override void FromXmlString(string xmlString) { }
Expand Down Expand Up @@ -396,6 +399,7 @@ public virtual void ImportParameters(System.Security.Cryptography.ECParameters p
protected virtual byte[] SignHashCore(System.ReadOnlySpan<byte> hash, System.Security.Cryptography.DSASignatureFormat signatureFormat) { throw null; }
public override string ToXmlString(bool includePrivateParameters) { throw null; }
public virtual bool TryExportECPrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
public bool TryExportECPrivateKeyPem(System.Span<char> destination, out int charsWritten) { throw null; }
public override bool TryExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<byte> passwordBytes, System.Security.Cryptography.PbeParameters pbeParameters, System.Span<byte> destination, out int bytesWritten) { throw null; }
public override bool TryExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.Security.Cryptography.PbeParameters pbeParameters, System.Span<byte> destination, out int bytesWritten) { throw null; }
public override bool TryExportPkcs8PrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
Expand Down Expand Up @@ -692,7 +696,9 @@ protected RSA() { }
public virtual byte[] EncryptValue(byte[] rgb) { throw null; }
public abstract System.Security.Cryptography.RSAParameters ExportParameters(bool includePrivateParameters);
public virtual byte[] ExportRSAPrivateKey() { throw null; }
public string ExportRSAPrivateKeyPem() { throw null; }
public virtual byte[] ExportRSAPublicKey() { throw null; }
public string ExportRSAPublicKeyPem() { throw null; }
public override void FromXmlString(string xmlString) { }
protected virtual byte[] HashData(byte[] data, int offset, int count, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; }
protected virtual byte[] HashData(System.IO.Stream data, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; }
Expand All @@ -717,7 +723,9 @@ public override void ImportFromPem(System.ReadOnlySpan<char> input) { }
public override bool TryExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.Security.Cryptography.PbeParameters pbeParameters, System.Span<byte> destination, out int bytesWritten) { throw null; }
public override bool TryExportPkcs8PrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
public virtual bool TryExportRSAPrivateKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
public bool TryExportRSAPrivateKeyPem(System.Span<char> destination, out int charsWritten) { throw null; }
public virtual bool TryExportRSAPublicKey(System.Span<byte> destination, out int bytesWritten) { throw null; }
public bool TryExportRSAPublicKeyPem(System.Span<char> destination, out int charsWritten) { throw null; }
public override bool TryExportSubjectPublicKeyInfo(System.Span<byte> destination, out int bytesWritten) { throw null; }
protected virtual bool TryHashData(System.ReadOnlySpan<byte> data, System.Span<byte> destination, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, out int bytesWritten) { throw null; }
public virtual bool TrySignData(System.ReadOnlySpan<byte> data, System.Span<byte> destination, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.RSASignaturePadding padding, out int bytesWritten) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,5 +631,87 @@ public override void ImportFromEncryptedPem(ReadOnlySpan<char> input, ReadOnlySp
// override remains for compatibility.
base.ImportFromEncryptedPem(input, passwordBytes);
}

/// <summary>
/// Exports the current key in the ECPrivateKey format, PEM encoded.
/// </summary>
/// <returns>A string containing the PEM-encoded ECPrivateKey.</returns>
/// <exception cref="CryptographicException">
/// The key could not be exported.
/// </exception>
/// <remarks>
/// <p>
/// A PEM-encoded ECPrivateKey will begin with <c>-----BEGIN EC PRIVATE KEY-----</c>
/// and end with <c>-----END EC PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public unsafe string ExportECPrivateKeyPem()
{
byte[] exported = ExportECPrivateKey();

// Fixed to prevent GC moves.
fixed (byte* pExported = exported)
{
try
{
return PemKeyHelpers.CreatePemFromData(PemLabels.EcPrivateKey, exported);
}
finally
{
CryptographicOperations.ZeroMemory(exported);
}
}
}

/// <summary>
/// Attempts to export the current key in the PEM-encoded
/// ECPrivateKey format into a provided buffer.
/// </summary>
/// <param name="destination">
/// The character span to receive the PEM-encoded ECPrivateKey data.
/// </param>
/// <param name="charsWritten">
/// When this method returns, contains a value that indicates the number
/// of characters 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>
/// <remarks>
/// <p>
/// A PEM-encoded ECPrivateKey will begin with
/// <c>-----BEGIN EC PRIVATE KEY-----</c> and end with
/// <c>-----END EC PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public bool TryExportECPrivateKeyPem(Span<char> destination, out int charsWritten)
{
static bool Export(ECDiffieHellman alg, Span<byte> destination, out int bytesWritten)
{
return alg.TryExportECPrivateKey(destination, out bytesWritten);
}

return PemKeyHelpers.TryExportToPem(
this,
PemLabels.EcPrivateKey,
Export,
destination,
out charsWritten);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1495,5 +1495,87 @@ public override void ImportFromEncryptedPem(ReadOnlySpan<char> input, ReadOnlySp
// override remains for compatibility.
base.ImportFromEncryptedPem(input, passwordBytes);
}

/// <summary>
/// Exports the current key in the ECPrivateKey format, PEM encoded.
/// </summary>
/// <returns>A string containing the PEM-encoded ECPrivateKey.</returns>
/// <exception cref="CryptographicException">
/// The key could not be exported.
/// </exception>
/// <remarks>
/// <p>
/// A PEM-encoded ECPrivateKey will begin with <c>-----BEGIN EC PRIVATE KEY-----</c>
/// and end with <c>-----END EC PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public unsafe string ExportECPrivateKeyPem()
{
byte[] exported = ExportECPrivateKey();

// Fixed to prevent GC moves.
fixed (byte* pExported = exported)
{
try
{
return PemKeyHelpers.CreatePemFromData(PemLabels.EcPrivateKey, exported);
}
finally
{
CryptographicOperations.ZeroMemory(exported);
}
}
}

/// <summary>
/// Attempts to export the current key in the PEM-encoded
/// ECPrivateKey format into a provided buffer.
/// </summary>
/// <param name="destination">
/// The character span to receive the PEM-encoded ECPrivateKey data.
/// </param>
/// <param name="charsWritten">
/// When this method returns, contains a value that indicates the number
/// of characters 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>
/// <remarks>
/// <p>
/// A PEM-encoded ECPrivateKey will begin with
/// <c>-----BEGIN EC PRIVATE KEY-----</c> and end with
/// <c>-----END EC PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public bool TryExportECPrivateKeyPem(Span<char> destination, out int charsWritten)
{
static bool Export(ECDsa alg, Span<byte> destination, out int bytesWritten)
{
return alg.TryExportECPrivateKey(destination, out bytesWritten);
}

return PemKeyHelpers.TryExportToPem(
this,
PemLabels.EcPrivateKey,
Export,
destination,
out charsWritten);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,159 @@ public override void ImportFromEncryptedPem(ReadOnlySpan<char> input, ReadOnlySp
base.ImportFromEncryptedPem(input, passwordBytes);
}

/// <summary>
/// Exports the current key in the PKCS#1 RSAPrivateKey format, PEM encoded.
/// </summary>
/// <returns>A string containing the PEM-encoded PKCS#1 RSAPrivateKey.</returns>
/// <exception cref="CryptographicException">
/// The key could not be exported.
/// </exception>
/// <remarks>
/// <p>
/// A PEM-encoded PKCS#1 RSAPrivateKey will begin with <c>-----BEGIN RSA PRIVATE KEY-----</c>
/// and end with <c>-----END RSA PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public unsafe string ExportRSAPrivateKeyPem()
{
byte[] exported = ExportRSAPrivateKey();

// Fixed to prevent GC moves.
fixed (byte* pExported = exported)
{
try
{
return PemKeyHelpers.CreatePemFromData(PemLabels.RsaPrivateKey, exported);
}
finally
{
CryptographicOperations.ZeroMemory(exported);
}
}
}

/// <summary>
/// Exports the public-key portion of the current key in the PKCS#1
/// RSAPublicKey format, PEM encoded.
/// </summary>
/// <returns>A string containing the PEM-encoded PKCS#1 RSAPublicKey.</returns>
/// <exception cref="CryptographicException">
/// The key could not be exported.
/// </exception>
/// <remarks>
/// <p>
/// A PEM-encoded PKCS#1 RSAPublicKey will begin with <c>-----BEGIN RSA PUBLIC KEY-----</c>
/// and end with <c>-----END RSA PUBLIC KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public string ExportRSAPublicKeyPem()
{
byte[] exported = ExportRSAPublicKey();
return PemKeyHelpers.CreatePemFromData(PemLabels.RsaPublicKey, exported);
}

/// <summary>
/// Attempts to export the current key in the PEM-encoded PKCS#1
/// RSAPrivateKey format into a provided buffer.
/// </summary>
/// <param name="destination">
/// The character span to receive the PEM-encoded PKCS#1 RSAPrivateKey data.
/// </param>
/// <param name="charsWritten">
/// When this method returns, contains a value that indicates the number
/// of characters 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>
/// <remarks>
/// <p>
/// A PEM-encoded PKCS#1 RSAPrivateKey will begin with
/// <c>-----BEGIN RSA PRIVATE KEY-----</c> and end with
/// <c>-----END RSA PRIVATE KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public bool TryExportRSAPrivateKeyPem(Span<char> destination, out int charsWritten)
{
static bool Export(RSA alg, Span<byte> destination, out int bytesWritten)
{
return alg.TryExportRSAPrivateKey(destination, out bytesWritten);
}

return PemKeyHelpers.TryExportToPem(
this,
PemLabels.RsaPrivateKey,
Export,
destination,
out charsWritten);
}

/// <summary>
/// Attempts to export the current key in the PEM-encoded PKCS#1
/// RSAPublicKey format into a provided buffer.
/// </summary>
/// <param name="destination">
/// The character span to receive the PEM-encoded PKCS#1 RSAPublicKey data.
/// </param>
/// <param name="charsWritten">
/// When this method returns, contains a value that indicates the number
/// of characters 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>
/// <remarks>
/// <p>
/// A PEM-encoded PKCS#1 RSAPublicKey will begin with
/// <c>-----BEGIN RSA PUBLIC KEY-----</c> and end with
/// <c>-----END RSA PUBLIC KEY-----</c>, with the base64 encoded DER
/// contents of the key between the PEM boundaries.
/// </p>
/// <p>
/// The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
/// encoding rules.
/// </p>
/// </remarks>
public bool TryExportRSAPublicKeyPem(Span<char> destination, out int charsWritten)
{
static bool Export(RSA alg, Span<byte> destination, out int bytesWritten)
{
return alg.TryExportRSAPublicKey(destination, out bytesWritten);
}

return PemKeyHelpers.TryExportToPem(
this,
PemLabels.RsaPublicKey,
Export,
destination,
out charsWritten);
}

private static void ClearPrivateParameters(in RSAParameters rsaParameters)
{
CryptographicOperations.ZeroMemory(rsaParameters.D);
Expand Down
Loading