@@ -23,7 +23,7 @@ def get_ocsp_status(host: str, port: Any = None) -> list:
2323 """ Main function with two inputs: host, and port.
2424 Port defaults to TCP 443 """
2525
26- results = []
26+ results : list = []
2727 results .append (f"Host: { host } :{ port } " )
2828
2929 # pylint: disable=W0703
@@ -49,34 +49,19 @@ def get_ocsp_status(host: str, port: Any = None) -> list:
4949 return results
5050
5151 try :
52+ # Get the remote certificate chain
5253 cert_chain = get_certificate_chain (host , port )
5354
54- except Exception as err :
55- results .append ("Error: " + str (err ))
56- return results
57-
58- try :
55+ # Extract OCSP URL from leaf certificate
5956 ocsp_url = extract_ocsp_url (cert_chain )
6057
61- except Exception as err :
62- results .append ("Error: " + str (err ))
63- return results
64-
65- try :
58+ # Build OCSP request
6659 ocsp_request = build_ocsp_request (cert_chain )
6760
68- except Exception as err :
69- results .append ("Error: " + str (err ))
70- return results
71-
72- try :
61+ # Send OCSP request to responder and get result
7362 ocsp_response = get_ocsp_response (ocsp_url , ocsp_request )
7463
75- except Exception as err :
76- results .append ("Error: " + str (err ))
77- return results
78-
79- try :
64+ # Extract OCSP result from OCSP response
8065 ocsp_result = extract_ocsp_result (ocsp_response )
8166
8267 except Exception as err :
@@ -91,10 +76,11 @@ def get_ocsp_status(host: str, port: Any = None) -> list:
9176
9277def get_certificate_chain (host : str , port : int ) -> List [str ]:
9378
94- """ Connect to the host on the port and obtain certificate chain.
95- TODO: Tests against WantReadError and WantX509LookupError needed. """
79+ """ Connect to the host on the port and obtain certificate chain """
80+
81+ func_name : str = "get_certificate_chain"
9682
97- cert_chain = []
83+ cert_chain : list = []
9884
9985 soc = socket (AF_INET , SOCK_STREAM , proto = 0 )
10086 soc .settimeout (3 )
@@ -103,42 +89,49 @@ def get_certificate_chain(host: str, port: int) -> List[str]:
10389 soc .connect ((host , port ))
10490
10591 except gaierror :
106- raise Exception (f"{ host } :{ port } is invalid or not known." ) from None
92+ raise Exception (f"{ func_name } : { host } :{ port } is invalid or not known." ) from None
10793
10894 except timeout :
109- raise Exception (f"Connection to { host } :{ port } timed out." ) from None
95+ raise Exception (f"{ func_name } : Connection to { host } :{ port } timed out." ) from None
11096
111- except OverflowError :
112- raise Exception (f"Illegal port: { port } . Port must be between 0-65535." ) from None
97+ except ( OverflowError , TypeError ) :
98+ raise Exception (f"{ func_name } : Illegal port: { port } . Port must be between 0-65535." ) from None
11399
114- except TypeError :
115- raise Exception (f"Illegal port: { port } . Port must be between 0-65535 ." ) from None
100+ except ConnectionRefusedError :
101+ raise Exception (f"{ func_name } : Connection to { host } : { port } refused ." ) from None
116102
117103 ssl_client = SslClient (
118104 ssl_version = OpenSslVersionEnum .SSLV23 ,
119105 underlying_socket = soc ,
120106 ssl_verify = OpenSslVerifyEnum .NONE
121107 )
122108
123- # Add Server Name Indication (SNI) extension to the CLIENT HELLO
109+ # Add Server Name Indication (SNI) extension to the Client Hello
124110 ssl_client .set_tlsext_host_name (host )
125111
126112 try :
127113 ssl_client .do_handshake ()
128114 cert_chain = ssl_client .get_received_chain ()
129115
116+ except IOError as err :
117+ raise ValueError (f"{ func_name } : { host } did not respond to the Client Hello." ) from None
118+
130119 except WantReadError as err :
131- raise ValueError (err .strerror ) from None
120+ raise ValueError (f" { func_name } : err.strerror" ) from None
132121
133122 except WantX509LookupError as err :
134- raise ValueError (err .strerror ) from None
123+ raise ValueError (f" { func_name } : err.strerror" ) from None
135124
136125 except OpenSSLError as err :
137- raise ValueError (err ) from None
126+ if "1408F10B" in err .args [0 ]:
127+ # https://github.com/openssl/openssl/issues/6805
128+ raise ValueError (f"{ func_name } : Remote host is not using SSL/TLS on port: { port } " ) from None
129+
130+ raise ValueError (f"{ func_name } : err" ) from None
138131
139132 finally :
133+ # shutdown() will also close the underlying socket
140134 ssl_client .shutdown ()
141- soc = None
142135
143136 return cert_chain
144137
@@ -149,7 +142,9 @@ def extract_ocsp_url(cert_chain: List[str]) -> str:
149142 access location AUTHORITY_INFORMATION_ACCESS extensions to
150143 get the ocsp url """
151144
152- ocsp_url = ""
145+ func_name : str = "extract_ocsp_url"
146+
147+ ocsp_url : str = ""
153148
154149 # Convert to a certificate object in cryptography.io
155150 certificate = x509 .load_pem_x509_certificate (
@@ -167,9 +162,12 @@ def extract_ocsp_url(cert_chain: List[str]) -> str:
167162 if aia_extensions [index ].access_method ._name == "OCSP" :
168163 ocsp_url = aia_extensions [index ].access_location .value
169164
165+ if ocsp_url == "" :
166+ raise ValueError (f"{ func_name } : OCSP URL missing from Certificate AIA Extension." )
167+
170168 except ExtensionNotFound :
171169 raise ValueError (
172- " Certificate Authority Information Access (AIA) Extension Missing. Possible MITM Proxy."
170+ f" { func_name } : Certificate Authority Information Access (AIA) Extension Missing. Possible MITM Proxy."
173171 ) from None
174172
175173 return ocsp_url
@@ -181,6 +179,8 @@ def build_ocsp_request(cert_chain: List[str]) -> bytes:
181179 see: https://cryptography.io/en/latest/x509/ocsp/#cryptography.x509.ocsp.OCSPRequestBuilder
182180 for more information """
183181
182+ func_name : str = "build_ocsp_request"
183+
184184 try :
185185 leaf_cert = x509 .load_pem_x509_certificate (
186186 str .encode (cert_chain [0 ]), default_backend ()
@@ -190,7 +190,7 @@ def build_ocsp_request(cert_chain: List[str]) -> bytes:
190190 )
191191
192192 except ValueError :
193- raise Exception (" Unable to load x509 certificate." ) from None
193+ raise Exception (f" { func_name } : Unable to load x509 certificate." ) from None
194194
195195 # Build OCSP request
196196 builder = ocsp .OCSPRequestBuilder ()
@@ -205,9 +205,11 @@ def get_ocsp_response(ocsp_url: str, ocsp_request_data: bytes):
205205
206206 """ Send OCSP request to ocsp responder and retrieve response """
207207
208+ func_name : str = "get_ocsp_response"
209+
208210 # Confirm that the ocsp_url is a valid url
209211 if not url (ocsp_url ):
210- raise Exception (f"URL failed validation for { ocsp_url } " )
212+ raise Exception (f"{ func_name } : URL failed validation for { ocsp_url } " )
211213
212214 try :
213215 ocsp_response = requests .post (
@@ -218,13 +220,13 @@ def get_ocsp_response(ocsp_url: str, ocsp_request_data: bytes):
218220 )
219221
220222 except requests .exceptions .Timeout :
221- raise Exception (f"Request timeout for { ocsp_url } " ) from None
223+ raise Exception (f"{ func_name } : Request timeout for { ocsp_url } " ) from None
222224
223225 except requests .exceptions .ConnectionError :
224- raise Exception (f"Unknown Connection Error to { ocsp_url } " ) from None
226+ raise Exception (f"{ func_name } : Unknown Connection Error to { ocsp_url } " ) from None
225227
226228 except requests .exceptions .RequestException :
227- raise Exception (f"Unknown Connection Error to { ocsp_url } " ) from None
229+ raise Exception (f"{ func_name } : Unknown Connection Error to { ocsp_url } " ) from None
228230
229231 return ocsp_response
230232
@@ -233,6 +235,8 @@ def extract_ocsp_result(ocsp_response):
233235
234236 """ Extract the OCSP result from the provided ocsp_response """
235237
238+ func_name : str = "extract_ocsp_result"
239+
236240 try :
237241 ocsp_response = ocsp .load_der_ocsp_response (ocsp_response .content )
238242 # OCSP Response Status here:
@@ -248,14 +252,14 @@ def extract_ocsp_result(ocsp_response):
248252 # UNAUTHORIZED = 6
249253 ocsp_response = str (ocsp_response .response_status )
250254 ocsp_response = ocsp_response .split ("." )
251- raise Exception (f"OCSP Request Error: { ocsp_response [1 ]} " )
255+ raise Exception (f"{ func_name } : OCSP Request Error: { ocsp_response [1 ]} " )
252256
253257 certificate_status = str (ocsp_response .certificate_status )
254258 certificate_status = certificate_status .split ("." )
255259 return f"OCSP Status: { certificate_status [1 ]} "
256260
257261 except ValueError as err :
258- return f"{ str (err )} "
262+ return f"{ func_name } : { str (err )} "
259263
260264
261265def verify_port (port : Any ) -> int :
0 commit comments