| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/private/ecc.h> |
| 6 |
|
| 7 |
#include <aws/cal/cal.h> |
| 8 |
#include <aws/cal/private/der.h> |
| 9 |
|
| 10 |
#include <CoreFoundation/CoreFoundation.h> |
| 11 |
#include <Security/SecKey.h> |
| 12 |
#include <Security/Security.h> |
| 13 |
|
| 14 |
struct commoncrypto_ecc_key_pair { |
| 15 |
struct aws_ecc_key_pair key_pair; |
| 16 |
SecKeyRef priv_key_ref; |
| 17 |
SecKeyRef pub_key_ref; |
| 18 |
CFAllocatorRef cf_allocator; |
| 19 |
}; |
| 20 |
|
| 21 |
static uint8_t s_preamble = 0x04; |
| 22 |
|
| 23 |
static size_t s_der_overhead = 8; |
| 24 |
|
| 25 |
|
| 26 |
const static uint8_t s_fake_x_ecdsa_p256[] = { |
| 27 |
0xd0, 0x72, 0x0d, 0xc6, 0x91, 0xaa, 0x80, 0x09, 0x6b, 0xa3, 0x2f, 0xed, 0x1c, 0xb9, 0x7c, 0x2b, |
| 28 |
0x62, 0x06, 0x90, 0xd0, 0x6d, 0xe0, 0x31, 0x7b, 0x86, 0x18, 0xd5, 0xce, 0x65, 0xeb, 0x72, 0x8f, |
| 29 |
}; |
| 30 |
|
| 31 |
const static uint8_t s_fake_y_ecdsa_p256[] = { |
| 32 |
0x96, 0x81, 0xb5, 0x17, 0xb1, 0xcd, 0xa1, 0x7d, 0x0d, 0x83, 0xd3, 0x35, 0xd9, 0xc4, 0xa8, 0xa9, |
| 33 |
0xa9, 0xb0, 0xb1, 0xb3, 0xc7, 0x10, 0x6d, 0x8f, 0x3c, 0x72, 0xbc, 0x50, 0x93, 0xdc, 0x27, 0x5f, |
| 34 |
}; |
| 35 |
|
| 36 |
const static uint8_t s_fake_x_ecdsa_p384[] = { |
| 37 |
0xfd, 0x3c, 0x84, 0xe5, 0x68, 0x9b, 0xed, 0x27, 0x0e, 0x60, 0x1b, 0x3d, 0x80, 0xf9, 0x0d, 0x67, |
| 38 |
0xa9, 0xae, 0x45, 0x1c, 0xce, 0x89, 0x0f, 0x53, 0xe5, 0x83, 0x22, 0x9a, 0xd0, 0xe2, 0xee, 0x64, |
| 39 |
0x56, 0x11, 0xfa, 0x99, 0x36, 0xdf, 0xa4, 0x53, 0x06, 0xec, 0x18, 0x06, 0x67, 0x74, 0xaa, 0x24, |
| 40 |
}; |
| 41 |
|
| 42 |
const static uint8_t s_fake_y_ecdsa_p384[] = { |
| 43 |
0xb8, 0x3c, 0xa4, 0x12, 0x6c, 0xfc, 0x4c, 0x4d, 0x1d, 0x18, 0xa4, 0xb6, 0xc2, 0x1c, 0x7f, 0x69, |
| 44 |
0x9d, 0x51, 0x23, 0xdd, 0x9c, 0x24, 0xf6, 0x6f, 0x83, 0x38, 0x46, 0xee, 0xb5, 0x82, 0x96, 0x19, |
| 45 |
0x6b, 0x42, 0xec, 0x06, 0x42, 0x5d, 0xb5, 0xb7, 0x0a, 0x4b, 0x81, 0xb7, 0xfc, 0xf7, 0x05, 0xa0, |
| 46 |
}; |
| 47 |
|
| 48 |
static int s_sign_message( |
| 49 |
const struct aws_ecc_key_pair *key_pair, |
| 50 |
const struct aws_byte_cursor *message, |
| 51 |
struct aws_byte_buf *signature_output) { |
| 52 |
struct commoncrypto_ecc_key_pair *cc_key = key_pair->impl; |
| 53 |
|
| 54 |
if (!cc_key->priv_key_ref) { |
| 55 |
return aws_raise_error(AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT); |
| 56 |
} |
| 57 |
|
| 58 |
CFDataRef hash_ref = CFDataCreateWithBytesNoCopy(NULL, message->ptr, message->len, kCFAllocatorNull); |
| 59 |
AWS_FATAL_ASSERT(hash_ref && "No allocations should have happened here, this function shouldn't be able to fail."); |
| 60 |
|
| 61 |
CFErrorRef error = NULL; |
| 62 |
CFDataRef signature = |
| 63 |
SecKeyCreateSignature(cc_key->priv_key_ref, kSecKeyAlgorithmECDSASignatureDigestX962, hash_ref, &error); |
| 64 |
|
| 65 |
if (error) { |
| 66 |
CFRelease(hash_ref); |
| 67 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 68 |
} |
| 69 |
|
| 70 |
struct aws_byte_cursor to_write = |
| 71 |
aws_byte_cursor_from_array(CFDataGetBytePtr(signature), CFDataGetLength(signature)); |
| 72 |
|
| 73 |
if (aws_byte_buf_append(signature_output, &to_write)) { |
| 74 |
CFRelease(signature); |
| 75 |
CFRelease(hash_ref); |
| 76 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 77 |
} |
| 78 |
|
| 79 |
CFRelease(signature); |
| 80 |
CFRelease(hash_ref); |
| 81 |
|
| 82 |
return AWS_OP_SUCCESS; |
| 83 |
} |
| 84 |
|
| 85 |
static size_t s_signature_length(const struct aws_ecc_key_pair *key_pair) { |
| 86 |
return aws_ecc_key_coordinate_byte_size_from_curve_name(key_pair->curve_name) * 2 + s_der_overhead; |
| 87 |
} |
| 88 |
|
| 89 |
static int s_verify_signature( |
| 90 |
const struct aws_ecc_key_pair *key_pair, |
| 91 |
const struct aws_byte_cursor *message, |
| 92 |
const struct aws_byte_cursor *signature) { |
| 93 |
struct commoncrypto_ecc_key_pair *cc_key = key_pair->impl; |
| 94 |
|
| 95 |
if (!cc_key->pub_key_ref) { |
| 96 |
return aws_raise_error(AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT); |
| 97 |
} |
| 98 |
|
| 99 |
CFDataRef hash_ref = CFDataCreateWithBytesNoCopy(NULL, message->ptr, message->len, kCFAllocatorNull); |
| 100 |
CFDataRef signature_ref = CFDataCreateWithBytesNoCopy(NULL, signature->ptr, signature->len, kCFAllocatorNull); |
| 101 |
|
| 102 |
AWS_FATAL_ASSERT(hash_ref && "No allocations should have happened here, this function shouldn't be able to fail."); |
| 103 |
AWS_FATAL_ASSERT( |
| 104 |
signature_ref && "No allocations should have happened here, this function shouldn't be able to fail."); |
| 105 |
|
| 106 |
CFErrorRef error = NULL; |
| 107 |
|
| 108 |
bool verified = SecKeyVerifySignature( |
| 109 |
cc_key->pub_key_ref, kSecKeyAlgorithmECDSASignatureDigestX962, hash_ref, signature_ref, &error); |
| 110 |
|
| 111 |
CFRelease(signature_ref); |
| 112 |
CFRelease(hash_ref); |
| 113 |
|
| 114 |
return verified ? AWS_OP_SUCCESS : aws_raise_error(AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED); |
| 115 |
} |
| 116 |
|
| 117 |
static int s_derive_public_key(struct aws_ecc_key_pair *key_pair) { |
| 118 |
|
| 119 |
if (key_pair->pub_x.buffer && key_pair->pub_x.len) { |
| 120 |
return AWS_OP_SUCCESS; |
| 121 |
} |
| 122 |
|
| 123 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 124 |
} |
| 125 |
|
| 126 |
static void s_destroy_key(struct aws_ecc_key_pair *key_pair) { |
| 127 |
if (key_pair) { |
| 128 |
struct commoncrypto_ecc_key_pair *cc_key = key_pair->impl; |
| 129 |
|
| 130 |
if (cc_key->pub_key_ref) { |
| 131 |
CFRelease(cc_key->pub_key_ref); |
| 132 |
} |
| 133 |
|
| 134 |
if (cc_key->priv_key_ref) { |
| 135 |
CFRelease(cc_key->priv_key_ref); |
| 136 |
} |
| 137 |
|
| 138 |
if (cc_key->cf_allocator) { |
| 139 |
aws_wrapped_cf_allocator_destroy(cc_key->cf_allocator); |
| 140 |
} |
| 141 |
|
| 142 |
aws_byte_buf_clean_up_secure(&key_pair->key_buf); |
| 143 |
aws_mem_release(key_pair->allocator, cc_key); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static struct aws_ecc_key_pair_vtable s_key_pair_vtable = { |
| 148 |
.sign_message = s_sign_message, |
| 149 |
.signature_length = s_signature_length, |
| 150 |
.verify_signature = s_verify_signature, |
| 151 |
.derive_pub_key = s_derive_public_key, |
| 152 |
.destroy = s_destroy_key, |
| 153 |
}; |
| 154 |
|
| 155 |
static struct commoncrypto_ecc_key_pair *s_alloc_pair_and_init_buffers( |
| 156 |
struct aws_allocator *allocator, |
| 157 |
enum aws_ecc_curve_name curve_name, |
| 158 |
struct aws_byte_cursor pub_x, |
| 159 |
struct aws_byte_cursor pub_y, |
| 160 |
struct aws_byte_cursor priv_key) { |
| 161 |
struct commoncrypto_ecc_key_pair *cc_key_pair = |
| 162 |
aws_mem_calloc(allocator, 1, sizeof(struct commoncrypto_ecc_key_pair)); |
| 163 |
|
| 164 |
if (!cc_key_pair) { |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
aws_atomic_init_int(&cc_key_pair->key_pair.ref_count, 1); |
| 169 |
cc_key_pair->key_pair.impl = cc_key_pair; |
| 170 |
cc_key_pair->key_pair.allocator = allocator; |
| 171 |
cc_key_pair->cf_allocator = aws_wrapped_cf_allocator_new(allocator); |
| 172 |
|
| 173 |
if (!cc_key_pair->cf_allocator) { |
| 174 |
goto error; |
| 175 |
} |
| 176 |
|
| 177 |
size_t s_key_coordinate_size = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 178 |
|
| 179 |
if (!s_key_coordinate_size) { |
| 180 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 181 |
goto error; |
| 182 |
} |
| 183 |
|
| 184 |
if ((pub_x.ptr && pub_x.len != s_key_coordinate_size) || (pub_y.ptr && pub_y.len != s_key_coordinate_size) || |
| 185 |
(priv_key.ptr && priv_key.len != s_key_coordinate_size)) { |
| 186 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 187 |
goto error; |
| 188 |
} |
| 189 |
|
| 190 |
size_t total_buffer_size = s_key_coordinate_size * 3 + 1; |
| 191 |
|
| 192 |
if (aws_byte_buf_init(&cc_key_pair->key_pair.key_buf, allocator, total_buffer_size)) { |
| 193 |
goto error; |
| 194 |
} |
| 195 |
|
| 196 |
aws_byte_buf_write_u8(&cc_key_pair->key_pair.key_buf, s_preamble); |
| 197 |
|
| 198 |
if (pub_x.ptr && pub_y.ptr) { |
| 199 |
aws_byte_buf_append(&cc_key_pair->key_pair.key_buf, &pub_x); |
| 200 |
aws_byte_buf_append(&cc_key_pair->key_pair.key_buf, &pub_y); |
| 201 |
} else { |
| 202 |
aws_byte_buf_write_u8_n(&cc_key_pair->key_pair.key_buf, 0x0, s_key_coordinate_size * 2); |
| 203 |
} |
| 204 |
|
| 205 |
if (priv_key.ptr) { |
| 206 |
aws_byte_buf_append(&cc_key_pair->key_pair.key_buf, &priv_key); |
| 207 |
} |
| 208 |
|
| 209 |
if (pub_x.ptr) { |
| 210 |
cc_key_pair->key_pair.pub_x = |
| 211 |
aws_byte_buf_from_array(cc_key_pair->key_pair.key_buf.buffer + 1, s_key_coordinate_size); |
| 212 |
|
| 213 |
cc_key_pair->key_pair.pub_y = |
| 214 |
aws_byte_buf_from_array(cc_key_pair->key_pair.pub_x.buffer + s_key_coordinate_size, s_key_coordinate_size); |
| 215 |
} |
| 216 |
|
| 217 |
cc_key_pair->key_pair.priv_d = aws_byte_buf_from_array( |
| 218 |
cc_key_pair->key_pair.key_buf.buffer + 1 + (s_key_coordinate_size * 2), s_key_coordinate_size); |
| 219 |
cc_key_pair->key_pair.vtable = &s_key_pair_vtable; |
| 220 |
cc_key_pair->key_pair.curve_name = curve_name; |
| 221 |
|
| 222 |
return cc_key_pair; |
| 223 |
|
| 224 |
error: |
| 225 |
s_destroy_key(&cc_key_pair->key_pair); |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
|
| 229 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key_impl( |
| 230 |
struct aws_allocator *allocator, |
| 231 |
enum aws_ecc_curve_name curve_name, |
| 232 |
const struct aws_byte_cursor *priv_key) { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
struct aws_byte_cursor fake_pub_x; |
| 249 |
AWS_ZERO_STRUCT(fake_pub_x); |
| 250 |
struct aws_byte_cursor fake_pub_y; |
| 251 |
AWS_ZERO_STRUCT(fake_pub_y); |
| 252 |
switch (curve_name) { |
| 253 |
case AWS_CAL_ECDSA_P256: |
| 254 |
fake_pub_x = aws_byte_cursor_from_array(s_fake_x_ecdsa_p256, AWS_ARRAY_SIZE(s_fake_x_ecdsa_p256)); |
| 255 |
fake_pub_y = aws_byte_cursor_from_array(s_fake_y_ecdsa_p256, AWS_ARRAY_SIZE(s_fake_y_ecdsa_p256)); |
| 256 |
break; |
| 257 |
case AWS_CAL_ECDSA_P384: |
| 258 |
fake_pub_x = aws_byte_cursor_from_array(s_fake_x_ecdsa_p384, AWS_ARRAY_SIZE(s_fake_x_ecdsa_p384)); |
| 259 |
fake_pub_y = aws_byte_cursor_from_array(s_fake_y_ecdsa_p384, AWS_ARRAY_SIZE(s_fake_y_ecdsa_p384)); |
| 260 |
break; |
| 261 |
default: |
| 262 |
aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
struct commoncrypto_ecc_key_pair *cc_key_pair = |
| 267 |
s_alloc_pair_and_init_buffers(allocator, curve_name, fake_pub_x, fake_pub_y, *priv_key); |
| 268 |
|
| 269 |
if (!cc_key_pair) { |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
CFMutableDictionaryRef key_attributes = NULL; |
| 274 |
CFDataRef private_key_data = CFDataCreate( |
| 275 |
cc_key_pair->cf_allocator, cc_key_pair->key_pair.key_buf.buffer, cc_key_pair->key_pair.key_buf.len); |
| 276 |
|
| 277 |
if (!private_key_data) { |
| 278 |
goto error; |
| 279 |
} |
| 280 |
|
| 281 |
key_attributes = CFDictionaryCreateMutable(cc_key_pair->cf_allocator, 6, NULL, NULL); |
| 282 |
|
| 283 |
if (!key_attributes) { |
| 284 |
goto error; |
| 285 |
} |
| 286 |
|
| 287 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyType, kSecAttrKeyTypeECSECPrimeRandom); |
| 288 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyClass, kSecAttrKeyClassPrivate); |
| 289 |
CFIndex key_size_bits = cc_key_pair->key_pair.priv_d.len * 8; |
| 290 |
CFDictionaryAddValue(key_attributes, kSecAttrKeySizeInBits, &key_size_bits); |
| 291 |
CFDictionaryAddValue(key_attributes, kSecAttrCanSign, kCFBooleanTrue); |
| 292 |
CFDictionaryAddValue(key_attributes, kSecAttrCanVerify, kCFBooleanFalse); |
| 293 |
CFDictionaryAddValue(key_attributes, kSecAttrCanDerive, kCFBooleanTrue); |
| 294 |
|
| 295 |
CFErrorRef error = NULL; |
| 296 |
|
| 297 |
cc_key_pair->priv_key_ref = SecKeyCreateWithData(private_key_data, key_attributes, &error); |
| 298 |
|
| 299 |
if (error) { |
| 300 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 301 |
CFRelease(error); |
| 302 |
goto error; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
aws_byte_buf_secure_zero(&cc_key_pair->key_pair.pub_x); |
| 307 |
aws_byte_buf_secure_zero(&cc_key_pair->key_pair.pub_y); |
| 308 |
|
| 309 |
CFRelease(key_attributes); |
| 310 |
CFRelease(private_key_data); |
| 311 |
|
| 312 |
return &cc_key_pair->key_pair; |
| 313 |
|
| 314 |
error: |
| 315 |
if (private_key_data) { |
| 316 |
CFRelease(private_key_data); |
| 317 |
} |
| 318 |
|
| 319 |
if (key_attributes) { |
| 320 |
CFRelease(key_attributes); |
| 321 |
} |
| 322 |
s_destroy_key(&cc_key_pair->key_pair); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key_impl( |
| 327 |
struct aws_allocator *allocator, |
| 328 |
enum aws_ecc_curve_name curve_name, |
| 329 |
const struct aws_byte_cursor *public_key_x, |
| 330 |
const struct aws_byte_cursor *public_key_y) { |
| 331 |
|
| 332 |
struct aws_byte_cursor empty_cur; |
| 333 |
AWS_ZERO_STRUCT(empty_cur); |
| 334 |
struct commoncrypto_ecc_key_pair *cc_key_pair = |
| 335 |
s_alloc_pair_and_init_buffers(allocator, curve_name, *public_key_x, *public_key_y, empty_cur); |
| 336 |
|
| 337 |
if (!cc_key_pair) { |
| 338 |
return NULL; |
| 339 |
} |
| 340 |
|
| 341 |
CFMutableDictionaryRef key_attributes = NULL; |
| 342 |
CFDataRef pub_key_data = CFDataCreate( |
| 343 |
cc_key_pair->cf_allocator, cc_key_pair->key_pair.key_buf.buffer, cc_key_pair->key_pair.key_buf.len); |
| 344 |
|
| 345 |
if (!pub_key_data) { |
| 346 |
goto error; |
| 347 |
} |
| 348 |
|
| 349 |
key_attributes = CFDictionaryCreateMutable(cc_key_pair->cf_allocator, 6, NULL, NULL); |
| 350 |
|
| 351 |
if (!key_attributes) { |
| 352 |
goto error; |
| 353 |
} |
| 354 |
|
| 355 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyType, kSecAttrKeyTypeECSECPrimeRandom); |
| 356 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyClass, kSecAttrKeyClassPublic); |
| 357 |
CFIndex key_size_bits = cc_key_pair->key_pair.pub_x.len * 8; |
| 358 |
CFDictionaryAddValue(key_attributes, kSecAttrKeySizeInBits, &key_size_bits); |
| 359 |
CFDictionaryAddValue(key_attributes, kSecAttrCanSign, kCFBooleanFalse); |
| 360 |
CFDictionaryAddValue(key_attributes, kSecAttrCanVerify, kCFBooleanTrue); |
| 361 |
CFDictionaryAddValue(key_attributes, kSecAttrCanDerive, kCFBooleanFalse); |
| 362 |
|
| 363 |
CFErrorRef error = NULL; |
| 364 |
|
| 365 |
cc_key_pair->pub_key_ref = SecKeyCreateWithData(pub_key_data, key_attributes, &error); |
| 366 |
|
| 367 |
if (error) { |
| 368 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 369 |
CFRelease(error); |
| 370 |
goto error; |
| 371 |
} |
| 372 |
|
| 373 |
CFRelease(key_attributes); |
| 374 |
CFRelease(pub_key_data); |
| 375 |
|
| 376 |
return &cc_key_pair->key_pair; |
| 377 |
|
| 378 |
error: |
| 379 |
if (key_attributes) { |
| 380 |
CFRelease(key_attributes); |
| 381 |
} |
| 382 |
|
| 383 |
if (pub_key_data) { |
| 384 |
CFRelease(pub_key_data); |
| 385 |
} |
| 386 |
|
| 387 |
s_destroy_key(&cc_key_pair->key_pair); |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random_impl( |
| 392 |
struct aws_allocator *allocator, |
| 393 |
enum aws_ecc_curve_name curve_name) { |
| 394 |
struct commoncrypto_ecc_key_pair *cc_key_pair = |
| 395 |
aws_mem_calloc(allocator, 1, sizeof(struct commoncrypto_ecc_key_pair)); |
| 396 |
|
| 397 |
if (!cc_key_pair) { |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
CFStringRef key_size_cf_str = NULL; |
| 402 |
CFMutableDictionaryRef key_attributes = NULL; |
| 403 |
CFErrorRef error = NULL; |
| 404 |
CFDataRef keyData = NULL; |
| 405 |
|
| 406 |
aws_atomic_init_int(&cc_key_pair->key_pair.ref_count, 1); |
| 407 |
cc_key_pair->key_pair.impl = cc_key_pair; |
| 408 |
cc_key_pair->key_pair.allocator = allocator; |
| 409 |
cc_key_pair->cf_allocator = aws_wrapped_cf_allocator_new(allocator); |
| 410 |
|
| 411 |
if (!cc_key_pair->cf_allocator) { |
| 412 |
goto error; |
| 413 |
} |
| 414 |
|
| 415 |
size_t key_coordinate_size = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 416 |
|
| 417 |
if (!key_coordinate_size) { |
| 418 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 419 |
goto error; |
| 420 |
} |
| 421 |
|
| 422 |
key_attributes = CFDictionaryCreateMutable(cc_key_pair->cf_allocator, 6, NULL, NULL); |
| 423 |
|
| 424 |
if (!key_attributes) { |
| 425 |
goto error; |
| 426 |
} |
| 427 |
|
| 428 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyType, kSecAttrKeyTypeECSECPrimeRandom); |
| 429 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyClass, kSecAttrKeyClassPrivate); |
| 430 |
CFIndex key_size_bits = key_coordinate_size * 8; |
| 431 |
char key_size_str[32] = {0}; |
| 432 |
snprintf(key_size_str, sizeof(key_size_str), "%d", (int)key_size_bits); |
| 433 |
key_size_cf_str = CFStringCreateWithCString(cc_key_pair->cf_allocator, key_size_str, kCFStringEncodingASCII); |
| 434 |
|
| 435 |
if (!key_size_cf_str) { |
| 436 |
goto error; |
| 437 |
} |
| 438 |
|
| 439 |
CFDictionaryAddValue(key_attributes, kSecAttrKeySizeInBits, key_size_cf_str); |
| 440 |
|
| 441 |
cc_key_pair->priv_key_ref = SecKeyCreateRandomKey(key_attributes, &error); |
| 442 |
|
| 443 |
if (error) { |
| 444 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 445 |
goto error; |
| 446 |
} |
| 447 |
|
| 448 |
cc_key_pair->pub_key_ref = SecKeyCopyPublicKey(cc_key_pair->priv_key_ref); |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
keyData = SecKeyCopyExternalRepresentation(cc_key_pair->priv_key_ref, &error); |
| 453 |
|
| 454 |
if (!keyData || error) { |
| 455 |
if (error) { |
| 456 |
AWS_LOGF_ERROR( |
| 457 |
AWS_LS_CAL_ECC, "SecKeyCopyExternalRepresentation call failed with %ld", CFErrorGetCode(error)); |
| 458 |
} |
| 459 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 460 |
goto error; |
| 461 |
} |
| 462 |
|
| 463 |
struct aws_byte_cursor key_data_cur = |
| 464 |
aws_byte_cursor_from_array(CFDataGetBytePtr(keyData), CFDataGetLength(keyData)); |
| 465 |
|
| 466 |
size_t total_buffer_size = key_coordinate_size * 3 + 1; |
| 467 |
if (key_data_cur.len != total_buffer_size) { |
| 468 |
AWS_LOGF_ERROR(AWS_LS_CAL_ECC, "Something went wrong. Apple Security Framework didn't return full key data."); |
| 469 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 470 |
goto error; |
| 471 |
} |
| 472 |
|
| 473 |
if (aws_byte_buf_init_copy_from_cursor(&cc_key_pair->key_pair.key_buf, allocator, key_data_cur)) { |
| 474 |
goto error; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
cc_key_pair->key_pair.pub_x = |
| 479 |
aws_byte_buf_from_array(cc_key_pair->key_pair.key_buf.buffer + 1, key_coordinate_size); |
| 480 |
cc_key_pair->key_pair.pub_y = |
| 481 |
aws_byte_buf_from_array(cc_key_pair->key_pair.pub_x.buffer + key_coordinate_size, key_coordinate_size); |
| 482 |
cc_key_pair->key_pair.priv_d = |
| 483 |
aws_byte_buf_from_array(cc_key_pair->key_pair.pub_y.buffer + key_coordinate_size, key_coordinate_size); |
| 484 |
|
| 485 |
cc_key_pair->key_pair.curve_name = curve_name; |
| 486 |
cc_key_pair->key_pair.vtable = &s_key_pair_vtable; |
| 487 |
|
| 488 |
CFRelease(keyData); |
| 489 |
CFRelease(key_size_cf_str); |
| 490 |
CFRelease(key_attributes); |
| 491 |
|
| 492 |
return &cc_key_pair->key_pair; |
| 493 |
|
| 494 |
error: |
| 495 |
if (keyData) { |
| 496 |
CFRelease(keyData); |
| 497 |
} |
| 498 |
|
| 499 |
if (key_attributes) { |
| 500 |
CFRelease(key_attributes); |
| 501 |
} |
| 502 |
|
| 503 |
if (key_size_cf_str) { |
| 504 |
CFRelease(key_size_cf_str); |
| 505 |
} |
| 506 |
|
| 507 |
if (error) { |
| 508 |
CFRelease(error); |
| 509 |
} |
| 510 |
|
| 511 |
s_destroy_key(&cc_key_pair->key_pair); |
| 512 |
return NULL; |
| 513 |
} |
| 514 |
|
| 515 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1_impl( |
| 516 |
struct aws_allocator *allocator, |
| 517 |
const struct aws_byte_cursor *encoded_keys) { |
| 518 |
|
| 519 |
struct aws_ecc_key_pair *key_pair = NULL; |
| 520 |
struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, *encoded_keys); |
| 521 |
CFMutableDictionaryRef key_attributes = NULL; |
| 522 |
CFDataRef key_data = NULL; |
| 523 |
|
| 524 |
if (!decoder) { |
| 525 |
return NULL; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
struct aws_byte_cursor pub_x; |
| 530 |
AWS_ZERO_STRUCT(pub_x); |
| 531 |
struct aws_byte_cursor pub_y; |
| 532 |
AWS_ZERO_STRUCT(pub_y); |
| 533 |
struct aws_byte_cursor priv_d; |
| 534 |
AWS_ZERO_STRUCT(priv_d); |
| 535 |
enum aws_ecc_curve_name curve_name; |
| 536 |
|
| 537 |
if (aws_der_decoder_load_ecc_key_pair(decoder, &pub_x, &pub_y, &priv_d, &curve_name)) { |
| 538 |
goto error; |
| 539 |
} |
| 540 |
|
| 541 |
if (!pub_x.ptr && !priv_d.ptr) { |
| 542 |
aws_raise_error(AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT); |
| 543 |
goto error; |
| 544 |
} |
| 545 |
|
| 546 |
struct commoncrypto_ecc_key_pair *cc_key_pair = |
| 547 |
s_alloc_pair_and_init_buffers(allocator, curve_name, pub_x, pub_y, priv_d); |
| 548 |
|
| 549 |
if (!cc_key_pair) { |
| 550 |
goto error; |
| 551 |
} |
| 552 |
|
| 553 |
key_pair = &cc_key_pair->key_pair; |
| 554 |
|
| 555 |
key_data = CFDataCreate( |
| 556 |
cc_key_pair->cf_allocator, cc_key_pair->key_pair.key_buf.buffer, cc_key_pair->key_pair.key_buf.len); |
| 557 |
|
| 558 |
if (!key_data) { |
| 559 |
goto error; |
| 560 |
} |
| 561 |
|
| 562 |
key_attributes = CFDictionaryCreateMutable(cc_key_pair->cf_allocator, 6, NULL, NULL); |
| 563 |
|
| 564 |
if (!key_attributes) { |
| 565 |
goto error; |
| 566 |
} |
| 567 |
|
| 568 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyType, kSecAttrKeyTypeECSECPrimeRandom); |
| 569 |
|
| 570 |
if (priv_d.ptr) { |
| 571 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyClass, kSecAttrKeyClassPrivate); |
| 572 |
|
| 573 |
CFDictionaryAddValue(key_attributes, kSecAttrCanSign, kCFBooleanTrue); |
| 574 |
CFDictionaryAddValue(key_attributes, kSecAttrCanDerive, kCFBooleanTrue); |
| 575 |
|
| 576 |
if (pub_x.ptr) { |
| 577 |
CFDictionaryAddValue(key_attributes, kSecAttrCanVerify, kCFBooleanTrue); |
| 578 |
} |
| 579 |
} else if (pub_x.ptr) { |
| 580 |
CFDictionaryAddValue(key_attributes, kSecAttrKeyClass, kSecAttrKeyClassPublic); |
| 581 |
CFDictionaryAddValue(key_attributes, kSecAttrCanSign, kCFBooleanFalse); |
| 582 |
CFDictionaryAddValue(key_attributes, kSecAttrCanVerify, kCFBooleanTrue); |
| 583 |
} |
| 584 |
|
| 585 |
CFErrorRef error = NULL; |
| 586 |
|
| 587 |
cc_key_pair->priv_key_ref = SecKeyCreateWithData(key_data, key_attributes, &error); |
| 588 |
|
| 589 |
if (error) { |
| 590 |
aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 591 |
} |
| 592 |
|
| 593 |
if (pub_x.ptr) { |
| 594 |
cc_key_pair->pub_key_ref = SecKeyCopyPublicKey(cc_key_pair->priv_key_ref); |
| 595 |
} |
| 596 |
|
| 597 |
CFRelease(key_attributes); |
| 598 |
CFRelease(key_data); |
| 599 |
aws_der_decoder_destroy(decoder); |
| 600 |
|
| 601 |
return key_pair; |
| 602 |
|
| 603 |
error: |
| 604 |
if (decoder) { |
| 605 |
aws_der_decoder_destroy(decoder); |
| 606 |
} |
| 607 |
|
| 608 |
if (key_attributes) { |
| 609 |
CFRelease(key_attributes); |
| 610 |
} |
| 611 |
|
| 612 |
if (key_data) { |
| 613 |
CFRelease(key_data); |
| 614 |
} |
| 615 |
|
| 616 |
if (key_pair) { |
| 617 |
s_destroy_key(key_pair); |
| 618 |
} |
| 619 |
return NULL; |
| 620 |
} |