-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateSelfSigned.cs
132 lines (110 loc) · 4.63 KB
/
GenerateSelfSigned.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Cryptool
{
class GenerateSelfSigned
{
private int _keySize = 2048;
private bool _isCA = false;
private bool isEnchancedKeySrvClientAuth = false;
//
private RSA createRSA(int keySize)
{
if (keySize != null | keySize != 0)
{
_keySize = keySize;
}
RSA rsaParameter = RSA.Create(_keySize);
return rsaParameter;
}
// Hash Algorithm for certifacate
private HashAlgorithmName choiceHashAlgorithm(string hashAlg)
{
HashAlgorithmName algorithm;
switch (hashAlg)
{
case "MD5":
algorithm = HashAlgorithmName.MD5;
break;
case "SHA1":
algorithm = HashAlgorithmName.SHA1;
break;
case "SHA256":
algorithm = HashAlgorithmName.SHA256;
break;
case "SHA384":
algorithm = HashAlgorithmName.SHA384;
break;
case "SHA512":
algorithm = HashAlgorithmName.SHA512;
break;
}
return algorithm;
}
// pass the list of selection as a list string and return KeyUsageflags
private X509KeyUsageFlags keyUsageSelection(List<string> keyUsages)
{
var keyUsageFlags = X509KeyUsageFlags.KeyCertSign;
foreach (string usage in keyUsages) {
switch (usage)
{
case "crlsign":
keyUsageFlags |= X509KeyUsageFlags.CrlSign;
break;
case "DigitalSignature":
keyUsageFlags |= X509KeyUsageFlags.DigitalSignature;
break;
case "DataEncipher":
keyUsageFlags |= X509KeyUsageFlags.DataEncipherment;
break;
case "keyEncipher":
keyUsageFlags |= X509KeyUsageFlags.KeyEncipherment;
break;
case "certSigner":
keyUsageFlags |= X509KeyUsageFlags.KeyCertSign;
break;
case "serverClientAuth":
isEnchancedKeySrvClientAuth = true;
break;
}
}
return keyUsageFlags;
}
public X509Certificate2 CreateCertificate(int keySize, string subjectDN, string hashAlg, List<string> keyUsages, int pathLenght, int numberofYears, bool isCA)
{
// Create a Key Pair
var certificateKey = createRSA(keySize);
// Add DN
var _subjectDN = new X500DistinguishedName(subjectDN);
// Hashing Algorithm
var algorithm = choiceHashAlgorithm(hashAlg);
// create instance of certificate request
var request = new CertificateRequest(_subjectDN, certificateKey, algorithm, RSASignaturePadding.Pkcs1);
// Load Key Usage selection to keyUsageFlags
X509KeyUsageFlags keyUsageFlags = keyUsageSelection(keyUsages);
request.CertificateExtensions.Add(new X509KeyUsageExtension(keyUsageFlags, true));
//is CA: true for CA certificate
//basic constrain : Path length Constrain default 1
//pathContrain : enable and length
//Extension critical: true
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(isCA, true, pathLenght, true));
//Enhanced Key usage
if (isEnchancedKeySrvClientAuth)
{
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new Oid("1.3.6.1.5.5.7.3.1"), new Oid("1.3.6.1.5.5.7.3.8") }, false));
}
//
// Add Subject Key identifier extension
request.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(request.PublicKey, false));
// Create Certificate return
X509Certificate2 certificateGenerated = request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(numberofYears));
// return the certificate
return certificateGenerated;
}
}
}