5 // Created by Mitch Adler on 2/17/16.
9 #import <Foundation/Foundation.h>
10 #import <AssertMacros.h>
12 #import <KeychainCircle/KCDer.h>
13 #import <KeychainCircle/KCError.h>
14 #import <KeychainCircle/KCJoiningMessages.h>
15 #import <utilities/debugging.h>
16 #import <Security/SecureObjectSync/SOSTypes.h>
17 #include <corecrypto/ccder.h>
20 @implementation KCJoiningMessage
22 + (nullable instancetype) messageWithDER: (NSData*) message
23 error: (NSError**) error {
24 return [[KCJoiningMessage alloc] initWithDER: message error: nil];
27 + (nullable instancetype) messageWithType: (KCJoiningMessageType) type
28 data: (NSData*) firstData
29 error: (NSError**) error {
30 return [[KCJoiningMessage alloc] initWithType:type data:firstData payload:nil error:error];
33 + (nullable instancetype) messageWithType: (KCJoiningMessageType) type
34 data: (NSData*) firstData
35 secondData: (NSData*) secondData
36 error: (NSError**) error {
37 return [[KCJoiningMessage alloc] initWithType:type data:firstData payload:secondData error:error];
40 + (nullable instancetype) messageWithType: (KCJoiningMessageType) type
41 data: (NSData*) firstData
42 payload: (NSData*) secondData
43 error: (NSError**) error {
44 return [[KCJoiningMessage alloc] initWithType:type data:firstData payload:secondData error:error];
48 - (bool) inflatePartsOfEncoding: (NSError**) error {
49 const uint8_t *der = self.der.bytes;
50 const uint8_t *der_end = der + self.der.length;
52 const uint8_t *sequence_end = 0;
54 der = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, der, der_end);
57 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Not sequence");
61 if (sequence_end != der_end) {
62 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Extra data at end of message");
67 der = ccder_decode_uint64(&type, der, der_end);
69 self->_type = (type > kLargestMessageType) ? kUnknown : (KCJoiningMessageType) type;
74 der = kcder_decode_data_nocopy(&firstData, error, der, der_end);
77 der = kcder_decode_data_nocopy(&secondData, error, der, der_end);
80 self->_firstData = firstData;
81 self->_secondData = secondData;
84 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Extra in sequence");
91 + (size_t) encodedSizeType: (KCJoiningMessageType) type
92 data: (NSData*) firstData
93 payload: (nullable NSData*) secondData
94 error: (NSError**) error {
95 size_t type_size = ccder_sizeof_uint64(type);
97 size_t srp_data_size = kcder_sizeof_data(firstData, error);
98 if (srp_data_size == 0) return 0;
100 size_t encrypted_payload_size = 0;
102 if (secondData != nil) {
103 encrypted_payload_size = kcder_sizeof_data(secondData, error);
104 if (srp_data_size == 0) return 0;
108 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, type_size + srp_data_size + encrypted_payload_size);
111 + (nullable NSData*) encodeToDERType: (KCJoiningMessageType) type
112 data: (NSData*) firstData
113 payload: (nullable NSData*) secondData
114 error: (NSError**) error {
116 size_t length = [KCJoiningMessage encodedSizeType:type
120 if (length == 0) return nil;
122 NSMutableData* encoded = [NSMutableData dataWithLength: length];
124 uint8_t* der = encoded.mutableBytes;
125 uint8_t* der_end = der + encoded.length;
127 uint8_t* encode_end = ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
128 ccder_encode_uint64(type, der,
129 kcder_encode_data(firstData, error, der,
130 kcder_encode_data_optional(secondData, error, der, der_end))));
132 if (encode_end == NULL) return nil;
133 if (encode_end != der) {
134 KCJoiningErrorCreate(kDEREncodingFailed, error, @"Size didn't match encoding");
141 - (nullable instancetype) initWithDER: (NSData*) message
142 error: (NSError**) error {
145 self->_der = [NSData dataWithData: message];
147 return [self inflatePartsOfEncoding: error] ? self : nil;
150 - (nullable instancetype) initWithType: (KCJoiningMessageType) type
151 data: (NSData*) firstData
152 payload: (nullable NSData*) secondData
153 error: (NSError**) error {
156 self->_der = [KCJoiningMessage encodeToDERType:type
160 if (self->_der == nil) return nil;
162 return [self inflatePartsOfEncoding: error] ? self : nil;
168 @implementation NSData(KCJoiningMessages)
170 + (nullable instancetype) dataWithEncodedString: (NSString*) string
171 error: (NSError**) error {
172 size_t result_size = kcder_sizeof_string(string, error);
173 if (result_size == 0) return nil;
175 NSMutableData *result = [NSMutableData dataWithLength: result_size];
177 uint8_t *der = result.mutableBytes;
178 uint8_t *der_end = der + result.length;
180 uint8_t *encode_done = kcder_encode_string(string, error,
183 if (encode_done != der) {
184 KCJoiningErrorCreate(kDEREncodingFailed, error, @"extra data");
191 + (nullable instancetype) dataWithEncodedSequenceData: (NSData*) data1
192 data: (NSData*) data2
193 error: (NSError**) error {
194 size_t result_size = sizeof_seq_data_data(data1, data2, error);
195 if (result_size == 0) return nil;
197 NSMutableData *result = [NSMutableData dataWithLength: result_size];
199 uint8_t *der = result.mutableBytes;
200 uint8_t *der_end = der + result.length;
202 uint8_t *encode_done = encode_seq_data_data(data1, data2, error,
205 if (encode_done != der) {
206 KCJoiningErrorCreate(kDEREncodingFailed, error, @"extra data");
213 - (bool) decodeSequenceData: (NSData* _Nullable * _Nonnull) data1
214 data: (NSData* _Nullable * _Nonnull) data2
215 error: (NSError** _Nullable) error {
217 return NULL != decode_seq_data_data(data1, data2, error, self.bytes, self.bytes + self.length);
220 + (nullable instancetype) dataWithEncodedSequenceString: (NSString*) string
222 error: (NSError**) error {
223 size_t result_size = sizeof_seq_string_data(string, data, error);
224 if (result_size == 0) return nil;
226 NSMutableData *result = [NSMutableData dataWithLength: result_size];
228 uint8_t *der = result.mutableBytes;
229 uint8_t *der_end = der + result.length;
231 uint8_t *encode_done = encode_seq_string_data(string, data, error,
234 if (encode_done != der) {
235 KCJoiningErrorCreate(kDEREncodingFailed, error, @"extra data");
242 - (bool) decodeSequenceString: (NSString* _Nullable * _Nonnull) string
243 data: (NSData* _Nullable * _Nonnull) data
244 error: (NSError** _Nullable) error {
245 return NULL != decode_seq_string_data(string, data, error, self.bytes, self.bytes + self.length);
250 @implementation NSString(KCJoiningMessages)
251 + (nullable instancetype) decodeFromDER: (NSData*)der error: (NSError** _Nullable) error {
252 NSString* result = nil;
253 const uint8_t* decode_result = kcder_decode_string(&result, error, der.bytes, der.bytes+der.length);
254 if (decode_result == nil) return nil;
255 if (decode_result != der.bytes + der.length) {
256 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"extra data in string");
265 NSData* extractStartFromInitialMessage(NSData* initialMessage, uint64_t* version, NSString** uuidString, NSData** octagon, NSError** error) {
266 NSData* result = nil;
267 const uint8_t *der = [initialMessage bytes];
268 const uint8_t *der_end = der + [initialMessage length];
269 const uint8_t *parse_end = decode_initialmessage(&result, error, der, der_end);
271 // Allow extra stuff in here for future start messages.
272 if (parse_end == NULL) {
275 else if (parse_end != der_end) {
276 NSData *extraStuff = nil;
277 NSData *uuidData = nil;
278 uint64_t piggy_version = 0;
279 NSData* octagonData = nil;
281 parse_end = decode_version2(&extraStuff, &uuidData, &octagonData, &piggy_version, error, parse_end, der_end);
282 require_action_quiet(parse_end != NULL, fail, secerror("decoding piggybacking message failed for version (%llu)", piggy_version));
284 switch(piggy_version){
286 *octagon = octagonData;
287 //fall through to pick up v1
289 NSUUID *uuid = [[NSUUID alloc]initWithUUIDBytes:uuidData.bytes];
290 *uuidString = uuid.UUIDString;
291 *version = piggy_version;
298 secerror("unsupported version");
307 const uint8_t* decode_version1(NSData** data, NSData** uuid, uint64_t *piggy_version, NSError** error,
308 const uint8_t* der, const uint8_t *der_end)
313 uint64_t versionFromBlob = 0;
314 der = ccder_decode_uint64(&versionFromBlob, der, der_end);
317 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Version mising");
321 if(versionFromBlob == 1){ //decode uuid
322 size_t payloadSize = 0;
323 const uint8_t *payload = ccder_decode_tl(CCDER_OCTET_STRING, &payloadSize, der, der_end);
325 *uuid = [NSData dataWithBytes: (void*)payload length: payloadSize];
326 *piggy_version = versionFromBlob;
327 der = payload + payloadSize;
330 KCJoiningErrorCreate(kDERUnknownVersion, error, @"Bad version: %llu", versionFromBlob);
337 const uint8_t* decode_version2(NSData** data, NSData** uuid, NSData** octagon, uint64_t *piggy_version, NSError** error,
338 const uint8_t* der, const uint8_t *der_end)
340 const uint8_t* end = nil;
342 const uint8_t* parse_version1 = decode_version1(data, uuid, piggy_version, error, der, der_end);
344 if(parse_version1 == NULL){
345 secerror("error parsing version 1");
348 else if (parse_version1 == der_end){
349 secnotice("octagon", "first message is piggybacking v1, no more data");
350 return parse_version1;
353 end = kcder_decode_data(octagon, error, parse_version1, der_end);
356 secerror("failed to decode v2");
359 else if(*octagon && [*octagon length] != 0){
360 *piggy_version = kPiggyV2;
363 secerror("no octagon version set");
371 const uint8_t* decode_initialmessage(NSData** data, NSError** error,
372 const uint8_t* der, const uint8_t *der_end)
377 const uint8_t *sequence_end = 0;
378 der = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, der, der_end);
380 if (der == NULL || sequence_end != der_end) {
381 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Decoding failed");
384 uint64_t version = 0;
385 der = ccder_decode_uint64(&version, der, der_end);
388 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Version mising");
393 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Bad version: %llu", version);
397 return kcder_decode_data(data, error, der, der_end);
400 size_t sizeof_initialmessage(NSData*data) {
401 size_t version_size = ccder_sizeof_uint64(0);
402 if (version_size == 0) {
405 size_t message_size = kcder_sizeof_data(data, nil);
406 if (message_size == 0) {
409 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, version_size + message_size);
412 uint8_t* encode_initialmessage(NSData* data, NSError**error,
413 const uint8_t *der, uint8_t *der_end)
415 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
416 ccder_encode_uint64(0, der,
417 kcder_encode_data(data, error, der, der_end)));
420 size_t sizeof_initialmessage_version2(NSData*data, uint64_t version1, NSData *uuid, NSData* octagon)
422 size_t version_size = ccder_sizeof_uint64(0);
423 if (version_size == 0) {
426 size_t message_size = kcder_sizeof_data(data, nil);
427 if (message_size == 0) {
430 size_t version1_size = ccder_sizeof_uint64(version1);
431 if (version1_size == 0) {
434 size_t uuid_size = kcder_sizeof_data(uuid, nil);
435 if (uuid_size == 0) {
438 size_t octagon_size = kcder_sizeof_data(octagon, nil);
439 if (octagon_size == 0) {
442 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, version_size + message_size +
443 version1_size + uuid_size +
447 size_t sizeof_initialmessage_version1(NSData*data, uint64_t version1, NSData *uuid) {
448 size_t version_size = ccder_sizeof_uint64(0);
449 if (version_size == 0) {
452 size_t message_size = kcder_sizeof_data(data, nil);
453 if (message_size == 0) {
456 size_t version1_size = ccder_sizeof_uint64(version1);
457 if (version1_size == 0) {
460 size_t uuid_size = kcder_sizeof_data(uuid, nil);
461 if (uuid_size == 0) {
464 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, version_size + message_size + version1_size + uuid_size);
468 uint8_t* encode_initialmessage_version1(NSData* data, NSData* uuidData, uint64_t piggy_version, NSError**error,
469 const uint8_t *der, uint8_t *der_end)
471 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
472 ccder_encode_uint64(0, der,
473 kcder_encode_data(data, error, der,
474 ccder_encode_uint64(piggy_version, der,
475 kcder_encode_data(uuidData, error, der, der_end)))));
479 uint8_t* encode_initialmessage_version2(NSData* data, NSData* uuidData, NSData* octagon_version, NSError**error,
480 const uint8_t *der, uint8_t *der_end)
483 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
484 ccder_encode_uint64(0, der,
485 kcder_encode_data(data, error, der,
486 ccder_encode_uint64(kPiggyV1, der,
487 kcder_encode_data(uuidData, error, der, kcder_encode_data(octagon_version, error, der, der_end))))));
490 size_t sizeof_seq_data_data(NSData*data1, NSData*data2, NSError**error) {
491 size_t data1_size = kcder_sizeof_data(data1, error);
492 if (data1_size == 0) {
495 size_t data2_size = kcder_sizeof_data(data2, error);
496 if (data2_size == 0) {
499 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, data1_size + data2_size);
502 uint8_t* encode_seq_data_data(NSData* data1, NSData*data2, NSError**error,
503 const uint8_t *der, uint8_t *der_end) {
504 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
505 kcder_encode_data(data1, error, der,
506 kcder_encode_data(data2, error, der, der_end)));
509 const uint8_t* decode_seq_data_data(NSData** data1, NSData** data2, NSError** error,
510 const uint8_t* der, const uint8_t *der_end) {
514 const uint8_t *sequence_end = 0;
515 der = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, der, der_end);
517 if (der == NULL || sequence_end != der_end) {
518 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"decode failed");
522 der = kcder_decode_data(data1, error, der, der_end);
523 return kcder_decode_data(data2, error, der, der_end);
526 size_t sizeof_seq_string_data(NSString*string, NSData*data, NSError** error) {
527 size_t string_size = kcder_sizeof_string(string, error);
528 if (string_size == 0) {
531 size_t data_size = kcder_sizeof_data(data, error);
532 if (data_size == 0) {
535 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, string_size + data_size);
538 uint8_t* _Nullable encode_seq_string_data(NSString* string, NSData*data, NSError**error,
539 const uint8_t *der, uint8_t *der_end) {
540 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
541 kcder_encode_string(string, error, der,
542 kcder_encode_data(data, error, der, der_end)));
545 const uint8_t* _Nullable decode_seq_string_data(NSString* _Nonnull * _Nonnull string, NSData* _Nonnull * _Nonnull data,
547 const uint8_t* der, const uint8_t *der_end) {
551 const uint8_t *sequence_end = 0;
552 der = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, der, der_end);
554 if (der == NULL || sequence_end != der_end) {
555 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"decode failed");
559 der = kcder_decode_string(string, error, der, der_end);
560 return kcder_decode_data(data, error, der, der_end);