|
1 | 1 | import ssl |
| 2 | +import sys |
2 | 3 | import urllib.request |
3 | 4 | import json |
4 | 5 | import os |
|
7 | 8 | # Example code testing oqs signature functionality. See more example code at |
8 | 9 | # https://github.com/open-quantum-safe/liboqs-python/tree/main/examples |
9 | 10 |
|
10 | | -message = "This is the message to sign".encode() |
| 11 | +message = b"This is the message to sign" |
11 | 12 |
|
12 | 13 | # create signer and verifier with sample signature mechanisms |
13 | 14 | sigalg = "Dilithium2" |
|
17 | 18 | signature = signer.sign(message) |
18 | 19 | is_valid = verifier.verify(message, signature, signer_public_key) |
19 | 20 |
|
20 | | -if (not is_valid): |
| 21 | +if not is_valid: |
21 | 22 | print("Failed to validate signature. Exiting.") |
22 | | - exit(1) |
| 23 | + sys.exit(1) |
23 | 24 | else: |
24 | 25 | print("Validated signature for OQS algorithm %s" % (sigalg)) |
25 | 26 |
|
26 | 27 | # Example code iterating over all supported OQS algorithms integrated into TLS |
27 | 28 |
|
28 | | -sslContext= ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 29 | +sslContext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
29 | 30 | sslContext.verify_mode = ssl.CERT_REQUIRED |
30 | 31 | # Trust LetsEncrypt root CA: |
31 | 32 | sslContext.load_verify_locations(cafile="isrgrootx1.pem") |
32 | 33 |
|
33 | 34 | # Retrieve interop test server root CA |
34 | | -with urllib.request.urlopen('https://test.openquantumsafe.org/CA.crt', context=sslContext) as response: |
35 | | - data=response.read() |
| 35 | +with urllib.request.urlopen( |
| 36 | + "https://test.openquantumsafe.org/CA.crt", context=sslContext |
| 37 | +) as response: |
| 38 | + data = response.read() |
36 | 39 | with open("CA.crt", "w+b") as f: |
37 | 40 | f.write(data) |
38 | 41 |
|
39 | 42 | # Retrieve JSON structure of all alg/port combinations: |
40 | | -with urllib.request.urlopen('https://test.openquantumsafe.org/assignments.json', context=sslContext) as response: |
41 | | - assignments=json.loads(response.read()) |
| 43 | +with urllib.request.urlopen( |
| 44 | + "https://test.openquantumsafe.org/assignments.json", context=sslContext |
| 45 | +) as response: |
| 46 | + assignments = json.loads(response.read()) |
42 | 47 |
|
43 | 48 | # Trust test.openquantumsafe.org root CA: |
44 | 49 | sslContext.load_verify_locations(cafile="CA.crt") |
45 | 50 |
|
46 | 51 | # Iterate over all algorithm/port combinations: |
47 | 52 | for sigs, kexs in assignments.items(): |
48 | 53 | for kex, port in kexs.items(): |
49 | | - if (kex != "*"): # '*' denoting any classic KEX alg |
| 54 | + if kex != "*": # '*' denoting any classic KEX alg |
50 | 55 | # Enable use of the specific QSC KEX algorithm |
51 | | - os.environ["TLS_DEFAULT_GROUPS"]=kex |
52 | | - try: |
53 | | - with urllib.request.urlopen('https://test.openquantumsafe.org:'+str(port), context=sslContext) as response: |
54 | | - if response.getcode() != 200: |
55 | | - print("Failed to test %s successfully" % (kex)) |
56 | | - else: |
57 | | - print("Success testing %s at port %d" % (kex, port)) |
58 | | - except: |
59 | | - print("Test of algorithm combination SIG %s/KEX %s failed. Are all algorithms supported by current OQS library?" % (sigs, kex)) |
| 56 | + os.environ["TLS_DEFAULT_GROUPS"] = kex |
| 57 | + try: |
| 58 | + with urllib.request.urlopen( |
| 59 | + "https://test.openquantumsafe.org:" + str(port), context=sslContext |
| 60 | + ) as response: |
| 61 | + if response.getcode() != 200: |
| 62 | + print("Failed to test %s successfully" % (kex)) |
| 63 | + else: |
| 64 | + print("Success testing %s at port %d" % (kex, port)) |
| 65 | + except: |
| 66 | + print( |
| 67 | + "Test of algorithm combination SIG %s/KEX %s failed. " |
| 68 | + "Are all algorithms supported by current OQS library?" |
| 69 | + % (sigs, kex) |
| 70 | + ) |
60 | 71 |
|
61 | 72 | if "SHORT_TEST" in os.environ: |
62 | | - exit(0) |
| 73 | + sys.exit(0) |
0 commit comments