/
CryptoUtils.cs
278 lines (245 loc) · 9.14 KB
/
CryptoUtils.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace Target365.Sdk
{
internal static class CryptoUtils
{
private static readonly byte[] pemP256PublicKeyPrefix = new byte[]
{
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08,
0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04
};
private static readonly byte[] pemP521PublicKeyPrefix = new byte[]
{
0x30, 0x81, 0x9b, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06,
0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x81, 0x86, 0x00, 0x04
};
private static readonly byte[] pemRsa1024PublicKeyPrefix = new byte[]
{
0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
0x01, 0x05, 0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00
};
private static readonly byte[] pemRsa1024PublicKeySuffix = new byte[]
{
0x02, 0x03, 0x01, 0x00, 0x01
};
private static readonly byte[] pemRsa2048PublicKeyPrefix = new byte[]
{
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
0x01, 0x01, 0x00,
};
private static readonly byte[] pemRsa2048PublicKeySuffix = new byte[]
{
0x02, 0x03, 0x01, 0x00, 0x01,
};
private static readonly byte[] cngP256PublicKeyPrefix = new byte[]
{
0x45, 0x43, 0x53, 0x31, 0x20, 0x00, 0x00, 0x00
};
private static readonly byte[] cngP521PublicKeyPrefix = new byte[]
{
0x45, 0x43, 0x53, 0x35, 0x42, 0x00, 0x00, 0x00
};
private static readonly byte[] cngRsa1024PublicKeyPrefix = new byte[]
{
0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01
};
private static readonly byte[] cngRsa2048PublicKeyPrefix = new byte[]
{
0x52, 0x53, 0x41, 0x31, 0x00, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01
};
public const string ECDsaP256 = "ECDsaP256";
public const string ECDsaP521 = "ECDsaP521";
public const string Rsa = "Rsa";
public static byte[] GetRawKey(byte[] keyData)
{
_ = keyData ?? throw new ArgumentNullException(nameof(keyData));
if (StartsWith(keyData, pemP256PublicKeyPrefix))
{
return keyData.Skip(pemP256PublicKeyPrefix.Length).ToArray();
}
else if (StartsWith(keyData, pemP521PublicKeyPrefix))
{
return keyData.Skip(pemP521PublicKeyPrefix.Length).ToArray();
}
else if (StartsWith(keyData, pemRsa1024PublicKeyPrefix))
{
return keyData.Skip(pemRsa1024PublicKeyPrefix.Length).Take(128).ToArray();
}
else if (StartsWith(keyData, pemRsa2048PublicKeyPrefix))
{
return keyData.Skip(pemRsa2048PublicKeyPrefix.Length).Take(256).ToArray();
}
else if (StartsWith(keyData, cngP256PublicKeyPrefix))
{
return keyData.Skip(cngP256PublicKeyPrefix.Length).ToArray();
}
else if (StartsWith(keyData, cngP521PublicKeyPrefix))
{
return keyData.Skip(cngP521PublicKeyPrefix.Length).ToArray();
}
else if (StartsWith(keyData, cngRsa1024PublicKeyPrefix))
{
return keyData.Skip(cngRsa1024PublicKeyPrefix.Length).ToArray();
}
else if (StartsWith(keyData, cngRsa2048PublicKeyPrefix))
{
return keyData.Skip(cngRsa2048PublicKeyPrefix.Length).ToArray();
}
throw new ArgumentException("Unsupported PEM format. Only P256, P521, RSA-1024 and RSA-2048 public keys are supported.");
}
public static byte[] GetPemBytes(byte[] rawKey, string algorithm)
{
_ = rawKey ?? throw new ArgumentNullException(nameof(rawKey));
if (algorithm == ECDsaP256)
{
return pemP256PublicKeyPrefix.Concat(rawKey).ToArray();
}
if (algorithm == ECDsaP521)
{
return pemP521PublicKeyPrefix.Concat(rawKey).ToArray();
}
else if (algorithm == Rsa && rawKey.Length == 128)
{
return pemRsa1024PublicKeyPrefix.Concat(rawKey).Concat(pemRsa1024PublicKeySuffix).ToArray();
}
else if (algorithm == Rsa && rawKey.Length == 256)
{
return pemRsa2048PublicKeyPrefix.Concat(rawKey).Concat(pemRsa2048PublicKeySuffix).ToArray();
}
throw new ArgumentException("Unsupported raw key. Only P256, P521, RSA-1024 and RSA-2048 public keys are supported.");
}
public static byte[] GetCngBytes(byte[] rawKey, string algorithm)
{
_ = rawKey ?? throw new ArgumentNullException(nameof(rawKey));
if (algorithm == ECDsaP256)
{
return cngP256PublicKeyPrefix.Concat(rawKey).ToArray();
}
if (algorithm == ECDsaP521)
{
return cngP521PublicKeyPrefix.Concat(rawKey).ToArray();
}
else if (algorithm == Rsa && rawKey.Length == 128)
{
return cngRsa1024PublicKeyPrefix.Concat(rawKey).ToArray();
}
else if (algorithm == Rsa && rawKey.Length == 256)
{
return cngRsa2048PublicKeyPrefix.Concat(rawKey).ToArray();
}
throw new ArgumentException("Unsupported raw key. Only ECDsaP256, ECDsaP521 and RSA is supported.");
}
public static ECDsa GetEcdsaFromPrivateKey(byte[] privateKey)
{
#if NET461
if (IsPemPrivateKey(privateKey))
privateKey = PemPrivateKeyToCng(privateKey);
return new ECDsaCng(CngKey.Import(privateKey, CngKeyBlobFormat.EccPrivateBlob));
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (IsPemPrivateKey(privateKey))
privateKey = PemPrivateKeyToCng(privateKey);
return new ECDsaCng(CngKey.Import(privateKey, CngKeyBlobFormat.EccPrivateBlob));
}
else
{
var ecdsa = ECDsa.Create();
var pemBytes = IsPemPrivateKey(privateKey) ? privateKey : GetPemBytesFromCngPrivateKeyBytes(privateKey);
ecdsa.ImportPkcs8PrivateKey(pemBytes, out _);
return ecdsa;
}
#endif
}
private static bool IsPemPrivateKey(byte[] privateKey)
{
return privateKey[0] == 0x30 && privateKey.Length == 138;
}
public static byte[] PemPrivateKeyToCng(byte[] pemPrivateKey)
{
var prefix = new byte[] { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
var d = pemPrivateKey.Skip(36).Take(32);
var x = pemPrivateKey.Skip(36 + 32 + 6).Take(32);
var y = pemPrivateKey.Skip(36 + 32 + 6 + 32).Take(32);
return prefix.Concat(x).Concat(y).Concat(d).ToArray();
}
public static ECDsa GetEcdsaFromPemPrivateKey(byte[] pemPrivateKey)
{
#if NET461
var cngPrivateKey = GetCngBytesFromPemPrivateKeyBytes(pemPrivateKey);
return new ECDsaCng(CngKey.Import(cngPrivateKey, CngKeyBlobFormat.EccPrivateBlob));
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var cngPrivateKey = GetCngBytesFromPemPrivateKeyBytes(pemPrivateKey);
return new ECDsaCng(CngKey.Import(cngPrivateKey, CngKeyBlobFormat.EccPrivateBlob));
}
else
{
var ecdsa = ECDsa.Create();
ecdsa.ImportPkcs8PrivateKey(pemPrivateKey, out _);
return ecdsa;
}
#endif
}
public static ECDsa GetEcdsaFromPemPublicKey(byte[] pemBytes)
{
#if NET461
var rawBytes = GetRawKey(pemBytes);
var cngBytes = GetCngBytes(rawBytes, ECDsaP256);
return new ECDsaCng(CngKey.Import(cngBytes, CngKeyBlobFormat.GenericPublicBlob));
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var rawBytes = GetRawKey(pemBytes);
var cngBytes = GetCngBytes(rawBytes, ECDsaP256);
return new ECDsaCng(CngKey.Import(cngBytes, CngKeyBlobFormat.GenericPublicBlob));
}
else
{
var ecdsa = ECDsa.Create();
ecdsa.ImportSubjectPublicKeyInfo(pemBytes, out _);
return ecdsa;
}
#endif
}
private static byte[] GetPemBytesFromCngPrivateKeyBytes(byte[] cngEcPrivateBlob)
{
var derPrefix = new byte[] { 0x30, 0x81, 0xA2, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x04, 0x79, 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20 };
var derMid = new byte[] { 0xA0, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0xA1, 0x44, 0x03, 0x42, 0x00, 0x04 };
var derPostfix = new byte[] { 0xA0, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x31, 0x04, 0x03, 0x02, 0x00, 0x90 };
var publicKey = cngEcPrivateBlob.Skip(8).Take(64);
var privateKey = cngEcPrivateBlob.Skip(8 + 64).Take(32);
return derPrefix.Concat(privateKey).Concat(derMid).Concat(publicKey).Concat(derPostfix).ToArray();
}
private static byte[] GetCngBytesFromPemPrivateKeyBytes(byte[] PemEcPrivateKey)
{
var cngPrefix = new byte[] { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
var derPrefixLen = 36;
var derMidLen = 18;
var privateKey = PemEcPrivateKey.Skip(derPrefixLen).Take(32).ToArray();
var publicKey = PemEcPrivateKey.Skip(derPrefixLen + 32 + derMidLen).Take(64).ToArray();
return cngPrefix.Concat(publicKey).Concat(privateKey).ToArray();
}
private static bool StartsWith(byte[] data, byte[] prefix)
{
_ = data ?? throw new ArgumentNullException(nameof(data));
_ = prefix ?? throw new ArgumentNullException(nameof(prefix));
if (data.Length < prefix.Length)
return false;
for (var i = 0; i < prefix.Length; i++)
{
if (data[i] != prefix[i])
return false;
}
return true;
}
}
}