-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathocsptest.py
61 lines (50 loc) · 2 KB
/
ocsptest.py
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
# Copyright 2020-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
from __future__ import annotations
import argparse
import logging
import socket
from pymongo.pyopenssl_context import SSLContext
from pymongo.ssl_support import get_ssl_context
# Enable logs in this format:
# 2020-06-08 23:49:35,982 DEBUG ocsp_support Peer did not staple an OCSP response
FORMAT = "%(asctime)s %(levelname)s %(module)s %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
def check_ocsp(host: str, port: int, capath: str) -> None:
ctx = get_ssl_context(
None, # certfile
None, # passphrase
capath, # ca_certs
None, # crlfile
False, # allow_invalid_certificates
False, # allow_invalid_hostnames
False,
) # disable_ocsp_endpoint_check
# Ensure we're using pyOpenSSL.
assert isinstance(ctx, SSLContext)
s = socket.socket()
s.connect((host, port))
try:
s = ctx.wrap_socket(s, server_hostname=host) # type: ignore[assignment]
finally:
s.close()
def main() -> None:
parser = argparse.ArgumentParser(description="Debug OCSP")
parser.add_argument("--host", type=str, required=True, help="Host to connect to")
parser.add_argument("-p", "--port", type=int, default=443, help="Port to connect to")
parser.add_argument("--ca_file", type=str, default=None, help="CA file for host")
args = parser.parse_args()
check_ocsp(args.host, args.port, args.ca_file)
if __name__ == "__main__":
main()