@@ -455,37 +455,43 @@ from cryptography.hazmat.primitives import hashes
455
455
456
456
def validate_cert (request ):
457
457
458
- cert_value = request.headers.get(' X-ARR-ClientCert' )
459
- if cert_value is None :
460
- return False
458
+ try :
459
+ cert_value = request.headers.get(' X-ARR-ClientCert' )
460
+ if cert_value is None :
461
+ return False
462
+
463
+ cert_data = ' ' .join([' -----BEGIN CERTIFICATE-----\n ' , cert_value, ' \n -----END CERTIFICATE-----\n ' ,])
464
+ cert = x509.load_pem_x509_certificate(cert_data.encode(' utf-8' ))
461
465
462
- cert_data = ' ' .join([' -----BEGIN CERTIFICATE-----\n ' , cert_value, ' \n -----END CERTIFICATE-----\n ' ,])
463
- cert = x509.load_pem_x509_certificate(cert_data.encode(' utf-8' ))
464
-
465
- fingerprint = cert.fingerprint(hashes.SHA1())
466
- if fingerprint != b ' 12345678901234567890' :
467
- return False
466
+ fingerprint = cert.fingerprint(hashes.SHA1())
467
+ if fingerprint != b ' 12345678901234567890' :
468
+ return False
469
+
470
+ subject = cert.subject
471
+ subject_cn = subject.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
472
+ if subject_cn != " contoso.com" :
473
+ return False
474
+
475
+ issuer = cert.issuer
476
+ issuer_cn = issuer.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
477
+ if issuer_cn != " contoso.com" :
478
+ return False
468
479
469
- subject = cert.subject
470
- subject_cn = subject.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
471
- if subject_cn != " contoso.com" :
472
- return False
480
+ current_time = datetime.now(timezone.utc)
473
481
474
- issuer = cert.issuer
475
- issuer_cn = issuer.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
476
- if issuer_cn != " contosoauthority.com" :
477
- return False
478
-
479
- current_time = datetime.now(timezone.utc)
482
+ if current_time < cert.not_valid_before_utc:
483
+ return False
484
+
485
+ if current_time > cert.not_valid_after_utc:
486
+ return False
487
+
488
+ return True
480
489
481
- if current_time < cert.not_valid_before_utc:
490
+ except Exception as e:
491
+ # Handle any errors encountered during validation
492
+ print (f " Encountered the following error during certificate validation: { e} " )
482
493
return False
483
494
484
- if current_time > cert.not_valid_after_utc:
485
- return False
486
-
487
- return True
488
-
489
495
def authorize_certificate (f ):
490
496
@wraps (f)
491
497
def decorated_function (* args , ** kwargs ):
@@ -518,36 +524,42 @@ from cryptography.hazmat.primitives import hashes
518
524
519
525
def validate_cert (request ):
520
526
521
- cert_value = request.headers.get(' X-ARR-ClientCert' )
522
- if cert_value is None :
523
- return False
527
+ try :
528
+ cert_value = request.headers.get(' X-ARR-ClientCert' )
529
+ if cert_value is None :
530
+ return False
531
+
532
+ cert_data = ' ' .join([' -----BEGIN CERTIFICATE-----\n ' , cert_value, ' \n -----END CERTIFICATE-----\n ' ,])
533
+ cert = x509.load_pem_x509_certificate(cert_data.encode(' utf-8' ))
524
534
525
- cert_data = ' ' .join([' -----BEGIN CERTIFICATE-----\n ' , cert_value, ' \n -----END CERTIFICATE-----\n ' ,])
526
- cert = x509.load_pem_x509_certificate(cert_data.encode(' utf-8' ))
527
-
528
- fingerprint = cert.fingerprint(hashes.SHA1())
529
- if fingerprint != b ' 12345678901234567890' :
530
- return False
535
+ fingerprint = cert.fingerprint(hashes.SHA1())
536
+ if fingerprint != b ' 12345678901234567890' :
537
+ return False
538
+
539
+ subject = cert.subject
540
+ subject_cn = subject.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
541
+ if subject_cn != " contoso.com" :
542
+ return False
543
+
544
+ issuer = cert.issuer
545
+ issuer_cn = issuer.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
546
+ if issuer_cn != " contoso.com" :
547
+ return False
531
548
532
- subject = cert.subject
533
- subject_cn = subject.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
534
- if subject_cn != " contoso.com" :
535
- return False
549
+ current_time = datetime.now(timezone.utc)
536
550
537
- issuer = cert.issuer
538
- issuer_cn = issuer.get_attributes_for_oid(NameOID.COMMON_NAME )[0 ].value
539
- if issuer_cn != " contosoauthority.com" :
540
- return False
541
-
542
- current_time = datetime.now(timezone.utc)
551
+ if current_time < cert.not_valid_before_utc:
552
+ return False
553
+
554
+ if current_time > cert.not_valid_after_utc:
555
+ return False
556
+
557
+ return True
543
558
544
- if current_time < cert.not_valid_before_utc:
559
+ except Exception as e:
560
+ # Handle any errors encountered during validation
561
+ print (f " Encountered the following error during certificate validation: { e} " )
545
562
return False
546
-
547
- if current_time > cert.not_valid_after_utc:
548
- return False
549
-
550
- return True
551
563
552
564
def authorize_certificate (view ):
553
565
@wraps (view)
0 commit comments