Skip to content

Commit

Permalink
Enable more nullable reference types in asn.xslt.
Browse files Browse the repository at this point in the history
string and Oid types on Choice can be null.
Reference types that are @optional can be null.

Also, remove #nullable enable, because all projects that include the ASN.1 generate files should already have nullable enable project-wide.
  • Loading branch information
vcsjones authored Apr 6, 2020
1 parent 5346d57 commit 986518e
Show file tree
Hide file tree
Showing 33 changed files with 50 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand All @@ -14,11 +13,11 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct DirectoryStringAsn
{
internal string TeletexString;
internal string PrintableString;
internal string? TeletexString;
internal string? PrintableString;
internal ReadOnlyMemory<byte>? UniversalString;
internal string Utf8String;
internal string BmpString;
internal string? Utf8String;
internal string? BmpString;

#if DEBUG
static DirectoryStringAsn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand All @@ -15,7 +14,7 @@ namespace System.Security.Cryptography.Asn1
internal partial struct ECDomainParameters
{
internal System.Security.Cryptography.Asn1.SpecifiedECDomain? Specified;
internal Oid Named;
internal Oid? Named;

#if DEBUG
static ECDomainParameters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand All @@ -15,14 +14,14 @@ namespace System.Security.Cryptography.Asn1
internal partial struct GeneralNameAsn
{
internal System.Security.Cryptography.Asn1.OtherNameAsn? OtherName;
internal string Rfc822Name;
internal string DnsName;
internal string? Rfc822Name;
internal string? DnsName;
internal ReadOnlyMemory<byte>? X400Address;
internal ReadOnlyMemory<byte>? DirectoryName;
internal System.Security.Cryptography.Asn1.EdiPartyNameAsn? EdiPartyName;
internal string Uri;
internal string? Uri;
internal ReadOnlyMemory<byte>? IPAddress;
internal string RegisteredId;
internal string? RegisteredId;

#if DEBUG
static GeneralNameAsn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal partial struct SafeBagAsn
{
internal string BagId;
internal ReadOnlyMemory<byte> BagValue;
internal System.Security.Cryptography.Asn1.AttributeAsn[] BagAttributes;
internal System.Security.Cryptography.Asn1.AttributeAsn[]? BagAttributes;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal partial struct EncryptedDataAsn
{
internal int Version;
internal System.Security.Cryptography.Asn1.Pkcs7.EncryptedContentInfoAsn EncryptedContentInfo;
internal System.Security.Cryptography.Asn1.AttributeAsn[] UnprotectedAttributes;
internal System.Security.Cryptography.Asn1.AttributeAsn[]? UnprotectedAttributes;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal partial struct PrivateKeyInfoAsn
internal byte Version;
internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn PrivateKeyAlgorithm;
internal ReadOnlyMemory<byte> PrivateKey;
internal System.Security.Cryptography.Asn1.AttributeAsn[] Attributes;
internal System.Security.Cryptography.Asn1.AttributeAsn[]? Attributes;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal partial struct SpecifiedECDomain
internal ReadOnlyMemory<byte> Base;
internal ReadOnlyMemory<byte> Order;
internal ReadOnlyMemory<byte>? Cofactor;
internal Oid Hash;
internal Oid? Hash;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ namespace <xsl:value-of select="@namespace" />
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;<xsl:if test="asn:SequenceOf | asn:SetOf">
using System.Collections.Generic;</xsl:if>
Expand Down Expand Up @@ -690,9 +689,9 @@ namespace <xsl:value-of select="@namespace" />
<xsl:template match="asn:OctetString" mode="DefaultTag">Asn1Tag.PrimitiveOctetString</xsl:template>

<xsl:template match="asn:ObjectIdentifier[not(@backingType)] | asn:ObjectIdentifier[@backingType = 'Oid']" mode="FieldDef">
internal Oid <xsl:value-of select="@name" />;</xsl:template>
internal Oid<xsl:if test="@optional | parent::asn:Choice">?</xsl:if> <xsl:value-of select="@name" />;</xsl:template>
<xsl:template match="asn:ObjectIdentifier[@backingType = 'string']" mode="FieldDef">
internal string <xsl:value-of select="@name" />;</xsl:template>
internal string<xsl:if test="@optional | parent::asn:Choice">?</xsl:if> <xsl:value-of select="@name" />;</xsl:template>

<xsl:template match="asn:ObjectIdentifier[not(@backingType)] | asn:ObjectIdentifier[@backingType = 'Oid']" mode="CollectionElementType">Oid</xsl:template>
<xsl:template match="asn:ObjectIdentifier[@backingType = 'string']" mode="CollectionElementType">string</xsl:template>
Expand Down Expand Up @@ -761,7 +760,7 @@ namespace <xsl:value-of select="@namespace" />

<!-- All character string types -->
<xsl:template match="asn:UTF8String | asn:PrintableString | asn:T61String | asn:IA5String | asn:VisibleString | asn:BMPString" mode="FieldDef">
internal string <xsl:value-of select="@name"/>;</xsl:template>
internal string<xsl:if test="@optional | parent::asn:Choice">?</xsl:if> <xsl:value-of select="@name"/>;</xsl:template>

<xsl:template match="asn:UTF8String | asn:PrintableString | asn:T61String | asn:IA5String | asn:VisibleString | asn:BMPString" mode="CollectionElementType">string</xsl:template>

Expand All @@ -784,7 +783,7 @@ namespace <xsl:value-of select="@namespace" />
<xsl:template match="asn:UTF8String | asn:PrintableString | asn:T61String | asn:IA5String | asn:VisibleString | asn:BMPString" mode="DefaultTag">new Asn1Tag(UniversalTagNumber.<xsl:value-of select="local-name()"/>)</xsl:template>

<xsl:template match="asn:SequenceOf | asn:SetOf" mode="FieldDef">
internal <xsl:apply-templates mode="CollectionElementType"/>[] <xsl:value-of select="@name"/>;</xsl:template>
internal <xsl:apply-templates mode="CollectionElementType"/>[]<xsl:if test="@optional | parent::asn:Choice">?</xsl:if> <xsl:value-of select="@name"/>;</xsl:template>

<xsl:template match="asn:SequenceOf | asn:SetOf" mode="EncodeSimpleValue" xml:space="default">
<xsl:param name="writerName"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal partial struct EnvelopedDataAsn
internal System.Security.Cryptography.Pkcs.Asn1.OriginatorInfoAsn? OriginatorInfo;
internal System.Security.Cryptography.Pkcs.Asn1.RecipientInfoAsn[] RecipientInfos;
internal System.Security.Cryptography.Asn1.Pkcs7.EncryptedContentInfoAsn EncryptedContentInfo;
internal System.Security.Cryptography.Asn1.AttributeAsn[] UnprotectedAttributes;
internal System.Security.Cryptography.Asn1.AttributeAsn[]? UnprotectedAttributes;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace System.Security.Cryptography.Pkcs.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct OriginatorInfoAsn
{
internal System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn[] CertificateSet;
internal ReadOnlyMemory<byte>[] RevocationInfoChoices;
internal System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn[]? CertificateSet;
internal ReadOnlyMemory<byte>[]? RevocationInfoChoices;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.Security.Cryptography.Pkcs.Asn1
internal partial struct PolicyInformation
{
internal string PolicyIdentifier;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyQualifierInfo[] PolicyQualifiers;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyQualifierInfo[]? PolicyQualifiers;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal partial struct Rfc3161TimeStampReq
internal Oid? ReqPolicy;
internal ReadOnlyMemory<byte>? Nonce;
internal bool CertReq;
internal System.Security.Cryptography.Asn1.X509ExtensionAsn[] Extensions;
internal System.Security.Cryptography.Asn1.X509ExtensionAsn[]? Extensions;

#if DEBUG
static Rfc3161TimeStampReq()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal partial struct Rfc3161TstInfo
internal bool Ordering;
internal ReadOnlyMemory<byte>? Nonce;
internal System.Security.Cryptography.Asn1.GeneralNameAsn? Tsa;
internal System.Security.Cryptography.Asn1.X509ExtensionAsn[] Extensions;
internal System.Security.Cryptography.Asn1.X509ExtensionAsn[]? Extensions;

#if DEBUG
static Rfc3161TstInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Collections.Generic;
Expand All @@ -15,7 +14,7 @@ namespace System.Security.Cryptography.Pkcs
[StructLayout(LayoutKind.Sequential)]
internal partial struct SignedAttributesSet
{
internal System.Security.Cryptography.Asn1.AttributeAsn[] SignedAttributes;
internal System.Security.Cryptography.Asn1.AttributeAsn[]? SignedAttributes;

#if DEBUG
static SignedAttributesSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal partial struct SignedDataAsn
internal int Version;
internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn[] DigestAlgorithms;
internal System.Security.Cryptography.Pkcs.Asn1.EncapsulatedContentInfoAsn EncapContentInfo;
internal System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn[] CertificateSet;
internal ReadOnlyMemory<byte>[] Crls;
internal System.Security.Cryptography.Pkcs.Asn1.CertificateChoiceAsn[]? CertificateSet;
internal ReadOnlyMemory<byte>[]? Crls;
internal System.Security.Cryptography.Pkcs.Asn1.SignerInfoAsn[] SignerInfos;

internal void Encode(AsnWriter writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
#pragma warning disable SA1028 // ignore whitespace warnings for generated code
using System;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.Security.Cryptography.Pkcs.Asn1
internal partial struct SigningCertificateAsn
{
internal System.Security.Cryptography.Pkcs.Asn1.EssCertId[] Certs;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyInformation[] Policies;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyInformation[]? Policies;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.Security.Cryptography.Pkcs.Asn1
internal partial struct SigningCertificateV2Asn
{
internal System.Security.Cryptography.Pkcs.Asn1.EssCertIdV2[] Certs;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyInformation[] Policies;
internal System.Security.Cryptography.Pkcs.Asn1.PolicyInformation[]? Policies;

internal void Encode(AsnWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public X509ExtensionCollection GetExtensions()
return coll;
}

X509ExtensionAsn[] rawExtensions = _parsedData.Extensions;
X509ExtensionAsn[] rawExtensions = _parsedData.Extensions!;

foreach (X509ExtensionAsn rawExtension in rawExtensions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public X509ExtensionCollection GetExtensions()
return coll;
}

X509ExtensionAsn[] rawExtensions = _parsedData.Extensions;
X509ExtensionAsn[] rawExtensions = _parsedData.Extensions!;

foreach (X509ExtensionAsn rawExtension in rawExtensions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public X509Certificate2Collection Certificates
return coll;
}

CertificateChoiceAsn[] certChoices = _signedData.CertificateSet;
CertificateChoiceAsn[]? certChoices = _signedData.CertificateSet;

if (certChoices == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,23 @@ private void BuildBags(
attrBytes[1] = sizeof(int);
MemoryMarshal.Write(attrBytes.AsSpan(2), ref keyIdx);

AttributeAsn attribute = new AttributeAsn
{
AttrType = new Oid(Oids.LocalKeyId, null),
AttrValues = new ReadOnlyMemory<byte>[]
{
attrBytes,
}
};
keyBags[keyIdx] = new SafeBagAsn
{
BagId = Oids.Pkcs12ShroudedKeyBag,
BagValue = ExportPkcs8(certPal, passwordSpan),
BagAttributes = new[]
{
new AttributeAsn
{
AttrType = new Oid(Oids.LocalKeyId, null),
AttrValues = new ReadOnlyMemory<byte>[]
{
attrBytes,
}
}
}
BagAttributes = new[] { attribute }
};

// Reuse the attribute between the cert and the key.
certAttrs[certIdx] = keyBags[keyIdx].BagAttributes[0];
certAttrs[certIdx] = attribute;
keyIdx++;
}

Expand Down
Loading

0 comments on commit 986518e

Please sign in to comment.