]> git.saurik.com Git - apple/security.git/blob - keychain/ot/tests/OTEscrowKeyTests.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ot / tests / OTEscrowKeyTests.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if OCTAGON
25
26 #import <Foundation/Foundation.h>
27 #import <XCTest/XCTest.h>
28 #import <OCMock/OCMock.h>
29 #import "keychain/ot/OTEscrowKeys.h"
30 #import "keychain/ckks/CKKS.h"
31 #import "OTTestsBase.h"
32
33 static NSString* const testDSID = @"123456789";
34
35 static const uint8_t signingKey_384[] = {
36 0x04, 0xe4, 0x1b, 0x3e, 0x88, 0x81, 0x9f, 0x3b, 0x80, 0xd0, 0x28, 0x1c,
37 0xd9, 0x07, 0xa0, 0x8c, 0xa1, 0x89, 0xa8, 0x3b, 0x69, 0x91, 0x17, 0xa7,
38 0x1f, 0x00, 0x31, 0x91, 0x82, 0x89, 0x1f, 0x5c, 0x44, 0x2d, 0xd6, 0xa8,
39 0x22, 0x1f, 0x22, 0x7d, 0x27, 0x21, 0xf2, 0xc9, 0x75, 0xf2, 0xda, 0x41,
40 0x61, 0x55, 0x29, 0x11, 0xf7, 0x71, 0xcf, 0x66, 0x52, 0x2a, 0x27, 0xfe,
41 0x77, 0x1e, 0xd4, 0x3d, 0xfb, 0xbc, 0x59, 0xe4, 0xed, 0xa4, 0x79, 0x2a,
42 0x9b, 0x73, 0x3e, 0xf4, 0xf4, 0xe3, 0xaf, 0xf2, 0x8d, 0x34, 0x90, 0x92,
43 0x47, 0x53, 0xd0, 0x34, 0x1e, 0x49, 0x87, 0xeb, 0x11, 0x89, 0x0f, 0x9c,
44 0xa4, 0x99, 0xe8, 0x4f, 0x39, 0xbe, 0x21, 0x94, 0x88, 0xba, 0x4c, 0xa5,
45 0x6a, 0x60, 0x1c, 0x2f, 0x77, 0x80, 0xd2, 0x73, 0x14, 0x33, 0x46, 0x5c,
46 0xda, 0xee, 0x13, 0x8a, 0x3a, 0xdb, 0x4e, 0x05, 0x4d, 0x0f, 0x6d, 0x96,
47 0xcd, 0x28, 0xab, 0x52, 0x4c, 0x12, 0x2b, 0x79, 0x80, 0xfe, 0x9a, 0xe4,
48 0xf4
49 };
50
51 static const uint8_t encryptionKey_384[] = {
52 0x04, 0x99, 0xf9, 0x9a, 0x9b, 0x48, 0xe2, 0xf8, 0x69, 0xd3, 0xf9, 0x60,
53 0xa0, 0xf4, 0x86, 0xda, 0xb3, 0x35, 0x3d, 0x97, 0x7d, 0xc3, 0xf4, 0x13,
54 0x24, 0x78, 0x06, 0x10, 0xd5, 0x46, 0x55, 0x7a, 0x8a, 0x4d, 0x80, 0x0d,
55 0x71, 0x19, 0x46, 0x4b, 0x15, 0x93, 0x36, 0xb0, 0xf4, 0x6e, 0x41, 0x30,
56 0x09, 0x55, 0x25, 0x3b, 0x06, 0xdd, 0xf8, 0x85, 0xdc, 0xf2, 0x0b, 0xc7,
57 0x33, 0x21, 0x99, 0x3c, 0x79, 0xa6, 0xb1, 0x0f, 0xf0, 0x55, 0xfa, 0xe8,
58 0x6d, 0x3f, 0x0d, 0x57, 0x21, 0x08, 0xd2, 0x7e, 0x73, 0x4a, 0xe7, 0x4a,
59 0xb3, 0xdf, 0xed, 0x86, 0x06, 0xa6, 0xf2, 0x03, 0xe6, 0x20, 0xd4, 0x82,
60 0x39, 0x29, 0xcf, 0x6d, 0x76, 0x3e, 0x9a, 0xaa, 0x29, 0x4f, 0x33, 0x84,
61 0x5a, 0x38, 0x50, 0x35, 0xca, 0x3f, 0x69, 0x92, 0xb1, 0xb3, 0x8b, 0x26,
62 0x2b, 0xb5, 0xd6, 0x25, 0xcf, 0x2d, 0x18, 0xc4, 0x5e, 0x24, 0x34, 0xc5,
63 0xcc, 0x83, 0x2f, 0xff, 0x08, 0x85, 0x0f, 0x89, 0xb5, 0xb1, 0xc1, 0x17,
64 0x2a
65 };
66
67 static const uint8_t symmetricKey_384[] = {
68 0x31, 0xf1, 0xe3, 0x7b, 0x76, 0x3f, 0x99, 0x65, 0x74, 0xab, 0xe8, 0x2b,
69 0x8f, 0x06, 0x78, 0x57, 0x1b, 0xaa, 0x07, 0xb3, 0xab, 0x79, 0x81, 0xcb,
70 0xc5, 0x89, 0x1e, 0x78, 0x28, 0x8d, 0x8e, 0x36
71 };
72
73 @interface UnitTestEscrowKeys : OTTestsBase
74
75 @end
76
77 @implementation UnitTestEscrowKeys
78
79 - (void)setUp
80 {
81 [super setUp];
82 NSError *error = nil;
83
84 self.continueAfterFailure = NO;
85 NSString* secretString = @"I'm a secretI'm a secretI'm a secretI'm a secretI'm a secretI'm a secret";
86
87 self.secret = [[NSData alloc]initWithBytes:[secretString UTF8String] length:[secretString length]];
88 self.escrowKeys = [[OTEscrowKeys alloc]initWithSecret:self.secret dsid:testDSID error:&error];
89
90 XCTAssertNil(error, @"error should be initialized");
91 XCTAssertNotNil(self.escrowKeys, @"escrow keys should be initialized");
92 }
93
94 - (void)tearDown
95 {
96 [super tearDown];
97 }
98
99 -(void) testEscrowKeyAllocations
100 {
101 XCTAssertNotNil(self.escrowKeys.symmetricKey, @"escrowed symmetric key pair should not be nil");
102 XCTAssertNotNil(self.escrowKeys.secret, @"escrowed secret should not be nil");
103 XCTAssertNotNil(self.escrowKeys.dsid, @"account dsid should not be nil");
104 XCTAssertNotNil(self.escrowKeys.signingKey, @"escrowed signing key should not be nil");
105 XCTAssertNotNil(self.escrowKeys.encryptionKey, @"escrowed encryption key should not be nil");
106 }
107 -(void) testEscrowKeyTestVectors
108 {
109 NSError* error = nil;
110
111 //test vectors
112 NSData* testv1 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeySigning masterSecret:self.secret dsid:testDSID error:&error];
113 NSData* signingFromBytes = [[NSData alloc] initWithBytes:signingKey_384 length:sizeof(signingKey_384)];
114 XCTAssertTrue([testv1 isEqualToData:signingFromBytes], @"signing keys should match");
115
116 NSData* testv2 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeyEncryption masterSecret:self.secret dsid:testDSID error:&error];
117 NSData* encryptionFromBytes = [[NSData alloc] initWithBytes:encryptionKey_384 length:sizeof(encryptionKey_384)];
118 XCTAssertTrue([testv2 isEqualToData:encryptionFromBytes], @"encryption keys should match");
119
120 NSData* testv3 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeySymmetric masterSecret:self.secret dsid:testDSID error:&error];
121 NSData* symmetricKeyFromBytes = [[NSData alloc]initWithBytes:symmetricKey_384 length:sizeof(symmetricKey_384)];
122 XCTAssertTrue([testv3 isEqualToData:symmetricKeyFromBytes], @"symmetric keys should match");
123
124 NSString* newSecretString = @"I'm f secretI'm a secretI'm a secretI'm a secretI'm a secretI'm a secret";
125 NSData* newSecret = [[NSData alloc]initWithBytes:[newSecretString UTF8String] length:[newSecretString length]];
126
127 NSData* testv4 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeySigning masterSecret:newSecret dsid:testDSID error:&error];
128 XCTAssertFalse([testv4 isEqualToData:signingFromBytes], @"signing keys should not match");
129
130 NSData* testv5 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeyEncryption masterSecret:newSecret dsid:testDSID error:&error];
131 XCTAssertFalse([testv5 isEqualToData:encryptionFromBytes], @"encryption keys should not match");
132
133 NSData* testv6 = [OTEscrowKeys generateEscrowKey:kOTEscrowKeySymmetric masterSecret:newSecret dsid:testDSID error:&error];
134 XCTAssertFalse([testv6 isEqualToData:symmetricKeyFromBytes], @"symmetric keys should not match");
135 }
136
137 -(void) testEmptyArguments
138 {
139 NSError* error = nil;
140 OTEscrowKeys* newSet = [[OTEscrowKeys alloc] initWithSecret:[NSData data] dsid:testDSID error:&error];
141 XCTAssertNotNil(error, @"error should be initialized");
142 XCTAssertNil(newSet, @"escrow keys should not be initialized");
143
144 newSet = [[OTEscrowKeys alloc] initWithSecret:self.secret dsid:[NSString string] error:&error];
145 XCTAssertNotNil(error, @"error should be initialized");
146 XCTAssertNil(newSet, @"escrow keys should not be initialized");
147 }
148
149 -(void) testEscrowKeyLoadingAndStoringInKeychain
150 {
151 NSError* error = nil;
152 NSString* secretString = @"secret";
153 NSData* secret = [[NSData alloc]initWithBytes:[secretString UTF8String] length:[secretString length]];
154
155 OTEscrowKeys* keys = [[OTEscrowKeys alloc] initWithSecret:secret dsid:@"dsid-123456789" error:&error];
156 XCTAssertNotNil(keys, @"keys should not be nil");
157 XCTAssertNil(error, "error should be nil");
158
159 NSString* label = [OTEscrowKeys hashEscrowedSigningPublicKey:[[keys.signingKey publicKey] encodeSubjectPublicKeyInfo]];
160 NSMutableDictionary* query = [@{
161 (id)kSecClass : (id)kSecClassKey,
162 (id)kSecAttrAccessGroup : @"com.apple.security.ckks",
163 (id)kSecAttrLabel: label,
164 (id)kSecReturnAttributes: @YES,
165 (id)kSecReturnData : @YES,
166 (id)kSecAttrSynchronizable : (id)kCFBooleanFalse,
167 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
168 } mutableCopy];
169
170 CFTypeRef result = NULL;
171 OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
172 XCTAssertNotNil((__bridge NSDictionary*)result, @"result not be nil");
173 XCTAssertEqual(CFDictionaryGetCount(result), 3, @"should be 3 entries");
174
175 XCTAssertEqual(status, 0, @"status should be 0");
176
177 query = [@{
178 (id)kSecClass : (id)kSecClassKey,
179 (id)kSecAttrAccessGroup : @"com.apple.security.ckks",
180 (id)kSecReturnAttributes: @YES,
181 (id)kSecAttrSynchronizable : (id)kCFBooleanFalse,
182 } mutableCopy];
183
184 status = SecItemDelete((__bridge CFDictionaryRef)query);
185 XCTAssertEqual(status, 0, @"status should be 0");
186 }
187
188
189 @end
190
191 #endif /* OCTAGON */
192