Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Internal.Cryptography;

namespace System.Security.Cryptography
Expand All @@ -12,6 +13,10 @@ public class AsnEncodedData
{
protected AsnEncodedData()
{
// Initialize _rawData to an empty array so that RawData may reasonably be a non-nullable byte[].
// It naturally is for the base type as well as for derived types behaving as intended.
// This, however, is a deviation from the original .NET Framework behavior.
_rawData = Array.Empty<byte>();
}

public AsnEncodedData(byte[] rawData)
Expand Down Expand Up @@ -57,6 +62,7 @@ public byte[] RawData
return _rawData;
}

[MemberNotNull(nameof(_rawData))]
set
{
if (value == null)
Expand All @@ -81,13 +87,14 @@ public virtual string Format(bool multiLine)
return AsnFormatter.Instance.Format(_oid, _rawData, multiLine);
}

[MemberNotNull(nameof(_rawData))]
private void Reset(Oid? oid, byte[] rawData)
{
this.Oid = oid;
this.RawData = rawData;
}

private Oid? _oid = null;
private byte[] _rawData = null!; // initialized in helper method Reset for all public constuctors
private byte[] _rawData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public static class Pkcs9LocalKeyIdTests
public static void DefaultCtor()
{
Pkcs9LocalKeyId localKeyId = new Pkcs9LocalKeyId();
Assert.Equal(0, localKeyId.KeyId.Length);
Assert.Throws<CryptographicException>(() => localKeyId.KeyId);

Oid oid = localKeyId.Oid;
Assert.NotNull(oid);
Assert.Equal(Oids.LocalKeyId, oid.Value);

Assert.Null(localKeyId.RawData);
Assert.Empty(localKeyId.RawData);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ public static void Pkcs9AttributeObjectNullaryCtor()
{
Pkcs9AttributeObject p = new Pkcs9AttributeObject();
Assert.Null(p.Oid);
Assert.Null(p.RawData);
if (PlatformDetection.IsNetCore)
{
Assert.Empty(p.RawData);
}
else
{
Assert.Null(p.RawData);
}
}

[Fact]
Expand Down Expand Up @@ -147,8 +154,16 @@ public static void Pkcs9AttributeCopyFromAsnNotAPkcs9Attribute()
public static void DocumentDescriptionNullary()
{
Pkcs9DocumentDescription p = new Pkcs9DocumentDescription();
Assert.Null(p.RawData);
Assert.Null(p.DocumentDescription);
if (PlatformDetection.IsNetCore)
{
Assert.Empty(p.RawData);
Assert.Throws<CryptographicException>(() => p.DocumentDescription);
}
else
{
Assert.Null(p.RawData);
Assert.Null(p.DocumentDescription);
}
string oid = p.Oid.Value;
Assert.Equal(s_OidDocumentDescription, oid);
}
Expand Down Expand Up @@ -188,8 +203,16 @@ public static void DocumentDescriptionNullValue()
public static void DocumentNameNullary()
{
Pkcs9DocumentName p = new Pkcs9DocumentName();
Assert.Null(p.RawData);
Assert.Null(p.DocumentName);
if (PlatformDetection.IsNetCore)
{
Assert.Empty(p.RawData);
Assert.Throws<CryptographicException>(() => p.DocumentName);
}
else
{
Assert.Null(p.RawData);
Assert.Null(p.DocumentName);
}
string oid = p.Oid.Value;
Assert.Equal(s_OidDocumentName, oid);
}
Expand Down Expand Up @@ -297,8 +320,16 @@ public static void SigningTimeFromCookedData_Utc()
public static void ContentTypeNullary()
{
Pkcs9ContentType p = new Pkcs9ContentType();
Assert.Null(p.RawData);
Assert.Null(p.ContentType);
if (PlatformDetection.IsNetCore)
{
Assert.Empty(p.RawData);
Assert.Throws<CryptographicException>(() => p.ContentType);
}
else
{
Assert.Null(p.RawData);
Assert.Null(p.ContentType);
}
string oid = p.Oid.Value;
Assert.Equal(s_OidContentType, oid);
}
Expand Down Expand Up @@ -357,8 +388,16 @@ public static void ContentTypeBadData()
public static void MessageDigestNullary()
{
Pkcs9MessageDigest p = new Pkcs9MessageDigest();
Assert.Null(p.RawData);
Assert.Null(p.MessageDigest);
if (PlatformDetection.IsNetCore)
{
Assert.Empty(p.RawData);
Assert.Throws<CryptographicException>(() => p.MessageDigest);
}
else
{
Assert.Null(p.RawData);
Assert.Null(p.MessageDigest);
}
string oid = p.Oid.Value;
Assert.Equal(s_OidMessageDigest, oid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public static void KeyUsageExtensionDefaultCtor()
X509KeyUsageExtension e = new X509KeyUsageExtension();
string oidValue = e.Oid.Value;
Assert.Equal("2.5.29.15", oidValue);
byte[] r = e.RawData;
Assert.Null(r);
Assert.Empty(e.RawData);
X509KeyUsageFlags keyUsages = e.KeyUsages;
Assert.Equal(X509KeyUsageFlags.None, keyUsages);
}
Expand Down Expand Up @@ -217,8 +216,7 @@ public static void BasicConstraintsExtensionDefault()
string oidValue = e.Oid.Value;
Assert.Equal("2.5.29.19", oidValue);

byte[] rawData = e.RawData;
Assert.Null(rawData);
Assert.Empty(e.RawData);

Assert.False(e.CertificateAuthority);
Assert.False(e.HasPathLengthConstraint);
Expand Down Expand Up @@ -291,8 +289,7 @@ public static void EnhancedKeyUsageExtensionDefault()
string oidValue = e.Oid.Value;
Assert.Equal("2.5.29.37", oidValue);

byte[] rawData = e.RawData;
Assert.Null(rawData);
Assert.Empty(e.RawData);

OidCollection usages = e.EnhancedKeyUsages;
Assert.Equal(0, usages.Count);
Expand Down Expand Up @@ -353,8 +350,7 @@ public static void SubjectKeyIdentifierExtensionDefault()
string oidValue = e.Oid.Value;
Assert.Equal("2.5.29.14", oidValue);

byte[] rawData = e.RawData;
Assert.Null(rawData);
Assert.Empty(e.RawData);

string skid = e.SubjectKeyIdentifier;
Assert.Null(skid);
Expand Down