/
DiffieHellman.m
267 lines (206 loc) · 7.75 KB
/
DiffieHellman.m
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
//
// DiffieHellman.m
// ECDHTest
//
// Created by David Benko on 2/2/15.
// Copyright (c) 2015 David Benko. All rights reserved.
//
#import "DiffieHellman.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#import <openssl/evp.h>
#import <openssl/ec.h>
#include <openssl/pem.h>
#include <sys/mman.h>
struct dh_fmem {
size_t pos;
size_t size;
char *buffer;
};
typedef struct dh_fmem dh_fmem_t;
@implementation DiffieHellman
CCPBKDFAlgorithm const KeyDerivationAlgorithm = kCCPBKDF2;
CCPseudoRandomAlgorithm const HmacAlgorithm = kCCPRFHmacAlgSHA1;
size_t const KeyDerivationRounds = 20000;
size_t const DHSecretLength = 32;
size_t const KeyLength = CC_SHA1_DIGEST_LENGTH;
size_t const SaltLength = CC_SHA256_DIGEST_LENGTH;
#pragma mark - fmemopen() implementation
static int dh_readfn(void *handler, char *buf, int size) {
dh_fmem_t *mem = handler;
size_t available = mem->size - mem->pos;
if (size > available) {
size = (int)available;
}
memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
mem->pos += size;
return size;
}
static int dh_writefn(void *handler, const char *buf, int size) {
dh_fmem_t *mem = handler;
size_t available = mem->size - mem->pos;
if (size > available) {
size = (int)available;
}
memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
mem->pos += size;
return size;
}
static fpos_t dh_seekfn(void *handler, fpos_t offset, int whence) {
size_t pos;
dh_fmem_t *mem = handler;
switch (whence) {
case SEEK_SET: {
if (offset >= 0) {
pos = (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_CUR: {
if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
pos = mem->pos + (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_END: pos = mem->size + (size_t)offset; break;
default: return -1;
}
if (pos > mem->size) {
return -1;
}
mem->pos = pos;
return (fpos_t)pos;
}
static int dh_closefn(void *handler) {
free(handler);
return 0;
}
FILE *dh_fmemopen(void *buf, size_t size, const char *mode) {
// This data is released on fclose.
dh_fmem_t* mem = (dh_fmem_t *) malloc(sizeof(dh_fmem_t));
// Zero-out the structure.
memset(mem, 0, sizeof(dh_fmem_t));
mem->size = size;
mem->buffer = buf;
// funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
return funopen(mem, dh_readfn, dh_writefn, dh_seekfn, dh_closefn);
}
#pragma mark - Error Handling
- (void)throwError:(NSError *)error{
if ([self.delegate respondsToSelector:@selector(diffieHellmanHandleError:)]) {
[self.delegate diffieHellmanHandleError:error];
}
}
#pragma mark - EVP_PKEY Conversions
- (NSString *)pubKeyToString:(EVP_PKEY *)pubkey{
char *buf[256];
FILE *pFile;
NSString *pkey_string;
pFile = dh_fmemopen(buf, sizeof(buf), "w");
PEM_write_PUBKEY(pFile,pubkey);
fputc('\0', pFile);
fclose(pFile);
if (buf)
{
pkey_string = [NSString stringWithUTF8String:(char *)buf];
}
return pkey_string;
}
- (EVP_PKEY *)stringToPubkey:(NSString *)str {
char *buf[256];
FILE *pFile;
EVP_PKEY *key;
pFile = dh_fmemopen(buf, sizeof(buf), "r+");
fputs([str UTF8String], pFile);
fputc('\0', pFile);
rewind(pFile);
key = PEM_read_PUBKEY(pFile, NULL, NULL, NULL);
fclose(pFile);
return key;
}
#pragma mark - Elliptic Curve Key Generation
- (EVP_PKEY *)generateECKeys {
// new EVP_PKEY will hold the result once we create the EC_KEY and convert it to an EVP_PKEY
EVP_PKEY *pkey = EVP_PKEY_new();
// We're going to create a new EC_KEY with specific parameters
EC_KEY *key;
// Create an Elliptic Curve Key object and set it up to use the ANSI X9.62 Prime 256v1 curve
if(NULL == (key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))) [self throwError:nil];
// Generate the private and public key
if(1 != EC_KEY_generate_key(key)) [self throwError:nil];
// Set the option to output the public key using OpenSSL EC Named Curve only, rather than outputting a key with explicit parameters
EC_KEY_set_asn1_flag(key, OPENSSL_EC_NAMED_CURVE);
// Assign the EC_KEY to the EVP_PKEY - this method will also free the EC_KEY memory when EVP_PKEY memory is freed later on
if(1 != EVP_PKEY_assign_EC_KEY(pkey, key)) [self throwError:nil];
return pkey;
}
#pragma mark - Secret Derivation
- (NSString *)deriveSecretFromOurKey:(EVP_PKEY *)ourKey theirKey:(EVP_PKEY *)theirKey{
unsigned char *secret;
EVP_PKEY_CTX *ctx;
size_t secretLen = DHSecretLength;
/* Create the context for the shared secret derivation */
if(NULL == (ctx = EVP_PKEY_CTX_new(ourKey, NULL))) [self throwError:nil];
/* Initialise */
if(1 != EVP_PKEY_derive_init(ctx)) [self throwError:nil];
/* Provide the peer public key */
if(1 != EVP_PKEY_derive_set_peer(ctx, theirKey)) [self throwError:nil];
/* Create the buffer */
if(NULL == (secret = OPENSSL_malloc(secretLen))) [self throwError:nil];
/* Derive the shared secret */
if(1 != (EVP_PKEY_derive(ctx, secret, &secretLen))) [self throwError:nil];
NSMutableString *ms = [[NSMutableString alloc]init];
for(int i = 0; i < secretLen; i++){
[ms appendString:[NSString stringWithFormat:@"%02x",secret[i]]];
}
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(theirKey);
EVP_PKEY_free(ourKey);
return ms;
}
- (NSString *)deriveKeyFromSecret:(NSString *)secret salt:(NSData *)salt{
NSMutableData *finalKey = [NSMutableData dataWithLength:KeyLength];
NSData *data = [secret dataUsingEncoding:NSUTF8StringEncoding];
CCKeyDerivationPBKDF(KeyDerivationAlgorithm, data.bytes, data.length, salt.bytes, salt.length, HmacAlgorithm, KeyDerivationRounds, finalKey.mutableBytes, finalKey.length);
return [finalKey base64EncodedStringWithOptions:0];
}
#pragma mark - Key Exchange
- (void)keyExchange:(void (^)(NSString *ourPubKey, NSString *key, NSString *salt))callback{
EVP_PKEY *ourKey = [self generateECKeys];
NSString *ourPubKeyAsString = [self pubKeyToString:ourKey];
NSURLRequest *request = [self.delegate keyExchangeRequest:ourPubKeyAsString];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (error) {
NSLog(@"Error: %@",[error localizedDescription]);
}
else{
NSData *salt = [self generateSalt:SaltLength];
NSString *peerKeyAsString = [self.delegate parseKeyExchangeResponse:data];
EVP_PKEY *theirKey = [self stringToPubkey:peerKeyAsString];
NSString *key = [self deriveKeyFromSecret:[self deriveSecretFromOurKey:ourKey theirKey:theirKey] salt:salt];
NSString *saltString = [salt base64EncodedStringWithOptions:0];
if (callback) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(ourPubKeyAsString,key,saltString);
});
}
}
}];
}
#pragma mark - Generate Salt
- (NSData*)generateSalt:(size_t)length {
NSMutableData *data = [NSMutableData dataWithLength:length];
int result = SecRandomCopyBytes(kSecRandomDefault,
length,
data.mutableBytes);
assert(result == 0);
return data;
}
@end