forked from OPCFoundation/UA-.NETStandard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificateFactory.cs
More file actions
587 lines (534 loc) · 22.3 KB
/
CertificateFactory.cs
File metadata and controls
587 lines (534 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* Copyright (c) 1996-2022 The OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation Corporate Members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Opc.Ua.Security.Certificates;
namespace Opc.Ua
{
/// <summary>
/// Creates certificates.
/// </summary>
public static class CertificateFactory
{
/// <summary>
/// The default key size for RSA certificates in bits.
/// </summary>
/// <remarks>
/// Supported values are 1024(deprecated), 2048, 3072 or 4096.
/// </remarks>
public static readonly ushort DefaultKeySize = 2048;
/// <summary>
/// The default hash size for RSA certificates in bits.
/// </summary>
/// <remarks>
/// Supported values are 160 for SHA-1(deprecated) or 256, 384 and 512 for SHA-2.
/// </remarks>
public static readonly ushort DefaultHashSize = 256;
/// <summary>
/// The default lifetime of certificates in months.
/// </summary>
public static readonly ushort DefaultLifeTime = 12;
/// <summary>
/// Creates a certificate from a buffer with DER encoded certificate.
/// </summary>
[Obsolete("Use Create without useCache parameter")]
public static X509Certificate2 Create(
ReadOnlyMemory<byte> encodedData,
bool useCache)
{
return Create(encodedData);
}
/// <summary>
/// Creates a certificate from a buffer with DER encoded certificate.
/// </summary>
public static X509Certificate2 Create(ReadOnlyMemory<byte> encodedData)
{
#if NET6_0_OR_GREATER
return X509CertificateLoader.LoadCertificate(encodedData.Span);
#else
return X509CertificateLoader.LoadCertificate(encodedData.ToArray());
#endif
}
/// <summary>
/// Loads the cached version of a certificate.
/// </summary>
[Obsolete("This method just returns the certificate and can be removed")]
public static X509Certificate2 Load(
X509Certificate2 certificate,
bool ensurePrivateKeyAccessible)
{
return certificate;
}
/// <summary>
/// Create a certificate for any use.
/// </summary>
/// <param name="subjectName">The subject of the certificate</param>
/// <returns>Return the Certificate builder.</returns>
public static ICertificateBuilder CreateCertificate(string subjectName)
{
return CertificateBuilder.Create(subjectName);
}
/// <summary>
/// Create a certificate for an OPC UA application.
/// </summary>
/// <param name="applicationUri">The application Uri</param>
/// <param name="applicationName">The friendly name of the application</param>
/// <param name="subjectName">The subject of the certificate</param>
/// <param name="domainNames">The domain names for the alt name extension</param>
/// <returns>
/// Return the Certificate builder with X509 Subject Alt Name extension
/// to create the certificate.
/// </returns>
public static ICertificateBuilder CreateCertificate(
string applicationUri,
string applicationName,
string subjectName,
IList<string> domainNames)
{
SetSuitableDefaults(
ref applicationUri,
ref applicationName,
ref subjectName,
ref domainNames);
return CertificateBuilder
.Create(subjectName)
.AddExtension(new X509SubjectAltNameExtension(applicationUri, domainNames));
}
/// <summary>
/// Revoke the certificate.
/// The CRL number is increased by one and the new CRL is returned.
/// </summary>
public static X509CRL RevokeCertificate(
X509Certificate2 issuerCertificate,
X509CRLCollection issuerCrls,
X509Certificate2Collection revokedCertificates)
{
return RevokeCertificate(
issuerCertificate,
issuerCrls,
revokedCertificates,
DateTime.UtcNow,
DateTime.UtcNow.AddMonths(12));
}
/// <summary>
/// Revoke the certificates.
/// </summary>
/// <remarks>
/// Merge all existing revoked certificates from CRL list.
/// Add serialnumbers of new revoked certificates.
/// The CRL number is increased by one and the new CRL is returned.
/// </remarks>
/// <exception cref="ServiceResultException"></exception>
/// <exception cref="NotSupportedException"></exception>
public static X509CRL RevokeCertificate(
X509Certificate2 issuerCertificate,
X509CRLCollection issuerCrls,
X509Certificate2Collection revokedCertificates,
DateTime thisUpdate,
DateTime nextUpdate)
{
if (!issuerCertificate.HasPrivateKey)
{
throw new ServiceResultException(
StatusCodes.BadCertificateInvalid,
"Issuer certificate has no private key, cannot revoke certificate.");
}
BigInteger crlSerialNumber = 0;
var crlRevokedList = new Dictionary<string, RevokedCertificate>();
// merge all existing revocation list
if (issuerCrls != null)
{
foreach (X509CRL issuerCrl in issuerCrls)
{
X509CrlNumberExtension extension = issuerCrl.CrlExtensions
.FindExtension<X509CrlNumberExtension>();
if (extension != null && extension.CrlNumber > crlSerialNumber)
{
crlSerialNumber = extension.CrlNumber;
}
foreach (RevokedCertificate revokedCertificate in issuerCrl.RevokedCertificates)
{
if (!crlRevokedList.ContainsKey(revokedCertificate.SerialNumber))
{
crlRevokedList[revokedCertificate.SerialNumber] = revokedCertificate;
}
}
}
}
// add existing serial numbers
if (revokedCertificates != null)
{
foreach (X509Certificate2 cert in revokedCertificates)
{
if (!crlRevokedList.ContainsKey(cert.SerialNumber))
{
crlRevokedList[cert.SerialNumber] = new RevokedCertificate(
cert.SerialNumber,
CRLReason.PrivilegeWithdrawn);
}
}
}
CrlBuilder crlBuilder = CrlBuilder
.Create(issuerCertificate.SubjectName)
.AddRevokedCertificates([.. crlRevokedList.Values])
.SetThisUpdate(thisUpdate)
.SetNextUpdate(nextUpdate)
.AddCRLExtension(issuerCertificate.BuildAuthorityKeyIdentifier())
.AddCRLExtension(X509Extensions.BuildCRLNumber(crlSerialNumber + 1));
if (X509PfxUtils.IsECDsaSignature(issuerCertificate))
{
#if ECC_SUPPORT
return new X509CRL(crlBuilder.CreateForECDsa(issuerCertificate));
#else
throw new NotSupportedException(
"CRL can only be created for an RSA Certificate on this system");
#endif
}
return new X509CRL(crlBuilder.CreateForRSA(issuerCertificate));
}
/// <summary>
/// Create a X509Certificate2 with a private key by combining
/// the certificate with a private key from a PEM stream
/// </summary>
public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
X509Certificate2 certificate,
byte[] pemDataBlob)
{
return CreateCertificateWithPEMPrivateKey(certificate, pemDataBlob, default);
}
#if NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
/// <summary>
/// Creates a certificate signing request from an existing certificate.
/// </summary>
/// <exception cref="NotSupportedException"></exception>
public static byte[] CreateSigningRequest(
X509Certificate2 certificate,
// TODO: provide CertificateType to return CSR per certificate type
IList<string> domainNames = null)
{
if (!certificate.HasPrivateKey)
{
throw new NotSupportedException("Need a certificate with a private key.");
}
CertificateRequest request = null;
bool isECDsaSignature = X509PfxUtils.IsECDsaSignature(certificate);
if (!isECDsaSignature)
{
RSA rsaPublicKey = certificate.GetRSAPublicKey();
request = new CertificateRequest(
certificate.SubjectName,
rsaPublicKey,
Oids.GetHashAlgorithmName(certificate.SignatureAlgorithm.Value),
RSASignaturePadding.Pkcs1);
}
else
{
ECDsa eCDsaPublicKey = certificate.GetECDsaPublicKey();
request = new CertificateRequest(
certificate.SubjectName,
eCDsaPublicKey,
Oids.GetHashAlgorithmName(certificate.SignatureAlgorithm.Value));
}
X509SubjectAltNameExtension alternateName = certificate
.FindExtension<X509SubjectAltNameExtension>();
domainNames ??= [];
if (alternateName != null)
{
foreach (string name in alternateName.DomainNames)
{
if (!domainNames.Any(s => s.Equals(name, StringComparison.OrdinalIgnoreCase)))
{
domainNames.Add(name);
}
}
foreach (string ipAddress in alternateName.IPAddresses)
{
if (!domainNames.Any(
s => s.Equals(ipAddress, StringComparison.OrdinalIgnoreCase)))
{
domainNames.Add(ipAddress);
}
}
}
var applicationUris = X509Utils.GetApplicationUrisFromCertificate(certificate);
// Subject Alternative Name
var subjectAltName = new X509SubjectAltNameExtension(applicationUris, domainNames);
request.CertificateExtensions.Add(new X509Extension(subjectAltName, false));
if (!isECDsaSignature)
{
using RSA rsa = certificate.GetRSAPrivateKey();
var x509SignatureGenerator = X509SignatureGenerator.CreateForRSA(
rsa,
RSASignaturePadding.Pkcs1);
return request.CreateSigningRequest(x509SignatureGenerator);
}
else
{
using ECDsa key = certificate.GetECDsaPrivateKey();
var x509SignatureGenerator = X509SignatureGenerator.CreateForECDsa(key);
return request.CreateSigningRequest(x509SignatureGenerator);
}
}
/// <summary>
/// Create a X509Certificate2 with a private key by combining
/// the new certificate with a private key from an existing certificate
/// </summary>
/// <exception cref="NotSupportedException"></exception>
public static X509Certificate2 CreateCertificateWithPrivateKey(
X509Certificate2 certificate,
X509Certificate2 certificateWithPrivateKey)
{
if (!certificateWithPrivateKey.HasPrivateKey)
{
throw new NotSupportedException("Need a certificate with a private key.");
}
if (X509Utils.IsECDsaSignature(certificate))
{
if (!X509Utils.VerifyECDsaKeyPair(certificate, certificateWithPrivateKey))
{
throw new NotSupportedException(
"The public and the private key pair doesn't match.");
}
using ECDsa privateKey = certificateWithPrivateKey.GetECDsaPrivateKey();
return certificate.CopyWithPrivateKey(privateKey);
}
else
{
if (!X509Utils.VerifyRSAKeyPair(certificate, certificateWithPrivateKey))
{
throw new NotSupportedException(
"The public and the private key pair doesn't match.");
}
using RSA privateKey = certificateWithPrivateKey.GetRSAPrivateKey();
return certificate.CopyWithPrivateKey(privateKey);
}
}
/// <summary>
/// Create a X509Certificate2 with a private key by combining
/// the certificate with a private key from a PEM stream
/// </summary>
public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
X509Certificate2 certificate,
byte[] pemDataBlob,
ReadOnlySpan<char> password)
{
if (X509Utils.IsECDsaSignature(certificate))
{
using ECDsa ecdsaPrivateKey = PEMReader.ImportECDsaPrivateKeyFromPEM(
pemDataBlob,
password);
return Create(certificate.RawData).CopyWithPrivateKey(ecdsaPrivateKey);
}
using RSA rsaPrivateKey = PEMReader.ImportRsaPrivateKeyFromPEM(pemDataBlob, password);
return Create(certificate.RawData).CopyWithPrivateKey(rsaPrivateKey);
}
#else
/// <summary>
/// Creates a certificate signing request from an existing certificate.
/// </summary>
public static byte[] CreateSigningRequest(
X509Certificate2 certificate,
IList<string> domainNames = null)
{
return CertificateBuilder.CreateSigningRequest(certificate, domainNames);
}
/// <summary>
/// Create a X509Certificate2 with a private key by combining
/// the new certificate with a private key from an existing certificate
/// </summary>
/// <exception cref="NotSupportedException"></exception>
public static X509Certificate2 CreateCertificateWithPrivateKey(
X509Certificate2 certificate,
X509Certificate2 certificateWithPrivateKey)
{
if (!certificateWithPrivateKey.HasPrivateKey)
{
throw new NotSupportedException("Need a certificate with a private key.");
}
if (!X509Utils.VerifyRSAKeyPair(certificate, certificateWithPrivateKey))
{
throw new NotSupportedException(
"The public and the private key pair doesn't match.");
}
char[] passcode = X509Utils.GeneratePasscode();
try
{
using RSA rsaPrivateKey = certificateWithPrivateKey.GetRSAPrivateKey();
byte[] pfxData = CertificateBuilder.CreatePfxWithRSAPrivateKey(
certificate,
certificate.FriendlyName,
rsaPrivateKey,
passcode);
return X509Utils.CreateCertificateFromPKCS12(pfxData, passcode);
}
finally
{
Array.Clear(passcode, 0, passcode.Length);
}
}
/// <summary>
/// Create a X509Certificate2 with a private key by combining
/// the certificate with a private key from a PEM stream
/// </summary>
/// <exception cref="ServiceResultException"></exception>
public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
X509Certificate2 certificate,
byte[] pemDataBlob,
ReadOnlySpan<char> password)
{
#if ECC_SUPPORT
if (X509Utils.IsECDsaSignature(certificate))
{
using (ECDsa privateKey = PEMReader.ImportECDsaPrivateKeyFromPEM(pemDataBlob, password))
{
if (privateKey == null)
{
throw new ServiceResultException("PEM data blob does not contain a private key.");
}
string passcode = X509Utils.GeneratePasscode();
byte[] pfxData = CertificateBuilder.CreatePfxWithECdsaPrivateKey(
certificate,
certificate.FriendlyName,
privateKey,
passcode);
return X509Utils.CreateCertificateFromPKCS12(pfxData, passcode);
}
}
else
#endif
{
using RSA privateKey =
PEMReader.ImportRsaPrivateKeyFromPEM(pemDataBlob, password)
?? throw new ServiceResultException(
"PEM data blob does not contain a private key.");
char[] passcode = X509Utils.GeneratePasscode();
try
{
byte[] pfxData = CertificateBuilder.CreatePfxWithRSAPrivateKey(
certificate,
certificate.FriendlyName,
privateKey,
passcode);
return X509Utils.CreateCertificateFromPKCS12(pfxData, passcode);
}
finally
{
Array.Clear(passcode, 0, passcode.Length);
}
}
}
#endif
/// <summary>
/// Sets the parameters to suitable defaults.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
private static void SetSuitableDefaults(
ref string applicationUri,
ref string applicationName,
ref string subjectName,
ref IList<string> domainNames)
{
// parse the subject name if specified.
List<string> subjectNameEntries = null;
if (!string.IsNullOrEmpty(subjectName))
{
subjectNameEntries = X509Utils.ParseDistinguishedName(subjectName);
}
// check the application name.
if (string.IsNullOrEmpty(applicationName))
{
if (subjectNameEntries == null)
{
throw new ArgumentNullException(
nameof(applicationName),
"Must specify a applicationName or a subjectName.");
}
// use the common name as the application name.
for (int ii = 0; ii < subjectNameEntries.Count; ii++)
{
if (subjectNameEntries[ii].StartsWith("CN=", StringComparison.Ordinal))
{
applicationName = subjectNameEntries[ii][3..].Trim();
break;
}
}
}
if (string.IsNullOrEmpty(applicationName))
{
throw new ArgumentNullException(
nameof(applicationName),
"Must specify a applicationName or a subjectName.");
}
// remove special characters from name.
var buffer = new StringBuilder();
for (int ii = 0; ii < applicationName.Length; ii++)
{
char ch = applicationName[ii];
if (char.IsControl(ch) || ch == '/' || ch == ',' || ch == ';')
{
ch = '+';
}
buffer.Append(ch);
}
applicationName = buffer.ToString();
// ensure at least one host name.
if (domainNames == null || domainNames.Count == 0)
{
domainNames = [Utils.GetHostName()];
}
// create the application uri.
if (string.IsNullOrEmpty(applicationUri))
{
var builder = new StringBuilder();
builder.Append("urn:")
.Append(domainNames[0])
.Append(':')
.Append(applicationName);
applicationUri = builder.ToString();
}
_ = Utils.ParseUri(applicationUri)
?? throw new ArgumentNullException(
nameof(applicationUri),
"Must specify a valid URL.");
// create the subject name,
if (string.IsNullOrEmpty(subjectName))
{
subjectName = Utils.Format("CN={0}", applicationName);
}
if (!subjectName.Contains("CN=", StringComparison.Ordinal))
{
subjectName = Utils.Format("CN={0}", subjectName);
}
if (!subjectName.Contains("O=", StringComparison.Ordinal))
{
subjectName += Utils.Format(", O={0}", "OPC Foundation");
}
if (domainNames != null && domainNames.Count > 0)
{
if (!subjectName.Contains("DC=", StringComparison.Ordinal) &&
!subjectName.Contains('=', StringComparison.Ordinal))
{
subjectName += Utils.Format(", DC={0}", domainNames[0]);
}
else
{
subjectName = Utils.ReplaceDCLocalhost(subjectName, domainNames[0]);
}
}
}
}
}