| 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 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <time.h> |
| 39 |
#include "os_port.h" |
| 40 |
#include "crypto.h" |
| 41 |
#include "crypto_misc.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static const uint8_t sig_oid_prefix[] = |
| 50 |
{ |
| 51 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01 |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
static const uint8_t sig_sha1WithRSAEncrypt[] = |
| 56 |
{ |
| 57 |
0x2b, 0x0e, 0x03, 0x02, 0x1d |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
static const uint8_t sig_sha256[] = |
| 62 |
{ |
| 63 |
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01 |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
static const uint8_t sig_sha384[] = |
| 68 |
{ |
| 69 |
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02 |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
static const uint8_t sig_sha512[] = |
| 74 |
{ |
| 75 |
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03 |
| 76 |
}; |
| 77 |
|
| 78 |
static const uint8_t sig_subject_alt_name[] = |
| 79 |
{ |
| 80 |
0x55, 0x1d, 0x11 |
| 81 |
}; |
| 82 |
|
| 83 |
static const uint8_t sig_basic_constraints[] = |
| 84 |
{ |
| 85 |
0x55, 0x1d, 0x13 |
| 86 |
}; |
| 87 |
|
| 88 |
static const uint8_t sig_key_usage[] = |
| 89 |
{ |
| 90 |
0x55, 0x1d, 0x0f |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
static const uint8_t g_dn_types[] = { 3, 10, 11, 7, 6, 8 }; |
| 95 |
|
| 96 |
uint32_t get_asn1_length(const uint8_t *buf, int *offset) |
| 97 |
{ |
| 98 |
int i; |
| 99 |
uint32_t len; |
| 100 |
|
| 101 |
if (!(buf[*offset] & 0x80)) |
| 102 |
{ |
| 103 |
len = buf[(*offset)++]; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
int length_bytes = buf[(*offset)++]&0x7f; |
| 108 |
if (length_bytes > 4) |
| 109 |
return 0; |
| 110 |
|
| 111 |
len = 0; |
| 112 |
for (i = 0; i < length_bytes; i++) |
| 113 |
{ |
| 114 |
len <<= 8; |
| 115 |
len += buf[(*offset)++]; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return len; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type) |
| 127 |
{ |
| 128 |
if (buf[*offset] != obj_type) |
| 129 |
return X509_NOT_OK; |
| 130 |
|
| 131 |
(*offset)++; |
| 132 |
return get_asn1_length(buf, offset); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type) |
| 140 |
{ |
| 141 |
int len; |
| 142 |
|
| 143 |
if (buf[*offset] != obj_type) |
| 144 |
return X509_NOT_OK; |
| 145 |
(*offset)++; |
| 146 |
len = get_asn1_length(buf, offset); |
| 147 |
*offset += len; |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object) |
| 156 |
{ |
| 157 |
int len; |
| 158 |
|
| 159 |
if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0) |
| 160 |
goto end_big_int; |
| 161 |
|
| 162 |
if (len > 1 && buf[*offset] == 0x00) |
| 163 |
{ |
| 164 |
len--; |
| 165 |
(*offset)++; |
| 166 |
} |
| 167 |
|
| 168 |
*object = (uint8_t *)malloc(len); |
| 169 |
memcpy(*object, &buf[*offset], len); |
| 170 |
*offset += len; |
| 171 |
|
| 172 |
end_big_int: |
| 173 |
return len; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val) |
| 180 |
{ |
| 181 |
int res = X509_OK; |
| 182 |
int len; |
| 183 |
int i; |
| 184 |
|
| 185 |
if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0 || |
| 186 |
len > sizeof(int32_t)) |
| 187 |
{ |
| 188 |
res = X509_NOT_OK; |
| 189 |
goto end_int; |
| 190 |
} |
| 191 |
|
| 192 |
*val = 0; |
| 193 |
for (i = 0; i < len; i++) |
| 194 |
{ |
| 195 |
*val <<= 8; |
| 196 |
*val |= buf[(*offset)++]; |
| 197 |
} |
| 198 |
|
| 199 |
end_int: |
| 200 |
return res; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
int asn1_get_bool(const uint8_t *buf, int *offset, bool *val) |
| 207 |
{ |
| 208 |
int res = X509_OK; |
| 209 |
|
| 210 |
if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) != 1) |
| 211 |
{ |
| 212 |
res = X509_NOT_OK; |
| 213 |
goto end_bool; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
*val = buf[(*offset)++] == 0xFF; |
| 220 |
|
| 221 |
end_bool: |
| 222 |
return res; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val) |
| 229 |
{ |
| 230 |
int res = X509_OK; |
| 231 |
int len, i; |
| 232 |
|
| 233 |
if ((len = asn1_next_obj(buf, offset, ASN1_BIT_STRING)) < 0 || len > 5) |
| 234 |
{ |
| 235 |
res = X509_NOT_OK; |
| 236 |
goto end_bit_string_as_int; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
(*offset)++; |
| 241 |
len--; |
| 242 |
*val = 0; |
| 243 |
|
| 244 |
|
| 245 |
for (i = len-1; i >= 0; --i) |
| 246 |
{ |
| 247 |
*val <<= 8; |
| 248 |
*val |= buf[(*offset) + i]; |
| 249 |
} |
| 250 |
|
| 251 |
*offset += len; |
| 252 |
|
| 253 |
end_bit_string_as_int: |
| 254 |
return res; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx) |
| 261 |
{ |
| 262 |
int offset = 7; |
| 263 |
uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL; |
| 264 |
int mod_len, priv_len, pub_len; |
| 265 |
#ifdef CONFIG_BIGINT_CRT |
| 266 |
uint8_t *p = NULL, *q = NULL, *dP = NULL, *dQ = NULL, *qInv = NULL; |
| 267 |
int p_len, q_len, dP_len, dQ_len, qInv_len; |
| 268 |
#endif |
| 269 |
|
| 270 |
|
| 271 |
if (buf[0] != ASN1_SEQUENCE) |
| 272 |
{ |
| 273 |
#ifdef CONFIG_DEBUG |
| 274 |
printf("Error: This is not a valid ASN.1 file\n"); |
| 275 |
#endif |
| 276 |
return X509_INVALID_PRIV_KEY; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
RNG_custom_init(buf, len); |
| 281 |
|
| 282 |
mod_len = asn1_get_big_int(buf, &offset, &modulus); |
| 283 |
pub_len = asn1_get_big_int(buf, &offset, &pub_exp); |
| 284 |
priv_len = asn1_get_big_int(buf, &offset, &priv_exp); |
| 285 |
|
| 286 |
if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0) |
| 287 |
return X509_INVALID_PRIV_KEY; |
| 288 |
|
| 289 |
#ifdef CONFIG_BIGINT_CRT |
| 290 |
p_len = asn1_get_big_int(buf, &offset, &p); |
| 291 |
q_len = asn1_get_big_int(buf, &offset, &q); |
| 292 |
dP_len = asn1_get_big_int(buf, &offset, &dP); |
| 293 |
dQ_len = asn1_get_big_int(buf, &offset, &dQ); |
| 294 |
qInv_len = asn1_get_big_int(buf, &offset, &qInv); |
| 295 |
|
| 296 |
if (p_len <= 0 || q_len <= 0 || dP_len <= 0 || dQ_len <= 0 || qInv_len <= 0) |
| 297 |
return X509_INVALID_PRIV_KEY; |
| 298 |
|
| 299 |
RSA_priv_key_new(rsa_ctx, |
| 300 |
modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len, |
| 301 |
p, p_len, q, p_len, dP, dP_len, dQ, dQ_len, qInv, qInv_len); |
| 302 |
|
| 303 |
free(p); |
| 304 |
free(q); |
| 305 |
free(dP); |
| 306 |
free(dQ); |
| 307 |
free(qInv); |
| 308 |
#else |
| 309 |
RSA_priv_key_new(rsa_ctx, |
| 310 |
modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len); |
| 311 |
#endif |
| 312 |
|
| 313 |
free(modulus); |
| 314 |
free(priv_exp); |
| 315 |
free(pub_exp); |
| 316 |
return X509_OK; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
int asn1_get_public_key(const uint8_t *cert, int *offset, RSA_CTX **rsa_ctx) |
| 324 |
{ |
| 325 |
int ret = X509_NOT_OK, mod_len, pub_len; |
| 326 |
uint8_t *modulus = NULL, *pub_exp = NULL; |
| 327 |
int len; |
| 328 |
|
| 329 |
|
| 330 |
if ((len = asn1_next_obj(cert, offset, ASN1_SEQUENCE)) < 0) |
| 331 |
goto end_pub_key; |
| 332 |
|
| 333 |
if ((mod_len = asn1_get_big_int(cert, offset, &modulus)) <= 0) { |
| 334 |
ret = X509_INVALID_PUB_KEY; |
| 335 |
goto end_pub_key; |
| 336 |
} |
| 337 |
if ((pub_len = asn1_get_big_int(cert, offset, &pub_exp)) <= 0) { |
| 338 |
free(modulus); |
| 339 |
ret = X509_INVALID_PUB_KEY; |
| 340 |
goto end_pub_key; |
| 341 |
} |
| 342 |
|
| 343 |
RSA_pub_key_new(rsa_ctx, modulus, mod_len, pub_exp, pub_len); |
| 344 |
|
| 345 |
free(modulus); |
| 346 |
free(pub_exp); |
| 347 |
|
| 348 |
ret = X509_OK; |
| 349 |
|
| 350 |
end_pub_key: |
| 351 |
return ret; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
static int asn1_get_utc_time(const uint8_t *buf, int *offset, time_t *t) |
| 358 |
{ |
| 359 |
int ret = X509_NOT_OK, len, t_offset, abs_year; |
| 360 |
struct tm tm; |
| 361 |
|
| 362 |
|
| 363 |
if (buf[*offset] == ASN1_UTC_TIME) |
| 364 |
{ |
| 365 |
(*offset)++; |
| 366 |
|
| 367 |
len = get_asn1_length(buf, offset); |
| 368 |
t_offset = *offset; |
| 369 |
|
| 370 |
memset(&tm, 0, sizeof(struct tm)); |
| 371 |
tm.tm_year = (buf[t_offset] - '0')*10 + (buf[t_offset+1] - '0'); |
| 372 |
|
| 373 |
|
| 374 |
{ |
| 375 |
tm.tm_year += 100; |
| 376 |
} |
| 377 |
|
| 378 |
tm.tm_mon = (buf[t_offset+2] - '0')*10 + (buf[t_offset+3] - '0') - 1; |
| 379 |
tm.tm_mday = (buf[t_offset+4] - '0')*10 + (buf[t_offset+5] - '0'); |
| 380 |
tm.tm_hour = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0'); |
| 381 |
tm.tm_min = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0'); |
| 382 |
tm.tm_sec = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0'); |
| 383 |
*t = mktime(&tm); |
| 384 |
*offset += len; |
| 385 |
ret = X509_OK; |
| 386 |
} |
| 387 |
else if (buf[*offset] == ASN1_GENERALIZED_TIME) |
| 388 |
{ |
| 389 |
(*offset)++; |
| 390 |
|
| 391 |
len = get_asn1_length(buf, offset); |
| 392 |
t_offset = *offset; |
| 393 |
|
| 394 |
memset(&tm, 0, sizeof(struct tm)); |
| 395 |
abs_year = ((buf[t_offset] - '0')*1000 + |
| 396 |
(buf[t_offset+1] - '0')*100 + (buf[t_offset+2] - '0')*10 + |
| 397 |
(buf[t_offset+3] - '0')); |
| 398 |
|
| 399 |
if (abs_year <= 1901) |
| 400 |
{ |
| 401 |
tm.tm_year = 1; |
| 402 |
tm.tm_mon = 0; |
| 403 |
tm.tm_mday = 1; |
| 404 |
} |
| 405 |
else |
| 406 |
{ |
| 407 |
tm.tm_year = abs_year - 1900; |
| 408 |
tm.tm_mon = (buf[t_offset+4] - '0')*10 + |
| 409 |
(buf[t_offset+5] - '0') - 1; |
| 410 |
tm.tm_mday = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0'); |
| 411 |
tm.tm_hour = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0'); |
| 412 |
tm.tm_min = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0'); |
| 413 |
tm.tm_sec = (buf[t_offset+12] - '0')*10 + (buf[t_offset+13] - '0'); |
| 414 |
*t = mktime(&tm); |
| 415 |
} |
| 416 |
|
| 417 |
*offset += len; |
| 418 |
ret = X509_OK; |
| 419 |
} |
| 420 |
|
| 421 |
return ret; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
int asn1_version(const uint8_t *cert, int *offset, int *val) |
| 428 |
{ |
| 429 |
(*offset) += 2; |
| 430 |
return asn1_get_int(cert, offset, val); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx) |
| 437 |
{ |
| 438 |
return (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 || |
| 439 |
asn1_get_utc_time(cert, offset, &x509_ctx->not_before) || |
| 440 |
asn1_get_utc_time(cert, offset, &x509_ctx->not_after)); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static int asn1_get_oid_x520(const uint8_t *buf, int *offset) |
| 447 |
{ |
| 448 |
int dn_type = 0; |
| 449 |
int len; |
| 450 |
|
| 451 |
if ((len = asn1_next_obj(buf, offset, ASN1_OID)) < 0) |
| 452 |
goto end_oid; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
if (len == 3 && buf[(*offset)++] == 0x55 && buf[(*offset)++] == 0x04) |
| 457 |
dn_type = buf[(*offset)++]; |
| 458 |
else |
| 459 |
{ |
| 460 |
*offset += len; |
| 461 |
} |
| 462 |
|
| 463 |
end_oid: |
| 464 |
return dn_type; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
static int asn1_get_printable_str(const uint8_t *buf, int *offset, char **str) |
| 471 |
{ |
| 472 |
int len = X509_NOT_OK; |
| 473 |
int asn1_type = buf[*offset]; |
| 474 |
|
| 475 |
|
| 476 |
if (asn1_type != ASN1_PRINTABLE_STR && |
| 477 |
asn1_type != ASN1_PRINTABLE_STR2 && |
| 478 |
asn1_type != ASN1_TELETEX_STR && |
| 479 |
asn1_type != ASN1_IA5_STR && |
| 480 |
asn1_type != ASN1_UNICODE_STR) |
| 481 |
goto end_pnt_str; |
| 482 |
|
| 483 |
(*offset)++; |
| 484 |
len = get_asn1_length(buf, offset); |
| 485 |
|
| 486 |
if (asn1_type == ASN1_UNICODE_STR) |
| 487 |
{ |
| 488 |
int i; |
| 489 |
*str = (char *)malloc(len/2+1); |
| 490 |
|
| 491 |
for (i = 0; i < len; i += 2) |
| 492 |
(*str)[i/2] = buf[*offset + i + 1]; |
| 493 |
|
| 494 |
(*str)[len/2] = 0; |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
*str = (char *)malloc(len+1); |
| 499 |
memcpy(*str, &buf[*offset], len); |
| 500 |
(*str)[len] = 0; |
| 501 |
} |
| 502 |
|
| 503 |
*offset += len; |
| 504 |
|
| 505 |
end_pnt_str: |
| 506 |
return len; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
int asn1_name(const uint8_t *cert, int *offset, char *dn[]) |
| 513 |
{ |
| 514 |
int ret = X509_NOT_OK; |
| 515 |
int dn_type; |
| 516 |
char *tmp; |
| 517 |
|
| 518 |
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0) |
| 519 |
goto end_name; |
| 520 |
|
| 521 |
while (asn1_next_obj(cert, offset, ASN1_SET) >= 0) |
| 522 |
{ |
| 523 |
int i, found = 0; |
| 524 |
|
| 525 |
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 || |
| 526 |
(dn_type = asn1_get_oid_x520(cert, offset)) < 0) |
| 527 |
goto end_name; |
| 528 |
|
| 529 |
tmp = NULL; |
| 530 |
|
| 531 |
if (asn1_get_printable_str(cert, offset, &tmp) < 0) |
| 532 |
{ |
| 533 |
free(tmp); |
| 534 |
goto end_name; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
for (i = 0; i < X509_NUM_DN_TYPES; i++) |
| 539 |
{ |
| 540 |
if (dn_type == g_dn_types[i]) |
| 541 |
{ |
| 542 |
if (dn[i] == NULL) |
| 543 |
{ |
| 544 |
dn[i] = tmp; |
| 545 |
found = 1; |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (found == 0) |
| 552 |
{ |
| 553 |
free(tmp); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
ret = X509_OK; |
| 558 |
end_name: |
| 559 |
return ret; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx) |
| 566 |
{ |
| 567 |
int ret = X509_NOT_OK, mod_len, pub_len; |
| 568 |
uint8_t *modulus = NULL, *pub_exp = NULL; |
| 569 |
|
| 570 |
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 || |
| 571 |
asn1_skip_obj(cert, offset, ASN1_SEQUENCE) || |
| 572 |
asn1_next_obj(cert, offset, ASN1_BIT_STRING) < 0) |
| 573 |
goto end_pub_key; |
| 574 |
|
| 575 |
(*offset)++; |
| 576 |
|
| 577 |
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0) |
| 578 |
goto end_pub_key; |
| 579 |
|
| 580 |
mod_len = asn1_get_big_int(cert, offset, &modulus); |
| 581 |
pub_len = asn1_get_big_int(cert, offset, &pub_exp); |
| 582 |
|
| 583 |
RSA_pub_key_new(&x509_ctx->rsa_ctx, modulus, mod_len, pub_exp, pub_len); |
| 584 |
|
| 585 |
free(modulus); |
| 586 |
free(pub_exp); |
| 587 |
ret = X509_OK; |
| 588 |
|
| 589 |
end_pub_key: |
| 590 |
return ret; |
| 591 |
} |
| 592 |
|
| 593 |
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG) |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx) |
| 598 |
{ |
| 599 |
int ret = X509_NOT_OK; |
| 600 |
|
| 601 |
if (cert[(*offset)++] != ASN1_BIT_STRING) |
| 602 |
goto end_sig; |
| 603 |
|
| 604 |
x509_ctx->sig_len = get_asn1_length(cert, offset)-1; |
| 605 |
(*offset)++; |
| 606 |
x509_ctx->signature = (uint8_t *)malloc(x509_ctx->sig_len); |
| 607 |
memcpy(x509_ctx->signature, &cert[*offset], x509_ctx->sig_len); |
| 608 |
*offset += x509_ctx->sig_len; |
| 609 |
ret = X509_OK; |
| 610 |
|
| 611 |
end_sig: |
| 612 |
return ret; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx) |
| 619 |
{ |
| 620 |
int i = 0; |
| 621 |
|
| 622 |
if (ca_cert_ctx == NULL) |
| 623 |
return; |
| 624 |
|
| 625 |
while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i]) |
| 626 |
{ |
| 627 |
x509_free(ca_cert_ctx->cert[i]); |
| 628 |
ca_cert_ctx->cert[i++] = NULL; |
| 629 |
} |
| 630 |
|
| 631 |
free(ca_cert_ctx); |
| 632 |
} |
| 633 |
|
| 634 |
#endif |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
static int asn1_compare_dn_comp(const char *dn1, const char *dn2) |
| 641 |
{ |
| 642 |
int ret; |
| 643 |
|
| 644 |
if (dn1 == NULL && dn2 == NULL) |
| 645 |
ret = 0; |
| 646 |
else |
| 647 |
ret = (dn1 && dn2) ? strcmp(dn1, dn2) : 1; |
| 648 |
|
| 649 |
return ret; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
int asn1_compare_dn(char * const dn1[], char * const dn2[]) |
| 657 |
{ |
| 658 |
int i; |
| 659 |
|
| 660 |
for (i = 0; i < X509_NUM_DN_TYPES; i++) |
| 661 |
{ |
| 662 |
if (asn1_compare_dn_comp(dn1[i], dn2[i])) |
| 663 |
return 1; |
| 664 |
} |
| 665 |
|
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
int asn1_find_oid(const uint8_t* cert, int* offset, |
| 670 |
const uint8_t* oid, int oid_length) |
| 671 |
{ |
| 672 |
int seqlen; |
| 673 |
if ((seqlen = asn1_next_obj(cert, offset, ASN1_SEQUENCE))> 0) |
| 674 |
{ |
| 675 |
int end = *offset + seqlen; |
| 676 |
|
| 677 |
while (*offset < end) |
| 678 |
{ |
| 679 |
int type = cert[(*offset)++]; |
| 680 |
int length = get_asn1_length(cert, offset); |
| 681 |
int noffset = *offset + length; |
| 682 |
|
| 683 |
if (type == ASN1_SEQUENCE) |
| 684 |
{ |
| 685 |
type = cert[(*offset)++]; |
| 686 |
length = get_asn1_length(cert, offset); |
| 687 |
|
| 688 |
if (type == ASN1_OID && length == oid_length && |
| 689 |
memcmp(cert + *offset, oid, oid_length) == 0) |
| 690 |
{ |
| 691 |
*offset += oid_length; |
| 692 |
return 1; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
*offset = noffset; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG) |
| 704 |
int asn1_is_subject_alt_name(const uint8_t *cert, int offset) |
| 705 |
{ |
| 706 |
if (asn1_find_oid(cert, &offset, sig_subject_alt_name, |
| 707 |
sizeof(sig_subject_alt_name))) |
| 708 |
{ |
| 709 |
return offset; |
| 710 |
} |
| 711 |
|
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
int asn1_is_basic_constraints(const uint8_t *cert, int offset) |
| 716 |
{ |
| 717 |
if (asn1_find_oid(cert, &offset, sig_basic_constraints, |
| 718 |
sizeof(sig_basic_constraints))) |
| 719 |
{ |
| 720 |
return offset; |
| 721 |
} |
| 722 |
|
| 723 |
return 0; |
| 724 |
} |
| 725 |
|
| 726 |
int asn1_is_key_usage(const uint8_t *cert, int offset) |
| 727 |
{ |
| 728 |
if (asn1_find_oid(cert, &offset, sig_key_usage, |
| 729 |
sizeof(sig_key_usage))) |
| 730 |
{ |
| 731 |
return offset; |
| 732 |
} |
| 733 |
|
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
bool asn1_is_critical_ext(const uint8_t *buf, int *offset) |
| 738 |
{ |
| 739 |
|
| 740 |
bool res = false; |
| 741 |
|
| 742 |
if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) == 1) |
| 743 |
res = buf[(*offset)++] == 0xFF; |
| 744 |
|
| 745 |
return res; |
| 746 |
} |
| 747 |
|
| 748 |
#endif |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
int asn1_signature_type(const uint8_t *cert, |
| 755 |
int *offset, X509_CTX *x509_ctx) |
| 756 |
{ |
| 757 |
int ret = X509_NOT_OK, len; |
| 758 |
|
| 759 |
if (cert[(*offset)++] != ASN1_OID) |
| 760 |
goto end_check_sig; |
| 761 |
|
| 762 |
len = get_asn1_length(cert, offset); |
| 763 |
|
| 764 |
if (len == sizeof(sig_sha1WithRSAEncrypt) && |
| 765 |
memcmp(sig_sha1WithRSAEncrypt, &cert[*offset], |
| 766 |
sizeof(sig_sha1WithRSAEncrypt)) == 0) |
| 767 |
{ |
| 768 |
x509_ctx->sig_type = SIG_TYPE_SHA1; |
| 769 |
} |
| 770 |
else if (len == sizeof(sig_sha256) && |
| 771 |
memcmp(sig_sha256, &cert[*offset], |
| 772 |
sizeof(sig_sha256)) == 0) |
| 773 |
{ |
| 774 |
x509_ctx->sig_type = SIG_TYPE_SHA256; |
| 775 |
} |
| 776 |
else if (len == sizeof(sig_sha384) && |
| 777 |
memcmp(sig_sha384, &cert[*offset], |
| 778 |
sizeof(sig_sha384)) == 0) |
| 779 |
{ |
| 780 |
x509_ctx->sig_type = SIG_TYPE_SHA384; |
| 781 |
} |
| 782 |
else if (len == sizeof(sig_sha512) && |
| 783 |
memcmp(sig_sha512, &cert[*offset], |
| 784 |
sizeof(sig_sha512)) == 0) |
| 785 |
{ |
| 786 |
x509_ctx->sig_type = SIG_TYPE_SHA512; |
| 787 |
} |
| 788 |
else |
| 789 |
{ |
| 790 |
if (memcmp(sig_oid_prefix, &cert[*offset], sizeof(sig_oid_prefix))) |
| 791 |
{ |
| 792 |
#ifdef CONFIG_DEBUG |
| 793 |
int i; |
| 794 |
printf("invalid digest: "); |
| 795 |
|
| 796 |
for (i = 0; i < len; i++) |
| 797 |
printf("%02x ", cert[*offset + i]); |
| 798 |
|
| 799 |
printf("\n"); |
| 800 |
#endif |
| 801 |
goto end_check_sig; |
| 802 |
} |
| 803 |
|
| 804 |
x509_ctx->sig_type = cert[*offset + sizeof(sig_oid_prefix)]; |
| 805 |
} |
| 806 |
|
| 807 |
*offset += len; |
| 808 |
asn1_skip_obj(cert, offset, ASN1_NULL); |
| 809 |
ret = X509_OK; |
| 810 |
|
| 811 |
end_check_sig: |
| 812 |
return ret; |
| 813 |
} |
| 814 |
|