-
Notifications
You must be signed in to change notification settings - Fork 138
/
ProtectedPrivateKey.cs
404 lines (372 loc) · 16 KB
/
ProtectedPrivateKey.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
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
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.Json;
using Libplanet.Crypto;
using Libplanet.KeyStore.Ciphers;
using Libplanet.KeyStore.Kdfs;
using Org.BouncyCastle.Crypto.Digests;
namespace Libplanet.KeyStore
{
/// <summary>
/// Protects a <see cref="PrivateKey"/> with a passphrase (i.e., password).
/// </summary>
public sealed class ProtectedPrivateKey
{
/// <summary>
/// Loads a protected private key.
/// </summary>
/// <param name="address">The address of the protected private key.</param>
/// <param name="kdf">A key derivation function to derive a symmetric key to decrypt
/// a <see cref="PrivateKey"/>.</param>
/// <param name="mac">MAC digest to check if a derived key is correct or not.</param>
/// <param name="cipher">A symmetric cipher to decrypt a <see cref="PrivateKey"/>.</param>
/// <param name="ciphertext">An encrypted <see cref="PrivateKey"/>.</param>
public ProtectedPrivateKey(
Address address,
IKdf kdf,
byte[] mac,
ICipher cipher,
byte[] ciphertext
)
: this(
address,
kdf,
ImmutableArray.Create(mac),
cipher,
ImmutableArray.Create(ciphertext)
)
{
}
/// <summary>
/// Loads a protected private key.
/// </summary>
/// <param name="address">The address of the protected private key.</param>
/// <param name="kdf">A key derivation function to derive a symmetric key to decrypt
/// a <see cref="PrivateKey"/>.</param>
/// <param name="mac">MAC digest to check if a derived key is correct or not.</param>
/// <param name="cipher">A symmetric cipher to decrypt a <see cref="PrivateKey"/>.</param>
/// <param name="ciphertext">An encrypted <see cref="PrivateKey"/>.</param>
public ProtectedPrivateKey(
Address address,
IKdf kdf,
ImmutableArray<byte> mac,
ICipher cipher,
ImmutableArray<byte> ciphertext
)
{
Address = address;
Kdf = kdf;
Mac = mac;
Cipher = cipher;
Ciphertext = ciphertext;
}
/// <summary>
/// The address of the protected private key.
/// </summary>
public Address Address { get; }
/// <summary>
/// A key derivation function to derive a symmetric key to decrypt
/// a <see cref="PrivateKey"/>.
/// </summary>
[Pure]
public IKdf Kdf { get; }
[Pure]
public ImmutableArray<byte> Mac { get; }
/// <summary>
/// A symmetric cipher to decrypt a <see cref="PrivateKey"/>.
/// </summary>
[Pure]
public ICipher Cipher { get; }
/// <summary>
/// An encrypted <see cref="PrivateKey"/>.
/// </summary>
[Pure]
public ImmutableArray<byte> Ciphertext { get; }
/// <summary>
/// Protects a bare <see cref="PrivateKey"/> using a user input
/// <paramref name="passphrase"/>.
/// </summary>
/// <param name="privateKey">A bare private key to protect.</param>
/// <param name="passphrase">A user input passphrase (i.e., password).</param>
/// <returns>A passphrase-protected private key.</returns>
[Pure]
public static ProtectedPrivateKey Protect(PrivateKey privateKey, string passphrase)
{
var salt = new byte[32];
using RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(salt);
var kdf = new Pbkdf2<Sha256Digest>(10240, salt, 32);
ImmutableArray<byte> derivedKey = kdf.Derive(passphrase);
ImmutableArray<byte> encKey = MakeEncryptionKey(derivedKey);
var iv = new byte[16];
rng.GetBytes(iv);
var cipher = new Aes128Ctr(iv);
ImmutableArray<byte> ciphertext = cipher.Encrypt(encKey, privateKey.ByteArray);
ImmutableArray<byte> mac = CalculateMac(derivedKey, ciphertext);
Address address = privateKey.ToAddress();
return new ProtectedPrivateKey(address, kdf, mac, cipher, ciphertext);
}
/// <summary>
/// Loads a <see cref="ProtectedPrivateKey"/> from a JSON, according to Ethereum's
/// <a href="https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition">Web3
/// Secret Storage Definition</a>.
/// </summary>
/// <param name="json">A JSON string that encodes a <see cref="ProtectedPrivateKey"/>.
/// </param>
/// <returns>A protected private key loaded from the given <paramref name="json"/>.
/// </returns>
/// <exception cref="JsonException">Thrown when the given <paramref name="json"/> is not
/// a valid JSON.</exception>
/// <exception cref="InvalidKeyJsonException">Thrown when the given key data lacks some
/// required fields or consists of wrong types.</exception>
/// <exception cref="UnsupportedKeyJsonException">Thrown when the given key data depends on
/// an unsupported features (e.g., KDF).</exception>
public static ProtectedPrivateKey FromJson(string json)
{
var options = new JsonDocumentOptions
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip,
};
using JsonDocument doc = JsonDocument.Parse(json, options);
JsonElement rootElement = doc.RootElement;
if (rootElement.ValueKind != JsonValueKind.Object)
{
throw new InvalidKeyJsonException(
"The root of the key JSON must be an object, but it is a/an " +
$"{rootElement.ValueKind}."
);
}
if (!rootElement.TryGetProperty("version", out JsonElement versionElement))
{
throw new InvalidKeyJsonException(
"The key JSON must contain \"version\" field, but it lacks."
);
}
if (versionElement.ValueKind != JsonValueKind.Number ||
!versionElement.TryGetDecimal(out decimal versionNum))
{
throw new InvalidKeyJsonException("The \"version\" field must be a number.");
}
else if (versionNum != 3)
{
throw new UnsupportedKeyJsonException(
$"The key JSON format version {versionNum} is unsupported; " +
"Only version 3 is supported."
);
}
string GetStringProperty(JsonElement element, string fieldName)
{
if (!element.TryGetProperty(fieldName, out JsonElement fieldElement))
{
throw new InvalidKeyJsonException(
$"The key JSON must contain \"{fieldName}\" field, but it lacks."
);
}
string str;
try
{
str = fieldElement.GetString();
}
catch (InvalidOperationException)
{
throw new InvalidKeyJsonException(
$"The \"{fieldName}\" field must be a string."
);
}
if (str is null)
{
throw new InvalidKeyJsonException(
$"The \"{fieldName}\" field must not be null, but a string."
);
}
return str;
}
JsonElement GetObjectProperty(JsonElement element, string fieldName)
{
if (!element.TryGetProperty(fieldName, out var fieldElement))
{
throw new InvalidKeyJsonException(
$"The key JSON must contain \"{fieldName}\" field, but it lacks."
);
}
else if (fieldElement.ValueKind != JsonValueKind.Object)
{
throw new InvalidKeyJsonException(
$"The \"{fieldName}\" field must be an object, but it is a/an " +
$"{fieldElement.ValueKind}."
);
}
return fieldElement;
}
byte[] GetHexProperty(JsonElement element, string fieldName)
{
string str = GetStringProperty(element, fieldName);
byte[] bytes;
try
{
bytes = ByteUtil.ParseHex(str);
}
catch (Exception e)
{
throw new InvalidKeyJsonException(
$"The \"{fieldName}\" field must be a hexadecimal string.\n{e}"
);
}
return bytes;
}
JsonElement crypto = GetObjectProperty(rootElement, "crypto");
string cipherType = GetStringProperty(crypto, "cipher");
JsonElement cipherParamsElement = GetObjectProperty(crypto, "cipherparams");
byte[] ciphertext = GetHexProperty(crypto, "ciphertext");
byte[] mac = GetHexProperty(crypto, "mac");
string kdfType = GetStringProperty(crypto, "kdf");
JsonElement kdfParamsElement = GetObjectProperty(crypto, "kdfparams");
byte[] addressBytes = GetHexProperty(rootElement, "address");
Address address;
try
{
address = new Address(addressBytes);
}
catch (ArgumentException e)
{
throw new InvalidKeyJsonException(
"The \"address\" field must contain an Ethereum-style address which " +
"consists of 40 hexadecimal letters: " + e
);
}
var cipher = cipherType switch
{
"aes-128-ctr" => Aes128Ctr.FromJson(cipherParamsElement),
_ =>
throw new UnsupportedKeyJsonException(
$"Unsupported cipher type: \"{cipherType}\"."),
};
IKdf kdf;
try
{
kdf = kdfType switch
{
"pbkdf2" => Pbkdf2.FromJson(kdfParamsElement),
"scrypt" => Scrypt.FromJson(kdfParamsElement),
_ =>
throw new UnsupportedKeyJsonException(
$"Unsupported cipher type: \"{kdfType}\"."),
};
}
catch (ArgumentException e)
{
throw new InvalidKeyJsonException(e.Message);
}
return new ProtectedPrivateKey(address, kdf, mac, cipher, ciphertext);
}
/// <summary>
/// Gets the protected <see cref="PrivateKey"/> using a user input
/// <paramref name="passphrase"/>.
/// </summary>
/// <param name="passphrase">A user input passphrase (i.e., password).</param>
/// <returns>A bare <see cref="PrivateKey"/>.</returns>
/// <exception cref="IncorrectPassphraseException">Thrown when the given
/// <paramref name="passphrase"/> does not match to the <see cref="ProtectedPrivateKey"/>'s
/// passphrase.</exception>
/// <exception cref="MismatchedAddressException">Thrown when the unprotected
/// <see cref="PrivateKey"/> does not match to the expected <see cref="Address"/>.
/// </exception>
[Pure]
public PrivateKey Unprotect(string passphrase)
{
ImmutableArray<byte> derivedKey = Kdf.Derive(passphrase);
var mac = CalculateMac(derivedKey, Ciphertext);
if (!Mac.SequenceEqual(mac))
{
throw new IncorrectPassphraseException(
"The input passphrase is incorrect.",
nameof(passphrase),
Mac,
mac
);
}
ImmutableArray<byte> encKey = MakeEncryptionKey(derivedKey);
ImmutableArray<byte> plaintext = Cipher.Decrypt(encKey, Ciphertext);
var key = new PrivateKey(plaintext);
Address actualAddress = key.ToAddress();
if (!Address.Equals(actualAddress))
{
throw new MismatchedAddressException(
"The actual address of the unprotected private key does not match to " +
"the expected address of the protected private key.",
Address,
actualAddress
);
}
return key;
}
/// <summary>
/// Dumps the cipher parameters as a JSON representation according to Ethereum's
/// <a href="https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition">Web3
/// Secret Storage Definition</a>.
/// </summary>
/// <param name="writer">A JSON writer which has not begun object nor array.</param>
/// <param name="id">A unique identifier, which goes to the <c>id</c> field in the key JSON
/// file. If <c>null</c> (which is default) it is random-generated.</param>
public void WriteJson(Utf8JsonWriter writer, [Pure] in Guid? id = null)
{
writer.WriteStartObject();
writer.WriteNumber("version", 3);
writer.WriteString(
"id",
(id ?? Guid.NewGuid()).ToString().ToLower(CultureInfo.InvariantCulture)
);
writer.WriteString("address", Address.ToHex().ToLower(CultureInfo.InvariantCulture));
writer.WriteStartObject("crypto");
writer.WriteString("ciphertext", ByteUtil.Hex(Ciphertext));
writer.WritePropertyName("cipherparams");
string cipherName = Cipher.WriteJson(writer);
writer.WriteString("cipher", cipherName);
writer.WritePropertyName("kdfparams");
string kdfName = Kdf.WriteJson(writer);
writer.WriteString("kdf", kdfName);
writer.WriteString("mac", ByteUtil.Hex(Mac));
writer.WriteEndObject();
writer.WriteEndObject();
}
/// <summary>
/// Dumps the cipher parameters as a JSON representation according to Ethereum's
/// <a href="https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition">Web3
/// Secret Storage Definition</a>.
/// </summary>
/// <param name="stream">The destination for writing JSON text.</param>
/// <param name="id">A unique identifier, which goes to the <c>id</c> field in the key JSON
/// file. If <c>null</c> (which is default) it is random-generated.</param>
public void WriteJson(Stream stream, [Pure] in Guid? id = null)
{
using var writer = new Utf8JsonWriter(stream);
WriteJson(writer, id);
}
private static ImmutableArray<byte> MakeEncryptionKey(ImmutableArray<byte> derivedKey)
{
const int keySubBytes = 16;
return ImmutableArray.Create(derivedKey, 0, derivedKey.Length - keySubBytes);
}
private static ImmutableArray<byte> CalculateMac(
ImmutableArray<byte> derivedKey,
ImmutableArray<byte> ciphertext
)
{
const int keySubBytes = 16;
var seal = new byte[keySubBytes + ciphertext.Length];
derivedKey.CopyTo(derivedKey.Length - keySubBytes, seal, 0, keySubBytes);
ciphertext.CopyTo(seal, keySubBytes);
var digest = new KeccakDigest(256);
var mac = new byte[digest.GetDigestSize()];
digest.BlockUpdate(seal, 0, seal.Length);
digest.DoFinal(mac, 0);
return ImmutableArray.Create(mac);
}
}
}