]> git.saurik.com Git - apple/security.git/blob - KeychainCircle/KCSRPContext.m
Security-59306.140.5.tar.gz
[apple/security.git] / KeychainCircle / KCSRPContext.m
1 //
2 // SRPSession.m
3 // Security
4 //
5 //
6
7
8 #import <Foundation/Foundation.h>
9 #import "KCSRPContext.h"
10
11 #include <os/base.h>
12
13 #include <corecrypto/ccsrp.h>
14 #include <corecrypto/ccsha2.h>
15 #include <corecrypto/ccdh_gp.h>
16 #include <corecrypto/ccder.h>
17
18 #import "NSError+KCCreationHelpers.h"
19
20 static const NSStringEncoding srpStringEncoding = NSUTF8StringEncoding;
21
22 @interface KCSRPContext ()
23 @property (readwrite) struct ccsrp_ctx* context;
24 @property (readwrite) struct ccrng_state *rng;
25 @property (readwrite) NSString* user;
26 @end
27
28 @implementation KCSRPContext
29
30 + (KCSRPContext*) createWithUser: (NSString*) user
31 digestInfo: (const struct ccdigest_info *) di
32 group: (ccsrp_const_gp_t) gp
33 randomSource: (struct ccrng_state *) rng {
34 return [[self alloc] initWithUser:user
35 digestInfo:di
36 group:gp
37 randomSource:rng];
38 }
39
40 - (NSData*) dataForPassword: (NSString*) password {
41 return [password dataUsingEncoding:srpStringEncoding];
42 }
43
44 - (nullable const char *) userNameString {
45 return [self.user cStringUsingEncoding:srpStringEncoding];
46 }
47
48 - (instancetype) initWithUser: (NSString*) user
49 digestInfo: (const struct ccdigest_info *) di
50 group: (ccsrp_const_gp_t) gp
51 randomSource: (struct ccrng_state *) rng
52 {
53 self = [super init];
54
55 self.context = malloc(ccsrp_sizeof_srp(di, gp));
56 ccsrp_ctx_init(self.context, di, gp);
57
58 self.user = user;
59 self.rng = rng;
60
61 return self;
62 }
63
64 #pragma clang diagnostic push
65 #pragma clang diagnostic ignored "-Wdeprecated-implementations"
66 - (void) finalize {
67 ccsrp_ctx_clear(ccsrp_ctx_di(self.context),
68 ccsrp_ctx_gp(self.context),
69 self.context);
70
71 free(self.context);
72 }
73 #pragma clang diagnostic pop
74
75 - (NSData*) getKey {
76 size_t key_length = 0;
77 const void * key = ccsrp_get_session_key(self.context, &key_length);
78
79 return key ? [NSData dataWithBytesNoCopy:(void *)key length:key_length freeWhenDone:false] : nil;
80 }
81
82 - (bool) isAuthenticated {
83 return ccsrp_is_authenticated(self.context);
84 }
85
86 @end
87
88
89 @implementation KCSRPClientContext
90
91 - (NSData*) copyStart: (NSError**) error {
92 NSMutableData* A_data = [NSMutableData dataWithLength: ccsrp_exchange_size(self.context)];
93
94 int result = ccsrp_client_start_authentication(self.context, self.rng, A_data.mutableBytes);
95 if (!CoreCryptoError(result, error, @"Start packet copy failed: %d", result)) {
96 A_data = NULL;
97 }
98
99 return A_data;
100 }
101
102 static bool ExactDataSizeRequirement(NSData* data, NSUInteger expectedLength, NSError**error, NSString* name) {
103 return RequirementError(data.length == expectedLength, error, @"%@ incorrect size, Expected %ld, got %ld", name, (unsigned long)expectedLength, (unsigned long)data.length);
104 }
105
106 - (nullable NSData*) copyResposeToChallenge: (NSData*) B_data
107 password: (NSString*) password
108 salt: (NSData*) salt
109 error: (NSError**) error {
110
111 if (!ExactDataSizeRequirement(B_data, ccsrp_exchange_size(self.context), error, @"challenge data"))
112 return nil;
113
114 NSMutableData* M_data = [NSMutableData dataWithLength: ccsrp_session_size(self.context)];
115 NSData* passwordData = [self dataForPassword: password];
116
117 int result = ccsrp_client_process_challenge(self.context,
118 [self userNameString],
119 passwordData.length,
120 passwordData.bytes,
121 salt.length,
122 salt.bytes,
123 B_data.bytes,
124 M_data.mutableBytes);
125
126 if (!CoreCryptoError(result, error, @"Challenge processing failed: %d", result)) {
127 M_data = NULL;
128 }
129
130 return M_data;
131 }
132
133 - (bool) verifyConfirmation: (NSData*) HAMK_data
134 error: (NSError**) error {
135 if (!ExactDataSizeRequirement(HAMK_data, ccsrp_session_size(self.context), error, @"confirmation data"))
136 return nil;
137
138 return ccsrp_client_verify_session(self.context, HAMK_data.bytes);
139 }
140
141 @end
142
143 @interface KCSRPServerContext ()
144 @property (readwrite) NSData* verifier;
145 @end
146
147 @implementation KCSRPServerContext
148
149 - (bool) resetWithPassword: (NSString*) password
150 error: (NSError**) error {
151 const int salt_length = 16;
152
153 NSMutableData* salt = [NSMutableData dataWithLength: salt_length];
154 NSMutableData* verifier = [NSMutableData dataWithLength: ccsrp_ctx_sizeof_n(self.context)];
155
156 NSData* passwordData = [self dataForPassword: password];
157
158 int generateResult = ccsrp_generate_salt_and_verification(self.context,
159 self.rng,
160 [self userNameString],
161 passwordData.length,
162 passwordData.bytes,
163 salt.length,
164 salt.mutableBytes,
165 verifier.mutableBytes);
166
167 if (!CoreCryptoError(generateResult, error, @"Error generating SRP salt/verifier")) {
168 return false;
169 }
170
171 self.verifier = verifier;
172 self->_salt = salt;
173
174 return true;
175 }
176
177 - (instancetype) initWithUser: (NSString*)user
178 password: (NSString*)password
179 digestInfo: (const struct ccdigest_info *) di
180 group: (ccsrp_const_gp_t) gp
181 randomSource: (struct ccrng_state *) rng {
182 self = [super initWithUser: user
183 digestInfo: di
184 group: gp
185 randomSource: rng];
186
187 if (![self resetWithPassword:password error:nil]) {
188 return nil;
189 }
190
191 return self;
192 }
193
194 - (instancetype) initWithUser: (NSString*) user
195 salt: (NSData*) salt
196 verifier: (NSData*) verifier
197 digestInfo: (const struct ccdigest_info *) di
198 group: (ccsrp_const_gp_t) gp
199 randomSource: (struct ccrng_state *) rng {
200 self = [super initWithUser: user
201 digestInfo: di
202 group: gp
203 randomSource: rng];
204
205 self.verifier = verifier;
206 self->_salt = salt;
207
208 return self;
209 }
210
211
212 - (NSData*) copyChallengeFor: (NSData*) A_data
213 error: (NSError**) error {
214 if (!ExactDataSizeRequirement(A_data, ccsrp_exchange_size(self.context), error, @"start data"))
215 return nil;
216
217 NSMutableData* B_data = [NSMutableData dataWithLength: ccsrp_exchange_size(self.context)];
218
219 int result = ccsrp_server_start_authentication(self.context, self.rng,
220 [self userNameString],
221 self.salt.length, self.salt.bytes,
222 self.verifier.bytes, A_data.bytes,
223 B_data.mutableBytes);
224
225 if (!CoreCryptoError(result, error, @"Server start authentication failed: %d", result)) {
226 B_data = NULL;
227 }
228
229 return B_data;
230 }
231
232 - (NSData*) copyConfirmationFor: (NSData*) M_data
233 error: (NSError**) error {
234 if (!ExactDataSizeRequirement(M_data, ccsrp_session_size(self.context), error, @"response data"))
235 return nil;
236
237 NSMutableData* HAMK_data = [NSMutableData dataWithLength: ccsrp_session_size(self.context)];
238
239 bool verify = ccsrp_server_verify_session(self.context, M_data.bytes, HAMK_data.mutableBytes);
240
241 if (!CoreCryptoError(!verify, error, @"SRP verification failed")) {
242 HAMK_data = NULL;
243 }
244
245 return HAMK_data;
246 }
247
248 @end