-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWritableIdentityCredential.cpp
582 lines (466 loc) · 21.4 KB
/
WritableIdentityCredential.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
**
** Copyright 2018, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define LOG_TAG "[email protected]"
#include <log/log.h>
#include "WritableIdentityCredential.h"
#include "IdentityCredentialStore.h"
#include "ICUtils.h"
#include <cn-cbor/cn-cbor.h>
using ::android::hardware::secure_element::V1_0::SecureElementStatus;
using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
using ::android::hardware::keymaster::capability::V1_0::CapabilityType;
namespace android {
namespace hardware {
namespace identity_credential {
namespace V1_0 {
namespace implementation {
static constexpr uint8_t kCLAProprietary = 0x80;
static constexpr uint8_t kINSCreateCredential = 0x10;
static constexpr uint8_t kINSGetAttestationCertificate = 0x11;
static constexpr uint8_t kINSPersonalizeAccessControl = 0x12;
static constexpr uint8_t kINSPersonalizeNamespace = 0x13;
static constexpr uint8_t kINSPersonalizeAttribute = 0x14;
static constexpr uint8_t kINSSignPersonalizedData = 0x15;
static constexpr uint8_t kMaxAttestChallengeSize = 8;
WritableIdentityCredential::~WritableIdentityCredential(){
mAppletConnection.close();
}
Result WritableIdentityCredential::initializeCredential(const hidl_string& docType,
bool testCredential) {
if (!mAppletConnection.connectToSEService()) {
return result(ResultCode::FAILED, "Error while trying to connect to SE service.");
}
// Initiate communication to applet
if (!mAppletConnection.isChannelOpen()) {
ResponseApdu selectResponse = mAppletConnection.openChannelToApplet();
if (!selectResponse.ok() || selectResponse.status() != AppletConnection::SW_OK) {
return result(ResultCode::FAILED, "Could not select the applet. ");
}
}
// Reste the current state
resetPersonalizationState();
// Check docType size
if (docType.size() > 255) {
return result(ResultCode::INVALID_DATA, "DocType string too long.");
}
mDocType = docType;
mIsTestCredential = testCredential;
// Send the command to the applet to create a new credential
CommandApdu command{kCLAProprietary, kINSCreateCredential, 0,
mIsTestCredential, mDocType.size(), 256};
std::string cred = mDocType;
std::copy(cred.begin(), cred.end(), command.dataBegin());
ResponseApdu response = mAppletConnection.transmit(command);
if (!response.ok() || (response.status() != AppletConnection::SW_OK)) {
mAppletConnection.close();
return swToErrorMessage(response, "Error initializing credential");
}
cn_cbor_errback err;
auto cbor_resultBlob = CBORPtr(cn_cbor_decode(&(*response.dataBegin()), response.dataSize(), &err));
if(cbor_resultBlob.get() == nullptr || cbor_resultBlob.get()->type != CN_CBOR_BYTES){
return result(ResultCode::INVALID_DATA, "SE response could not be decoded.");
}
auto len = cbor_resultBlob.get()->length;
mCredentialBlob.resize(len);
std::copy(cbor_resultBlob.get()->v.bytes, cbor_resultBlob.get()->v.bytes + len,
mCredentialBlob.begin());
return resultOk();
}
void WritableIdentityCredential::resetPersonalizationState(){
mCredentialBlob.clear();
mPersonalizationStarted = false;
mCurrentNamespaceEntryCount = 0;
mCurrentNamespaceId = 0;
mCurrentValueEncryptedContent = 0;
mCurrentValueEntrySize = 0;
mAccessControlProfilesPersonalized = 0;
}
bool WritableIdentityCredential::verifyAppletPersonalizationStatus() {
if (!mAppletConnection.isChannelOpen()) {
ALOGE("No connection to applet");
return false;
}
if (!mPersonalizationStarted) {
ALOGE("Personalization not started yet");
return false;
}
return true;
}
Return<void> WritableIdentityCredential::getAttestationCertificate(
const hidl_vec<uint8_t>& attestationChallenge,
getAttestationCertificate_cb _hidl_cb) {
hidl_vec<uint8_t> cert;
if (!mAppletConnection.isChannelOpen()) {
ResponseApdu selectResponse = mAppletConnection.openChannelToApplet();
if (!selectResponse.ok() || selectResponse.status() != AppletConnection::SW_OK) {
_hidl_cb(result(ResultCode::FAILED, "Could not select the applet. "), cert);
return Void();
}
}
cn_cbor_errback err;
if (attestationChallenge.size() > kMaxAttestChallengeSize) {
_hidl_cb(result(ResultCode::INVALID_DATA,
"Challenge is too large. Maximum length is %d bytes. ",
kMaxAttestChallengeSize),
cert);
return Void();
}
// Request for the attestation certificate
CommandApdu command{kCLAProprietary, kINSGetAttestationCertificate, 0, 0, kMaxAttestChallengeSize, 0};
std::copy(attestationChallenge.begin(), attestationChallenge.end(), command.dataBegin());
ResponseApdu response = mAppletConnection.transmit(command);
if (!response.ok() || response.status() != AppletConnection::SW_OK) {
_hidl_cb(swToErrorMessage(response, "Attestation certificate creation failed. "), cert);
return Void();
}
auto cbor_attestCert = CBORPtr(cn_cbor_decode(&(*response.dataBegin()), response.dataSize(), &err));
if (cbor_attestCert.get() == nullptr || cbor_attestCert.get()->type != CN_CBOR_BYTES ) {
_hidl_cb(result(ResultCode::FAILED, "Error decoding SE response. "), cert);
return Void();
}
cert.resize(cbor_attestCert->length);
std::copy(cbor_attestCert->v.bytes, cbor_attestCert->v.bytes + cbor_attestCert->length,
cert.begin());
_hidl_cb(resultOk(), cert);
return Void();
}
Return<void> WritableIdentityCredential::startPersonalization(uint8_t accessControlProfileCount,
const hidl_vec<uint16_t>& entryCounts,
startPersonalization_cb _hidl_cb) {
// Initiate communication to applet
if (!mAppletConnection.isChannelOpen()) {
ResponseApdu selectResponse = mAppletConnection.openChannelToApplet();
if (!selectResponse.ok() || selectResponse.status() != AppletConnection::SW_OK) {
_hidl_cb(result(ResultCode::FAILED, "Could not select the applet. "));
return Void();
}
}
if (entryCounts.size() <= 0) {
_hidl_cb(result(ResultCode::INVALID_DATA, "Nothing to personalize."));
return Void();
}
mNamespaceEntries = entryCounts;
mAccessControlProfileCount = accessControlProfileCount;
mPersonalizationStarted = true;
_hidl_cb(resultOk());
return Void();
}
Return<void> WritableIdentityCredential::addAccessControlProfile(
uint8_t id, const hidl_vec<uint8_t>& readerCertificate, uint64_t capabilityId,
const CapabilityType capabilityType, uint32_t timeout, addAccessControlProfile_cb _hidl_cb) {
SecureAccessControlProfile acpResult;
if (!verifyAppletPersonalizationStatus()) {
_hidl_cb(result(ResultCode::FAILED, "Personalization not started yet. "), acpResult);
return Void();
}
// Set the number of profiles in p2
uint8_t p1 = 0u;
uint8_t p2 = mAccessControlProfileCount & 0xFF;
acpResult.id = id;
// Check if reader authentication is specified
if (readerCertificate.size() != 0u) {
if (getECPublicKeyFromCertificate(readerCertificate).size() == 0) {
_hidl_cb(result(ResultCode::INVALID_DATA, "Certificate parsing error."), acpResult);
return Void();
}
acpResult.readerCertificate = readerCertificate;
}
// Check if user authentication is specified
if (capabilityId != 0u) {
acpResult.capabilityId = capabilityId;
acpResult.capabilityType = capabilityType;
acpResult.timeout = timeout;
} else {
acpResult.capabilityId = 0u;
acpResult.capabilityType = CapabilityType::NOT_APPLICABLE;
acpResult.timeout = 0u;
}
cn_cbor_errback err;
auto acp = CBORPtr(
encodeCborAccessControlProfile(id, getECPublicKeyFromCertificate(readerCertificate),
capabilityId, capabilityType, timeout));
if (acp.get() == nullptr) {
_hidl_cb(result(ResultCode::FAILED,
"Error encoding the access control profile as CBOR. "),
acpResult);
return Void();
}
// Data of command APDU is a CBOR array with the specified authentication parameters
// Send command
CommandApdu command =
createCommandApduFromCbor(kINSPersonalizeAccessControl, p1, p2, acp.get(), &err);
if (err.err != CN_CBOR_NO_ERROR) {
_hidl_cb(result(ResultCode::FAILED,
"Error encoding the access control profile as CBOR. "),
acpResult);
return Void();
}
ResponseApdu response = mAppletConnection.transmit(command);
// Check response
if (!response.ok() || response.status() != AppletConnection::SW_OK) {
_hidl_cb(swToErrorMessage(response, "Error personalizing access control profile"), acpResult);
return Void();
}
// Get the byte string in the response
auto cbor_mac = CBORPtr(cn_cbor_decode(&(*response.dataBegin()), response.dataSize(), &err));
if(cbor_mac.get() == nullptr || cbor_mac.get()->type != CN_CBOR_BYTES){
_hidl_cb(result(ResultCode::FAILED, "Error decoding SE response. "), acpResult);
return Void();
}
auto len = cbor_mac.get()->length;
acpResult.mac.resize(len);
std::copy(cbor_mac.get()->v.bytes, cbor_mac.get()->v.bytes + len, acpResult.mac.begin());
mAccessControlProfilesPersonalized++;
_hidl_cb(resultOk(), acpResult);
return Void();
}
Return<void> WritableIdentityCredential::beginAddEntry(
const hidl_vec<uint8_t>& accessControlProfiles, const hidl_string& nameSpace,
const hidl_string& name, bool directlyAvailable, uint32_t entrySize,
beginAddEntry_cb _hidl_cb) {
if (!verifyAppletPersonalizationStatus()) {
_hidl_cb(result(ResultCode::FAILED, "Personalization not started yet. "));
return Void();
}
if (mAccessControlProfilesPersonalized != mAccessControlProfileCount) {
_hidl_cb(result(ResultCode::FAILED,
"Need to finish access control profile configuration first. "));
return Void();
}
// Set the number of entries in p1p2
uint8_t p1 = 0;
uint8_t p2 = 0;
cn_cbor_errback err;
// Check if a new namespace has started
if (mCurrentNamespaceEntryCount == 0 && mCurrentNamespaceName != nameSpace) {
// Set the number of namespaces in p1p2
p1 = (mNamespaceEntries.size() >> 8) & 0x3F;
p2 = mNamespaceEntries.size() & 0xFF;
mCurrentNamespaceEntryCount = mNamespaceEntries[mCurrentNamespaceId];
auto commandData =
CBORPtr(encodeCborNamespaceConf(nameSpace, mCurrentNamespaceEntryCount));
if (commandData.get() == nullptr) {
_hidl_cb(result(ResultCode::INVALID_DATA, "Error encoding namespace."));
return Void();
}
CommandApdu command = createCommandApduFromCbor(kINSPersonalizeNamespace, p1, p2,
commandData.get(), &err);
if (err.err != CN_CBOR_NO_ERROR) {
_hidl_cb(result(ResultCode::FAILED, "Error encoding new CBOR structure."));
return Void();
}
ResponseApdu response = mAppletConnection.transmit(command);
if (response.ok() && response.status() == AppletConnection::SW_OK) {
mCurrentNamespaceName = nameSpace;
mCurrentNamespaceId++;
} else {
_hidl_cb(swToErrorMessage(response, "Error during namespace initialization"));
return Void();
}
} else if (mCurrentNamespaceName != nameSpace) {
_hidl_cb(result(ResultCode::FAILED,
"Cannot start a new namespace, %hu entries remain to be added.",
mCurrentNamespaceEntryCount));
return Void();
} else if (mCurrentNamespaceEntryCount == 0) {
_hidl_cb(result(ResultCode::FAILED,
"No more entries remain to be added for this namespace."));
return Void();
}
p1 = 0;
p2 = 0;
// If this is a directly available entry, set the upper most flag
if (directlyAvailable) {
p1 |= 0x80;
}
// Encode the additional data and send it to the applet
auto commandData =
CBORPtr(encodeCborAdditionalData(nameSpace, name, accessControlProfiles));
if (commandData.get() == nullptr) {
_hidl_cb(result(ResultCode::FAILED, "Error encoding additional data as CBOR."));
return Void();
}
CommandApdu command =
createCommandApduFromCbor(kINSPersonalizeAttribute, p1, p2, commandData.get(), &err);
if (err.err != CN_CBOR_NO_ERROR) {
_hidl_cb(result(ResultCode::FAILED, "Error encoding additional data as CBOR."));
return Void();
}
ResponseApdu response = mAppletConnection.transmit(command);
if (response.ok() && response.status() == AppletConnection::SW_OK) {
mCurrentValueEncryptedContent = 0;
mCurrentValueEntrySize = entrySize;
mCurrentValueDirectlyAvailable = directlyAvailable;
}
_hidl_cb(swToErrorMessage(response, ""));
return Void();
}
Return<void> WritableIdentityCredential::addEntryValue(const EntryValue& value,
addEntryValue_cb _hidl_cb) {
hidl_vec<uint8_t> encryptedVal;
if (!verifyAppletPersonalizationStatus()) {
_hidl_cb(result(ResultCode::FAILED, "Personalization not started yet. "), encryptedVal);
return Void();
}
uint8_t p1 = 0;
uint8_t p2 = 0;
int64_t stringSize = -1;
cn_cbor_errback err;
auto cmdData = CBORPtr(nullptr);
// START Data entry
switch(value.getDiscriminator()){
case EntryValue::hidl_discriminator::integer:
cmdData = CBORPtr(cn_cbor_int_create(value.integer(), &err));
break;
case EntryValue::hidl_discriminator::textString:
stringSize = value.textString().size();
cmdData = CBORPtr(cn_cbor_string_create((char*) value.textString().data(), &err));
cmdData.get()->length = stringSize;
break;
case EntryValue::hidl_discriminator::byteString:
stringSize = value.byteString().size();
cmdData = CBORPtr(cn_cbor_data_create(value.byteString().data(), stringSize, &err));
break;
case EntryValue::hidl_discriminator::booleanValue:
cmdData = CBORPtr(encodeCborBoolean(value.booleanValue(), &err));
break;
break;
default: // Should never happen
_hidl_cb(result(ResultCode::INVALID_DATA, "Invalid data entry."), encryptedVal);
return Void();
break;
}
// END Data entry
if (cmdData.get() == nullptr || err.err != CN_CBOR_NO_ERROR) {
_hidl_cb(result(ResultCode::FAILED, "Error in CBOR initalization."), encryptedVal);
return Void();
}
if (mCurrentValueDirectlyAvailable) {
p1 |= 0x80; // Bit 8 indicates if this is a directly available entry
}
std::vector<uint8_t> buffer = encodeCborAsVector(cmdData.get(), &err);
if (stringSize != -1) {
if (stringSize != mCurrentValueEntrySize) { // Chunking
p1 |= 0x4; // Bit 3 indicates chunking
if (mCurrentValueEncryptedContent == 0) {
// First chunk, need to encode the full length at the beginning
auto entrySize = CBORPtr(cn_cbor_int_create(mCurrentValueEntrySize, &err));
std::vector<uint8_t> encodedEntrySize = encodeCborAsVector(entrySize.get(), &err);
// Major type from data buffer
encodedEntrySize[0] &= 0x1F;
encodedEntrySize[0] |= buffer[0] & 0xE0;
// Copy type and length to buffer
if (encodedEntrySize.size() + stringSize > buffer.size()) {
uint8_t diff = buffer.size() - (encodedEntrySize.size() + stringSize);
buffer.resize(encodedEntrySize.size() + stringSize);
std::rotate(buffer.begin(), buffer.end() - diff, buffer.end());
}
std::copy(encodedEntrySize.begin(), encodedEntrySize.end(), buffer.begin());
} else { //
p1 |= 0x2; // Bit 2 indicates a chunk "inbetween"
}
}
mCurrentValueEncryptedContent += stringSize;
// Validate that the entry is not too large
if (mCurrentValueEncryptedContent > mCurrentValueEntrySize) {
_hidl_cb(result(ResultCode::FAILED, "Entry value is exceeding the defined entry size"),
encryptedVal);
return Void();
} else if (mCurrentValueEncryptedContent != mCurrentValueEntrySize &&
stringSize != mAppletConnection.chunkSize()) {
_hidl_cb(result(ResultCode::FAILED, "Entry size does not match chunk size"),
encryptedVal);
return Void();
}
}
if (stringSize == -1 || mCurrentValueEncryptedContent == mCurrentValueEntrySize){
p1 |= 0x1; // Indicates that this is the last (or only) value in chain
}
CommandApdu command{kCLAProprietary, kINSPersonalizeAttribute, p1, p2, buffer.size(), 0};
std::copy(buffer.begin(), buffer.end(), command.dataBegin());
ResponseApdu response = mAppletConnection.transmit(command);
if (!response.ok() || response.status() != AppletConnection::SW_OK) {
_hidl_cb(swToErrorMessage(response, "Error personalizing attribute"), encryptedVal);
return Void();
}
encryptedVal.resize(response.dataSize());
std::copy(response.dataBegin(), response.dataEnd(), encryptedVal.begin());
if (stringSize == -1 || mCurrentValueEncryptedContent == mCurrentValueEntrySize) {
// Finish this entry
mCurrentNamespaceEntryCount--;
}
_hidl_cb(resultOk(), encryptedVal);
return Void();
}
Return<void> WritableIdentityCredential::finishAddingEntries(finishAddingEntries_cb _hidl_cb) {
hidl_vec<uint8_t> signature;
if (!verifyAppletPersonalizationStatus()) {
_hidl_cb(result(ResultCode::FAILED, "Personalization not started yet. "), signature, signature);
return Void();
}
// Check if this was the last entry in the last namespace
if (mCurrentNamespaceEntryCount != 0 || mCurrentNamespaceId != mNamespaceEntries.size()) {
_hidl_cb(result(ResultCode::FAILED,
"Missing entries to personalize. Personalized %d of %d entries.",
mNamespaceEntries[mCurrentNamespaceId] - mCurrentNamespaceEntryCount,
mNamespaceEntries[mCurrentNamespaceId]),
signature, signature);
return Void();
}
// Retrieve signedData
CommandApdu signDataCmd{kCLAProprietary, kINSSignPersonalizedData, 0, 0};
ResponseApdu signResponse = mAppletConnection.transmit(signDataCmd);
if (!signResponse.ok() || signResponse.status() != AppletConnection::SW_OK) {
_hidl_cb(swToErrorMessage(signResponse, "Signature creation failed"), signature, signature);
// Personalization failed
mPersonalizationStarted = false;
mAppletConnection.close();
return Void();
}
// Success, prepare return data
// Combine credential information into CBOR credentialData structure
cn_cbor_errback err;
cn_cbor* credentialData = cn_cbor_array_create(&err);
std::vector<uint8_t> resultCredData;
if (!cn_cbor_array_append(credentialData, cn_cbor_string_create(mDocType.c_str(), &err), &err) ||
!cn_cbor_array_append(credentialData, encodeCborBoolean(mIsTestCredential, &err), &err) ||
!cn_cbor_array_append(credentialData, cn_cbor_data_create(mCredentialBlob.data(),
mCredentialBlob.size(), &err), &err)) {
_hidl_cb(result(ResultCode::FAILED, "Error encoding credentialData as CBOR structure. "),
resultCredData, signature);
return Void();
}
// Return values
signature.resize(signResponse.dataSize());
std::copy(signResponse.dataBegin(), signResponse.dataEnd(), signature.begin());
resultCredData = encodeCborAsVector(credentialData, &err);
if (err.err == CN_CBOR_NO_ERROR) {
_hidl_cb(resultOk(), resultCredData, signature);
} else {
_hidl_cb(result(ResultCode::FAILED, "Error encoding credentialData as CBOR structure. "),
resultCredData, signature);
}
// Finish personalization
mPersonalizationStarted = false;
mAppletConnection.close();
return Void();
}
} // namespace implementation
} // namespace V1_0
} // namespace identity_credential
} // namespace hardware
} // namespace android