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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AuthrsBridge_ffi.h"
#include "mozilla/Base64.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/AuthenticatorAttestationResponse.h"
#include "mozilla/dom/WebAuthenticationBinding.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_CLASS(AuthenticatorAttestationResponse)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
AuthenticatorAttestationResponse, AuthenticatorResponse)
tmp->mAttestationObjectCachedObj = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(AuthenticatorAttestationResponse,
AuthenticatorResponse)
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mAttestationObjectCachedObj)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
AuthenticatorAttestationResponse, AuthenticatorResponse)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_ADDREF_INHERITED(AuthenticatorAttestationResponse,
AuthenticatorResponse)
NS_IMPL_RELEASE_INHERITED(AuthenticatorAttestationResponse,
AuthenticatorResponse)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AuthenticatorAttestationResponse)
NS_INTERFACE_MAP_END_INHERITING(AuthenticatorResponse)
AuthenticatorAttestationResponse::AuthenticatorAttestationResponse(
nsPIDOMWindowInner* aParent)
: AuthenticatorResponse(aParent), mAttestationObjectCachedObj(nullptr) {
mozilla::HoldJSObjects(this);
}
AuthenticatorAttestationResponse::~AuthenticatorAttestationResponse() {
mozilla::DropJSObjects(this);
}
JSObject* AuthenticatorAttestationResponse::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return AuthenticatorAttestationResponse_Binding::Wrap(aCx, this, aGivenProto);
}
void AuthenticatorAttestationResponse::GetAttestationObject(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
if (!mAttestationObjectCachedObj) {
mAttestationObjectCachedObj =
ArrayBuffer::Create(aCx, mAttestationObject, aRv);
if (aRv.Failed()) {
return;
}
}
aValue.set(mAttestationObjectCachedObj);
}
void AuthenticatorAttestationResponse::SetAttestationObject(
const nsTArray<uint8_t>& aBuffer) {
mAttestationObject.Assign(aBuffer);
}
void AuthenticatorAttestationResponse::GetTransports(
nsTArray<nsString>& aTransports) {
aTransports.Assign(mTransports);
}
void AuthenticatorAttestationResponse::SetTransports(
const nsTArray<nsString>& aTransports) {
mTransports.Assign(aTransports);
}
nsresult AuthenticatorAttestationResponse::GetAuthenticatorDataBytes(
nsTArray<uint8_t>& aAuthenticatorData) {
if (!mAttestationObjectParsed) {
nsresult rv = authrs_webauthn_att_obj_constructor(
mAttestationObject, /* anonymize */ false,
getter_AddRefs(mAttestationObjectParsed));
if (NS_FAILED(rv)) {
return rv;
}
}
nsresult rv =
mAttestationObjectParsed->GetAuthenticatorData(aAuthenticatorData);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
void AuthenticatorAttestationResponse::GetAuthenticatorData(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
nsTArray<uint8_t> authenticatorData;
nsresult rv = GetAuthenticatorDataBytes(authenticatorData);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
JS::Heap<JSObject*> buffer(ArrayBuffer::Create(aCx, authenticatorData, aRv));
if (aRv.Failed()) {
return;
}
aValue.set(buffer);
}
nsresult AuthenticatorAttestationResponse::GetPublicKeyBytes(
nsTArray<uint8_t>& aPublicKeyBytes) {
if (!mAttestationObjectParsed) {
nsresult rv = authrs_webauthn_att_obj_constructor(
mAttestationObject, /* anonymize */ false,
getter_AddRefs(mAttestationObjectParsed));
if (NS_FAILED(rv)) {
return rv;
}
}
nsresult rv = mAttestationObjectParsed->GetPublicKey(aPublicKeyBytes);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
void AuthenticatorAttestationResponse::GetPublicKey(
JSContext* aCx, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) {
nsTArray<uint8_t> publicKey;
nsresult rv = GetPublicKeyBytes(publicKey);
if (NS_FAILED(rv)) {
if (rv == NS_ERROR_NOT_AVAILABLE) {
aValue.set(nullptr);
} else {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
}
return;
}
JS::Heap<JSObject*> buffer(ArrayBuffer::Create(aCx, publicKey, aRv));
if (aRv.Failed()) {
return;
}
aValue.set(buffer);
}
COSEAlgorithmIdentifier AuthenticatorAttestationResponse::GetPublicKeyAlgorithm(
ErrorResult& aRv) {
if (!mAttestationObjectParsed) {
nsresult rv = authrs_webauthn_att_obj_constructor(
mAttestationObject, false, getter_AddRefs(mAttestationObjectParsed));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return 0;
}
}
COSEAlgorithmIdentifier alg;
mAttestationObjectParsed->GetPublicKeyAlgorithm(&alg);
return alg;
}
void AuthenticatorAttestationResponse::ToJSON(
AuthenticatorAttestationResponseJSON& aJSON, ErrorResult& aError) {
nsAutoCString clientDataJSONBase64;
nsresult rv = Base64URLEncode(
mClientDataJSON.Length(),
reinterpret_cast<const uint8_t*>(mClientDataJSON.get()),
mozilla::Base64URLEncodePaddingPolicy::Omit, clientDataJSONBase64);
// This will only fail if the length is so long that it overflows 32-bits
// when calculating the encoded size.
if (NS_FAILED(rv)) {
aError.ThrowDataError("clientDataJSON too long");
return;
}
aJSON.mClientDataJSON.Assign(NS_ConvertUTF8toUTF16(clientDataJSONBase64));
nsTArray<uint8_t> authenticatorData;
rv = GetAuthenticatorDataBytes(authenticatorData);
if (NS_FAILED(rv)) {
aError.ThrowUnknownError("could not get authenticatorData");
return;
}
nsAutoCString authenticatorDataBase64;
rv = Base64URLEncode(authenticatorData.Length(), authenticatorData.Elements(),
mozilla::Base64URLEncodePaddingPolicy::Omit,
authenticatorDataBase64);
if (NS_FAILED(rv)) {
aError.ThrowDataError("authenticatorData too long");
return;
}
aJSON.mAuthenticatorData.Assign(
NS_ConvertUTF8toUTF16(authenticatorDataBase64));
if (!aJSON.mTransports.Assign(mTransports, mozilla::fallible)) {
aError.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
nsTArray<uint8_t> publicKeyBytes;
rv = GetPublicKeyBytes(publicKeyBytes);
if (NS_SUCCEEDED(rv)) {
nsAutoCString publicKeyBytesBase64;
rv = Base64URLEncode(publicKeyBytes.Length(), publicKeyBytes.Elements(),
mozilla::Base64URLEncodePaddingPolicy::Omit,
publicKeyBytesBase64);
if (NS_FAILED(rv)) {
aError.ThrowDataError("publicKey too long");
return;
}
aJSON.mPublicKey.Construct(NS_ConvertUTF8toUTF16(publicKeyBytesBase64));
} else if (rv != NS_ERROR_NOT_AVAILABLE) {
aError.ThrowUnknownError("could not get publicKey");
return;
}
COSEAlgorithmIdentifier publicKeyAlgorithm = GetPublicKeyAlgorithm(aError);
if (aError.Failed()) {
aError.ThrowUnknownError("could not get publicKeyAlgorithm");
return;
}
aJSON.mPublicKeyAlgorithm = publicKeyAlgorithm;
nsAutoCString attestationObjectBase64;
rv = Base64URLEncode(
mAttestationObject.Length(), mAttestationObject.Elements(),
mozilla::Base64URLEncodePaddingPolicy::Omit, attestationObjectBase64);
if (NS_FAILED(rv)) {
aError.ThrowDataError("attestationObject too long");
return;
}
aJSON.mAttestationObject.Assign(
NS_ConvertUTF8toUTF16(attestationObjectBase64));
}
} // namespace mozilla::dom
|