forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebAuthn.cpp
123 lines (96 loc) · 3.48 KB
/
WebAuthn.cpp
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#include "Cbor.h"
#include "PublicKey.h"
#include <utility>
#include <algorithm>
#include <optional>
namespace TW::WebAuthn {
// https://www.w3.org/TR/webauthn-2/#authenticator-data
struct AuthData {
Data rpIdHash;
std::array<std::uint8_t, 1> flagsBuf;
struct {
bool up;
bool uv;
bool at;
bool ed;
std::uint8_t flagsInt;
} flags;
std::uint32_t counter;
Data counterBuf;
Data aaguid;
Data credID;
Data COSEPublicKey;
};
AuthData parseAuthData(const Data& buffer) {
AuthData authData;
authData.rpIdHash = subData(buffer, 0, 32);
auto it = buffer.begin() + 32;
authData.flagsBuf = { *it };
++it;
std::uint8_t flagsInt = authData.flagsBuf[0];
authData.flags.up = !!(flagsInt & 0x01);
authData.flags.uv = !!(flagsInt & 0x04);
authData.flags.at = !!(flagsInt & 0x40);
authData.flags.ed = !!(flagsInt & 0x80);
authData.flags.flagsInt = flagsInt;
authData.counterBuf = Data(it, it + 4);
authData.counter = static_cast<std::uint32_t>((authData.counterBuf[0] << 24) |
(authData.counterBuf[1] << 16) |
(authData.counterBuf[2] << 8) |
authData.counterBuf[3]);
it += 4;
if (authData.flags.at) {
authData.aaguid = Data(it, it + 16);
it += 16;
std::array<std::uint8_t, 2> credIDLenBuf = { *(it), *(it + 1) };
std::uint16_t credIDLen = static_cast<std::uint16_t>((credIDLenBuf[0] << 8) |
credIDLenBuf[1]);
it += 2;
authData.credID = Data(it, it + credIDLen);
it += credIDLen;
authData.COSEPublicKey = Data(it, buffer.end());
}
return authData;
}
auto findIntKey = [](const auto& map, const auto& key) {
return std::find_if(map.begin(), map.end(), [&](const auto& p) {
return p.first.dumpToString() == key;
});
};
auto findStringKey = [](const auto& map, const auto& key) {
return std::find_if(map.begin(), map.end(), [&](const auto& p) {
return p.first.getString() == key;
});
};
std::optional<PublicKey> getPublicKey(const Data& attestationObject) {
const Data authData = findStringKey(TW::Cbor::Decode(attestationObject).getMapElements(), "authData")->second.getBytes();
if (authData.empty()) {
return std::nullopt;
}
const AuthData authDataParsed = parseAuthData(authData);
const auto COSEPublicKey = TW::Cbor::Decode(authDataParsed.COSEPublicKey).getMapElements();
if (COSEPublicKey.empty()) {
return std::nullopt;
}
// https://www.w3.org/TR/webauthn-2/#sctn-encoded-credPubKey-examples
const std::string xKey = "-2";
const std::string yKey = "-3";
const auto x = findIntKey(COSEPublicKey, xKey);
const auto y = findIntKey(COSEPublicKey, yKey);
Data publicKey;
append(publicKey, 0x04);
append(publicKey, x->second.getBytes());
append(publicKey, y->second.getBytes());
return PublicKey(publicKey, TWPublicKeyTypeNIST256p1Extended);
}
Data reconstructSignedMessage(const Data& authenticatorData, const Data& clientDataJSON) {
const auto& clientHash = Hash::sha256(clientDataJSON);
Data message;
append(message, authenticatorData);
append(message, clientHash);
return Hash::sha256(message);
}
} // namespace TW::Webauthn