Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obsolete ToXmlString and FromXmlString on ECC types #67246

Merged
merged 2 commits into from
Mar 28, 2022
Merged
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
1 change: 1 addition & 0 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0039`__ | TLS versions 1.0 and 1.1 have known vulnerabilities and are not recommended. Use a newer TLS version instead, or use SslProtocols.None to defer to OS defaults. |
| __`SYSLIB0040`__ | EncryptionPolicy.NoEncryption and AllowEncryption significantly reduce security and should not be used in production code. |
| __`SYSLIB0041`__ | The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations. |
| __`SYSLIB0042`__ | ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys. |

## Analyzer Warnings

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ internal static class Obsoletions

internal const string Rfc2898OutdatedCtorMessage = "The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.";
internal const string Rfc2898OutdatedCtorDiagId = "SYSLIB0041";

internal const string EccXmlExportImportMessage = "ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.";
internal const string EccXmlExportImportDiagId = "SYSLIB0042";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ internal ECDiffieHellmanAndroidPublicKey(ECParameters parameters)
_key = new ECAndroid(parameters);
}

#pragma warning disable 0672 // Member overrides an obsolete member.
public override string ToXmlString()
#pragma warning restore 0672
{
throw new PlatformNotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ internal ECDiffieHellmanOpenSslPublicKey(ECParameters parameters)
_key = new ECOpenSsl(parameters);
}

#pragma warning disable 0672 // Member overrides an obsolete member.
public override string ToXmlString()
#pragma warning restore 0672
{
throw new PlatformNotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ public ECDiffieHellmanSecurityTransformsPublicKey(ECParameters ecParameters)
_ecc.ImportParameters(ecParameters);
}

#pragma warning disable 0672 // Member overrides an obsolete member.
public override string ToXmlString()
#pragma warning restore 0672
{
throw new PlatformNotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,12 +996,14 @@ protected override void Dispose(bool disposing) { }
public override byte[] ExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.Security.Cryptography.PbeParameters pbeParameters) { throw null; }
public override System.Security.Cryptography.ECParameters ExportExplicitParameters(bool includePrivateParameters) { throw null; }
public override System.Security.Cryptography.ECParameters ExportParameters(bool includePrivateParameters) { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public void FromXmlString(string xml, System.Security.Cryptography.ECKeyXmlFormat format) { }
public override void GenerateKey(System.Security.Cryptography.ECCurve curve) { }
public override void ImportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<byte> passwordBytes, System.ReadOnlySpan<byte> source, out int bytesRead) { throw null; }
public override void ImportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.ReadOnlySpan<byte> source, out int bytesRead) { throw null; }
public override void ImportParameters(System.Security.Cryptography.ECParameters parameters) { }
public override void ImportPkcs8PrivateKey(System.ReadOnlySpan<byte> source, out int bytesRead) { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public string ToXmlString(System.Security.Cryptography.ECKeyXmlFormat format) { 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; }
Expand All @@ -1016,8 +1018,10 @@ protected override void Dispose(bool disposing) { }
public override System.Security.Cryptography.ECParameters ExportParameters() { throw null; }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public static System.Security.Cryptography.ECDiffieHellmanPublicKey FromByteArray(byte[] publicKeyBlob, System.Security.Cryptography.CngKeyBlobFormat format) { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Security.Cryptography.ECDiffieHellmanCngPublicKey FromXmlString(string xml) { throw null; }
public System.Security.Cryptography.CngKey Import() { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override string ToXmlString() { throw null; }
}
public enum ECDiffieHellmanKeyDerivationFunction
Expand Down Expand Up @@ -1073,6 +1077,7 @@ protected virtual void Dispose(bool disposing) { }
public virtual System.Security.Cryptography.ECParameters ExportParameters() { throw null; }
public virtual byte[] ExportSubjectPublicKeyInfo() { throw null; }
public virtual byte[] ToByteArray() { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual string ToXmlString() { throw null; }
public virtual bool TryExportSubjectPublicKeyInfo(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand Down Expand Up @@ -1147,6 +1152,7 @@ protected override void Dispose(bool disposing) { }
public override byte[] ExportEncryptedPkcs8PrivateKey(System.ReadOnlySpan<char> password, System.Security.Cryptography.PbeParameters pbeParameters) { throw null; }
public override System.Security.Cryptography.ECParameters ExportExplicitParameters(bool includePrivateParameters) { throw null; }
public override System.Security.Cryptography.ECParameters ExportParameters(bool includePrivateParameters) { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public void FromXmlString(string xml, System.Security.Cryptography.ECKeyXmlFormat format) { }
public override void GenerateKey(System.Security.Cryptography.ECCurve curve) { }
protected override byte[] HashData(byte[] data, int offset, int count, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; }
Expand All @@ -1159,6 +1165,7 @@ public override void ImportParameters(System.Security.Cryptography.ECParameters
public byte[] SignData(byte[] data, int offset, int count) { throw null; }
public byte[] SignData(System.IO.Stream data) { throw null; }
public override byte[] SignHash(byte[] hash) { throw null; }
[System.ObsoleteAttribute("ToXmlString and FromXmlString have no implementation for ECC types, and are obsolete. Use a standard import and export format such as ExportSubjectPublicKeyInfo or ImportSubjectPublicKeyInfo for public keys and ExportPkcs8PrivateKey or ImportPkcs8PrivateKey for private keys.", DiagnosticId="SYSLIB0042", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public string ToXmlString(System.Security.Cryptography.ECKeyXmlFormat format) { 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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ public ECDiffieHellmanCng(ECCurve curve)
public SafeNCryptSecretHandle DeriveSecretAgreementHandle(CngKey otherPartyPublicKey) => null!;
public SafeNCryptSecretHandle DeriveSecretAgreementHandle(ECDiffieHellmanPublicKey otherPartyPublicKey) => null!;
public byte[] DeriveKeyMaterial(CngKey otherPartyPublicKey) => null!;
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public void FromXmlString(string xml, ECKeyXmlFormat format) { }
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public string ToXmlString(ECKeyXmlFormat format) => null!;
public override ECDiffieHellmanPublicKey PublicKey => null!;
}
Expand All @@ -275,11 +277,17 @@ public static ECDiffieHellmanPublicKey FromByteArray(byte[] publicKeyBlob, CngKe
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
}

// Also throws in Windows, no SupportedOSPlatform required.
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public static ECDiffieHellmanCngPublicKey FromXmlString(string xml)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
}

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public override string ToXmlString()
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
}
}
public sealed partial class ECDsaCng : ECDsa
{
Expand Down Expand Up @@ -309,10 +317,12 @@ public ECDsaCng(ECCurve curve)

public CngAlgorithm HashAlgorithm { get => null!; set { } }
public CngKey Key => null!;
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public void FromXmlString(string xml, ECKeyXmlFormat format) { }
public byte[] SignData(byte[] data) => null!;
public byte[] SignData(byte[] data, int offset, int count) => null!;
public byte[] SignData(System.IO.Stream data) => null!;
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public string ToXmlString(ECKeyXmlFormat format) => null!;
public bool VerifyData(byte[] data, byte[] signature) => false;
public bool VerifyData(byte[] data, int offset, int count, byte[] signature) => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace System.Security.Cryptography
{
public sealed partial class ECDiffieHellmanCng : ECDiffieHellman
{
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public void FromXmlString(string xml, ECKeyXmlFormat format)
{
throw new PlatformNotSupportedException();
}

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public string ToXmlString(ECKeyXmlFormat format)
{
throw new PlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public override string ToXmlString()
{
throw new PlatformNotSupportedException();
}

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public static ECDiffieHellmanCngPublicKey FromXmlString(string xml)
{
throw new PlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public virtual byte[] ToByteArray()
}

// This method must be implemented by derived classes. In order to conform to the contract, it cannot be abstract.
[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public virtual string ToXmlString()
{
throw new NotImplementedException(SR.NotSupported_SubclassOverride);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ protected override void Dispose(bool disposing)

public override byte[] ToByteArray() => _wrapped.ToByteArray();

#pragma warning disable 0672, SYSLIB0042 // Member overrides an obsolete member, ToXmlString is obsolete.
public override string ToXmlString() => _wrapped.ToXmlString();
#pragma warning restore 0672, SYSLIB0042

public override bool Equals(object? obj) => _wrapped.Equals(obj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private bool TryExportEncryptedPkcs8(
out bytesWritten);
}

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public void FromXmlString(string xml, ECKeyXmlFormat format)
=> throw new PlatformNotSupportedException();

Expand All @@ -139,6 +140,7 @@ public byte[] SignData(byte[] data, int offset, int count) =>
public byte[] SignData(Stream data)
=> SignData(data, new HashAlgorithmName(HashAlgorithm.Algorithm));

[Obsolete(Obsoletions.EccXmlExportImportMessage, DiagnosticId = Obsoletions.EccXmlExportImportDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public string ToXmlString(ECKeyXmlFormat format)
=> throw new PlatformNotSupportedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public void TestToXmlString()
byte[] arg = new byte[1] { 1 };
var pk = new TestDerived(arg);

#pragma warning disable SYSLIB0042 // ToXmlString and FromXmlString are obsolete
Assert.Throws<NotImplementedException>(() => pk.ToXmlString());
#pragma warning restore SYSLIB0042
}
}
}