Skip to content

Commit c59b517

Browse files
authored
Add X509AuthorityKeyIdentifierExtension
Additionally fixes the recently added SubjectAltNames extension to use a shared OID instance.
1 parent 5a073b2 commit c59b517

File tree

15 files changed

+1441
-44
lines changed

15 files changed

+1441
-44
lines changed

src/libraries/Common/src/System/Security/Cryptography/Oids.Shared.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ internal static partial class Oids
2727
private static volatile Oid? s_basicConstraints2Oid;
2828
private static volatile Oid? s_enhancedKeyUsageOid;
2929
private static volatile Oid? s_keyUsageOid;
30+
private static volatile Oid? s_subjectAltNameOid;
3031
private static volatile Oid? s_subjectKeyIdentifierOid;
32+
private static volatile Oid? s_authorityKeyIdentifierOid;
3133
private static volatile Oid? s_authorityInformationAccessOid;
3234
private static volatile Oid? s_commonNameOid;
3335
private static volatile Oid? s_countryOrRegionOid;
@@ -58,7 +60,9 @@ internal static partial class Oids
5860
internal static Oid BasicConstraints2Oid => s_basicConstraints2Oid ??= InitializeOid(BasicConstraints2);
5961
internal static Oid EnhancedKeyUsageOid => s_enhancedKeyUsageOid ??= InitializeOid(EnhancedKeyUsage);
6062
internal static Oid KeyUsageOid => s_keyUsageOid ??= InitializeOid(KeyUsage);
63+
internal static Oid AuthorityKeyIdentifierOid => s_authorityKeyIdentifierOid ??= InitializeOid(AuthorityKeyIdentifier);
6164
internal static Oid SubjectKeyIdentifierOid => s_subjectKeyIdentifierOid ??= InitializeOid(SubjectKeyIdentifier);
65+
internal static Oid SubjectAltNameOid => s_subjectAltNameOid ??= InitializeOid(SubjectAltName);
6266
internal static Oid AuthorityInformationAccessOid => s_authorityInformationAccessOid ??= InitializeOid(AuthorityInformationAccess);
6367

6468
internal static Oid CommonNameOid => s_commonNameOid ??= InitializeOid(CommonName);

src/libraries/Common/src/System/Security/Cryptography/Oids.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ internal static partial class Oids
110110
internal const string CertPolicies = "2.5.29.32";
111111
internal const string AnyCertPolicy = "2.5.29.32.0";
112112
internal const string CertPolicyMappings = "2.5.29.33";
113+
internal const string AuthorityKeyIdentifier = "2.5.29.35";
113114
internal const string CertPolicyConstraints = "2.5.29.36";
114115
internal const string EnhancedKeyUsage = "2.5.29.37";
115116
internal const string InhibitAnyPolicyExtension = "2.5.29.54";

src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ internal sealed class CertificateAuthority : IDisposable
4242
private static readonly Asn1Tag s_context0 = new Asn1Tag(TagClass.ContextSpecific, 0);
4343
private static readonly Asn1Tag s_context1 = new Asn1Tag(TagClass.ContextSpecific, 1);
4444
private static readonly Asn1Tag s_context2 = new Asn1Tag(TagClass.ContextSpecific, 2);
45-
private static readonly Asn1Tag s_context4 = new Asn1Tag(TagClass.ContextSpecific, 4);
4645

4746
private static readonly X500DistinguishedName s_nonParticipatingName =
4847
new X500DistinguishedName("CN=The Ghost in the Machine");
@@ -738,47 +737,15 @@ private X509Extension CreateAkidExtension()
738737
X509SubjectKeyIdentifierExtension skid =
739738
_cert.Extensions.OfType<X509SubjectKeyIdentifierExtension>().SingleOrDefault();
740739

741-
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
742-
743-
// AuthorityKeyIdentifier
744-
using (writer.PushSequence())
740+
if (skid is null)
745741
{
746-
if (skid == null)
747-
{
748-
// authorityCertIssuer [1] GeneralNames (SEQUENCE OF)
749-
using (writer.PushSequence(s_context1))
750-
{
751-
// directoryName [4] Name
752-
byte[] dn = _cert.SubjectName.RawData;
753-
754-
if (s_context4.Encode(dn) != 1)
755-
{
756-
throw new InvalidOperationException();
757-
}
758-
759-
writer.WriteEncodedValue(dn);
760-
}
761-
762-
// authorityCertSerialNumber [2] CertificateSerialNumber (INTEGER)
763-
writer.WriteInteger(_cert.SerialNumberBytes.Span, s_context2);
764-
}
765-
else
766-
{
767-
// keyIdentifier [0] KeyIdentifier (OCTET STRING)
768-
AsnReader reader = new AsnReader(skid.RawData, AsnEncodingRules.BER);
769-
ReadOnlyMemory<byte> contents;
770-
771-
if (!reader.TryReadPrimitiveOctetString(out contents))
772-
{
773-
throw new InvalidOperationException();
774-
}
775-
776-
reader.ThrowIfNotEmpty();
777-
writer.WriteOctetString(contents.Span, s_context0);
778-
}
742+
return X509AuthorityKeyIdentifierExtension.CreateFromCertificate(
743+
_cert,
744+
includeKeyIdentifier: false,
745+
includeIssuerAndSerial: true);
779746
}
780747

781-
return new X509Extension("2.5.29.35", writer.Encode(), false);
748+
return X509AuthorityKeyIdentifierExtension.CreateFromSubjectKeyIdentifier(skid);
782749
}
783750

784751
private enum OcspResponseStatus

0 commit comments

Comments
 (0)