forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocsp_pyopenssl.py
More file actions
1366 lines (1228 loc) · 51.3 KB
/
ocsp_pyopenssl.py
File metadata and controls
1366 lines (1228 loc) · 51.3 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2017 Snowflake Computing Inc. All right reserved.
#
"""
Use openssl command line to validate the certification revocation status
using OCSP.
"""
import base64
import calendar
import codecs
import hashlib
import json
import logging
import os
import platform
import re
import socket
import time
from logging import getLogger
from multiprocessing.pool import ThreadPool
from os import path
from threading import (Lock)
from time import gmtime, strftime, strptime
from OpenSSL.crypto import (dump_certificate, FILETYPE_PEM, FILETYPE_ASN1,
load_certificate, dump_publickey)
from OpenSSL.crypto import verify as crypto_verify
from botocore.vendored import requests
from botocore.vendored.requests.adapters import HTTPAdapter
from pyasn1.codec.der import decoder as der_decoder
from pyasn1.codec.der import encoder as der_encoder
from pyasn1.type import (univ, tag)
from pyasn1_modules import (rfc2459, rfc2437, rfc2560)
from .compat import (PY2, urlsplit, OK)
from .errorcode import (ER_FAILED_TO_GET_OCSP_URI,
ER_INVALID_OCSP_RESPONSE,
ER_SERVER_CERTIFICATE_REVOKED,
ER_CA_CERTIFICATE_NOT_FOUND)
from .errors import (OperationalError)
from .rfc6960 import (OCSPRequest, OCSPResponse, TBSRequest, CertID, Request,
Version, BasicOCSPResponse,
OCSPResponseStatus)
ROOT_CERTIFICATES_DICT_LOCK = Lock()
ROOT_CERTIFICATES_DICT = {} # root certificates
def _read_ca_bundle(ca_bundle_file):
"""
Reads a cabundle file including certificates in PEM format
"""
logger = getLogger(__name__)
logger.debug('reading ca cabundle: %s', ca_bundle_file)
# cabundle file encoding varies. Tries reading it in utf-8 but ignore
# all errors
all_certs = codecs.open(
ca_bundle_file, 'r', encoding='utf-8', errors='ignore').read()
state = 0
contents = []
for line in all_certs.split('\n'):
if state == 0 and line.startswith('-----BEGIN CERTIFICATE-----'):
state = 1
contents.append(line)
elif state == 1:
contents.append(line)
if line.startswith('-----END CERTIFICATE-----'):
cert = load_certificate(
FILETYPE_PEM,
'\n'.join(contents).encode('utf-8'))
ROOT_CERTIFICATES_DICT[cert.get_subject().der()] = cert
state = 0
contents = []
def _lazy_read_ca_bundle():
"""
Reads the local cabundle file and cache it in memory
"""
if len(ROOT_CERTIFICATES_DICT) > 0:
return
logger = getLogger(__name__)
try:
ca_bundle = (os.environ.get('REQUESTS_CA_BUNDLE') or
os.environ.get('CURL_CA_BUNDLE'))
if ca_bundle and path.exists(ca_bundle):
# if the user/application specifies cabundle.
_read_ca_bundle(ca_bundle)
else:
import sys
from botocore.vendored.requests import certs
if hasattr(certs, '__file__') and \
path.exists(certs.__file__) and \
path.exists(path.join(
path.dirname(certs.__file__), 'cacert.pem')):
# if cacert.pem exists next to certs.py in request pacakage
ca_bundle = path.join(
path.dirname(certs.__file__), 'cacert.pem')
_read_ca_bundle(ca_bundle)
elif hasattr(sys, '_MEIPASS'):
# if pyinstaller includes cacert.pem
cabundle_candidates = [
['botocore', 'vendored', 'requests', 'cacert.pem'],
['requests', 'cacert.pem'],
['cacert.pem'],
]
for filename in cabundle_candidates:
ca_bundle = path.join(sys._MEIPASS, *filename)
if path.exists(ca_bundle):
_read_ca_bundle(ca_bundle)
break
else:
logger.error('No cabundle file is found in _MEIPASS')
try:
import certifi
_read_ca_bundle(certifi.where())
except:
logger.debug('no certifi is installed. ignored.')
except Exception as e:
logger.error('Failed to read ca_bundle: %s', e)
if len(ROOT_CERTIFICATES_DICT) == 0:
logger.error('No CA bundle file is found in the system. '
'Set REQUESTS_CA_BUNDLE to the file.')
def octet_string_to_bytearray(octet_string):
"""
Converts Octet string to bytearray
"""
ret = []
for ch in octet_string:
ret.append(ch)
return bytearray(ret)
def bit_string_to_bytearray(bit_string):
"""
Converts Bitstring to bytearray
"""
ret = []
for idx in range(int(len(bit_string) / 8)):
v = 0
for idx0, bit in enumerate(bit_string[idx * 8:idx * 8 + 8]):
v = v | (bit << (7 - idx0))
ret.append(v)
return bytearray(ret)
def _get_pubickey_sha1_hash(cert):
"""
Gets pubkey sha1 hash
"""
pkey = cert.get_pubkey()
pkey_asn1 = dump_publickey(FILETYPE_ASN1, pkey)
decoded_pkey, _ = der_decoder.decode(
pkey_asn1, rfc2459.SubjectPublicKeyInfo())
pubkey = bit_string_to_bytearray(decoded_pkey['subjectPublicKey'])
# algorithm = decoded_pkey['algorithm'] # RSA encryption
sha1_hash = hashlib.sha1()
sha1_hash.update(pubkey)
return sha1_hash
def _extract_values_from_certificate(cert):
"""
Gets Serial Number, DN and Public Key Hashes. Currently SHA1 is used
to generate hashes for DN and Public Key.
"""
logger = getLogger(__name__)
# cert and serial number
data = {
u'cert': cert,
u'issuer': cert.get_issuer().der(),
u'serial_number': cert.get_serial_number(),
u'algorithm': rfc2437.id_sha1,
u'algorithm_parameter': univ.Any(hexValue='0500') # magic number
}
# DN Hash
data[u'name'] = cert.get_subject()
cert_der = data[u'name'].der()
sha1_hash = hashlib.sha1()
sha1_hash.update(cert_der)
data[u'name_hash'] = sha1_hash.hexdigest()
# public key Hash
data['key_hash'] = _get_pubickey_sha1_hash(cert).hexdigest()
# ocsp uri
ocsp_uris0 = []
for idx in range(cert.get_extension_count()):
e = cert.get_extension(idx)
if e.get_short_name() == b'authorityInfoAccess':
for line in str(e).split(u"\n"):
m = OCSP_RE.match(line)
if m:
logger.debug(u'OCSP URL: %s', m.group(1))
ocsp_uris0.append(m.group(1))
if len(ocsp_uris0) == 1:
data['ocsp_uri'] = ocsp_uris0[0]
elif len(ocsp_uris0) == 0:
data['ocsp_uri'] = u''
else:
raise OperationalError(
msg=u'More than one OCSP URI entries are specified in '
u'the certificate',
errno=ER_FAILED_TO_GET_OCSP_URI,
)
data[u'is_root_ca'] = cert.get_subject() == cert.get_issuer()
return data
def _extract_certificate_chain(connection):
"""
Gets certificate chain and extract the key info from certificate
"""
logger = getLogger(__name__)
cert_data = {}
logger.debug(
"# of certificates: %s",
len(connection.get_peer_cert_chain()))
for cert in connection.get_peer_cert_chain():
logger.debug(
u'subject: %s, issuer: %s', cert.get_subject(),
cert.get_issuer())
data = _extract_values_from_certificate(cert)
logger.debug('is_root_ca: %s', data[u'is_root_ca'])
cert_data[cert.get_subject().der()] = data
issuer_and_subject = []
for subject_der in cert_data:
if not cert_data[subject_der][u'is_root_ca']:
# Root certificate will not be validated
# but it is used to validate the subject certificate
issuer_der = cert_data[subject_der]['issuer']
if issuer_der not in cert_data:
# IF NO ROOT certificate is attached in the certificate chain
# read it from the local disk
with ROOT_CERTIFICATES_DICT_LOCK:
_lazy_read_ca_bundle()
logger.debug('not found issuer_der: %s', issuer_der)
if issuer_der in ROOT_CERTIFICATES_DICT:
issuer = _extract_values_from_certificate(
ROOT_CERTIFICATES_DICT[issuer_der])
issuer[u'is_root_ca'] = True
else:
raise OperationalError(
msg=u"CA certificate is not found in the root "
u"certificate list. Make sure you use the latest "
u"Python Connector package.",
errno=ER_CA_CERTIFICATE_NOT_FOUND,
)
else:
issuer = cert_data[issuer_der]
issuer_and_subject.append({
'subject': cert_data[subject_der],
'issuer': issuer,
})
return issuer_and_subject
def _verify_signature(
cert, signature_algorithm_seq, signature, data):
"""
Verifies the signature
"""
logger = getLogger(__name__)
value = bit_string_to_bytearray(signature)
if PY2:
value = str(value)
else:
value = value.decode('latin-1').encode('latin-1')
algorithm = signature_algorithm_seq[0]
if algorithm in SIGNATURE_HASH_ALGO_TO_NAME:
algorithm_name = SIGNATURE_HASH_ALGO_TO_NAME[algorithm]
else:
logger.exception(
"Unsupported Signature Algorithm: %s", algorithm)
return Exception("Unsupported Signature Algorithm: %s", algorithm)
data_der = der_encoder.encode(data)
try:
crypto_verify(cert, value, data_der, algorithm_name)
return None
except Exception as e:
logger.exception("Failed to verify the signature", e)
return e
def process_ocsp_response(response, ocsp_issuer):
"""
process OCSP response
"""
logger = getLogger(__name__)
ocsp_response, _ = der_decoder.decode(response, OCSPResponse())
if ocsp_response['responseStatus'] != OCSPResponseStatus(
'successful'):
raise OperationalError(
msg="Invalid Status: {0}".format(
OCSP_RESPONSE_STATUS[int(ocsp_response['responseStatus'])]),
errno=ER_INVALID_OCSP_RESPONSE)
response_bytes = ocsp_response['responseBytes']
response_type = response_bytes['responseType']
if response_type != rfc2560.id_pkix_ocsp_basic:
logger.error("Invalid Response Type: %s", response_type)
raise OperationalError(
msg="Invaid Response Type: {0}".format(response_type),
errno=ER_INVALID_OCSP_RESPONSE)
basic_ocsp_response, _ = der_decoder.decode(
response_bytes['response'],
BasicOCSPResponse())
if basic_ocsp_response['certs'] is not None:
logger.debug("Certificate is attached in Basic OCSP Response")
cert_der = der_encoder.encode(basic_ocsp_response['certs'][0])
ocsp_cert = load_certificate(FILETYPE_ASN1, cert_der)
else:
logger.debug("Certificate is NOT attached in Basic OCSP Response. "
"Using issuer's certificate")
ocsp_cert = ocsp_issuer['cert']
tbs_response_data = basic_ocsp_response['tbsResponseData']
if tbs_response_data['version'] != 0:
raise OperationalError(
msg='Invalid ResponseData Version: {0}'.format(
tbs_response_data['version']),
errno=ER_INVALID_OCSP_RESPONSE)
if tbs_response_data['responderID']['byName']:
# Noop
logger.debug(
'Responder Name: %s',
tbs_response_data['responderID']['byName'])
elif tbs_response_data['responderID']['byKey']:
# verify the public key
# But I don't know how much value of this checking
# because pubkey must have been known to anybody
# MITM can replicate it.
sha1_cert = _get_pubickey_sha1_hash(ocsp_cert).digest()
sha1_ocsp = tbs_response_data['responderID']['byKey']
sha1_ocsp = octet_string_to_bytearray(sha1_ocsp).decode(
'latin-1').encode('latin-1')
if sha1_cert != sha1_ocsp:
raise OperationalError(
msg=u"The responder id didn't match the public key"
u"of the issuer certificate/leaf certificate",
errno=ER_INVALID_OCSP_RESPONSE
)
if logger.getEffectiveLevel() == logging.DEBUG:
logger.debug(
'Responder PublicKey: %s',
base64.b64encode(octet_string_to_bytearray(
tbs_response_data['responderID']['byKey'])))
else:
raise OperationalError(
msg='Invalid Responder ID: {0}'.format(
tbs_response_data['responderID']),
errno=ER_INVALID_OCSP_RESPONSE)
produced_at = tbs_response_data['producedAt']
logger.debug('Produced At: %s', produced_at)
if tbs_response_data['responseExtensions']:
logger.debug('Response Extensions: %s',
tbs_response_data['responseExtensions'])
ocsp_no_check = False
if ocsp_issuer['cert'] != ocsp_cert:
if ocsp_issuer['cert'].get_subject() != ocsp_cert.get_issuer():
raise OperationalError(
msg=u"Failed to match the issuer of the certificate "
u"attached in OCSP response with the issuer' "
u"certificate.",
errno=ER_INVALID_OCSP_RESPONSE)
is_for_ocsp = False
for cnt in range(ocsp_cert.get_extension_count()):
ex = ocsp_cert.get_extension(cnt)
if ex.get_short_name() == b'extendedKeyUsage':
# ensure the purpose is OCSP signing
der_data, _ = der_decoder.decode(ex.get_data())
for idx in range(len(der_data)):
if der_data[idx] == OCSP_SIGNING:
is_for_ocsp = True
break
elif ex.get_short_name() == b'noCheck':
# check if CA wants to skip ocsp_checking
der_data, _ = der_decoder.decode(ex.get_data())
if str(der_data) != '': # non empty value means no check
ocsp_no_check = True
if not is_for_ocsp:
raise OperationalError(
msg=u'The certificate attached is not for OCSP signing.',
errno=ER_INVALID_OCSP_RESPONSE)
ocsp_cert_object, _ = der_decoder.decode(
dump_certificate(FILETYPE_ASN1, ocsp_cert),
rfc2459.Certificate())
err = _verify_signature(
ocsp_issuer['cert'],
ocsp_cert_object['signatureAlgorithm'],
ocsp_cert_object['signatureValue'],
ocsp_cert_object['tbsCertificate']
)
if err:
raise OperationalError(
msg=u"Signature in the certificate included in the "
u"OCSP response could NOT be verified by the "
u"issuer's certificate: err={0}".format(err),
errno=ER_INVALID_OCSP_RESPONSE)
if not ocsp_no_check:
err = _verify_signature(
ocsp_cert,
basic_ocsp_response['signatureAlgorithm'],
basic_ocsp_response['signature'],
tbs_response_data
)
if err:
raise OperationalError(
msg=u'Signature in the OCSP response could NOT be '
u'verified: err={0}'.format(err),
errno=ER_INVALID_OCSP_RESPONSE)
else:
logger.debug(
u'No OCSP validation was made as the certificate '
u'indicates noCheck')
single_response_map = {}
for single_response in tbs_response_data['responses']:
cert_id = single_response['certID']
cert_status = single_response['certStatus']
cert_id_der = der_encoder.encode(cert_id)
if cert_status['good'] is not None:
logger.debug('ok')
this_update = strptime(str(single_response['thisUpdate']),
'%Y%m%d%H%M%SZ')
next_update = strptime(str(single_response['nextUpdate']),
'%Y%m%d%H%M%SZ')
this_update = calendar.timegm(this_update)
next_update = calendar.timegm(next_update)
single_response_map[cert_id_der] = {
'status': 'good',
'this_update': this_update,
'next_update': next_update,
}
elif cert_status['revoked'] is not None:
logger.info('revoked: %s', cert_status['revoked'])
# revocation
revocation_time = cert_status['revoked']['revocationTime']
revocation_reason = cert_status['revoked']['revocationReason']
single_response_map[cert_id_der] = {
'status': 'revoked',
'time': revocation_time,
'reason': revocation_reason,
}
else:
logger.info('unknown')
single_response_map[cert_id_der] = {
'status': 'unknown',
}
return single_response_map
def execute_ocsp_request(ocsp_uri, cert_id, proxies=None, do_retry=True):
"""
Executes OCSP request for the given cert id
"""
logger = getLogger(__name__)
request = Request()
request['reqCert'] = cert_id
request_list = univ.SequenceOf(componentType=Request())
request_list[0] = request
tbs_request = TBSRequest()
tbs_request['requestList'] = request_list
tbs_request['version'] = Version(0).subtype(
explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple,
0))
ocsp_request = OCSPRequest()
ocsp_request['tbsRequest'] = tbs_request
# no signature for the client
# no nonce is set, because not all OCSP resopnder implements it yet
# transform objects into data in requests
data = der_encoder.encode(ocsp_request)
parsed_url = urlsplit(ocsp_uri)
max_retry = 100 if do_retry else 1
# NOTE: This retry is to retry getting HTTP 200.
headers = {
'Content-Type': 'application/ocsp-request',
'Content-Length': '{0}'.format(
len(data)),
'Host': parsed_url.hostname.encode(
'utf-8'),
}
logger.debug('url: %s, headers: %s, proxies: %s',
ocsp_uri, headers, proxies)
with requests.Session() as session:
session.mount('http://', HTTPAdapter(max_retries=5))
session.mount('https://', HTTPAdapter(max_retries=5))
for attempt in range(max_retry):
response = session.post(
ocsp_uri,
headers=headers,
proxies=proxies,
data=data,
timeout=60)
if response.status_code == OK:
logger.debug("OCSP response was successfully returned")
break
elif max_retry > 1:
wait_time = 2 ** attempt
wait_time = 16 if wait_time > 16 else wait_time
logger.debug("OCSP server returned %s. Retrying in %s(s)",
response.status_code, wait_time)
time.sleep(wait_time)
else:
logger.error("Failed to get OCSP response after %s attempt.",
max_retry)
return response.content
def is_cert_id_in_cache(ocsp_issuer, ocsp_subject, use_cache=True):
u"""
checks if cert_id is in the cache
"""
logger = getLogger(__name__)
cert_id = CertID()
cert_id[
'hashAlgorithm'] = rfc2459.AlgorithmIdentifier().setComponentByName(
'algorithm', ocsp_issuer[u'algorithm']).setComponentByName(
'parameters', ocsp_issuer[u'algorithm_parameter'])
cert_id['issuerNameHash'] = univ.OctetString(
hexValue=ocsp_issuer[u'name_hash'])
cert_id['issuerKeyHash'] = univ.OctetString(
hexValue=ocsp_issuer[u'key_hash'])
cert_id['serialNumber'] = rfc2459.CertificateSerialNumber(
ocsp_subject[u'serial_number'])
cert_id_der = der_encoder.encode(cert_id)
if logger.getEffectiveLevel() == logging.DEBUG:
base64_name_hash = base64.b64encode(
octet_string_to_bytearray(cert_id['issuerNameHash']))
else:
base64_name_hash = None
with OCSP_VALIDATION_CACHE_LOCK:
if use_cache and cert_id_der in OCSP_VALIDATION_CACHE:
current_time = int(time.time())
ts, cache = OCSP_VALIDATION_CACHE[cert_id_der]
if ts - CACHE_EXPIRATION <= current_time <= ts + CACHE_EXPIRATION:
# cache value is OCSP response
logger.debug(
u'hit cache. issuer name hash: %s, issuer name: %s, is subject root: %s',
base64_name_hash, ocsp_issuer['name'],
ocsp_issuer[u'is_root_ca'])
return True, cert_id, cache
else:
# more than 24 hours difference
del OCSP_VALIDATION_CACHE[cert_id_der]
global OCSP_VALIDATION_CACHE_UPDATED
OCSP_VALIDATION_CACHE_UPDATED = True
if logger.getEffectiveLevel() == logging.DEBUG:
logger.debug(
u'not hit cache. issuer name hash: %s, issuer name: %s, is subject root: %s, '
u'issuer name hash algorithm: %s, '
u'issuer key hash: %s, subject serial number: %s',
base64_name_hash, ocsp_issuer['name'], ocsp_issuer[u'is_root_ca'],
cert_id['hashAlgorithm'],
base64.b64encode(octet_string_to_bytearray(
cert_id['issuerKeyHash'])),
cert_id['serialNumber'])
with OCSP_VALIDATION_CACHE_LOCK:
for k in OCSP_VALIDATION_CACHE:
key_cert_id, _ = der_decoder.decode(k, asn1Spec=CertID())
logger.debug(
"issuer name hash: %s, issuer name hash algorithm: %s, "
"issuer key hash: %s, subject serial number: %s",
base64.b64encode(octet_string_to_bytearray(
key_cert_id['issuerNameHash'])),
key_cert_id['hashAlgorithm'],
base64.b64encode(octet_string_to_bytearray(
key_cert_id['issuerKeyHash'])),
key_cert_id['serialNumber']
)
return False, cert_id, None
def _decode_ocsp_response_cache(ocsp_response_cache_json,
ocsp_response_cache):
"""
Decodes OCSP response cache from JSON
"""
from base64 import b64decode
current_time = int(time.time())
for cert_id, (ts, ocsp_response) in ocsp_response_cache_json.items():
cert_id_der = b64decode(cert_id)
if ts - CACHE_EXPIRATION <= current_time <= ts + CACHE_EXPIRATION:
ocsp_response_cache[cert_id_der] = (ts, b64decode(
ocsp_response))
elif cert_id_der in ocsp_response_cache:
# invalidate the cache if exists
del ocsp_response_cache[cert_id_der]
def _encode_ocsp_response_cache(ocsp_response_cache,
ocsp_response_cache_json):
"""
Encodes OCSP response cache to JSON
"""
from base64 import b64encode
logger = getLogger(__name__)
logger.debug('encoding OCSP reponse cache to JSON')
for cert_id_der, (current_time, ocsp_response) in \
ocsp_response_cache.items():
if logger.getEffectiveLevel() == logging.DEBUG:
key_cert_id, _ = der_decoder.decode(
cert_id_der, asn1Spec=CertID())
logger.debug('name: %s, serial number: %s',
b64encode(octet_string_to_bytearray(
key_cert_id['issuerNameHash'])),
key_cert_id['serialNumber'])
k = b64encode(cert_id_der).decode('ascii')
v = b64encode(ocsp_response).decode('ascii')
ocsp_response_cache_json[k] = (current_time, v)
def touch(fname, times=None):
"""
Touch a file
"""
with open(fname, 'a'):
os.utime(fname, times)
def file_timestamp(filename):
if platform.system() == 'Windows':
ts = int(path.getctime(filename))
else:
stat = os.stat(filename)
if hasattr(stat, 'st_birthtime'): # odx
ts = int(stat.st_birthtime)
else:
ts = int(stat.st_mtime) # linux
return ts
def check_ocsp_response_cache_lock_file(filename):
logger = getLogger(__name__)
current_time = int(time.time())
lock_file = filename + '.lck'
try:
ts_cache_file = file_timestamp(filename)
if not path.exists(lock_file) and ts_cache_file >= current_time - \
CACHE_EXPIRATION:
# use cache only if no lock file exists and the cache file
# was created last 24 hours
return True
if path.exists(lock_file):
# delete lock file if older 60 seconds
ts_lock_file = file_timestamp(lock_file)
if ts_lock_file < current_time - 60:
os.unlink(lock_file)
logger.info(
"The lock file is older than 60 seconds. "
"Deleted the lock file and ignoring the cache: %s",
lock_file
)
else:
logger.info(
'The lock file exists. Other process may be updating the '
'cache file: %s, %s', filename, lock_file)
else:
os.unlink(filename)
logger.info(
"The cache is older than 1 day. "
"Deleted the cache file: %s", filename
)
except Exception as e:
logger.info(
"Failed to check OCSP response cache file. No worry. It will "
"validate with OCSP server: file: %s, lock file: %s, error: %s",
filename, lock_file, e
)
return False
def read_ocsp_response_cache_file(filename, ocsp_validation_cache):
"""
Reads OCSP Response cache
"""
logger = getLogger(__name__)
if check_ocsp_response_cache_lock_file(filename) and path.exists(filename):
_decode_ocsp_response_cache(
json.load(
codecs.open(
filename, 'r', encoding='utf-8', errors='ignore')),
ocsp_validation_cache)
logger.debug("Read OCSP response cache file: %s, count=%s",
filename, len(OCSP_VALIDATION_CACHE))
else:
logger.info(
"Failed to locate OCSP response cache file. "
"No worry. It will validate with OCSP server: %s",
filename
)
def write_ocsp_response_cache_file(filename, ocsp_validation_cache):
"""
Writes OCSP Response Cache
"""
logger = getLogger(__name__)
logger.debug('writing OCSP response cache file')
file_cache_data = {}
_encode_ocsp_response_cache(
ocsp_validation_cache,
file_cache_data
)
with codecs.open(filename, 'w', encoding='utf-8', errors='ignore') as f:
json.dump(file_cache_data, f)
def update_ocsp_response_cache_file(ocsp_response_cache_url):
"""
Updates OCSP Response Cache
"""
logger = getLogger(__name__)
lock_file = None
if ocsp_response_cache_url is not None:
try:
parsed_url = urlsplit(ocsp_response_cache_url)
if parsed_url.scheme == 'file':
filename = path.join(parsed_url.netloc, parsed_url.path)
lock_file = filename + '.lck'
for _ in range(100):
# wait until the lck file has been removed
# or up to 1 second (0.01 x 100)
if not path.exists(lock_file):
break
time.sleep(0.01)
if not path.exists(lock_file):
touch(lock_file)
try:
write_ocsp_response_cache_file(
filename,
OCSP_VALIDATION_CACHE)
finally:
os.unlink(lock_file)
lock_file = None
else:
logger.info(
"No OCSP response cache file is written, because the "
"given URI is not a file: %s. Ignoring...",
ocsp_response_cache_url)
except Exception as e:
logger.info(
"Failed to write OCSP response cache "
"file. file: %s, error: %s, Ignoring...",
ocsp_response_cache_url, e, exc_info=True)
if lock_file is not None and os.path.exists(lock_file):
try:
os.unlink(lock_file)
except Exception as e:
logger.debug(
"Failed to unlink OCS response cache lock file. Ignoring..."
)
def download_ocsp_response_cache(url):
"""
Downloads OCSP response cache from Snowflake.
"""
import binascii
with requests.Session() as session:
session.mount('http://', HTTPAdapter(max_retries=5))
session.mount('https://', HTTPAdapter(max_retries=5))
response = session.get(url)
if response.status_code == OK:
try:
_decode_ocsp_response_cache(response.json(), OCSP_VALIDATION_CACHE)
except (ValueError, binascii.Error) as err:
logger = getLogger(__name__)
logger.info(
'Failed to convert OCSP cache server response to '
'JSON. The cache was corrupted. No worry. It will'
'validate with OCSP server: %s', err)
else:
logger = getLogger(__name__)
logger.info("Failed to get OCSP response cache from %s: %s",
url, response.status_code)
def check_ocsp_response_status(
single_response_map,
ocsp_response, ocsp_response_cache_url):
"""
Checks the OCSP response status
"""
ret = []
for cert_id_der, data in single_response_map.items():
if data['status'] == 'good':
ret.append(_process_good_status(
cert_id_der, data, ocsp_response,
ocsp_response_cache_url))
elif data['status'] == 'revoked': # revoked
_process_revoked_status(cert_id_der, data)
else: # unknown
_process_unknown_status(cert_id_der)
if len(ret) != len(single_response_map):
raise OperationalError(
msg=u"Not all OCSP Response was returned",
errno=ER_INVALID_OCSP_RESPONSE,
)
def _calculate_tolerable_validity(this_update, next_update):
return max(int(TOLERABLE_VALIDITY_RANGE_RATIO * (
next_update - this_update)), MAX_CLOCK_SKEW)
def _is_validaity_range(current_time, this_update, next_update):
logger = getLogger(__name__)
tolerable_validity = _calculate_tolerable_validity(this_update, next_update)
logger.debug(u'Tolerable Validity range for OCSP response: +%s(s)',
tolerable_validity)
return this_update - MAX_CLOCK_SKEW <= \
current_time <= next_update + tolerable_validity
def _validity_error_message(current_time, this_update, next_update):
tolerable_validity = _calculate_tolerable_validity(this_update, next_update)
return (u"Response is unreliable. Its validity "
u"date is out of range: current_time={0}, "
u"this_update={1}, next_update={2}, "
u"tolerable next_update={3}. A potential cause is "
u"client clock is skewed, CA fails to update OCSP "
u"response in time.".format(
strftime('%Y%m%d%H%M%SZ', gmtime(current_time)),
strftime('%Y%m%d%H%M%SZ', gmtime(this_update)),
strftime('%Y%m%d%H%M%SZ', gmtime(next_update)),
strftime('%Y%m%d%H%M%SZ', gmtime(
next_update + tolerable_validity))))
def _process_good_status(
cert_id_der, data, ocsp_response, ocsp_response_cache_url):
"""
Process Good status
"""
logger = getLogger(__name__)
current_time = int(time.time())
this_update = data['this_update']
next_update = data['next_update']
if _is_validaity_range(current_time, this_update, next_update):
with OCSP_VALIDATION_CACHE_LOCK:
if cert_id_der not in OCSP_VALIDATION_CACHE:
OCSP_VALIDATION_CACHE[cert_id_der] = (
current_time, ocsp_response)
global OCSP_VALIDATION_CACHE_UPDATED
OCSP_VALIDATION_CACHE_UPDATED = True
if logger.getEffectiveLevel() == logging.DEBUG:
cert_id, _ = der_decoder.decode(
cert_id_der, asn1Spec=CertID())
logger.debug(
u'store cache: %s, this_update: %s, '
u'next_update: %s',
base64.b64encode(
octet_string_to_bytearray(
cert_id['issuerNameHash'])),
this_update, next_update)
return True
else:
raise OperationalError(
msg=_validity_error_message(current_time, this_update, next_update),
errno=ER_INVALID_OCSP_RESPONSE
)
def _process_revoked_status(cert_id_der, data):
"""
Process Revoked status
"""
with OCSP_VALIDATION_CACHE_LOCK:
if cert_id_der in OCSP_VALIDATION_CACHE:
global OCSP_VALIDATION_CACHE_UPDATED
OCSP_VALIDATION_CACHE_UPDATED = True
del OCSP_VALIDATION_CACHE[cert_id_der]
current_time = int(time.time())
revocation_time = data['time']
revocation_reason = data['reason']
raise OperationalError(
msg=u"The certificate has been revoked: current_time={0}, "
u"time={1}, reason={2}".format(
strftime('%Y%m%d%H%M%SZ', gmtime(current_time)),
revocation_time,
revocation_reason),
errno=ER_SERVER_CERTIFICATE_REVOKED,
)
def _process_unknown_status(cert_id_der):
"""
Process Unknown status
"""
with OCSP_VALIDATION_CACHE_LOCK:
if cert_id_der in OCSP_VALIDATION_CACHE:
global OCSP_VALIDATION_CACHE_UPDATED
OCSP_VALIDATION_CACHE_UPDATED = True
del OCSP_VALIDATION_CACHE[cert_id_der]
raise OperationalError(
msg=u"The certificate is in UNKNOWN revocation status.",
errno=ER_SERVER_CERTIFICATE_REVOKED,
)
# Signature Hash Algorithm
sha1WithRSAEncryption = univ.ObjectIdentifier('1.2.840.113549.1.1.5')
sha256WithRSAEncryption = univ.ObjectIdentifier('1.2.840.113549.1.1.11')
sha384WithRSAEncryption = univ.ObjectIdentifier('1.2.840.113549.1.1.12')
sha512WithRSAEncryption = univ.ObjectIdentifier('1.2.840.113549.1.1.13')
SIGNATURE_HASH_ALGO_TO_NAME = {
sha1WithRSAEncryption: 'sha1',
sha256WithRSAEncryption: 'sha256',
sha384WithRSAEncryption: 'sha384',
sha512WithRSAEncryption: 'sha512',
}
# OCSP SIGNING flag
OCSP_SIGNING = univ.ObjectIdentifier('1.3.6.1.5.5.7.3.9')
# Maximum clock skew in seconds (15 minutes) allowed when checking
# validity of OCSP responses
MAX_CLOCK_SKEW = 900
# Tolerable validity date range ratio. The OCSP response is valid up
# to (next update timestap) + (next update timestamp - this update timestap) *
# TOLERABLE_VALIDITY_RANGE_RATIO. This buffer yields some time for Root CA to
# update intermediate CA's certificate OCSP response. In fact, they don't
# update OCSP response in time. In Dec 2016, they left OCSP response expires for
# 5 hours at least, and it caused the connectivity issues in customers.
# With this buffer, about 2 days are given for 180 days validity date.
TOLERABLE_VALIDITY_RANGE_RATIO = 0.01
# Cache Expiration in seconds (24 hours). OCSP validation cache is
# invalidated every 24 hours
CACHE_EXPIRATION = 86400
# Known certificates that can skip OCSP validation
KNOWN_HOSTNAMES = {
'',
}
# OCSP cache
OCSP_VALIDATION_CACHE = {}
# OCSP cache lock
OCSP_VALIDATION_CACHE_LOCK = Lock()
# OCSP cache update flag
OCSP_VALIDATION_CACHE_UPDATED = False
# OCSP string match
OCSP_RE = re.compile(r'^OCSP\s+\-\s+URI:(.*)$')
# OCSP response mapping
OCSP_RESPONSE_STATUS = {
0: 'successful',
1: 'malformedRequest',
2: 'internalError',
3: 'tryLater',
4: 'not used',