| 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 |
#include <aws/common/encoding.h> |
| 10 |
|
| 11 |
#define STATIC_INIT_BYTE_CURSOR(a, name) \ |
| 12 |
static struct aws_byte_cursor s_##name = { \ |
| 13 |
.ptr = (a), \ |
| 14 |
.len = sizeof(a), \ |
| 15 |
}; |
| 16 |
|
| 17 |
static uint8_t s_p256_oid[] = { |
| 18 |
0x2A, |
| 19 |
0x86, |
| 20 |
0x48, |
| 21 |
0xCE, |
| 22 |
0x3D, |
| 23 |
0x03, |
| 24 |
0x01, |
| 25 |
0x07, |
| 26 |
}; |
| 27 |
STATIC_INIT_BYTE_CURSOR(s_p256_oid, ecc_p256_oid) |
| 28 |
|
| 29 |
static uint8_t s_p384_oid[] = { |
| 30 |
0x2B, |
| 31 |
0x81, |
| 32 |
0x04, |
| 33 |
0x00, |
| 34 |
0x22, |
| 35 |
}; |
| 36 |
STATIC_INIT_BYTE_CURSOR(s_p384_oid, ecc_p384_oid) |
| 37 |
|
| 38 |
static struct aws_byte_cursor *s_ecc_curve_oids[] = { |
| 39 |
[AWS_CAL_ECDSA_P256] = &s_ecc_p256_oid, |
| 40 |
[AWS_CAL_ECDSA_P384] = &s_ecc_p384_oid, |
| 41 |
}; |
| 42 |
|
| 43 |
int aws_ecc_curve_name_from_oid(struct aws_byte_cursor *oid, enum aws_ecc_curve_name *curve_name) { |
| 44 |
if (aws_byte_cursor_eq(oid, &s_ecc_p256_oid)) { |
| 45 |
*curve_name = AWS_CAL_ECDSA_P256; |
| 46 |
return AWS_OP_SUCCESS; |
| 47 |
} |
| 48 |
|
| 49 |
if (aws_byte_cursor_eq(oid, &s_ecc_p384_oid)) { |
| 50 |
*curve_name = AWS_CAL_ECDSA_P384; |
| 51 |
return AWS_OP_SUCCESS; |
| 52 |
} |
| 53 |
|
| 54 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 55 |
} |
| 56 |
|
| 57 |
int aws_ecc_oid_from_curve_name(enum aws_ecc_curve_name curve_name, struct aws_byte_cursor *oid) { |
| 58 |
if (curve_name < AWS_CAL_ECDSA_P256 || curve_name > AWS_CAL_ECDSA_P384) { |
| 59 |
return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM); |
| 60 |
} |
| 61 |
*oid = *s_ecc_curve_oids[curve_name]; |
| 62 |
return AWS_OP_SUCCESS; |
| 63 |
} |
| 64 |
|
| 65 |
typedef struct aws_ecc_key_pair *(aws_ecc_key_pair_new_from_public_key_fn)(struct aws_allocator *allocator, |
| 66 |
enum aws_ecc_curve_name curve_name, |
| 67 |
const struct aws_byte_cursor *public_key_x, |
| 68 |
const struct aws_byte_cursor *public_key_y); |
| 69 |
|
| 70 |
typedef struct aws_ecc_key_pair *(aws_ecc_key_pair_new_from_private_key_fn)(struct aws_allocator *allocator, |
| 71 |
enum aws_ecc_curve_name curve_name, |
| 72 |
const struct aws_byte_cursor *priv_key); |
| 73 |
|
| 74 |
typedef struct aws_ecc_key_pair *(aws_ecc_key_pair_new_from_asn1_fn)(struct aws_allocator *allocator, |
| 75 |
const struct aws_byte_cursor *encoded_keys); |
| 76 |
|
| 77 |
typedef struct aws_ecc_key_pair *(aws_ecc_key_pair_new_generate_random_fn)(struct aws_allocator *allocator, |
| 78 |
enum aws_ecc_curve_name curve_name); |
| 79 |
|
| 80 |
#ifndef BYO_CRYPTO |
| 81 |
|
| 82 |
extern struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key_impl( |
| 83 |
struct aws_allocator *allocator, |
| 84 |
enum aws_ecc_curve_name curve_name, |
| 85 |
const struct aws_byte_cursor *public_key_x, |
| 86 |
const struct aws_byte_cursor *public_key_y); |
| 87 |
|
| 88 |
extern struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key_impl( |
| 89 |
struct aws_allocator *allocator, |
| 90 |
enum aws_ecc_curve_name curve_name, |
| 91 |
const struct aws_byte_cursor *priv_key); |
| 92 |
|
| 93 |
extern struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1_impl( |
| 94 |
struct aws_allocator *allocator, |
| 95 |
const struct aws_byte_cursor *encoded_keys); |
| 96 |
|
| 97 |
extern struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random_impl( |
| 98 |
struct aws_allocator *allocator, |
| 99 |
enum aws_ecc_curve_name curve_name); |
| 100 |
|
| 101 |
#else |
| 102 |
|
| 103 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key_impl( |
| 104 |
struct aws_allocator *allocator, |
| 105 |
enum aws_ecc_curve_name curve_name, |
| 106 |
const struct aws_byte_cursor *public_key_x, |
| 107 |
const struct aws_byte_cursor *public_key_y) { |
| 108 |
(void)allocator; |
| 109 |
(void)curve_name; |
| 110 |
(void)public_key_x; |
| 111 |
(void)public_key_y; |
| 112 |
abort(); |
| 113 |
} |
| 114 |
|
| 115 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key_impl( |
| 116 |
struct aws_allocator *allocator, |
| 117 |
enum aws_ecc_curve_name curve_name, |
| 118 |
const struct aws_byte_cursor *priv_key) { |
| 119 |
(void)allocator; |
| 120 |
(void)curve_name; |
| 121 |
(void)priv_key; |
| 122 |
abort(); |
| 123 |
} |
| 124 |
|
| 125 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1_impl( |
| 126 |
struct aws_allocator *allocator, |
| 127 |
const struct aws_byte_cursor *encoded_keys) { |
| 128 |
(void)allocator; |
| 129 |
(void)encoded_keys; |
| 130 |
abort(); |
| 131 |
} |
| 132 |
|
| 133 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random_impl( |
| 134 |
struct aws_allocator *allocator, |
| 135 |
enum aws_ecc_curve_name curve_name) { |
| 136 |
(void)allocator; |
| 137 |
(void)curve_name; |
| 138 |
abort(); |
| 139 |
} |
| 140 |
|
| 141 |
#endif |
| 142 |
|
| 143 |
static aws_ecc_key_pair_new_from_public_key_fn *s_ecc_key_pair_new_from_public_key_fn = |
| 144 |
aws_ecc_key_pair_new_from_public_key_impl; |
| 145 |
|
| 146 |
static aws_ecc_key_pair_new_from_private_key_fn *s_ecc_key_pair_new_from_private_key_fn = |
| 147 |
aws_ecc_key_pair_new_from_private_key_impl; |
| 148 |
|
| 149 |
static aws_ecc_key_pair_new_from_asn1_fn *s_ecc_key_pair_new_from_asn1_fn = aws_ecc_key_pair_new_from_asn1_impl; |
| 150 |
|
| 151 |
static aws_ecc_key_pair_new_generate_random_fn *s_ecc_key_pair_new_generate_random1_fn = |
| 152 |
aws_ecc_key_pair_new_generate_random_impl; |
| 153 |
|
| 154 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_public_key( |
| 155 |
struct aws_allocator *allocator, |
| 156 |
enum aws_ecc_curve_name curve_name, |
| 157 |
const struct aws_byte_cursor *public_key_x, |
| 158 |
const struct aws_byte_cursor *public_key_y) { |
| 159 |
return s_ecc_key_pair_new_from_public_key_fn(allocator, curve_name, public_key_x, public_key_y); |
| 160 |
} |
| 161 |
|
| 162 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_private_key( |
| 163 |
struct aws_allocator *allocator, |
| 164 |
enum aws_ecc_curve_name curve_name, |
| 165 |
const struct aws_byte_cursor *priv_key) { |
| 166 |
return s_ecc_key_pair_new_from_private_key_fn(allocator, curve_name, priv_key); |
| 167 |
} |
| 168 |
|
| 169 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_from_asn1( |
| 170 |
struct aws_allocator *allocator, |
| 171 |
const struct aws_byte_cursor *encoded_keys) { |
| 172 |
return s_ecc_key_pair_new_from_asn1_fn(allocator, encoded_keys); |
| 173 |
} |
| 174 |
|
| 175 |
struct aws_ecc_key_pair *aws_ecc_key_pair_new_generate_random( |
| 176 |
struct aws_allocator *allocator, |
| 177 |
enum aws_ecc_curve_name curve_name) { |
| 178 |
return s_ecc_key_pair_new_generate_random1_fn(allocator, curve_name); |
| 179 |
} |
| 180 |
|
| 181 |
static void s_aws_ecc_key_pair_destroy(struct aws_ecc_key_pair *key_pair) { |
| 182 |
if (key_pair) { |
| 183 |
AWS_FATAL_ASSERT(key_pair->vtable->destroy && "ECC KEY PAIR destroy function must be included on the vtable"); |
| 184 |
key_pair->vtable->destroy(key_pair); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
int aws_ecc_key_pair_derive_public_key(struct aws_ecc_key_pair *key_pair) { |
| 189 |
AWS_FATAL_ASSERT(key_pair->vtable->derive_pub_key && "ECC KEY PAIR derive function must be included on the vtable"); |
| 190 |
return key_pair->vtable->derive_pub_key(key_pair); |
| 191 |
} |
| 192 |
|
| 193 |
int aws_ecc_key_pair_sign_message( |
| 194 |
const struct aws_ecc_key_pair *key_pair, |
| 195 |
const struct aws_byte_cursor *message, |
| 196 |
struct aws_byte_buf *signature) { |
| 197 |
AWS_FATAL_ASSERT(key_pair->vtable->sign_message && "ECC KEY PAIR sign message must be included on the vtable"); |
| 198 |
return key_pair->vtable->sign_message(key_pair, message, signature); |
| 199 |
} |
| 200 |
|
| 201 |
int aws_ecc_key_pair_verify_signature( |
| 202 |
const struct aws_ecc_key_pair *key_pair, |
| 203 |
const struct aws_byte_cursor *message, |
| 204 |
const struct aws_byte_cursor *signature) { |
| 205 |
AWS_FATAL_ASSERT( |
| 206 |
key_pair->vtable->verify_signature && "ECC KEY PAIR verify signature must be included on the vtable"); |
| 207 |
return key_pair->vtable->verify_signature(key_pair, message, signature); |
| 208 |
} |
| 209 |
|
| 210 |
size_t aws_ecc_key_pair_signature_length(const struct aws_ecc_key_pair *key_pair) { |
| 211 |
AWS_FATAL_ASSERT( |
| 212 |
key_pair->vtable->signature_length && "ECC KEY PAIR signature length must be included on the vtable"); |
| 213 |
return key_pair->vtable->signature_length(key_pair); |
| 214 |
} |
| 215 |
|
| 216 |
void aws_ecc_key_pair_get_public_key( |
| 217 |
const struct aws_ecc_key_pair *key_pair, |
| 218 |
struct aws_byte_cursor *pub_x, |
| 219 |
struct aws_byte_cursor *pub_y) { |
| 220 |
*pub_x = aws_byte_cursor_from_buf(&key_pair->pub_x); |
| 221 |
*pub_y = aws_byte_cursor_from_buf(&key_pair->pub_y); |
| 222 |
} |
| 223 |
|
| 224 |
void aws_ecc_key_pair_get_private_key(const struct aws_ecc_key_pair *key_pair, struct aws_byte_cursor *private_d) { |
| 225 |
*private_d = aws_byte_cursor_from_buf(&key_pair->priv_d); |
| 226 |
} |
| 227 |
|
| 228 |
size_t aws_ecc_key_coordinate_byte_size_from_curve_name(enum aws_ecc_curve_name curve_name) { |
| 229 |
switch (curve_name) { |
| 230 |
case AWS_CAL_ECDSA_P256: |
| 231 |
return 32; |
| 232 |
case AWS_CAL_ECDSA_P384: |
| 233 |
return 48; |
| 234 |
default: |
| 235 |
return 0; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static void s_parse_public_key( |
| 240 |
struct aws_byte_cursor public_key, |
| 241 |
size_t key_coordinate_size, |
| 242 |
struct aws_byte_cursor *out_public_x_coord, |
| 243 |
struct aws_byte_cursor *out_public_y_coord) { |
| 244 |
|
| 245 |
aws_byte_cursor_advance(&public_key, 1); |
| 246 |
*out_public_x_coord = aws_byte_cursor_advance(&public_key, key_coordinate_size); |
| 247 |
*out_public_y_coord = public_key; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static int s_der_decoder_sec1_private_key_helper( |
| 255 |
struct aws_der_decoder *decoder, |
| 256 |
struct aws_byte_cursor *out_private_cursor, |
| 257 |
struct aws_byte_cursor *out_public_cursor, |
| 258 |
enum aws_ecc_curve_name *out_curve_name, |
| 259 |
bool *curve_name_set) { |
| 260 |
|
| 261 |
AWS_ZERO_STRUCT(*out_private_cursor); |
| 262 |
AWS_ZERO_STRUCT(*out_public_cursor); |
| 263 |
|
| 264 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 265 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 266 |
} |
| 267 |
|
| 268 |
struct aws_byte_cursor version_cur; |
| 269 |
AWS_ZERO_STRUCT(version_cur); |
| 270 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_unsigned_integer(decoder, &version_cur)) { |
| 271 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 272 |
} |
| 273 |
|
| 274 |
if (version_cur.len != 1 || version_cur.ptr[0] != 1) { |
| 275 |
return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT); |
| 276 |
} |
| 277 |
|
| 278 |
struct aws_byte_cursor private_key_cur; |
| 279 |
AWS_ZERO_STRUCT(private_key_cur); |
| 280 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_string(decoder, &private_key_cur)) { |
| 281 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 282 |
} |
| 283 |
|
| 284 |
struct aws_byte_cursor oid; |
| 285 |
AWS_ZERO_STRUCT(oid); |
| 286 |
struct aws_byte_cursor public_key_cur; |
| 287 |
AWS_ZERO_STRUCT(public_key_cur); |
| 288 |
enum aws_ecc_curve_name curve_name = {0}; |
| 289 |
|
| 290 |
*curve_name_set = false; |
| 291 |
if (aws_der_decoder_next(decoder)) { |
| 292 |
|
| 293 |
bool version_found = false; |
| 294 |
|
| 295 |
if (aws_der_decoder_tlv_type(decoder) == (AWS_DER_CLASS_CONTEXT | AWS_DER_FORM_CONSTRUCTED)) { |
| 296 |
if (!aws_der_decoder_next(decoder)) { |
| 297 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 298 |
} |
| 299 |
|
| 300 |
if (aws_der_decoder_tlv_type(decoder) != AWS_DER_OBJECT_IDENTIFIER) { |
| 301 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 302 |
} |
| 303 |
|
| 304 |
if (aws_der_decoder_tlv_blob(decoder, &oid)) { |
| 305 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 306 |
} |
| 307 |
if (aws_ecc_curve_name_from_oid(&oid, &curve_name)) { |
| 308 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 309 |
} |
| 310 |
*curve_name_set = true; |
| 311 |
version_found = true; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (version_found && !aws_der_decoder_next(decoder)) { |
| 319 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 320 |
} |
| 321 |
|
| 322 |
if (aws_der_decoder_tlv_type(decoder) == (AWS_DER_CLASS_CONTEXT | AWS_DER_FORM_CONSTRUCTED | 1)) { |
| 323 |
if (!aws_der_decoder_next(decoder)) { |
| 324 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 325 |
} |
| 326 |
|
| 327 |
if (aws_der_decoder_tlv_string(decoder, &public_key_cur)) { |
| 328 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
*out_public_cursor = public_key_cur; |
| 334 |
*out_private_cursor = private_key_cur; |
| 335 |
*out_curve_name = curve_name; |
| 336 |
|
| 337 |
return AWS_OP_SUCCESS; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static int s_der_decoder_load_ecc_private_key_pair_from_sec1( |
| 350 |
struct aws_der_decoder *decoder, |
| 351 |
struct aws_byte_cursor *out_public_x_coord, |
| 352 |
struct aws_byte_cursor *out_public_y_coord, |
| 353 |
struct aws_byte_cursor *out_private_d, |
| 354 |
enum aws_ecc_curve_name *out_curve_name) { |
| 355 |
|
| 356 |
AWS_ZERO_STRUCT(*out_public_x_coord); |
| 357 |
AWS_ZERO_STRUCT(*out_public_y_coord); |
| 358 |
AWS_ZERO_STRUCT(*out_private_d); |
| 359 |
|
| 360 |
struct aws_byte_cursor private_key_cur; |
| 361 |
struct aws_byte_cursor public_key_cur; |
| 362 |
enum aws_ecc_curve_name curve_name; |
| 363 |
bool curve_name_set = false; |
| 364 |
|
| 365 |
if (s_der_decoder_sec1_private_key_helper( |
| 366 |
decoder, &private_key_cur, &public_key_cur, &curve_name, &curve_name_set)) { |
| 367 |
return AWS_OP_ERR; |
| 368 |
} |
| 369 |
|
| 370 |
if (!curve_name_set) { |
| 371 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 372 |
} |
| 373 |
|
| 374 |
size_t key_coordinate_size = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 375 |
size_t public_key_blob_size = key_coordinate_size * 2 + 1; |
| 376 |
|
| 377 |
if (private_key_cur.len != key_coordinate_size || |
| 378 |
(public_key_cur.len != 0 && public_key_cur.len != public_key_blob_size)) { |
| 379 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 380 |
} |
| 381 |
|
| 382 |
*out_private_d = private_key_cur; |
| 383 |
|
| 384 |
if (public_key_cur.len > 0) { |
| 385 |
s_parse_public_key(public_key_cur, key_coordinate_size, out_public_x_coord, out_public_y_coord); |
| 386 |
} |
| 387 |
|
| 388 |
*out_curve_name = curve_name; |
| 389 |
|
| 390 |
return AWS_OP_SUCCESS; |
| 391 |
} |
| 392 |
|
| 393 |
static uint8_t s_ec_private_key_oid[] = { |
| 394 |
0x2A, |
| 395 |
0x86, |
| 396 |
0x48, |
| 397 |
0xCE, |
| 398 |
0x3D, |
| 399 |
0x02, |
| 400 |
0x01, |
| 401 |
}; |
| 402 |
STATIC_INIT_BYTE_CURSOR(s_ec_private_key_oid, ec_private_key_oid_cursor) |
| 403 |
|
| 404 |
static uint8_t s_ec_public_key_oid[] = { |
| 405 |
0x2A, |
| 406 |
0x86, |
| 407 |
0x48, |
| 408 |
0xCE, |
| 409 |
0x3D, |
| 410 |
0x02, |
| 411 |
0x01, |
| 412 |
}; |
| 413 |
STATIC_INIT_BYTE_CURSOR(s_ec_public_key_oid, ec_public_key_oid_cursor) |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static int s_der_decoder_load_ecc_private_key_pair_from_pkcs8( |
| 434 |
struct aws_der_decoder *decoder, |
| 435 |
struct aws_byte_cursor *out_public_x_coord, |
| 436 |
struct aws_byte_cursor *out_public_y_coord, |
| 437 |
struct aws_byte_cursor *out_private_d, |
| 438 |
enum aws_ecc_curve_name *out_curve_name) { |
| 439 |
|
| 440 |
AWS_ZERO_STRUCT(*out_public_x_coord); |
| 441 |
AWS_ZERO_STRUCT(*out_public_y_coord); |
| 442 |
AWS_ZERO_STRUCT(*out_private_d); |
| 443 |
|
| 444 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 445 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 446 |
} |
| 447 |
|
| 448 |
if (!aws_der_decoder_next(decoder)) { |
| 449 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
struct aws_byte_cursor version_cur; |
| 454 |
AWS_ZERO_STRUCT(version_cur); |
| 455 |
|
| 456 |
|
| 457 |
if (aws_der_decoder_tlv_unsigned_integer(decoder, &version_cur) == AWS_OP_SUCCESS) { |
| 458 |
if (version_cur.len != 1 || version_cur.ptr[0] != 0) { |
| 459 |
return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT); |
| 460 |
} |
| 461 |
if (!aws_der_decoder_next(decoder)) { |
| 462 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if (aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 467 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 468 |
} |
| 469 |
|
| 470 |
struct aws_byte_cursor algo_oid; |
| 471 |
AWS_ZERO_STRUCT(algo_oid); |
| 472 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_OBJECT_IDENTIFIER || |
| 473 |
aws_der_decoder_tlv_blob(decoder, &algo_oid)) { |
| 474 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
if (!aws_byte_cursor_eq(&algo_oid, &s_ec_public_key_oid_cursor) && |
| 482 |
!aws_byte_cursor_eq(&algo_oid, &s_ec_private_key_oid_cursor)) { |
| 483 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 484 |
} |
| 485 |
|
| 486 |
struct aws_byte_cursor curve_oid; |
| 487 |
AWS_ZERO_STRUCT(curve_oid); |
| 488 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_OBJECT_IDENTIFIER || |
| 489 |
aws_der_decoder_tlv_blob(decoder, &curve_oid)) { |
| 490 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 491 |
} |
| 492 |
|
| 493 |
enum aws_ecc_curve_name curve_name; |
| 494 |
if (aws_ecc_curve_name_from_oid(&curve_oid, &curve_name)) { |
| 495 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_OCTET_STRING) { |
| 500 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 501 |
} |
| 502 |
|
| 503 |
struct aws_der_decoder *nested_decoder = aws_der_decoder_nested_tlv_decoder(decoder); |
| 504 |
|
| 505 |
if (!nested_decoder) { |
| 506 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 507 |
} |
| 508 |
|
| 509 |
struct aws_byte_cursor private_key_cur; |
| 510 |
struct aws_byte_cursor public_key_cur; |
| 511 |
enum aws_ecc_curve_name inner_curve_name; |
| 512 |
bool curve_name_set = false; |
| 513 |
if (s_der_decoder_sec1_private_key_helper( |
| 514 |
nested_decoder, &private_key_cur, &public_key_cur, &inner_curve_name, &curve_name_set)) { |
| 515 |
aws_der_decoder_destroy(nested_decoder); |
| 516 |
return AWS_OP_ERR; |
| 517 |
} |
| 518 |
|
| 519 |
aws_der_decoder_destroy(nested_decoder); |
| 520 |
|
| 521 |
if (curve_name_set && inner_curve_name != curve_name) { |
| 522 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 523 |
} |
| 524 |
|
| 525 |
size_t key_coordinate_size = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 526 |
size_t public_key_blob_size = key_coordinate_size * 2 + 1; |
| 527 |
|
| 528 |
if (private_key_cur.len != key_coordinate_size || |
| 529 |
(public_key_cur.len != 0 && public_key_cur.len != public_key_blob_size)) { |
| 530 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 531 |
} |
| 532 |
|
| 533 |
*out_private_d = private_key_cur; |
| 534 |
|
| 535 |
if (public_key_cur.len > 0) { |
| 536 |
s_parse_public_key(public_key_cur, key_coordinate_size, out_public_x_coord, out_public_y_coord); |
| 537 |
} |
| 538 |
|
| 539 |
*out_curve_name = curve_name; |
| 540 |
|
| 541 |
return AWS_OP_SUCCESS; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
static int s_der_decoder_load_ecc_public_key_pair_from_asn1( |
| 556 |
struct aws_der_decoder *decoder, |
| 557 |
struct aws_byte_cursor *out_public_x_coord, |
| 558 |
struct aws_byte_cursor *out_public_y_coord, |
| 559 |
struct aws_byte_cursor *out_private_d, |
| 560 |
enum aws_ecc_curve_name *out_curve_name) { |
| 561 |
|
| 562 |
AWS_ZERO_STRUCT(*out_public_x_coord); |
| 563 |
AWS_ZERO_STRUCT(*out_public_y_coord); |
| 564 |
AWS_ZERO_STRUCT(*out_private_d); |
| 565 |
|
| 566 |
|
| 567 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 568 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 573 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 574 |
} |
| 575 |
|
| 576 |
struct aws_byte_cursor algo_oid; |
| 577 |
AWS_ZERO_STRUCT(algo_oid); |
| 578 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_OBJECT_IDENTIFIER || |
| 579 |
aws_der_decoder_tlv_blob(decoder, &algo_oid)) { |
| 580 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 581 |
} |
| 582 |
|
| 583 |
if (!aws_byte_cursor_eq(&algo_oid, &s_ec_public_key_oid_cursor)) { |
| 584 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 585 |
} |
| 586 |
|
| 587 |
struct aws_byte_cursor curve_oid; |
| 588 |
AWS_ZERO_STRUCT(curve_oid); |
| 589 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_OBJECT_IDENTIFIER || |
| 590 |
aws_der_decoder_tlv_blob(decoder, &curve_oid)) { |
| 591 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 592 |
} |
| 593 |
|
| 594 |
enum aws_ecc_curve_name curve_name; |
| 595 |
if (aws_ecc_curve_name_from_oid(&curve_oid, &curve_name)) { |
| 596 |
return aws_raise_error(AWS_ERROR_CAL_UNKNOWN_OBJECT_IDENTIFIER); |
| 597 |
} |
| 598 |
|
| 599 |
struct aws_byte_cursor public_key_cur; |
| 600 |
AWS_ZERO_STRUCT(public_key_cur); |
| 601 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_string(decoder, &public_key_cur)) { |
| 602 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 603 |
} |
| 604 |
|
| 605 |
size_t key_coordinate_size = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 606 |
if ((public_key_cur.len != 0 && public_key_cur.len != (key_coordinate_size * 2 + 1))) { |
| 607 |
return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 608 |
} |
| 609 |
|
| 610 |
if (public_key_cur.len > 0) { |
| 611 |
s_parse_public_key(public_key_cur, key_coordinate_size, out_public_x_coord, out_public_y_coord); |
| 612 |
} |
| 613 |
|
| 614 |
*out_curve_name = curve_name; |
| 615 |
|
| 616 |
return AWS_OP_SUCCESS; |
| 617 |
} |
| 618 |
|
| 619 |
int aws_der_decoder_load_ecc_key_pair( |
| 620 |
struct aws_der_decoder *decoder, |
| 621 |
struct aws_byte_cursor *out_public_x_coord, |
| 622 |
struct aws_byte_cursor *out_public_y_coord, |
| 623 |
struct aws_byte_cursor *out_private_d, |
| 624 |
enum aws_ecc_curve_name *out_curve_name) { |
| 625 |
|
| 626 |
AWS_ERROR_PRECONDITION(decoder); |
| 627 |
AWS_ERROR_PRECONDITION(out_public_x_coord); |
| 628 |
AWS_ERROR_PRECONDITION(out_public_y_coord); |
| 629 |
AWS_ERROR_PRECONDITION(out_private_d); |
| 630 |
AWS_ERROR_PRECONDITION(out_curve_name); |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if (s_der_decoder_load_ecc_public_key_pair_from_asn1( |
| 638 |
decoder, out_public_x_coord, out_public_y_coord, out_private_d, out_curve_name) == AWS_OP_SUCCESS) { |
| 639 |
return AWS_OP_SUCCESS; |
| 640 |
} |
| 641 |
|
| 642 |
aws_der_decoder_reset(decoder); |
| 643 |
|
| 644 |
if (s_der_decoder_load_ecc_private_key_pair_from_pkcs8( |
| 645 |
decoder, out_public_x_coord, out_public_y_coord, out_private_d, out_curve_name) == AWS_OP_SUCCESS) { |
| 646 |
return AWS_OP_SUCCESS; |
| 647 |
} |
| 648 |
|
| 649 |
aws_der_decoder_reset(decoder); |
| 650 |
if (s_der_decoder_load_ecc_private_key_pair_from_sec1( |
| 651 |
decoder, out_public_x_coord, out_public_y_coord, out_private_d, out_curve_name) == AWS_OP_SUCCESS) { |
| 652 |
return AWS_OP_SUCCESS; |
| 653 |
} |
| 654 |
|
| 655 |
return aws_raise_error(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT); |
| 656 |
} |
| 657 |
|
| 658 |
void aws_ecc_key_pair_acquire(struct aws_ecc_key_pair *key_pair) { |
| 659 |
aws_atomic_fetch_add(&key_pair->ref_count, 1); |
| 660 |
} |
| 661 |
|
| 662 |
void aws_ecc_key_pair_release(struct aws_ecc_key_pair *key_pair) { |
| 663 |
if (key_pair == NULL) { |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
size_t old_value = aws_atomic_fetch_sub(&key_pair->ref_count, 1); |
| 668 |
|
| 669 |
if (old_value == 1) { |
| 670 |
s_aws_ecc_key_pair_destroy(key_pair); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
struct aws_ecc_key_pair *aws_ecc_key_new_from_hex_coordinates( |
| 675 |
struct aws_allocator *allocator, |
| 676 |
enum aws_ecc_curve_name curve_name, |
| 677 |
struct aws_byte_cursor pub_x_hex_cursor, |
| 678 |
struct aws_byte_cursor pub_y_hex_cursor) { |
| 679 |
struct aws_byte_buf pub_x_buffer; |
| 680 |
AWS_ZERO_STRUCT(pub_x_buffer); |
| 681 |
struct aws_byte_buf pub_y_buffer; |
| 682 |
AWS_ZERO_STRUCT(pub_y_buffer); |
| 683 |
|
| 684 |
struct aws_ecc_key_pair *key = NULL; |
| 685 |
|
| 686 |
size_t pub_x_length = 0; |
| 687 |
size_t pub_y_length = 0; |
| 688 |
if (aws_hex_compute_decoded_len(pub_x_hex_cursor.len, &pub_x_length) || |
| 689 |
aws_hex_compute_decoded_len(pub_y_hex_cursor.len, &pub_y_length)) { |
| 690 |
goto done; |
| 691 |
} |
| 692 |
|
| 693 |
if (aws_byte_buf_init(&pub_x_buffer, allocator, pub_x_length) || |
| 694 |
aws_byte_buf_init(&pub_y_buffer, allocator, pub_y_length)) { |
| 695 |
goto done; |
| 696 |
} |
| 697 |
|
| 698 |
if (aws_hex_decode(&pub_x_hex_cursor, &pub_x_buffer) || aws_hex_decode(&pub_y_hex_cursor, &pub_y_buffer)) { |
| 699 |
goto done; |
| 700 |
} |
| 701 |
|
| 702 |
struct aws_byte_cursor pub_x_cursor = aws_byte_cursor_from_buf(&pub_x_buffer); |
| 703 |
struct aws_byte_cursor pub_y_cursor = aws_byte_cursor_from_buf(&pub_y_buffer); |
| 704 |
|
| 705 |
key = aws_ecc_key_pair_new_from_public_key(allocator, curve_name, &pub_x_cursor, &pub_y_cursor); |
| 706 |
|
| 707 |
done: |
| 708 |
|
| 709 |
aws_byte_buf_clean_up(&pub_x_buffer); |
| 710 |
aws_byte_buf_clean_up(&pub_y_buffer); |
| 711 |
|
| 712 |
return key; |
| 713 |
} |
| 714 |
|
| 715 |
static struct aws_byte_cursor s_null_byte_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\0"); |
| 716 |
|
| 717 |
static int s_write_component_padded(struct aws_byte_buf *buf, struct aws_byte_cursor component, size_t pad_to) { |
| 718 |
for (size_t pad = 0; pad < pad_to - component.len; ++pad) { |
| 719 |
if (aws_byte_buf_append(buf, &s_null_byte_cursor)) { |
| 720 |
return AWS_OP_ERR; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
if (aws_byte_buf_append(buf, &component)) { |
| 725 |
return AWS_OP_ERR; |
| 726 |
} |
| 727 |
|
| 728 |
return AWS_OP_SUCCESS; |
| 729 |
} |
| 730 |
|
| 731 |
int aws_ecc_decode_signature_der_to_raw( |
| 732 |
struct aws_allocator *allocator, |
| 733 |
struct aws_byte_cursor signature, |
| 734 |
struct aws_byte_cursor *out_r, |
| 735 |
struct aws_byte_cursor *out_s) { |
| 736 |
|
| 737 |
AWS_ERROR_PRECONDITION(allocator); |
| 738 |
AWS_ERROR_PRECONDITION(out_r); |
| 739 |
AWS_ERROR_PRECONDITION(out_s); |
| 740 |
|
| 741 |
struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, signature); |
| 742 |
|
| 743 |
if (!decoder) { |
| 744 |
return AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED; |
| 745 |
} |
| 746 |
|
| 747 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_type(decoder) != AWS_DER_SEQUENCE) { |
| 748 |
aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 749 |
goto on_error; |
| 750 |
} |
| 751 |
|
| 752 |
struct aws_byte_cursor r; |
| 753 |
AWS_ZERO_STRUCT(r); |
| 754 |
struct aws_byte_cursor s; |
| 755 |
AWS_ZERO_STRUCT(s); |
| 756 |
|
| 757 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_unsigned_integer(decoder, &r)) { |
| 758 |
aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 759 |
goto on_error; |
| 760 |
} |
| 761 |
if (!aws_der_decoder_next(decoder) || aws_der_decoder_tlv_unsigned_integer(decoder, &s)) { |
| 762 |
aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED); |
| 763 |
goto on_error; |
| 764 |
} |
| 765 |
|
| 766 |
aws_der_decoder_destroy(decoder); |
| 767 |
*out_r = r; |
| 768 |
*out_s = s; |
| 769 |
return AWS_OP_SUCCESS; |
| 770 |
|
| 771 |
on_error: |
| 772 |
aws_der_decoder_destroy(decoder); |
| 773 |
return AWS_OP_ERR; |
| 774 |
} |
| 775 |
|
| 776 |
int aws_ecc_decode_signature_der_to_raw_padded( |
| 777 |
struct aws_allocator *allocator, |
| 778 |
struct aws_byte_cursor signature, |
| 779 |
struct aws_byte_buf *out, |
| 780 |
size_t pad_to) { |
| 781 |
struct aws_byte_cursor r; |
| 782 |
AWS_ZERO_STRUCT(r); |
| 783 |
struct aws_byte_cursor s; |
| 784 |
AWS_ZERO_STRUCT(s); |
| 785 |
|
| 786 |
AWS_ERROR_PRECONDITION(allocator); |
| 787 |
AWS_ERROR_PRECONDITION(out); |
| 788 |
AWS_ERROR_PRECONDITION(pad_to <= 256); |
| 789 |
|
| 790 |
if (aws_ecc_decode_signature_der_to_raw(allocator, signature, &r, &s)) { |
| 791 |
return AWS_OP_ERR; |
| 792 |
} |
| 793 |
|
| 794 |
if (r.len > pad_to || s.len > pad_to) { |
| 795 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 796 |
} |
| 797 |
|
| 798 |
size_t available_write_space = out->capacity - out->len; |
| 799 |
|
| 800 |
if (available_write_space < (2 * pad_to)) { |
| 801 |
return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE); |
| 802 |
} |
| 803 |
|
| 804 |
size_t old_len = out->len; |
| 805 |
|
| 806 |
if (s_write_component_padded(out, r, pad_to) || s_write_component_padded(out, s, pad_to)) { |
| 807 |
out->len = old_len; |
| 808 |
return AWS_OP_ERR; |
| 809 |
} |
| 810 |
|
| 811 |
return AWS_OP_SUCCESS; |
| 812 |
} |
| 813 |
|
| 814 |
static bool s_trim_zeros_predicate(uint8_t value) { |
| 815 |
return value == 0; |
| 816 |
} |
| 817 |
|
| 818 |
int aws_ecc_encode_signature_raw_to_der( |
| 819 |
struct aws_allocator *allocator, |
| 820 |
struct aws_byte_cursor r, |
| 821 |
struct aws_byte_cursor s, |
| 822 |
struct aws_byte_buf *out_signature) { |
| 823 |
|
| 824 |
AWS_ERROR_PRECONDITION(allocator); |
| 825 |
AWS_ERROR_PRECONDITION(out_signature); |
| 826 |
|
| 827 |
struct aws_der_encoder *encoder = aws_der_encoder_new(allocator, out_signature->capacity - out_signature->len); |
| 828 |
|
| 829 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 830 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 831 |
goto on_error; |
| 832 |
} |
| 833 |
|
| 834 |
r = aws_byte_cursor_left_trim_pred(&r, s_trim_zeros_predicate); |
| 835 |
if (aws_der_encoder_write_unsigned_integer(encoder, r)) { |
| 836 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 837 |
goto on_error; |
| 838 |
} |
| 839 |
|
| 840 |
s = aws_byte_cursor_left_trim_pred(&s, s_trim_zeros_predicate); |
| 841 |
if (aws_der_encoder_write_unsigned_integer(encoder, s)) { |
| 842 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 843 |
goto on_error; |
| 844 |
} |
| 845 |
|
| 846 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 847 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 848 |
goto on_error; |
| 849 |
} |
| 850 |
|
| 851 |
struct aws_byte_cursor contents; |
| 852 |
AWS_ZERO_STRUCT(contents); |
| 853 |
if (aws_der_encoder_get_contents(encoder, &contents)) { |
| 854 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 855 |
goto on_error; |
| 856 |
} |
| 857 |
|
| 858 |
aws_byte_buf_append(out_signature, &contents); |
| 859 |
|
| 860 |
aws_der_encoder_destroy(encoder); |
| 861 |
return AWS_OP_SUCCESS; |
| 862 |
|
| 863 |
on_error: |
| 864 |
aws_der_encoder_destroy(encoder); |
| 865 |
return AWS_OP_ERR; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
int s_export_sec1(const struct aws_ecc_key_pair *key_pair, bool should_skip_params, struct aws_byte_buf *out) { |
| 880 |
|
| 881 |
if (key_pair->priv_d.buffer == NULL) { |
| 882 |
return aws_raise_error(AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT); |
| 883 |
} |
| 884 |
|
| 885 |
struct aws_der_encoder *encoder = aws_der_encoder_new(key_pair->allocator, 256); |
| 886 |
|
| 887 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 888 |
goto on_error; |
| 889 |
} |
| 890 |
|
| 891 |
static uint8_t ver_one[] = {1}; |
| 892 |
if (aws_der_encoder_write_unsigned_integer(encoder, aws_byte_cursor_from_array(ver_one, 1))) { |
| 893 |
goto on_error; |
| 894 |
} |
| 895 |
|
| 896 |
if (aws_der_encoder_write_octet_string(encoder, aws_byte_cursor_from_buf(&key_pair->priv_d))) { |
| 897 |
goto on_error; |
| 898 |
} |
| 899 |
|
| 900 |
if (!should_skip_params) { |
| 901 |
if (aws_der_encoder_begin_context_aware_tag(encoder, true, 0)) { |
| 902 |
goto on_error; |
| 903 |
} |
| 904 |
|
| 905 |
struct aws_byte_cursor oid; |
| 906 |
AWS_ZERO_STRUCT(oid); |
| 907 |
if (aws_ecc_oid_from_curve_name(key_pair->curve_name, &oid)) { |
| 908 |
goto on_error; |
| 909 |
} |
| 910 |
|
| 911 |
if (aws_der_encoder_write_object_identifier(encoder, oid)) { |
| 912 |
goto on_error; |
| 913 |
} |
| 914 |
|
| 915 |
if (aws_der_encoder_end_context_aware_tag(encoder)) { |
| 916 |
goto on_error; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
if (aws_der_encoder_begin_context_aware_tag(encoder, true, 1)) { |
| 921 |
goto on_error; |
| 922 |
} |
| 923 |
|
| 924 |
struct aws_byte_buf pub_buf; |
| 925 |
aws_byte_buf_init(&pub_buf, key_pair->allocator, 128); |
| 926 |
|
| 927 |
if (aws_byte_buf_append_byte_dynamic(&pub_buf, 0x04)) { |
| 928 |
goto on_error_pub_key; |
| 929 |
} |
| 930 |
|
| 931 |
size_t coord_size = aws_ecc_key_coordinate_byte_size_from_curve_name(key_pair->curve_name); |
| 932 |
|
| 933 |
struct aws_byte_cursor pub_x_cur = aws_byte_cursor_from_buf(&key_pair->pub_x); |
| 934 |
if (s_write_component_padded(&pub_buf, pub_x_cur, coord_size)) { |
| 935 |
goto on_error_pub_key; |
| 936 |
} |
| 937 |
|
| 938 |
struct aws_byte_cursor pub_y_cur = aws_byte_cursor_from_buf(&key_pair->pub_y); |
| 939 |
if (s_write_component_padded(&pub_buf, pub_y_cur, coord_size)) { |
| 940 |
goto on_error_pub_key; |
| 941 |
} |
| 942 |
|
| 943 |
struct aws_byte_cursor pub_cur = aws_byte_cursor_from_buf(&pub_buf); |
| 944 |
if (aws_der_encoder_write_bit_string(encoder, pub_cur)) { |
| 945 |
goto on_error_pub_key; |
| 946 |
} |
| 947 |
aws_byte_buf_clean_up(&pub_buf); |
| 948 |
|
| 949 |
if (aws_der_encoder_end_context_aware_tag(encoder)) { |
| 950 |
goto on_error; |
| 951 |
} |
| 952 |
|
| 953 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 954 |
goto on_error; |
| 955 |
} |
| 956 |
|
| 957 |
struct aws_byte_cursor contents; |
| 958 |
AWS_ZERO_STRUCT(contents); |
| 959 |
if (aws_der_encoder_get_contents(encoder, &contents)) { |
| 960 |
goto on_error; |
| 961 |
} |
| 962 |
|
| 963 |
if (aws_byte_buf_append(out, &contents)) { |
| 964 |
goto on_error; |
| 965 |
} |
| 966 |
|
| 967 |
aws_der_encoder_destroy(encoder); |
| 968 |
return AWS_OP_SUCCESS; |
| 969 |
|
| 970 |
on_error: |
| 971 |
aws_der_encoder_destroy(encoder); |
| 972 |
return AWS_OP_ERR; |
| 973 |
|
| 974 |
on_error_pub_key: |
| 975 |
aws_byte_buf_clean_up(&pub_buf); |
| 976 |
aws_der_encoder_destroy(encoder); |
| 977 |
return AWS_OP_ERR; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
int s_export_pkcs8(const struct aws_ecc_key_pair *key_pair, struct aws_byte_buf *out) { |
| 999 |
if (key_pair->priv_d.buffer == NULL) { |
| 1000 |
return aws_raise_error(AWS_ERROR_CAL_MISSING_REQUIRED_KEY_COMPONENT); |
| 1001 |
} |
| 1002 |
|
| 1003 |
struct aws_der_encoder *encoder = aws_der_encoder_new(key_pair->allocator, 256); |
| 1004 |
|
| 1005 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 1006 |
goto on_error; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
static uint8_t ver_zero[] = {0}; |
| 1011 |
if (aws_der_encoder_write_unsigned_integer(encoder, aws_byte_cursor_from_array(ver_zero, 1))) { |
| 1012 |
goto on_error; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 1017 |
goto on_error; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
if (aws_der_encoder_write_object_identifier(encoder, s_ec_public_key_oid_cursor)) { |
| 1022 |
goto on_error; |
| 1023 |
} |
| 1024 |
|
| 1025 |
struct aws_byte_cursor oid; |
| 1026 |
AWS_ZERO_STRUCT(oid); |
| 1027 |
if (aws_ecc_oid_from_curve_name(key_pair->curve_name, &oid)) { |
| 1028 |
goto on_error; |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (aws_der_encoder_write_object_identifier(encoder, oid)) { |
| 1032 |
goto on_error; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 1036 |
goto on_error; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
struct aws_byte_buf priv_buf; |
| 1041 |
aws_byte_buf_init(&priv_buf, key_pair->allocator, 256); |
| 1042 |
if (s_export_sec1(key_pair, true, &priv_buf)) { |
| 1043 |
goto on_priv_error; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (aws_der_encoder_write_octet_string(encoder, aws_byte_cursor_from_buf(&priv_buf))) { |
| 1047 |
goto on_priv_error; |
| 1048 |
} |
| 1049 |
|
| 1050 |
aws_byte_buf_clean_up(&priv_buf); |
| 1051 |
|
| 1052 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 1053 |
goto on_error; |
| 1054 |
} |
| 1055 |
|
| 1056 |
struct aws_byte_cursor contents; |
| 1057 |
AWS_ZERO_STRUCT(contents); |
| 1058 |
if (aws_der_encoder_get_contents(encoder, &contents)) { |
| 1059 |
goto on_error; |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (aws_byte_buf_append(out, &contents)) { |
| 1063 |
goto on_error; |
| 1064 |
} |
| 1065 |
|
| 1066 |
aws_der_encoder_destroy(encoder); |
| 1067 |
return AWS_OP_SUCCESS; |
| 1068 |
|
| 1069 |
on_error: |
| 1070 |
aws_der_encoder_destroy(encoder); |
| 1071 |
return AWS_OP_ERR; |
| 1072 |
|
| 1073 |
on_priv_error: |
| 1074 |
aws_byte_buf_clean_up(&priv_buf); |
| 1075 |
aws_der_encoder_destroy(encoder); |
| 1076 |
return AWS_OP_ERR; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
int s_export_spki(const struct aws_ecc_key_pair *key_pair, struct aws_byte_buf *out) { |
| 1091 |
struct aws_der_encoder *encoder = aws_der_encoder_new(key_pair->allocator, 256); |
| 1092 |
|
| 1093 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 1094 |
goto on_error; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
if (aws_der_encoder_begin_sequence(encoder)) { |
| 1099 |
goto on_error; |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (aws_der_encoder_write_object_identifier(encoder, s_ec_public_key_oid_cursor)) { |
| 1103 |
goto on_error; |
| 1104 |
} |
| 1105 |
|
| 1106 |
struct aws_byte_cursor oid; |
| 1107 |
AWS_ZERO_STRUCT(oid); |
| 1108 |
if (aws_ecc_oid_from_curve_name(key_pair->curve_name, &oid)) { |
| 1109 |
goto on_error; |
| 1110 |
} |
| 1111 |
|
| 1112 |
if (aws_der_encoder_write_object_identifier(encoder, oid)) { |
| 1113 |
goto on_error; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 1117 |
goto on_error; |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
struct aws_byte_buf pub_buf; |
| 1122 |
aws_byte_buf_init(&pub_buf, key_pair->allocator, 128); |
| 1123 |
|
| 1124 |
if (aws_byte_buf_append_byte_dynamic(&pub_buf, 0x04)) { |
| 1125 |
goto on_pub_error; |
| 1126 |
} |
| 1127 |
|
| 1128 |
size_t coord_size = aws_ecc_key_coordinate_byte_size_from_curve_name(key_pair->curve_name); |
| 1129 |
|
| 1130 |
struct aws_byte_cursor pub_x_cur = aws_byte_cursor_from_buf(&key_pair->pub_x); |
| 1131 |
if (s_write_component_padded(&pub_buf, pub_x_cur, coord_size)) { |
| 1132 |
goto on_pub_error; |
| 1133 |
} |
| 1134 |
|
| 1135 |
struct aws_byte_cursor pub_y_cur = aws_byte_cursor_from_buf(&key_pair->pub_y); |
| 1136 |
if (s_write_component_padded(&pub_buf, pub_y_cur, coord_size)) { |
| 1137 |
goto on_pub_error; |
| 1138 |
} |
| 1139 |
|
| 1140 |
struct aws_byte_cursor pub_cur = aws_byte_cursor_from_buf(&pub_buf); |
| 1141 |
if (aws_der_encoder_write_bit_string(encoder, pub_cur)) { |
| 1142 |
aws_byte_buf_clean_up(&pub_buf); |
| 1143 |
goto on_pub_error; |
| 1144 |
} |
| 1145 |
|
| 1146 |
aws_byte_buf_clean_up(&pub_buf); |
| 1147 |
|
| 1148 |
if (aws_der_encoder_end_sequence(encoder)) { |
| 1149 |
goto on_error; |
| 1150 |
} |
| 1151 |
|
| 1152 |
struct aws_byte_cursor contents; |
| 1153 |
AWS_ZERO_STRUCT(contents); |
| 1154 |
if (aws_der_encoder_get_contents(encoder, &contents)) { |
| 1155 |
goto on_error; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (aws_byte_buf_append(out, &contents)) { |
| 1159 |
goto on_error; |
| 1160 |
} |
| 1161 |
|
| 1162 |
aws_der_encoder_destroy(encoder); |
| 1163 |
return AWS_OP_SUCCESS; |
| 1164 |
|
| 1165 |
on_error: |
| 1166 |
aws_der_encoder_destroy(encoder); |
| 1167 |
return AWS_OP_ERR; |
| 1168 |
|
| 1169 |
on_pub_error: |
| 1170 |
aws_byte_buf_clean_up(&pub_buf); |
| 1171 |
aws_der_encoder_destroy(encoder); |
| 1172 |
return AWS_OP_ERR; |
| 1173 |
} |
| 1174 |
|
| 1175 |
int aws_ecc_key_pair_export( |
| 1176 |
const struct aws_ecc_key_pair *key_pair, |
| 1177 |
enum aws_ecc_key_export_format format, |
| 1178 |
struct aws_byte_buf *out) { |
| 1179 |
AWS_ERROR_PRECONDITION(key_pair); |
| 1180 |
AWS_ERROR_PRECONDITION(out); |
| 1181 |
|
| 1182 |
switch (format) { |
| 1183 |
case AWS_CAL_ECC_KEY_EXPORT_PRIVATE_SEC1: |
| 1184 |
return s_export_sec1(key_pair, false , out); |
| 1185 |
case AWS_CAL_ECC_KEY_EXPORT_PRIVATE_PKCS8: |
| 1186 |
return s_export_pkcs8(key_pair, out); |
| 1187 |
case AWS_CAL_ECC_KEY_EXPORT_PUBLIC_SPKI: |
| 1188 |
return s_export_spki(key_pair, out); |
| 1189 |
default: |
| 1190 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 1191 |
} |
| 1192 |
} |