forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTWX509.cpp
More file actions
91 lines (80 loc) · 2.45 KB
/
Copy pathTWX509.cpp
File metadata and controls
91 lines (80 loc) · 2.45 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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include <TrustWalletCore/TWX509.h>
#include "../PublicKey.h"
using namespace TW;
TWData *_Nullable TWX509EncodeED25519PublicKey(TWData *_Nonnull publicKey) {
size_t dataSize = TWDataSize(publicKey);
uint8_t* dataBytes = TWDataBytes(publicKey);
if (dataSize != PublicKey::ed25519Size) {
return nullptr;
}
size_t totlen = 12 + dataSize;
TWData* rv = TWDataCreateWithSize(totlen);
size_t idx = 0;
// sequence
TWDataSet(rv, idx++, 0x30);
TWDataSet(rv, idx++, (uint8_t)(totlen - 2));
// Algorithm Identifier
// sequence
TWDataSet(rv, idx++, 0x30);
TWDataSet(rv, idx++, 5);
// OID
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb540809%28v=vs.85%29.aspx
TWDataSet(rv, idx++, 0x06);
TWDataSet(rv, idx++, 3);
TWDataSet(rv, idx++, (1 * 40) + 3);
TWDataSet(rv, idx++, 101);
TWDataSet(rv, idx++, (uint8_t) 112);
// params - absent
// the key
TWDataSet(rv, idx++, 0x03); // bit string
TWDataSet(rv, idx++, (byte) (1 + dataSize));
TWDataSet(rv, idx++, 0); // number of trailing unused bits
TWDataReplaceBytes(rv, idx, dataSize, dataBytes);
return rv;
}
TWData *_Nullable TWX509DecodeED25519PublicKey(TWData *_Nonnull data) {
size_t dataSize = TWDataSize(data);
uint8_t* dataBytes = TWDataBytes(data);
//
// Setup
//
size_t totlen = 44;
size_t idlen = 5;
//
// Pre-decoding check
//
if (dataSize != totlen) {
return nullptr;
}
uint8_t doid = dataBytes[8];
if (doid != 112) {
return nullptr;
}
//
// Decoding
//
size_t idx = 0;
if (dataBytes[idx++] != 0x30 ||
dataBytes[idx++] != (totlen - 2) ||
dataBytes[idx++] != 0x30 ||
dataBytes[idx++] != idlen ||
dataBytes[idx++] != 0x06 ||
dataBytes[idx++] != 3 ||
dataBytes[idx++] != (1 * 40) + 3 ||
dataBytes[idx++] != 101) {
return nullptr;
}
idx++; // OID, checked above
if (dataBytes[idx++] != 0x03 ||
dataBytes[idx++] != 33 ||
dataBytes[idx++] != 0) {
return nullptr;
}
TWData* rv = TWDataCreateWithBytes(dataBytes + idx, PublicKey::ed25519Size);
return rv;
}