-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPMFiHWAuth.c
569 lines (494 loc) · 20.7 KB
/
HAPMFiHWAuth.c
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
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.
#include "HAP+Internal.h"
static const HAPLogObject logObject = { .subsystem = kHAP_LogSubsystem, .category = "MFiHWAuth" };
void HAPMFiHWAuthCreate(HAPMFiHWAuth* mfiHWAuth, HAPPlatformMFiHWAuthRef platformMFiHWAuth) {
HAPPrecondition(mfiHWAuth);
mfiHWAuth->platformMFiHWAuth = platformMFiHWAuth;
mfiHWAuth->powerOffTimer = 0;
}
void HAPMFiHWAuthRelease(HAPMFiHWAuth* mfiHWAuth) {
HAPPrecondition(mfiHWAuth);
if (!HAPMFiHWAuthIsSafeToRelease(mfiHWAuth)) {
HAPLog(&logObject, "Deinitializing Apple Authentication Coprocessor that does not report ready for power off.");
}
// Deinitialize timer.
if (mfiHWAuth->powerOffTimer) {
HAPPlatformTimerDeregister(mfiHWAuth->powerOffTimer);
mfiHWAuth->powerOffTimer = 0;
}
HAPRawBufferZero(mfiHWAuth, sizeof *mfiHWAuth);
}
HAP_RESULT_USE_CHECK
bool HAPMFiHWAuthIsSafeToRelease(HAPMFiHWAuth* mfiHWAuth) {
HAPPrecondition(mfiHWAuth);
if (!mfiHWAuth->platformMFiHWAuth) {
return true;
}
if (!HAPPlatformMFiHWAuthIsPoweredOn(HAPNonnull(mfiHWAuth->platformMFiHWAuth))) {
return false;
}
HAPError err;
// Read Authentication Protocol Version.
uint8_t protocolVersionMajor;
{
uint8_t bytes[1];
err = HAPPlatformMFiHWAuthRead(
HAPNonnull(mfiHWAuth->platformMFiHWAuth),
kHAPMFiHWAuthRegister_AuthenticationProtocolMajorVersion,
bytes,
sizeof bytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
HAPLog(&logObject, "Failed to read Authentication Protocol Major Version. Reporting safe to disable.");
return true;
}
protocolVersionMajor = bytes[0];
}
if (protocolVersionMajor == 2) {
// "The System Event Counter (SEC) is a non-volatile register that holds the current value of the CP's event
// counter. The event counter automatically decrements one count per second while the CP is powered,
// stopping at 0. If the accessory controls power to the CP, it must wait until the SEC has decremented to 0
// before removing power."
// See Accessory Interface Specification R29
// Section 69.8.2.14 System Event Counter
uint8_t bytes[1];
err = HAPPlatformMFiHWAuthRead(
HAPNonnull(mfiHWAuth->platformMFiHWAuth),
kHAPMFiHWAuthRegister_SystemEventCounter,
bytes,
sizeof bytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
HAPLog(&logObject, "Failed to read System Event Counter. Reporting safe to disable.");
return true;
}
uint8_t systemEventCounter = bytes[0];
HAPLogDebug(&logObject, "System Event Counter = %u.", systemEventCounter);
return systemEventCounter == 0;
}
return true;
}
static void PowerOffTimerExpired(HAPPlatformTimerRef timer, void* _Nullable context) {
HAPPrecondition(context);
HAPMFiHWAuth* mfiHWAuth = context;
HAPPrecondition(timer == mfiHWAuth->powerOffTimer);
mfiHWAuth->powerOffTimer = 0;
HAPPrecondition(mfiHWAuth->platformMFiHWAuth);
HAPError err;
HAPAssert(HAPPlatformMFiHWAuthIsPoweredOn(HAPNonnull(mfiHWAuth->platformMFiHWAuth)));
if (!HAPMFiHWAuthIsSafeToRelease(mfiHWAuth)) {
// Apple Authentication Coprocessor should not be disabled. Extend power off timer.
err = HAPPlatformTimerRegister(
&mfiHWAuth->powerOffTimer,
HAPPlatformClockGetCurrent() + 1 * HAPSecond,
PowerOffTimerExpired,
mfiHWAuth);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLog(&logObject, "Not enough resources to extend power off timer. Leaving HW on!");
}
} else {
HAPLogInfo(&logObject, "Turning off Apple Authentication Coprocessor.");
// Disable Apple Authentication Coprocessor.
HAPPlatformMFiHWAuthPowerOff(HAPNonnull(mfiHWAuth->platformMFiHWAuth));
}
}
/**
* Enables the Apple Authentication Coprocessor, if necessary.
*
* @param mfiHWAuth Apple Authentication Coprocessor manager.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If communication with the Apple Authentication Coprocessor failed.
*/
HAP_RESULT_USE_CHECK
static HAPError HAPMFiHWAuthEnable(HAPMFiHWAuth* mfiHWAuth) {
HAPPrecondition(mfiHWAuth);
HAPError err;
if (!mfiHWAuth->platformMFiHWAuth) {
return kHAPError_Unknown;
}
if (HAPPlatformMFiHWAuthIsPoweredOn(HAPNonnull(mfiHWAuth->platformMFiHWAuth))) {
return kHAPError_None;
}
HAPLogInfo(&logObject, "Turning on Apple Authentication Coprocessor.");
// Switch MFi on.
err = HAPPlatformMFiHWAuthPowerOn(HAPNonnull(mfiHWAuth->platformMFiHWAuth));
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Schedule checking for power off.
err = HAPPlatformTimerRegister(
&mfiHWAuth->powerOffTimer, HAPPlatformClockGetCurrent() + 3 * HAPSecond, PowerOffTimerExpired, mfiHWAuth);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLog(&logObject, "Not enough resources to start power off timer. Leaving HW on!");
}
return kHAPError_None;
}
#define HAP_MFI_HW_AUTH_READ_OR_RETURN_FAIL_VALUE(mfiHWAuth, registerAddress, bytes, numBytes, failValue) \
do { \
err = HAPPlatformMFiHWAuthRead( \
HAPNonnull((mfiHWAuth)->platformMFiHWAuth), (registerAddress), (bytes), (numBytes)); \
if (err) { \
HAPAssert(err == kHAPError_Unknown); \
return (failValue); \
} \
} while (0)
#define HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(mfiHWAuth, registerAddress, bytes, numBytes) \
HAP_MFI_HW_AUTH_READ_OR_RETURN_FAIL_VALUE(mfiHWAuth, registerAddress, bytes, numBytes, false)
#define HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(mfiHWAuth, registerAddress, bytes, numBytes) \
HAP_MFI_HW_AUTH_READ_OR_RETURN_FAIL_VALUE(mfiHWAuth, registerAddress, bytes, numBytes, kHAPError_Unknown)
#define HAP_MFI_HW_AUTH_WRITE_OR_RETURN_FAIL_VALUE(mfiHWAuth, bytes, numBytes, failValue) \
do { \
err = HAPPlatformMFiHWAuthWrite(HAPNonnull((mfiHWAuth)->platformMFiHWAuth), (bytes), (numBytes)); \
if (err) { \
HAPAssert(err == kHAPError_Unknown); \
return (failValue); \
} \
} while (0)
#define HAP_MFI_HW_AUTH_WRITE_OR_RETURN_FALSE(mfiHWAuth, bytes, numBytes) \
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_FAIL_VALUE(mfiHWAuth, bytes, numBytes, false)
#define HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(mfiHWAuth, bytes, numBytes) \
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_FAIL_VALUE(mfiHWAuth, bytes, numBytes, kHAPError_Unknown)
HAP_RESULT_USE_CHECK
bool HAPMFiHWAuthIsAvailable(HAPMFiHWAuth* mfiHWAuth) {
HAPPrecondition(mfiHWAuth);
HAPError err;
// Enable Apple Authentication Coprocessor.
err = HAPMFiHWAuthEnable(mfiHWAuth);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return false;
}
// Reset Error Code.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(mfiHWAuth, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
}
// Read Device Version.
uint8_t deviceVersion;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(mfiHWAuth, kHAPMFiHWAuthRegister_DeviceVersion, bytes, sizeof bytes);
deviceVersion = bytes[0];
}
// Read Authentication Revision.
uint8_t authenticationRevision;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(
mfiHWAuth, kHAPMFiHWAuthRegister_AuthenticationRevision, bytes, sizeof bytes);
authenticationRevision = bytes[0];
}
// Read Authentication Protocol Version.
uint8_t protocolVersionMajor;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(
mfiHWAuth, kHAPMFiHWAuthRegister_AuthenticationProtocolMajorVersion, bytes, sizeof bytes);
protocolVersionMajor = bytes[0];
}
uint8_t protocolVersionMinor;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(
mfiHWAuth, kHAPMFiHWAuthRegister_AuthenticationProtocolMinorVersion, bytes, sizeof bytes);
protocolVersionMinor = bytes[0];
}
// Check for error.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(mfiHWAuth, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
HAPMFiHWAuthError errorCode = (HAPMFiHWAuthError) bytes[0];
if (errorCode) {
HAPLog(&logObject, "Error occurred while getting information: 0x%02x.", errorCode);
return false;
}
}
// Log coprocessor information.
const char* deviceVersionString;
switch (deviceVersion) {
case kHAPMFiHWAuthDeviceVersion_2_0C: {
deviceVersionString = "2.0C";
} break;
case kHAPMFiHWAuthDeviceVersion_3_0: {
deviceVersionString = "3.0";
} break;
default: {
deviceVersionString = "Unknown";
} break;
}
HAPLog(&logObject,
"Apple Authentication Coprocessor information:\n"
"- Device Version: %s (0x%02x)\n"
"- %s: %u\n"
"- Authentication Protocol Version: %u.%u",
deviceVersionString,
deviceVersion,
deviceVersion >= kHAPMFiHWAuthDeviceVersion_3_0 ? "Authentication Revision" : "Firmware Version",
authenticationRevision,
protocolVersionMajor,
protocolVersionMinor);
// Write selftest control status.
if (protocolVersionMajor == 2) {
uint8_t bytes[2];
bytes[0] = kHAPMFiHWAuthRegister_SelfTestStatus;
bytes[1] = 0x01; // Run X.509 certificate and private key tests.
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_FALSE(mfiHWAuth, bytes, sizeof bytes);
}
// Read selftest control status.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_FALSE(mfiHWAuth, kHAPMFiHWAuthRegister_SelfTestStatus, bytes, sizeof bytes);
// Verify if bit 7 and 6 are set.
// Bit 7: Certificate found in memory.
// Bit 6: Private key found in memory.
if (!((bytes[0] >> 7) & 1)) {
HAPLog(&logObject, "Apple Authentication Coprocessor reports %s not found in memory.", "certificate");
return false;
}
if (!((bytes[0] >> 6) & 1)) {
HAPLog(&logObject, "Apple Authentication Coprocessor reports %s not found in memory.", "private key");
return false;
}
}
return true;
}
HAP_RESULT_USE_CHECK
HAPError HAPMFiHWAuthCopyCertificate(
HAPAccessoryServerRef* server_,
void* certificateBytes,
size_t maxCertificateBytes,
size_t* numCertificateBytes) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(server->platform.authentication.mfiHWAuth);
HAPPrecondition(certificateBytes);
HAPPrecondition(numCertificateBytes);
HAPError err;
// Enable Apple Authentication Coprocessor.
err = HAPMFiHWAuthEnable(&server->mfi);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Reset Error Code.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(&server->mfi, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
}
// Read Authentication Protocol Version.
uint8_t protocolVersionMajor;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_AuthenticationProtocolMajorVersion, bytes, sizeof bytes);
protocolVersionMajor = bytes[0];
if (protocolVersionMajor != 2 && protocolVersionMajor != 3) {
HAPLog(&logObject, "Unsupported Authentication Protocol Major Version: %u.", protocolVersionMajor);
return kHAPError_Unknown;
}
}
// Read accessory certificate data length.
uint16_t accessoryCertificateDataLength;
{
uint8_t bytes[2];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_AccessoryCertificateDataLength, bytes, sizeof bytes);
accessoryCertificateDataLength = HAPReadBigUInt16(&bytes[0]);
// See Accessory Interface Specification R30
// Section 64.5.7.12 Accessory Certificate Data Length
// See Accessory Interface Specification R29
// Section 69.8.2.11 Accessory Certificate Data Length
if ((protocolVersionMajor == 3 &&
(accessoryCertificateDataLength < 607 || accessoryCertificateDataLength > 609)) ||
(protocolVersionMajor == 2 && accessoryCertificateDataLength > 1280)) {
HAPLog(&logObject,
"Apple Authentication Coprocessor returned %u for accessory certificate data length.",
accessoryCertificateDataLength);
return kHAPError_Unknown;
}
}
// Read accessory certificate data.
if (accessoryCertificateDataLength > maxCertificateBytes) {
HAPLog(&logObject, "Not enough space to get certificate.");
return kHAPError_OutOfResources;
}
*numCertificateBytes = 0;
for (uint8_t i = 0; accessoryCertificateDataLength; i++) {
if (protocolVersionMajor == 3) {
HAPAssert(i < 5);
} else {
HAPAssert(protocolVersionMajor == 2);
HAPAssert(i < 10);
}
uint16_t numBytes = HAPMin(accessoryCertificateDataLength, (uint16_t) 128);
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi,
(uint8_t)(kHAPMFiHWAuthRegister_AccessoryCertificateDataPart1 + i),
&((uint8_t*) certificateBytes)[*numCertificateBytes],
numBytes);
accessoryCertificateDataLength -= numBytes;
*numCertificateBytes += numBytes;
}
HAPAssert(*numCertificateBytes <= maxCertificateBytes);
// Check for error.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(&server->mfi, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
HAPMFiHWAuthError errorCode = (HAPMFiHWAuthError) bytes[0];
if (errorCode) {
HAPLog(&logObject, "Error occurred while getting accessory certificate: 0x%02x.", errorCode);
return kHAPError_Unknown;
}
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPMFiHWAuthCreateSignature(
HAPAccessoryServerRef* server_,
const void* challengeBytes,
size_t numChallengeBytes,
void* signatureBytes,
size_t maxSignatureBytes,
size_t* numSignatureBytes) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(server->platform.authentication.mfiHWAuth);
HAPPrecondition(challengeBytes);
HAPPrecondition(signatureBytes);
HAPPrecondition(numSignatureBytes);
HAPError err;
// Enable Apple Authentication Coprocessor.
err = HAPMFiHWAuthEnable(&server->mfi);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Reset Error Code.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(&server->mfi, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
}
// Read Authentication Protocol Version.
uint8_t protocolVersionMajor;
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_AuthenticationProtocolMajorVersion, bytes, sizeof bytes);
protocolVersionMajor = bytes[0];
if (protocolVersionMajor != 2 && protocolVersionMajor != 3) {
HAPLog(&logObject, "Unsupported Authentication Protocol Major Version: %u.", protocolVersionMajor);
return kHAPError_Unknown;
}
}
// Write challenge.
if (protocolVersionMajor == 3) {
// Write challenge data.
// Additional SHA256 hash computation is necessary.
// Apple Authentication Coprocessor will compute ECDSA signature.
// See HomeKit Accessory Protocol Specification R14
// Section 5.6.4 M4: Accessory -> iOS Device - `SRP Verify Response'
{
uint8_t bytes[1 + SHA256_BYTES];
bytes[0] = kHAPMFiHWAuthRegister_ChallengeData;
HAP_sha256(&bytes[1], challengeBytes, numChallengeBytes);
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(&server->mfi, bytes, sizeof bytes);
}
} else {
HAPAssert(protocolVersionMajor == 2);
// Write challenge data length.
{
uint8_t bytes[1 + sizeof(uint16_t)];
bytes[0] = kHAPMFiHWAuthRegister_ChallengeDataLength;
HAPWriteBigUInt16(&bytes[1], SHA1_BYTES);
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(&server->mfi, bytes, sizeof bytes);
}
// Write challenge data.
// Additional SHA1 hash computation is necessary.
// Apple Authentication Coprocessor will compute RSA signature.
// See HomeKit Accessory Protocol Specification R14
// Section 5.6.4 M4: Accessory -> iOS Device - `SRP Verify Response'
{
uint8_t bytes[1 + SHA1_BYTES];
bytes[0] = kHAPMFiHWAuthRegister_ChallengeData;
HAP_sha1(&bytes[1], challengeBytes, numChallengeBytes);
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(&server->mfi, bytes, sizeof bytes);
}
// Write challenge response data length.
// Before a challenge response-generation process begins, this register should contain 0x80.
// See Accessory Interface Specification R29
// Section 69.8.2.7 Challenge Response Data Length
{
uint8_t bytes[1 + sizeof(uint16_t)];
bytes[0] = kHAPMFiHWAuthRegister_ChallengeResponseDataLength;
HAPWriteBigUInt16(&bytes[1], 0x80);
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(&server->mfi, bytes, sizeof bytes);
}
}
// Write authentication control.
{
uint8_t bytes[2];
bytes[0] = kHAPMFiHWAuthRegister_AuthenticationControlAndStatus;
bytes[1] = 1; // PROC_CONTROL
HAP_MFI_HW_AUTH_WRITE_OR_RETURN_ERROR(&server->mfi, bytes, sizeof bytes);
}
// Read status.
// The proc results are stored in bits 6|5|4
// The bits 3, 2, 1 and 0 are 0.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_AuthenticationControlAndStatus, bytes, sizeof bytes);
if (bytes[0] != (1 << 4)) {
HAPLog(&logObject,
"Apple Authentication Coprocessor returned %02x for authentication protocol status.",
bytes[0]);
return kHAPError_Unknown;
}
}
// Read challenge response data length.
uint16_t challengeResponseDataLength;
{
uint8_t bytes[2];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_ChallengeResponseDataLength, bytes, sizeof bytes);
challengeResponseDataLength = HAPReadBigUInt16(&bytes[0]);
// See Accessory Interface Specification R30
// Section 64.5.7.8 Challenge Response Data Length
// See Accessory Interface Specification R29
// Section 69.8.2.7 Challenge Response Data Length
if ((protocolVersionMajor == 3 && challengeResponseDataLength != 64) ||
(protocolVersionMajor == 2 && challengeResponseDataLength > 0x80)) {
HAPLog(&logObject,
"Apple Authentication Coprocessor returned %u for challenge response data length.",
challengeResponseDataLength);
return kHAPError_Unknown;
}
}
// Read challenge response data.
if (challengeResponseDataLength > maxSignatureBytes) {
HAPLog(&logObject, "Not enough space to get signature.");
return kHAPError_OutOfResources;
}
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(
&server->mfi, kHAPMFiHWAuthRegister_ChallengeResponseData, signatureBytes, challengeResponseDataLength);
*numSignatureBytes = challengeResponseDataLength;
// Check for error.
{
uint8_t bytes[1];
HAP_MFI_HW_AUTH_READ_OR_RETURN_ERROR(&server->mfi, kHAPMFiHWAuthRegister_ErrorCode, bytes, sizeof bytes);
HAPMFiHWAuthError errorCode = (HAPMFiHWAuthError) bytes[0];
if (errorCode) {
HAPLog(&logObject, "Error occurred while getting signature: 0x%02x.", errorCode);
return kHAPError_Unknown;
}
}
return kHAPError_None;
}