| 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 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <time.h> |
| 41 |
#include "os_port.h" |
| 42 |
#include "crypto_misc.h" |
| 43 |
|
| 44 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 45 |
static int x509_v3_subject_alt_name(const uint8_t *cert, int offset, |
| 46 |
X509_CTX *x509_ctx); |
| 47 |
static int x509_v3_basic_constraints(const uint8_t *cert, int offset, |
| 48 |
X509_CTX *x509_ctx); |
| 49 |
static int x509_v3_key_usage(const uint8_t *cert, int offset, |
| 50 |
X509_CTX *x509_ctx); |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx) |
| 58 |
{ |
| 59 |
int begin_tbs, end_tbs; |
| 60 |
int ret = X509_NOT_OK, offset = 0, cert_size = 0; |
| 61 |
int version = 0; |
| 62 |
X509_CTX *x509_ctx; |
| 63 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 64 |
BI_CTX *bi_ctx; |
| 65 |
#endif |
| 66 |
|
| 67 |
*ctx = (X509_CTX *)calloc(1, sizeof(X509_CTX)); |
| 68 |
x509_ctx = *ctx; |
| 69 |
|
| 70 |
|
| 71 |
asn1_skip_obj(cert, &cert_size, ASN1_SEQUENCE); |
| 72 |
|
| 73 |
if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0) |
| 74 |
goto end_cert; |
| 75 |
|
| 76 |
begin_tbs = offset; |
| 77 |
end_tbs = begin_tbs; |
| 78 |
asn1_skip_obj(cert, &end_tbs, ASN1_SEQUENCE); |
| 79 |
|
| 80 |
if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0) |
| 81 |
goto end_cert; |
| 82 |
|
| 83 |
|
| 84 |
if (cert[offset] == ASN1_EXPLICIT_TAG && |
| 85 |
asn1_version(cert, &offset, &version) == X509_NOT_OK) |
| 86 |
goto end_cert; |
| 87 |
|
| 88 |
if (asn1_skip_obj(cert, &offset, ASN1_INTEGER) || |
| 89 |
asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0) |
| 90 |
goto end_cert; |
| 91 |
|
| 92 |
|
| 93 |
if (asn1_signature_type(cert, &offset, x509_ctx)) |
| 94 |
{ |
| 95 |
ret = X509_VFY_ERROR_UNSUPPORTED_DIGEST; |
| 96 |
goto end_cert; |
| 97 |
} |
| 98 |
|
| 99 |
if (asn1_name(cert, &offset, x509_ctx->ca_cert_dn) || |
| 100 |
asn1_validity(cert, &offset, x509_ctx) || |
| 101 |
asn1_name(cert, &offset, x509_ctx->cert_dn) || |
| 102 |
asn1_public_key(cert, &offset, x509_ctx)) |
| 103 |
{ |
| 104 |
goto end_cert; |
| 105 |
} |
| 106 |
|
| 107 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 108 |
bi_ctx = x509_ctx->rsa_ctx->bi_ctx; |
| 109 |
|
| 110 |
|
| 111 |
switch (x509_ctx->sig_type) |
| 112 |
{ |
| 113 |
case SIG_TYPE_MD5: |
| 114 |
{ |
| 115 |
MD5_CTX md5_ctx; |
| 116 |
uint8_t md5_dgst[MD5_SIZE]; |
| 117 |
MD5_Init(&md5_ctx); |
| 118 |
MD5_Update(&md5_ctx, &cert[begin_tbs], end_tbs-begin_tbs); |
| 119 |
MD5_Final(md5_dgst, &md5_ctx); |
| 120 |
x509_ctx->digest = bi_import(bi_ctx, md5_dgst, MD5_SIZE); |
| 121 |
} |
| 122 |
break; |
| 123 |
|
| 124 |
case SIG_TYPE_SHA1: |
| 125 |
{ |
| 126 |
SHA1_CTX sha_ctx; |
| 127 |
uint8_t sha_dgst[SHA1_SIZE]; |
| 128 |
SHA1_Init(&sha_ctx); |
| 129 |
SHA1_Update(&sha_ctx, &cert[begin_tbs], end_tbs-begin_tbs); |
| 130 |
SHA1_Final(sha_dgst, &sha_ctx); |
| 131 |
x509_ctx->digest = bi_import(bi_ctx, sha_dgst, SHA1_SIZE); |
| 132 |
} |
| 133 |
break; |
| 134 |
|
| 135 |
case SIG_TYPE_SHA256: |
| 136 |
{ |
| 137 |
SHA256_CTX sha256_ctx; |
| 138 |
uint8_t sha256_dgst[SHA256_SIZE]; |
| 139 |
SHA256_Init(&sha256_ctx); |
| 140 |
SHA256_Update(&sha256_ctx, &cert[begin_tbs], end_tbs-begin_tbs); |
| 141 |
SHA256_Final(sha256_dgst, &sha256_ctx); |
| 142 |
x509_ctx->digest = bi_import(bi_ctx, sha256_dgst, SHA256_SIZE); |
| 143 |
} |
| 144 |
break; |
| 145 |
|
| 146 |
case SIG_TYPE_SHA384: |
| 147 |
{ |
| 148 |
SHA384_CTX sha384_ctx; |
| 149 |
uint8_t sha384_dgst[SHA384_SIZE]; |
| 150 |
SHA384_Init(&sha384_ctx); |
| 151 |
SHA384_Update(&sha384_ctx, &cert[begin_tbs], end_tbs-begin_tbs); |
| 152 |
SHA384_Final(sha384_dgst, &sha384_ctx); |
| 153 |
x509_ctx->digest = bi_import(bi_ctx, sha384_dgst, SHA384_SIZE); |
| 154 |
} |
| 155 |
break; |
| 156 |
|
| 157 |
case SIG_TYPE_SHA512: |
| 158 |
{ |
| 159 |
SHA512_CTX sha512_ctx; |
| 160 |
uint8_t sha512_dgst[SHA512_SIZE]; |
| 161 |
SHA512_Init(&sha512_ctx); |
| 162 |
SHA512_Update(&sha512_ctx, &cert[begin_tbs], end_tbs-begin_tbs); |
| 163 |
SHA512_Final(sha512_dgst, &sha512_ctx); |
| 164 |
x509_ctx->digest = bi_import(bi_ctx, sha512_dgst, SHA512_SIZE); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
if (version == 2 && asn1_next_obj(cert, &offset, ASN1_V3_DATA) > 0) |
| 170 |
{ |
| 171 |
x509_v3_subject_alt_name(cert, offset, x509_ctx); |
| 172 |
x509_v3_basic_constraints(cert, offset, x509_ctx); |
| 173 |
x509_v3_key_usage(cert, offset, x509_ctx); |
| 174 |
} |
| 175 |
|
| 176 |
offset = end_tbs; |
| 177 |
if (asn1_skip_obj(cert, &offset, ASN1_SEQUENCE) || |
| 178 |
asn1_signature(cert, &offset, x509_ctx)) |
| 179 |
goto end_cert; |
| 180 |
#endif |
| 181 |
ret = X509_OK; |
| 182 |
end_cert: |
| 183 |
if (len) |
| 184 |
{ |
| 185 |
*len = cert_size; |
| 186 |
} |
| 187 |
|
| 188 |
if (ret) |
| 189 |
{ |
| 190 |
#ifdef CONFIG_DEBUG |
| 191 |
printf("Error: Invalid X509 ASN.1 file (%s)\n", |
| 192 |
x509_display_error(ret)); |
| 193 |
#endif |
| 194 |
x509_free(x509_ctx); |
| 195 |
*ctx = NULL; |
| 196 |
} |
| 197 |
|
| 198 |
return ret; |
| 199 |
} |
| 200 |
|
| 201 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 202 |
static int x509_v3_subject_alt_name(const uint8_t *cert, int offset, |
| 203 |
X509_CTX *x509_ctx) |
| 204 |
{ |
| 205 |
if ((offset = asn1_is_subject_alt_name(cert, offset)) > 0) |
| 206 |
{ |
| 207 |
x509_ctx->subject_alt_name_present = true; |
| 208 |
x509_ctx->subject_alt_name_is_critical = |
| 209 |
asn1_is_critical_ext(cert, &offset); |
| 210 |
|
| 211 |
if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) > 0) |
| 212 |
{ |
| 213 |
int altlen; |
| 214 |
|
| 215 |
if ((altlen = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) > 0) |
| 216 |
{ |
| 217 |
int endalt = offset + altlen; |
| 218 |
int totalnames = 0; |
| 219 |
|
| 220 |
while (offset < endalt) |
| 221 |
{ |
| 222 |
int type = cert[offset++]; |
| 223 |
int dnslen = get_asn1_length(cert, &offset); |
| 224 |
|
| 225 |
if (type == ASN1_CONTEXT_DNSNAME) |
| 226 |
{ |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (strnlen((const char *)&cert[offset], dnslen) != |
| 231 |
dnslen) |
| 232 |
{ |
| 233 |
return X509_VFY_ERROR_NO_TRUSTED_CERT; |
| 234 |
} |
| 235 |
|
| 236 |
x509_ctx->subject_alt_dnsnames = (char**) |
| 237 |
realloc(x509_ctx->subject_alt_dnsnames, |
| 238 |
(totalnames + 2) * sizeof(char*)); |
| 239 |
x509_ctx->subject_alt_dnsnames[totalnames] = |
| 240 |
(char*)malloc(dnslen + 1); |
| 241 |
x509_ctx->subject_alt_dnsnames[totalnames+1] = NULL; |
| 242 |
memcpy(x509_ctx->subject_alt_dnsnames[totalnames], |
| 243 |
cert + offset, dnslen); |
| 244 |
x509_ctx->subject_alt_dnsnames[totalnames][dnslen] = 0; |
| 245 |
totalnames++; |
| 246 |
} |
| 247 |
|
| 248 |
offset += dnslen; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return X509_OK; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
static int x509_v3_basic_constraints(const uint8_t *cert, int offset, |
| 261 |
X509_CTX *x509_ctx) |
| 262 |
{ |
| 263 |
int ret = X509_OK; |
| 264 |
int lenSeq = 0; |
| 265 |
|
| 266 |
if ((offset = asn1_is_basic_constraints(cert, offset)) == 0) |
| 267 |
goto end_contraints; |
| 268 |
|
| 269 |
x509_ctx->basic_constraint_present = true; |
| 270 |
x509_ctx->basic_constraint_is_critical = |
| 271 |
asn1_is_critical_ext(cert, &offset); |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
x509_ctx->basic_constraint_cA = 0; |
| 276 |
|
| 277 |
|
| 278 |
x509_ctx->basic_constraint_pathLenConstraint = 10000; |
| 279 |
|
| 280 |
if ((asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0) || |
| 281 |
((lenSeq = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) < 0)) |
| 282 |
{ |
| 283 |
ret = X509_NOT_OK; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if ((lenSeq>0)&&(asn1_get_bool(cert, &offset, |
| 289 |
&x509_ctx->basic_constraint_cA) < 0)) |
| 290 |
{ |
| 291 |
ret = X509_NOT_OK; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
if ((lenSeq>3) && (asn1_get_int(cert, &offset, |
| 297 |
&x509_ctx->basic_constraint_pathLenConstraint) < 0)) |
| 298 |
{ |
| 299 |
ret = X509_NOT_OK; |
| 300 |
} |
| 301 |
|
| 302 |
end_contraints: |
| 303 |
return ret; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static int x509_v3_key_usage(const uint8_t *cert, int offset, |
| 310 |
X509_CTX *x509_ctx) |
| 311 |
{ |
| 312 |
int ret = X509_OK; |
| 313 |
|
| 314 |
if ((offset = asn1_is_key_usage(cert, offset)) == 0) |
| 315 |
goto end_key_usage; |
| 316 |
|
| 317 |
x509_ctx->key_usage_present = true; |
| 318 |
x509_ctx->key_usage_is_critical = asn1_is_critical_ext(cert, &offset); |
| 319 |
|
| 320 |
if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0 || |
| 321 |
asn1_get_bit_string_as_int(cert, &offset, &x509_ctx->key_usage)) |
| 322 |
{ |
| 323 |
ret = X509_NOT_OK; |
| 324 |
} |
| 325 |
|
| 326 |
end_key_usage: |
| 327 |
return ret; |
| 328 |
} |
| 329 |
#endif |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
void x509_free(X509_CTX *x509_ctx) |
| 335 |
{ |
| 336 |
X509_CTX *next; |
| 337 |
int i; |
| 338 |
|
| 339 |
if (x509_ctx == NULL) |
| 340 |
return; |
| 341 |
|
| 342 |
for (i = 0; i < X509_NUM_DN_TYPES; i++) |
| 343 |
{ |
| 344 |
free(x509_ctx->ca_cert_dn[i]); |
| 345 |
free(x509_ctx->cert_dn[i]); |
| 346 |
} |
| 347 |
|
| 348 |
free(x509_ctx->signature); |
| 349 |
|
| 350 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 351 |
if (x509_ctx->digest) |
| 352 |
{ |
| 353 |
bi_free(x509_ctx->rsa_ctx->bi_ctx, x509_ctx->digest); |
| 354 |
} |
| 355 |
|
| 356 |
if (x509_ctx->subject_alt_dnsnames) |
| 357 |
{ |
| 358 |
for (i = 0; x509_ctx->subject_alt_dnsnames[i]; ++i) |
| 359 |
free(x509_ctx->subject_alt_dnsnames[i]); |
| 360 |
|
| 361 |
free(x509_ctx->subject_alt_dnsnames); |
| 362 |
} |
| 363 |
#endif |
| 364 |
|
| 365 |
RSA_free(x509_ctx->rsa_ctx); |
| 366 |
next = x509_ctx->next; |
| 367 |
free(x509_ctx); |
| 368 |
x509_free(next); |
| 369 |
} |
| 370 |
|
| 371 |
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG) |
| 372 |
|
| 373 |
static const uint8_t sig_prefix_md5[] = {0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10}; |
| 374 |
static const uint8_t sig_prefix_sha1[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14}; |
| 375 |
static const uint8_t sig_prefix_sha256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}; |
| 376 |
static const uint8_t sig_prefix_sha384[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}; |
| 377 |
static const uint8_t sig_prefix_sha512[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
static bigint *sig_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len, |
| 383 |
uint8_t sig_type, bigint *modulus, bigint *pub_exp) |
| 384 |
{ |
| 385 |
int i; |
| 386 |
bigint *decrypted_bi, *dat_bi; |
| 387 |
bigint *bir = NULL; |
| 388 |
uint8_t *block = (uint8_t *)malloc(sig_len); |
| 389 |
|
| 390 |
const uint8_t *sig_prefix = NULL; |
| 391 |
uint8_t sig_prefix_size = 0, hash_len = 0; |
| 392 |
|
| 393 |
switch (sig_type) |
| 394 |
{ |
| 395 |
case SIG_TYPE_MD5: |
| 396 |
sig_prefix = sig_prefix_md5; |
| 397 |
sig_prefix_size = sizeof(sig_prefix_md5); |
| 398 |
break; |
| 399 |
case SIG_TYPE_SHA1: |
| 400 |
sig_prefix = sig_prefix_sha1; |
| 401 |
sig_prefix_size = sizeof(sig_prefix_sha1); |
| 402 |
break; |
| 403 |
case SIG_TYPE_SHA256: |
| 404 |
sig_prefix = sig_prefix_sha256; |
| 405 |
sig_prefix_size = sizeof(sig_prefix_sha256); |
| 406 |
break; |
| 407 |
case SIG_TYPE_SHA384: |
| 408 |
sig_prefix = sig_prefix_sha384; |
| 409 |
sig_prefix_size = sizeof(sig_prefix_sha384); |
| 410 |
break; |
| 411 |
case SIG_TYPE_SHA512: |
| 412 |
sig_prefix = sig_prefix_sha512; |
| 413 |
sig_prefix_size = sizeof(sig_prefix_sha512); |
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
if (sig_prefix) |
| 418 |
hash_len = sig_prefix[sig_prefix_size - 1]; |
| 419 |
|
| 420 |
|
| 421 |
if (sig_len < 2 + 8 + 1 + sig_prefix_size + hash_len) |
| 422 |
goto err; |
| 423 |
|
| 424 |
|
| 425 |
dat_bi = bi_import(ctx, sig, sig_len); |
| 426 |
ctx->mod_offset = BIGINT_M_OFFSET; |
| 427 |
|
| 428 |
|
| 429 |
decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp); |
| 430 |
|
| 431 |
bi_export(ctx, decrypted_bi, block, sig_len); |
| 432 |
ctx->mod_offset = BIGINT_M_OFFSET; |
| 433 |
|
| 434 |
|
| 435 |
if (block[0] != 0 || block[1] != 1) |
| 436 |
goto err; |
| 437 |
|
| 438 |
|
| 439 |
i = 2; |
| 440 |
while (i < sig_len - 1 - sig_prefix_size - hash_len) |
| 441 |
{ |
| 442 |
if (block[i++] != 0xFF) |
| 443 |
goto err; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
if (block[i++] != 0) |
| 448 |
goto err; |
| 449 |
|
| 450 |
|
| 451 |
if (memcmp(block+i, sig_prefix, sig_prefix_size)) |
| 452 |
goto err; |
| 453 |
|
| 454 |
|
| 455 |
bir = bi_import(ctx, block + i + sig_prefix_size, hash_len); |
| 456 |
|
| 457 |
err: |
| 458 |
free(block); |
| 459 |
|
| 460 |
bi_clear_cache(ctx); |
| 461 |
return bir; |
| 462 |
} |
| 463 |
|
| 464 |
#endif |
| 465 |
|
| 466 |
#if defined (CONFIG_DEBUG) |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert, |
| 481 |
int *pathLenConstraint) |
| 482 |
{ |
| 483 |
int ret = X509_OK, i = 0; |
| 484 |
bigint *cert_sig; |
| 485 |
X509_CTX *next_cert = NULL; |
| 486 |
BI_CTX *ctx = NULL; |
| 487 |
bigint *mod = NULL, *expn = NULL; |
| 488 |
int match_ca_cert = 0; |
| 489 |
struct timeval tv; |
| 490 |
uint8_t is_self_signed = 0; |
| 491 |
|
| 492 |
if (cert == NULL) |
| 493 |
{ |
| 494 |
ret = X509_VFY_ERROR_NO_TRUSTED_CERT; |
| 495 |
goto end_verify; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if (asn1_compare_dn(cert->ca_cert_dn, cert->cert_dn) == 0) |
| 501 |
{ |
| 502 |
is_self_signed = 1; |
| 503 |
ctx = cert->rsa_ctx->bi_ctx; |
| 504 |
mod = cert->rsa_ctx->m; |
| 505 |
expn = cert->rsa_ctx->e; |
| 506 |
} |
| 507 |
|
| 508 |
gettimeofday(&tv, NULL); |
| 509 |
|
| 510 |
|
| 511 |
if (tv.tv_sec < cert->not_before) |
| 512 |
{ |
| 513 |
ret = X509_VFY_ERROR_NOT_YET_VALID; |
| 514 |
goto end_verify; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
if (tv.tv_sec > cert->not_after) |
| 519 |
{ |
| 520 |
ret = X509_VFY_ERROR_EXPIRED; |
| 521 |
goto end_verify; |
| 522 |
} |
| 523 |
|
| 524 |
if (cert->basic_constraint_present) |
| 525 |
{ |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
if (!cert->basic_constraint_cA && |
| 530 |
IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN)) |
| 531 |
{ |
| 532 |
ret = X509_VFY_ERROR_BASIC_CONSTRAINT; |
| 533 |
goto end_verify; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if (cert->basic_constraint_cA && |
| 542 |
(!cert->key_usage_present || |
| 543 |
IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN)) && |
| 544 |
(cert->basic_constraint_pathLenConstraint+1) < *pathLenConstraint) |
| 545 |
{ |
| 546 |
ret = X509_VFY_ERROR_BASIC_CONSTRAINT; |
| 547 |
goto end_verify; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
next_cert = cert->next; |
| 552 |
|
| 553 |
|
| 554 |
if (next_cert == NULL) |
| 555 |
{ |
| 556 |
if (ca_cert_ctx != NULL) |
| 557 |
{ |
| 558 |
|
| 559 |
while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i]) |
| 560 |
{ |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
if (cert->basic_constraint_present && |
| 565 |
!ca_cert_ctx->cert[i]->basic_constraint_cA) |
| 566 |
{ |
| 567 |
i++; |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
if (asn1_compare_dn(cert->ca_cert_dn, |
| 572 |
ca_cert_ctx->cert[i]->cert_dn) == 0) |
| 573 |
{ |
| 574 |
|
| 575 |
match_ca_cert = true; |
| 576 |
ctx = ca_cert_ctx->cert[i]->rsa_ctx->bi_ctx; |
| 577 |
mod = ca_cert_ctx->cert[i]->rsa_ctx->m; |
| 578 |
expn = ca_cert_ctx->cert[i]->rsa_ctx->e; |
| 579 |
|
| 580 |
|
| 581 |
break; |
| 582 |
} |
| 583 |
|
| 584 |
i++; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
if (!match_ca_cert && !is_self_signed) |
| 591 |
{ |
| 592 |
ret = X509_VFY_ERROR_NO_TRUSTED_CERT; |
| 593 |
goto end_verify; |
| 594 |
} |
| 595 |
} |
| 596 |
else if (asn1_compare_dn(cert->ca_cert_dn, next_cert->cert_dn) != 0) |
| 597 |
{ |
| 598 |
|
| 599 |
ret = X509_VFY_ERROR_INVALID_CHAIN; |
| 600 |
goto end_verify; |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
ctx = next_cert->rsa_ctx->bi_ctx; |
| 605 |
mod = next_cert->rsa_ctx->m; |
| 606 |
expn = next_cert->rsa_ctx->e; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
if (!match_ca_cert && is_self_signed) |
| 611 |
{ |
| 612 |
ret = X509_VFY_ERROR_SELF_SIGNED; |
| 613 |
goto end_verify; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
cert_sig = sig_verify(ctx, cert->signature, cert->sig_len, cert->sig_type, |
| 618 |
bi_clone(ctx, mod), bi_clone(ctx, expn)); |
| 619 |
|
| 620 |
if (cert_sig && cert->digest) |
| 621 |
{ |
| 622 |
if (bi_compare(cert_sig, cert->digest) != 0) |
| 623 |
ret = X509_VFY_ERROR_BAD_SIGNATURE; |
| 624 |
|
| 625 |
|
| 626 |
bi_free(ctx, cert_sig); |
| 627 |
} |
| 628 |
else |
| 629 |
{ |
| 630 |
ret = X509_VFY_ERROR_BAD_SIGNATURE; |
| 631 |
} |
| 632 |
|
| 633 |
if (ret) |
| 634 |
goto end_verify; |
| 635 |
|
| 636 |
|
| 637 |
if (next_cert != NULL) |
| 638 |
{ |
| 639 |
(*pathLenConstraint)++; |
| 640 |
ret = x509_verify(ca_cert_ctx, next_cert, pathLenConstraint); |
| 641 |
} |
| 642 |
|
| 643 |
end_verify: |
| 644 |
return ret; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
static const char *not_part_of_cert = "<Not Part Of Certificate>"; |
| 651 |
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx) |
| 652 |
{ |
| 653 |
if (cert == NULL) |
| 654 |
return; |
| 655 |
|
| 656 |
printf("=== CERTIFICATE ISSUED TO ===\n"); |
| 657 |
printf("Common Name (CN):\t\t"); |
| 658 |
printf("%s\n", cert->cert_dn[X509_COMMON_NAME] ? |
| 659 |
cert->cert_dn[X509_COMMON_NAME] : not_part_of_cert); |
| 660 |
|
| 661 |
printf("Organization (O):\t\t"); |
| 662 |
printf("%s\n", cert->cert_dn[X509_ORGANIZATION] ? |
| 663 |
cert->cert_dn[X509_ORGANIZATION] : not_part_of_cert); |
| 664 |
|
| 665 |
if (cert->cert_dn[X509_ORGANIZATIONAL_UNIT]) |
| 666 |
{ |
| 667 |
printf("Organizational Unit (OU):\t"); |
| 668 |
printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT]); |
| 669 |
} |
| 670 |
|
| 671 |
if (cert->cert_dn[X509_LOCATION]) |
| 672 |
{ |
| 673 |
printf("Location (L):\t\t\t"); |
| 674 |
printf("%s\n", cert->cert_dn[X509_LOCATION]); |
| 675 |
} |
| 676 |
|
| 677 |
if (cert->cert_dn[X509_COUNTRY]) |
| 678 |
{ |
| 679 |
printf("Country (C):\t\t\t"); |
| 680 |
printf("%s\n", cert->cert_dn[X509_COUNTRY]); |
| 681 |
} |
| 682 |
|
| 683 |
if (cert->cert_dn[X509_STATE]) |
| 684 |
{ |
| 685 |
printf("State (ST):\t\t\t"); |
| 686 |
printf("%s\n", cert->cert_dn[X509_STATE]); |
| 687 |
} |
| 688 |
|
| 689 |
if (cert->basic_constraint_present) |
| 690 |
{ |
| 691 |
printf("Basic Constraints:\t\t%sCA:%s, pathlen:%d\n", |
| 692 |
cert->basic_constraint_is_critical ? |
| 693 |
"critical, " : "", |
| 694 |
cert->basic_constraint_cA? "TRUE" : "FALSE", |
| 695 |
cert->basic_constraint_pathLenConstraint); |
| 696 |
} |
| 697 |
|
| 698 |
if (cert->key_usage_present) |
| 699 |
{ |
| 700 |
printf("Key Usage:\t\t\t%s", cert->key_usage_is_critical ? |
| 701 |
"critical, " : ""); |
| 702 |
bool has_started = false; |
| 703 |
|
| 704 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DIGITAL_SIGNATURE)) |
| 705 |
{ |
| 706 |
printf("Digital Signature"); |
| 707 |
has_started = true; |
| 708 |
} |
| 709 |
|
| 710 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_NON_REPUDIATION)) |
| 711 |
{ |
| 712 |
if (has_started) |
| 713 |
printf(", "); |
| 714 |
|
| 715 |
printf("Non Repudiation"); |
| 716 |
has_started = true; |
| 717 |
} |
| 718 |
|
| 719 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_ENCIPHERMENT)) |
| 720 |
{ |
| 721 |
if (has_started) |
| 722 |
printf(", "); |
| 723 |
|
| 724 |
printf("Key Encipherment"); |
| 725 |
has_started = true; |
| 726 |
} |
| 727 |
|
| 728 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DATA_ENCIPHERMENT)) |
| 729 |
{ |
| 730 |
if (has_started) |
| 731 |
printf(", "); |
| 732 |
|
| 733 |
printf("Data Encipherment"); |
| 734 |
has_started = true; |
| 735 |
} |
| 736 |
|
| 737 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_AGREEMENT)) |
| 738 |
{ |
| 739 |
if (has_started) |
| 740 |
printf(", "); |
| 741 |
|
| 742 |
printf("Key Agreement"); |
| 743 |
has_started = true; |
| 744 |
} |
| 745 |
|
| 746 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN)) |
| 747 |
{ |
| 748 |
if (has_started) |
| 749 |
printf(", "); |
| 750 |
|
| 751 |
printf("Key Cert Sign"); |
| 752 |
has_started = true; |
| 753 |
} |
| 754 |
|
| 755 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_CRL_SIGN)) |
| 756 |
{ |
| 757 |
if (has_started) |
| 758 |
printf(", "); |
| 759 |
|
| 760 |
printf("CRL Sign"); |
| 761 |
has_started = true; |
| 762 |
} |
| 763 |
|
| 764 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_ENCIPHER_ONLY)) |
| 765 |
{ |
| 766 |
if (has_started) |
| 767 |
printf(", "); |
| 768 |
|
| 769 |
printf("Encipher Only"); |
| 770 |
has_started = true; |
| 771 |
} |
| 772 |
|
| 773 |
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DECIPHER_ONLY)) |
| 774 |
{ |
| 775 |
if (has_started) |
| 776 |
printf(", "); |
| 777 |
|
| 778 |
printf("Decipher Only"); |
| 779 |
has_started = true; |
| 780 |
} |
| 781 |
|
| 782 |
printf("\n"); |
| 783 |
} |
| 784 |
|
| 785 |
if (cert->subject_alt_name_present) |
| 786 |
{ |
| 787 |
printf("Subject Alt Name:\t\t%s", cert->subject_alt_name_is_critical |
| 788 |
? "critical, " : ""); |
| 789 |
if (cert->subject_alt_dnsnames) |
| 790 |
{ |
| 791 |
int i = 0; |
| 792 |
|
| 793 |
while (cert->subject_alt_dnsnames[i]) |
| 794 |
printf("%s ", cert->subject_alt_dnsnames[i++]); |
| 795 |
} |
| 796 |
printf("\n"); |
| 797 |
|
| 798 |
} |
| 799 |
|
| 800 |
printf("=== CERTIFICATE ISSUED BY ===\n"); |
| 801 |
printf("Common Name (CN):\t\t"); |
| 802 |
printf("%s\n", cert->ca_cert_dn[X509_COMMON_NAME] ? |
| 803 |
cert->ca_cert_dn[X509_COMMON_NAME] : not_part_of_cert); |
| 804 |
|
| 805 |
printf("Organization (O):\t\t"); |
| 806 |
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATION] ? |
| 807 |
cert->ca_cert_dn[X509_ORGANIZATION] : not_part_of_cert); |
| 808 |
|
| 809 |
if (cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT]) |
| 810 |
{ |
| 811 |
printf("Organizational Unit (OU):\t"); |
| 812 |
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT]); |
| 813 |
} |
| 814 |
|
| 815 |
if (cert->ca_cert_dn[X509_LOCATION]) |
| 816 |
{ |
| 817 |
printf("Location (L):\t\t\t"); |
| 818 |
printf("%s\n", cert->ca_cert_dn[X509_LOCATION]); |
| 819 |
} |
| 820 |
|
| 821 |
if (cert->ca_cert_dn[X509_COUNTRY]) |
| 822 |
{ |
| 823 |
printf("Country (C):\t\t\t"); |
| 824 |
printf("%s\n", cert->ca_cert_dn[X509_COUNTRY]); |
| 825 |
} |
| 826 |
|
| 827 |
if (cert->ca_cert_dn[X509_STATE]) |
| 828 |
{ |
| 829 |
printf("State (ST):\t\t\t"); |
| 830 |
printf("%s\n", cert->ca_cert_dn[X509_STATE]); |
| 831 |
} |
| 832 |
|
| 833 |
printf("Not Before:\t\t\t%s", ctime(&cert->not_before)); |
| 834 |
printf("Not After:\t\t\t%s", ctime(&cert->not_after)); |
| 835 |
printf("RSA bitsize:\t\t\t%d\n", cert->rsa_ctx->num_octets*8); |
| 836 |
printf("Sig Type:\t\t\t"); |
| 837 |
switch (cert->sig_type) |
| 838 |
{ |
| 839 |
case SIG_TYPE_MD5: |
| 840 |
printf("MD5\n"); |
| 841 |
break; |
| 842 |
case SIG_TYPE_SHA1: |
| 843 |
printf("SHA1\n"); |
| 844 |
break; |
| 845 |
case SIG_TYPE_SHA256: |
| 846 |
printf("SHA256\n"); |
| 847 |
break; |
| 848 |
case SIG_TYPE_SHA384: |
| 849 |
printf("SHA384\n"); |
| 850 |
break; |
| 851 |
case SIG_TYPE_SHA512: |
| 852 |
printf("SHA512\n"); |
| 853 |
break; |
| 854 |
default: |
| 855 |
printf("Unrecognized: %d\n", cert->sig_type); |
| 856 |
break; |
| 857 |
} |
| 858 |
|
| 859 |
if (ca_cert_ctx) |
| 860 |
{ |
| 861 |
int pathLenConstraint = 0; |
| 862 |
printf("Verify:\t\t\t\t%s\n", |
| 863 |
x509_display_error(x509_verify(ca_cert_ctx, cert, |
| 864 |
&pathLenConstraint))); |
| 865 |
} |
| 866 |
|
| 867 |
#if 0 |
| 868 |
print_blob("Signature", cert->signature, cert->sig_len); |
| 869 |
bi_print("Modulus", cert->rsa_ctx->m); |
| 870 |
bi_print("Pub Exp", cert->rsa_ctx->e); |
| 871 |
#endif |
| 872 |
|
| 873 |
if (ca_cert_ctx) |
| 874 |
{ |
| 875 |
x509_print(cert->next, ca_cert_ctx); |
| 876 |
} |
| 877 |
|
| 878 |
TTY_FLUSH(); |
| 879 |
} |
| 880 |
#endif |
| 881 |
|
| 882 |
|
| 883 |
const char * x509_display_error(int error) |
| 884 |
{ |
| 885 |
switch (error) |
| 886 |
{ |
| 887 |
case X509_OK: |
| 888 |
return "Certificate verify successful"; |
| 889 |
|
| 890 |
case X509_NOT_OK: |
| 891 |
return "X509 not ok"; |
| 892 |
|
| 893 |
case X509_VFY_ERROR_NO_TRUSTED_CERT: |
| 894 |
return "No trusted cert is available"; |
| 895 |
|
| 896 |
case X509_VFY_ERROR_BAD_SIGNATURE: |
| 897 |
return "Bad signature"; |
| 898 |
|
| 899 |
case X509_VFY_ERROR_NOT_YET_VALID: |
| 900 |
return "Cert is not yet valid"; |
| 901 |
|
| 902 |
case X509_VFY_ERROR_EXPIRED: |
| 903 |
return "Cert has expired"; |
| 904 |
|
| 905 |
case X509_VFY_ERROR_SELF_SIGNED: |
| 906 |
return "Cert is self-signed"; |
| 907 |
|
| 908 |
case X509_VFY_ERROR_INVALID_CHAIN: |
| 909 |
return "Chain is invalid (check order of certs)"; |
| 910 |
|
| 911 |
case X509_VFY_ERROR_UNSUPPORTED_DIGEST: |
| 912 |
return "Unsupported digest"; |
| 913 |
|
| 914 |
case X509_INVALID_PRIV_KEY: |
| 915 |
return "Invalid private key"; |
| 916 |
|
| 917 |
case X509_VFY_ERROR_BASIC_CONSTRAINT: |
| 918 |
return "Basic constraint invalid"; |
| 919 |
|
| 920 |
default: |
| 921 |
return "Unknown"; |
| 922 |
} |
| 923 |
} |
| 924 |
|