Skip to content

Commit 28042c4

Browse files
authored
Remove password parameter from X.509 constructors (#259)
1 parent ef6900e commit 28042c4

File tree

4 files changed

+58
-104
lines changed

4 files changed

+58
-104
lines changed

nanoFramework.System.Net/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following
@@ -12,7 +11,7 @@
1211

1312
////////////////////////////////////////////////////////////////
1413
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.1.4.1")]
14+
[assembly: AssemblyNativeVersion("100.1.5.0")]
1615
////////////////////////////////////////////////////////////////
1716

1817
// Setting ComVisible to false makes the types in this assembly not visible

nanoFramework.System.Net/X509Certificates/X509Certificate.cs

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ namespace System.Security.Cryptography.X509Certificates
1414
/// Provides methods that help you use X.509 v.3 certificates.
1515
/// </summary>
1616
/// <remarks>
17-
/// ASN.1 DER is the only certificate format supported by this class.
17+
/// Supported formats: DER and PEM.
1818
/// </remarks>
1919
public class X509Certificate
2020
{
2121
private readonly byte[] _certificate;
22-
private readonly string _password;
2322

2423
/// <summary>
2524
/// Contains the certificate issuer.
@@ -58,36 +57,27 @@ public X509Certificate()
5857
/// </summary>
5958
/// <param name="certificate">A byte array containing data from an X.509 certificate.</param>
6059
/// <remarks>
61-
/// ASN.1 DER is the only certificate format supported by this class.
60+
/// DER and PEM encoding are the supported formats.
6261
/// </remarks>
6362
public X509Certificate(byte[] certificate)
64-
: this(certificate, null)
65-
{
66-
}
67-
68-
/// <summary>
69-
/// Initializes a new instance of the <see cref="X509Certificate"/> class using a byte array and a password.
70-
/// </summary>
71-
/// <param name="certificate">A byte array containing data from an X.509 certificate.</param>
72-
/// <param name="password">The password required to access the X.509 certificate data.</param>
73-
/// <remarks>
74-
/// ASN.1 DER is the only certificate format supported by this class.
75-
/// </remarks>
76-
public X509Certificate(byte[] certificate, string password)
7763
{
7864
_certificate = certificate;
79-
_password = password;
8065

81-
ParseCertificate(certificate, password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
66+
ParseCertificate(
67+
certificate,
68+
ref _issuer,
69+
ref _subject,
70+
ref _effectiveDate,
71+
ref _expirationDate);
8272
}
8373

8474
/// <summary>
8575
/// Initializes a new instance of the <see cref="X509Certificate"/> class defined from a string with the content of an X.509v3 certificate.
8676
/// </summary>
8777
/// <param name="certificate">A string containing a X.509 certificate.</param>
8878
/// <remarks>
89-
/// ASN.1 DER is the only certificate format supported by this class.
90-
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
79+
/// Supported formats: DER and PEM.
80+
/// This methods is exclusive of .NET nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
9181
/// </remarks>
9282
public X509Certificate(string certificate)
9383
{
@@ -101,33 +91,12 @@ public X509Certificate(string certificate)
10191
Array.Copy(tempCertificate, _certificate, tempCertificate.Length);
10292
_certificate[_certificate.Length - 1] = 0;
10393

104-
ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
105-
}
106-
107-
/// <summary>
108-
/// Initializes a new instance of the <see cref="X509Certificate"/> class defined from a string with the content of an X.509v3 certificate.
109-
/// </summary>
110-
/// <param name="certificate">A string containing a X.509 certificate.</param>
111-
/// <param name="password">The password required to access the X.509 certificate data.</param>
112-
/// <remarks>
113-
/// ASN.1 DER is the only certificate format supported by this class.
114-
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
115-
/// </remarks>
116-
public X509Certificate(string certificate, string password)
117-
{
118-
_password = password;
119-
120-
var tempCertificate = Encoding.UTF8.GetBytes(certificate);
121-
122-
//////////////////////////////////////////////
123-
// because this is parsing from a string //
124-
// we need to keep the terminator //
125-
//////////////////////////////////////////////
126-
_certificate = new byte[tempCertificate.Length + 1];
127-
Array.Copy(tempCertificate, _certificate, tempCertificate.Length);
128-
_certificate[_certificate.Length - 1] = 0;
129-
130-
ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
94+
ParseCertificate(
95+
_certificate,
96+
ref _issuer,
97+
ref _subject,
98+
ref _effectiveDate,
99+
ref _expirationDate);
131100
}
132101

133102
/// <summary>
@@ -186,7 +155,12 @@ public virtual byte[] GetRawCertData()
186155
}
187156

188157
[MethodImpl(MethodImplOptions.InternalCall)]
189-
internal static extern void ParseCertificate(byte[] cert, string password, ref string issuer, ref string subject, ref DateTime effectiveDate, ref DateTime expirationDate);
158+
internal static extern void ParseCertificate(
159+
byte[] cert,
160+
ref string issuer,
161+
ref string subject,
162+
ref DateTime effectiveDate,
163+
ref DateTime expirationDate);
190164
}
191165
}
192166

nanoFramework.System.Net/X509Certificates/X509Certificate2.cs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class X509Certificate2 : X509Certificate
1717
#pragma warning disable S3459 // Unassigned members should be removed
1818
// field required to be accessible by native code
1919
private readonly byte[] _privateKey;
20+
private readonly string _password;
2021
#pragma warning restore S3459 // Unassigned members should be removed
2122

2223
/// <summary>
@@ -25,7 +26,6 @@ public class X509Certificate2 : X509Certificate
2526
public X509Certificate2()
2627
: base()
2728
{
28-
2929
}
3030

3131
/// <summary>
@@ -37,58 +37,32 @@ public X509Certificate2(byte[] rawData)
3737
{
3838
}
3939

40-
/// <summary>
41-
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a byte array and a password.
42-
/// </summary>
43-
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
44-
/// <param name="password">The password required to access the X.509 certificate data.</param>
45-
public X509Certificate2(byte[] rawData, string password)
46-
: base(rawData, password)
47-
{
48-
}
49-
50-
5140
/// <summary>
5241
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate.
5342
/// </summary>
5443
/// <param name="certificate">A string containing a X.509 certificate.</param>
5544
/// <remarks>
56-
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
45+
/// This methods is exclusive of .NET nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
5746
/// </remarks>
5847
public X509Certificate2(string certificate)
5948
: base(certificate)
6049
{
6150
}
6251

6352
/// <summary>
64-
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate and a password used to access the certificate.
65-
/// </summary>
66-
/// <param name="certificate">A string containing a X.509 certificate.</param>
67-
/// <param name="password">The password required to access the X.509 certificate data.</param>
68-
/// <remarks>
69-
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
70-
/// </remarks>
71-
public X509Certificate2(string certificate, string password)
72-
: base(certificate, password)
73-
{
74-
}
75-
76-
/// <summary>
77-
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
53+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the private key.
7854
/// </summary>
7955
/// <param name="rawData">A string containing a X.509 certificate.</param>
80-
/// <param name="key">A string containing a PEM private key.</param>
81-
/// <param name="password">The password required to access the X.509 certificate data. Set to <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted and do not require a password.</param>
56+
/// <param name="key">A string containing a private key in PEM or DER format.</param>
57+
/// <param name="password">The password required to decrypt the private key. Set to <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted and do not require a password.</param>
8258
/// <remarks>
83-
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
59+
/// This methods is exclusive of .NET nanoFramework. There is no equivalent in .NET framework.
8460
/// </remarks>
8561
public X509Certificate2(
8662
string rawData,
8763
string key,
8864
string password)
89-
: base(
90-
rawData,
91-
password)
65+
: base(rawData)
9266
{
9367
var tempKey = Encoding.UTF8.GetBytes(key);
9468

@@ -101,26 +75,27 @@ public X509Certificate2(
10175
keyBuffer[keyBuffer.Length - 1] = 0;
10276

10377
_privateKey = keyBuffer;
78+
_password = password;
10479

105-
DecodePrivateKeyNative(keyBuffer, password);
80+
DecodePrivateKeyNative(
81+
keyBuffer,
82+
password);
10683
}
10784

10885
/// <summary>
10986
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
11087
/// </summary>
11188
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
112-
/// <param name="key">A string containing a PEM private key.</param>
113-
/// <param name="password">The password required to access the X.509 certificate data. Set to <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted and do not require a password.</param>
89+
/// <param name="key">A string containing a private key in PEM or DER format.</param>
90+
/// <param name="password">The password required to decrypt the private key. Set to <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted and do not require a password.</param>
11491
/// <remarks>
115-
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
92+
/// This methods is exclusive of .NET nanoFramework. There is no equivalent in .NET framework.
11693
/// </remarks>
11794
public X509Certificate2(
11895
byte[] rawData,
11996
string key,
12097
string password)
121-
: base(
122-
rawData,
123-
password)
98+
: base(rawData)
12499
{
125100
var tempKey = Encoding.UTF8.GetBytes(key);
126101

@@ -133,34 +108,38 @@ public X509Certificate2(
133108
keyBuffer[keyBuffer.Length - 1] = 0;
134109

135110
_privateKey = keyBuffer;
111+
_password = password;
136112

137-
DecodePrivateKeyNative(keyBuffer, password);
113+
DecodePrivateKeyNative(
114+
keyBuffer,
115+
password);
138116
}
139117

140118
/// <summary>
141119
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
142120
/// </summary>
143121
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
144122
/// <param name="key">A byte array containing a PEM private key.</param>
145-
/// <param name="password">The password required to access the X.509 certificate data. <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted.</param>
123+
/// <param name="password">The password required to decrypt the private key. <see langword="null"/> if the <paramref name="rawData"/> or <paramref name="key"/> are not encrypted.</param>
146124
/// <remarks>
147125
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
148126
/// </remarks>
149127
public X509Certificate2(
150128
byte[] rawData,
151129
byte[] key,
152130
string password)
153-
: base(
154-
rawData,
155-
password)
131+
: base(rawData)
156132
{
157133
_privateKey = key;
134+
_password = password;
158135

159-
DecodePrivateKeyNative(key, password);
136+
DecodePrivateKeyNative(
137+
key,
138+
password);
160139
}
161140

162141
/// <summary>
163-
/// Gets a value that indicates whether an X509Certificate2 object contains a private key.
142+
/// Gets a value that indicates whether an <see cref="X509Certificate2"/> object contains a private key.
164143
/// </summary>
165144
/// <value><see langword="true"/> if the <see cref="X509Certificate2"/> object contains a private key; otherwise, <see langword="false"/>.</value>
166145
public bool HasPrivateKey
@@ -172,19 +151,19 @@ public bool HasPrivateKey
172151
}
173152

174153
/// <summary>
175-
/// Gets the private key, null if no private key
154+
/// Gets the private key, null if there isn't a private key.
176155
/// </summary>
177156
/// <remarks>This will give you access directly to the raw decoded byte array of the private key</remarks>
178157
public byte[] PrivateKey => _privateKey;
179158

180159
/// <summary>
181-
/// Gets the public key
160+
/// Gets the public key.
182161
/// </summary>
183-
/// <remarks>This will give you access directly to the raw decoded byte array of the public key</remarks>
162+
/// <remarks>This will give you access directly to the raw decoded byte array of the public key.</remarks>
184163
public byte[] PublicKey => RawData;
185164

186165
/// <summary>
187-
/// Gets the date in local time after which a certificate is no longer valid.
166+
/// Gets the date (in UTC time) after which a certificate is no longer valid.
188167
/// </summary>
189168
/// <value>A <see cref="DateTime"/> object that represents the expiration date for the certificate.</value>
190169
public DateTime NotAfter
@@ -196,7 +175,7 @@ public DateTime NotAfter
196175
}
197176

198177
/// <summary>
199-
/// Gets the date in local time on which a certificate becomes valid.
178+
/// Gets the date (in UTC time) on which a certificate becomes valid.
200179
/// </summary>
201180
/// <value>A <see cref="DateTime"/> object that represents the effective date of the certificate.</value>
202181
public DateTime NotBefore
@@ -220,6 +199,8 @@ public byte[] RawData
220199
}
221200

222201
[MethodImpl(MethodImplOptions.InternalCall)]
223-
internal static extern void DecodePrivateKeyNative(byte[] keyBuffer, string password);
202+
internal static extern void DecodePrivateKeyNative(
203+
byte[] keyBuffer,
204+
string password);
224205
}
225206
}

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.8.3",
3+
"version": "1.9.0",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)