1
1
/*
2
- * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
2
+ * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3
3
*
4
4
* Licensed under the OpenSSL license (the "License"). You may not use
5
5
* this file except in compliance with the License. You can obtain a copy
@@ -81,6 +81,8 @@ int X509_check_purpose(X509 *x, int id, int ca)
81
81
const X509_PURPOSE * pt ;
82
82
83
83
x509v3_cache_extensions (x );
84
+ if (x -> ex_flags & EXFLAG_INVALID )
85
+ return -1 ;
84
86
85
87
/* Return if side-effect only call */
86
88
if (id == -1 )
@@ -300,10 +302,11 @@ int X509_supported_extension(X509_EXTENSION *ex)
300
302
return 0 ;
301
303
}
302
304
303
- static void setup_dp (X509 * x , DIST_POINT * dp )
305
+ static int setup_dp (X509 * x , DIST_POINT * dp )
304
306
{
305
307
X509_NAME * iname = NULL ;
306
308
int i ;
309
+
307
310
if (dp -> reasons ) {
308
311
if (dp -> reasons -> length > 0 )
309
312
dp -> dp_reasons = dp -> reasons -> data [0 ];
@@ -313,7 +316,7 @@ static void setup_dp(X509 *x, DIST_POINT *dp)
313
316
} else
314
317
dp -> dp_reasons = CRLDP_ALL_REASONS ;
315
318
if (!dp -> distpoint || (dp -> distpoint -> type != 1 ))
316
- return ;
319
+ return 1 ;
317
320
for (i = 0 ; i < sk_GENERAL_NAME_num (dp -> CRLissuer ); i ++ ) {
318
321
GENERAL_NAME * gen = sk_GENERAL_NAME_value (dp -> CRLissuer , i );
319
322
if (gen -> type == GEN_DIRNAME ) {
@@ -324,16 +327,21 @@ static void setup_dp(X509 *x, DIST_POINT *dp)
324
327
if (!iname )
325
328
iname = X509_get_issuer_name (x );
326
329
327
- DIST_POINT_set_dpname (dp -> distpoint , iname );
328
-
330
+ return DIST_POINT_set_dpname (dp -> distpoint , iname );
329
331
}
330
332
331
- static void setup_crldp (X509 * x )
333
+ static int setup_crldp (X509 * x )
332
334
{
333
335
int i ;
334
- x -> crldp = X509_get_ext_d2i (x , NID_crl_distribution_points , NULL , NULL );
335
- for (i = 0 ; i < sk_DIST_POINT_num (x -> crldp ); i ++ )
336
- setup_dp (x , sk_DIST_POINT_value (x -> crldp , i ));
336
+
337
+ x -> crldp = X509_get_ext_d2i (x , NID_crl_distribution_points , & i , NULL );
338
+ if (x -> crldp == NULL && i != -1 )
339
+ return 0 ;
340
+ for (i = 0 ; i < sk_DIST_POINT_num (x -> crldp ); i ++ ) {
341
+ if (!setup_dp (x , sk_DIST_POINT_value (x -> crldp , i )))
342
+ return 0 ;
343
+ }
344
+ return 1 ;
337
345
}
338
346
339
347
#define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
@@ -366,12 +374,13 @@ static void x509v3_cache_extensions(X509 *x)
366
374
return ;
367
375
}
368
376
369
- X509_digest (x , EVP_sha1 (), x -> sha1_hash , NULL );
377
+ if (!X509_digest (x , EVP_sha1 (), x -> sha1_hash , NULL ))
378
+ x -> ex_flags |= EXFLAG_INVALID ;
370
379
/* V1 should mean no extensions ... */
371
380
if (!X509_get_version (x ))
372
381
x -> ex_flags |= EXFLAG_V1 ;
373
382
/* Handle basic constraints */
374
- if ((bs = X509_get_ext_d2i (x , NID_basic_constraints , NULL , NULL ))) {
383
+ if ((bs = X509_get_ext_d2i (x , NID_basic_constraints , & i , NULL ))) {
375
384
if (bs -> ca )
376
385
x -> ex_flags |= EXFLAG_CA ;
377
386
if (bs -> pathlen ) {
@@ -385,9 +394,11 @@ static void x509v3_cache_extensions(X509 *x)
385
394
x -> ex_pathlen = -1 ;
386
395
BASIC_CONSTRAINTS_free (bs );
387
396
x -> ex_flags |= EXFLAG_BCONS ;
397
+ } else if (i != -1 ) {
398
+ x -> ex_flags |= EXFLAG_INVALID ;
388
399
}
389
400
/* Handle proxy certificates */
390
- if ((pci = X509_get_ext_d2i (x , NID_proxyCertInfo , NULL , NULL ))) {
401
+ if ((pci = X509_get_ext_d2i (x , NID_proxyCertInfo , & i , NULL ))) {
391
402
if (x -> ex_flags & EXFLAG_CA
392
403
|| X509_get_ext_by_NID (x , NID_subject_alt_name , -1 ) >= 0
393
404
|| X509_get_ext_by_NID (x , NID_issuer_alt_name , -1 ) >= 0 ) {
@@ -399,9 +410,11 @@ static void x509v3_cache_extensions(X509 *x)
399
410
x -> ex_pcpathlen = -1 ;
400
411
PROXY_CERT_INFO_EXTENSION_free (pci );
401
412
x -> ex_flags |= EXFLAG_PROXY ;
413
+ } else if (i != -1 ) {
414
+ x -> ex_flags |= EXFLAG_INVALID ;
402
415
}
403
416
/* Handle key usage */
404
- if ((usage = X509_get_ext_d2i (x , NID_key_usage , NULL , NULL ))) {
417
+ if ((usage = X509_get_ext_d2i (x , NID_key_usage , & i , NULL ))) {
405
418
if (usage -> length > 0 ) {
406
419
x -> ex_kusage = usage -> data [0 ];
407
420
if (usage -> length > 1 )
@@ -410,9 +423,11 @@ static void x509v3_cache_extensions(X509 *x)
410
423
x -> ex_kusage = 0 ;
411
424
x -> ex_flags |= EXFLAG_KUSAGE ;
412
425
ASN1_BIT_STRING_free (usage );
426
+ } else if (i != -1 ) {
427
+ x -> ex_flags |= EXFLAG_INVALID ;
413
428
}
414
429
x -> ex_xkusage = 0 ;
415
- if ((extusage = X509_get_ext_d2i (x , NID_ext_key_usage , NULL , NULL ))) {
430
+ if ((extusage = X509_get_ext_d2i (x , NID_ext_key_usage , & i , NULL ))) {
416
431
x -> ex_flags |= EXFLAG_XKUSAGE ;
417
432
for (i = 0 ; i < sk_ASN1_OBJECT_num (extusage ); i ++ ) {
418
433
switch (OBJ_obj2nid (sk_ASN1_OBJECT_value (extusage , i ))) {
@@ -455,18 +470,26 @@ static void x509v3_cache_extensions(X509 *x)
455
470
}
456
471
}
457
472
sk_ASN1_OBJECT_pop_free (extusage , ASN1_OBJECT_free );
473
+ } else if (i != -1 ) {
474
+ x -> ex_flags |= EXFLAG_INVALID ;
458
475
}
459
476
460
- if ((ns = X509_get_ext_d2i (x , NID_netscape_cert_type , NULL , NULL ))) {
477
+ if ((ns = X509_get_ext_d2i (x , NID_netscape_cert_type , & i , NULL ))) {
461
478
if (ns -> length > 0 )
462
479
x -> ex_nscert = ns -> data [0 ];
463
480
else
464
481
x -> ex_nscert = 0 ;
465
482
x -> ex_flags |= EXFLAG_NSCERT ;
466
483
ASN1_BIT_STRING_free (ns );
484
+ } else if (i != -1 ) {
485
+ x -> ex_flags |= EXFLAG_INVALID ;
467
486
}
468
- x -> skid = X509_get_ext_d2i (x , NID_subject_key_identifier , NULL , NULL );
469
- x -> akid = X509_get_ext_d2i (x , NID_authority_key_identifier , NULL , NULL );
487
+ x -> skid = X509_get_ext_d2i (x , NID_subject_key_identifier , & i , NULL );
488
+ if (x -> skid == NULL && i != -1 )
489
+ x -> ex_flags |= EXFLAG_INVALID ;
490
+ x -> akid = X509_get_ext_d2i (x , NID_authority_key_identifier , & i , NULL );
491
+ if (x -> akid == NULL && i != -1 )
492
+ x -> ex_flags |= EXFLAG_INVALID ;
470
493
/* Does subject name match issuer ? */
471
494
if (!X509_NAME_cmp (X509_get_subject_name (x ), X509_get_issuer_name (x ))) {
472
495
x -> ex_flags |= EXFLAG_SI ;
@@ -475,16 +498,22 @@ static void x509v3_cache_extensions(X509 *x)
475
498
!ku_reject (x , KU_KEY_CERT_SIGN ))
476
499
x -> ex_flags |= EXFLAG_SS ;
477
500
}
478
- x -> altname = X509_get_ext_d2i (x , NID_subject_alt_name , NULL , NULL );
501
+ x -> altname = X509_get_ext_d2i (x , NID_subject_alt_name , & i , NULL );
502
+ if (x -> altname == NULL && i != -1 )
503
+ x -> ex_flags |= EXFLAG_INVALID ;
479
504
x -> nc = X509_get_ext_d2i (x , NID_name_constraints , & i , NULL );
480
- if (!x -> nc && (i != -1 ))
505
+ if (x -> nc == NULL && i != -1 )
506
+ x -> ex_flags |= EXFLAG_INVALID ;
507
+ if (!setup_crldp (x ))
481
508
x -> ex_flags |= EXFLAG_INVALID ;
482
- setup_crldp (x );
483
509
484
510
#ifndef OPENSSL_NO_RFC3779
485
- x -> rfc3779_addr = X509_get_ext_d2i (x , NID_sbgp_ipAddrBlock , NULL , NULL );
486
- x -> rfc3779_asid = X509_get_ext_d2i (x , NID_sbgp_autonomousSysNum ,
487
- NULL , NULL );
511
+ x -> rfc3779_addr = X509_get_ext_d2i (x , NID_sbgp_ipAddrBlock , & i , NULL );
512
+ if (x -> rfc3779_addr == NULL && i != -1 )
513
+ x -> ex_flags |= EXFLAG_INVALID ;
514
+ x -> rfc3779_asid = X509_get_ext_d2i (x , NID_sbgp_autonomousSysNum , & i , NULL );
515
+ if (x -> rfc3779_asid == NULL && i != -1 )
516
+ x -> ex_flags |= EXFLAG_INVALID ;
488
517
#endif
489
518
for (i = 0 ; i < X509_get_ext_count (x ); i ++ ) {
490
519
ex = X509_get_ext (x , i );
@@ -777,7 +806,11 @@ int X509_check_issued(X509 *issuer, X509 *subject)
777
806
return X509_V_ERR_SUBJECT_ISSUER_MISMATCH ;
778
807
779
808
x509v3_cache_extensions (issuer );
809
+ if (issuer -> ex_flags & EXFLAG_INVALID )
810
+ return X509_V_ERR_UNSPECIFIED ;
780
811
x509v3_cache_extensions (subject );
812
+ if (subject -> ex_flags & EXFLAG_INVALID )
813
+ return X509_V_ERR_UNSPECIFIED ;
781
814
782
815
if (subject -> akid ) {
783
816
int ret = X509_check_akid (issuer , subject -> akid );
@@ -842,7 +875,8 @@ uint32_t X509_get_extension_flags(X509 *x)
842
875
uint32_t X509_get_key_usage (X509 * x )
843
876
{
844
877
/* Call for side-effect of computing hash and caching extensions */
845
- X509_check_purpose (x , -1 , -1 );
878
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
879
+ return 0 ;
846
880
if (x -> ex_flags & EXFLAG_KUSAGE )
847
881
return x -> ex_kusage ;
848
882
return UINT32_MAX ;
@@ -851,7 +885,8 @@ uint32_t X509_get_key_usage(X509 *x)
851
885
uint32_t X509_get_extended_key_usage (X509 * x )
852
886
{
853
887
/* Call for side-effect of computing hash and caching extensions */
854
- X509_check_purpose (x , -1 , -1 );
888
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
889
+ return 0 ;
855
890
if (x -> ex_flags & EXFLAG_XKUSAGE )
856
891
return x -> ex_xkusage ;
857
892
return UINT32_MAX ;
@@ -860,28 +895,32 @@ uint32_t X509_get_extended_key_usage(X509 *x)
860
895
const ASN1_OCTET_STRING * X509_get0_subject_key_id (X509 * x )
861
896
{
862
897
/* Call for side-effect of computing hash and caching extensions */
863
- X509_check_purpose (x , -1 , -1 );
898
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
899
+ return NULL ;
864
900
return x -> skid ;
865
901
}
866
902
867
903
const ASN1_OCTET_STRING * X509_get0_authority_key_id (X509 * x )
868
904
{
869
905
/* Call for side-effect of computing hash and caching extensions */
870
- X509_check_purpose (x , -1 , -1 );
906
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
907
+ return NULL ;
871
908
return (x -> akid != NULL ? x -> akid -> keyid : NULL );
872
909
}
873
910
874
911
const GENERAL_NAMES * X509_get0_authority_issuer (X509 * x )
875
912
{
876
913
/* Call for side-effect of computing hash and caching extensions */
877
- X509_check_purpose (x , -1 , -1 );
914
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
915
+ return NULL ;
878
916
return (x -> akid != NULL ? x -> akid -> issuer : NULL );
879
917
}
880
918
881
919
const ASN1_INTEGER * X509_get0_authority_serial (X509 * x )
882
920
{
883
921
/* Call for side-effect of computing hash and caching extensions */
884
- X509_check_purpose (x , -1 , -1 );
922
+ if (X509_check_purpose (x , -1 , -1 ) != 1 )
923
+ return NULL ;
885
924
return (x -> akid != NULL ? x -> akid -> serial : NULL );
886
925
}
887
926
0 commit comments