dotnet / dotnet/core

Setting private key on x509 certificate always throws PlatformNotSupportedException

Open
#4,723 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
22k
Forks
4.9k
Avg merge
5d 5h
Merged PRs (30d)
29

Description

Looking at this code in X509Certificate2.cs:

`public AsymmetricAlgorithm PrivateKey
{
get
{
this.ThrowIfInvalid();
if (!this.HasPrivateKey)
return (AsymmetricAlgorithm) null;
if (this._lazyPrivateKey == null)
{
switch (this.GetKeyAlgorithm())
{
case "1.2.840.113549.1.1.1":
this._lazyPrivateKey = (AsymmetricAlgorithm) this.Pal.GetRSAPrivateKey();
break;
case "1.2.840.10040.4.1":
this._lazyPrivateKey = (AsymmetricAlgorithm) this.Pal.GetDSAPrivateKey();
break;
default:
throw new NotSupportedException(SR.NotSupported_KeyAlgorithm);
}
}
return this._lazyPrivateKey;
}
set
{
throw new PlatformNotSupportedException();
}
}`

It would appear that trying to set the private key of a cert will always throw an exception, regardless of platform, despite the documentation and tooltip stating that the private key can be set via this property.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.