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

Add the ability to load a PKCS10 into CertificateRequest #73023

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Formats.Asn1;
using Internal.Cryptography;

namespace System.Security.Cryptography.Asn1
{
internal partial struct PssParamsAsn
{
internal RSASignaturePadding GetSignaturePadding(
int? digestValueLength = null)
{
if (TrailerField != 1)
{
throw new CryptographicException(SR.Cryptography_Pkcs_InvalidSignatureParameters);
}

if (MaskGenAlgorithm.Algorithm != Oids.Mgf1)
{
throw new CryptographicException(
SR.Cryptography_Pkcs_PssParametersMgfNotSupported,
MaskGenAlgorithm.Algorithm);
}

if (MaskGenAlgorithm.Parameters == null)
{
throw new CryptographicException(SR.Cryptography_Pkcs_InvalidSignatureParameters);
}

AlgorithmIdentifierAsn mgfParams = AlgorithmIdentifierAsn.Decode(
MaskGenAlgorithm.Parameters.Value,
AsnEncodingRules.DER);

if (mgfParams.Algorithm != HashAlgorithm.Algorithm)
{
throw new CryptographicException(
SR.Format(
SR.Cryptography_Pkcs_PssParametersMgfHashMismatch,
mgfParams.Algorithm,
HashAlgorithm.Algorithm));
}

int saltSize = digestValueLength.GetValueOrDefault();

if (!digestValueLength.HasValue)
{
saltSize = Helpers.HashOidToByteLength(HashAlgorithm.Algorithm);
}
Comment on lines +44 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int saltSize = digestValueLength.GetValueOrDefault();
if (!digestValueLength.HasValue)
{
saltSize = Helpers.HashOidToByteLength(HashAlgorithm.Algorithm);
}
int saltSize = digestValueLength ?? Helpers.HashOidToByteLength(HashAlgorithm.Algorithm);


if (SaltLength != saltSize)
{
throw new CryptographicException(
SR.Format(
SR.Cryptography_Pkcs_PssParametersSaltMismatch,
SaltLength,
HashAlgorithm.Algorithm));
}

// When RSASignaturePadding supports custom salt sizes this return will look different.
return RSASignaturePadding.Pss;
}
}
}
14 changes: 14 additions & 0 deletions src/libraries/Common/src/System/Security/Cryptography/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,19 @@ internal static bool TryCopyToDestination(this ReadOnlySpan<byte> source, Span<b
bytesWritten = 0;
return false;
}

internal static int HashOidToByteLength(string hashOid)
{
// This file is compiled in netstandard2.0, can't use the HashSizeInBytes consts.
return hashOid switch
{
Oids.Sha256 => 256 >> 3,
Oids.Sha384 => 384 >> 3,
Oids.Sha512 => 512 >> 3,
Oids.Sha1 => 160 >> 3,
Oids.Md5 => 128 >> 3,
_ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashOid)),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ internal static class PemLabels
internal const string X509Certificate = "CERTIFICATE";
internal const string Pkcs7Certificate = "PKCS7";
internal const string X509CertificateRevocationList = "X509 CRL";
internal const string Pkcs10CertificateRequest = "CERTIFICATE REQUEST";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ System.Security.Cryptography.Pkcs.EnvelopedCms</PackageDescription>
<AsnXml Include="$(CommonPath)System\Security\Cryptography\Asn1\PssParamsAsn.xml">
<Link>Common\System\Security\Cryptography\Asn1\PssParamsAsn.xml</Link>
</AsnXml>
<Compile Include="$(CommonPath)System\Security\Cryptography\Asn1\PssParamsAsn.manual.cs">
<Link>Common\System\Security\Cryptography\Asn1\PssParamsAsn.manual.cs</Link>
<DependentUpon>Common\System\Security\Cryptography\Asn1\PssParamsAsn.xml</DependentUpon>
</Compile>
<Compile Include="$(CommonPath)System\Security\Cryptography\Asn1\PssParamsAsn.xml.cs">
<Link>Common\System\Security\Cryptography\Asn1\PssParamsAsn.xml.cs</Link>
<DependentUpon>Common\System\Security\Cryptography\Asn1\PssParamsAsn.xml</DependentUpon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,47 +309,8 @@ protected override RSASignaturePadding GetSignaturePadding(
digestAlgorithmOid));
}

if (pssParams.TrailerField != 1)
{
throw new CryptographicException(SR.Cryptography_Pkcs_InvalidSignatureParameters);
}

if (pssParams.SaltLength != digestValueLength)
{
throw new CryptographicException(
SR.Format(
SR.Cryptography_Pkcs_PssParametersSaltMismatch,
pssParams.SaltLength,
digestAlgorithmName.Name));
}

if (pssParams.MaskGenAlgorithm.Algorithm != Oids.Mgf1)
{
throw new CryptographicException(
SR.Cryptography_Pkcs_PssParametersMgfNotSupported,
pssParams.MaskGenAlgorithm.Algorithm);
}

if (pssParams.MaskGenAlgorithm.Parameters == null)
{
throw new CryptographicException(SR.Cryptography_Pkcs_InvalidSignatureParameters);
}

AlgorithmIdentifierAsn mgfParams = AlgorithmIdentifierAsn.Decode(
pssParams.MaskGenAlgorithm.Parameters.Value,
AsnEncodingRules.DER);

if (mgfParams.Algorithm != digestAlgorithmOid)
{
throw new CryptographicException(
SR.Format(
SR.Cryptography_Pkcs_PssParametersMgfHashMismatch,
mgfParams.Algorithm,
digestAlgorithmOid));
}

// When RSASignaturePadding supports custom salt sizes this return will look different.
return RSASignaturePadding.Pss;
RSASignaturePadding padding = pssParams.GetSignaturePadding(digestValueLength);
return padding;
}

protected override bool Sign(
Expand Down
Loading