|
19 | 19 | import socket |
20 | 20 | import time |
21 | 21 | import timeit |
22 | | -import getopt |
| 22 | +import hashlib |
23 | 23 | from tempfile import mkstemp |
| 24 | + |
| 25 | +from tlslite.x509 import DelegatedCredential, Credential |
24 | 26 | try: |
25 | 27 | from BaseHTTPServer import HTTPServer |
26 | 28 | from SimpleHTTPServer import SimpleHTTPRequestHandler |
27 | 29 | except ImportError: |
28 | 30 | from http.server import HTTPServer, SimpleHTTPRequestHandler |
29 | 31 |
|
30 | 32 | from tlslite import TLSConnection, Fault, HandshakeSettings, \ |
31 | | - X509, X509CertChain, IMAP4_TLS, VerifierDB, Session, SessionCache, \ |
| 33 | + X509, X509CertChain, IMAP4_TLS, VerifierDB, SessionCache, \ |
32 | 34 | parsePEMKey, constants, \ |
33 | 35 | AlertDescription, HTTPTLSConnection, TLSSocketServerMixIn, \ |
34 | 36 | POP3_TLS, m2cryptoLoaded, pycryptoLoaded, gmpyLoaded, tackpyLoaded, \ |
35 | 37 | Checker, __version__ |
36 | | -from tlslite.handshakesettings import VirtualHost, Keypair |
| 38 | +from tlslite.handshakesettings import VirtualHost, Keypair, DC_VALID_TIME |
37 | 39 |
|
38 | 40 | from tlslite.errors import * |
39 | | -from tlslite.utils.cryptomath import prngName, getRandomBytes |
| 41 | +from tlslite.utils.cryptomath import prngName, getRandomBytes, \ |
| 42 | + numberToByteArray |
40 | 43 | try: |
41 | 44 | import xmlrpclib |
42 | 45 | except ImportError: |
43 | 46 | # Python 3 |
44 | 47 | from xmlrpc import client as xmlrpclib |
45 | 48 | import ssl |
46 | 49 | from tlslite import * |
47 | | -from tlslite.constants import KeyUpdateMessageType, ECPointFormat, SignatureScheme |
| 50 | +from tlslite.constants import TLS_1_3_BRAINPOOL_SIG_SCHEMES, \ |
| 51 | + HashAlgorithm, KeyUpdateMessageType, ECPointFormat, \ |
| 52 | + SignatureAlgorithm, SignatureScheme |
| 53 | +from tlslite.utils.pem import dePem |
| 54 | +from tlslite.utils.codec import Parser |
48 | 55 |
|
49 | 56 | try: |
50 | 57 | from tack.structures.Tack import Tack |
@@ -1910,6 +1917,84 @@ def heartbeat_response_check(message): |
1910 | 1917 |
|
1911 | 1918 | test_no += 1 |
1912 | 1919 |
|
| 1920 | + print("Test {0} - Delegated Credential test: RSA cert".format(test_no)) |
| 1921 | + synchro.recv(1) |
| 1922 | + connection = connect() |
| 1923 | + settings = HandshakeSettings() |
| 1924 | + settings.maxVersion = (3, 4) |
| 1925 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 1926 | + connection.handshakeClientCert(settings=settings) |
| 1927 | + assert connection.session.delegated_credential is not None |
| 1928 | + assert isinstance(connection.session.delegated_credential, |
| 1929 | + DelegatedCredential) |
| 1930 | + assert connection.session.delegated_credential.algorithm == SignatureScheme.rsa_pss_pss_sha256 |
| 1931 | + testConnClient(connection) |
| 1932 | + connection.close() |
| 1933 | + |
| 1934 | + test_no += 1 |
| 1935 | + |
| 1936 | + print("Test {0} - Delegated Credential test: ECDSA cert".format(test_no)) |
| 1937 | + synchro.recv(1) |
| 1938 | + connection = connect() |
| 1939 | + settings = HandshakeSettings() |
| 1940 | + settings.maxVersion = (3, 4) |
| 1941 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 1942 | + connection.handshakeClientCert(settings=settings) |
| 1943 | + assert connection.session.delegated_credential is not None |
| 1944 | + assert isinstance(connection.session.delegated_credential, |
| 1945 | + DelegatedCredential) |
| 1946 | + assert connection.session.delegated_credential.algorithm == SignatureScheme.ecdsa_secp256r1_sha256 |
| 1947 | + testConnClient(connection) |
| 1948 | + connection.close() |
| 1949 | + |
| 1950 | + test_no += 1 |
| 1951 | + |
| 1952 | + print("Test {0} - Delegated Credential test: Ed2551 cert".format(test_no)) |
| 1953 | + synchro.recv(1) |
| 1954 | + connection = connect() |
| 1955 | + settings = HandshakeSettings() |
| 1956 | + settings.maxVersion = (3, 4) |
| 1957 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 1958 | + connection.handshakeClientCert(settings=settings) |
| 1959 | + assert connection.session.delegated_credential is not None |
| 1960 | + assert isinstance(connection.session.delegated_credential, |
| 1961 | + DelegatedCredential) |
| 1962 | + assert connection.session.delegated_credential.algorithm == SignatureScheme.ed25519 |
| 1963 | + testConnClient(connection) |
| 1964 | + connection.close() |
| 1965 | + |
| 1966 | + test_no += 1 |
| 1967 | + |
| 1968 | + print("Test {0} - Delegated Credential test: brainpoolP256r1tls13 cert".format(test_no)) |
| 1969 | + synchro.recv(1) |
| 1970 | + connection = connect() |
| 1971 | + settings = HandshakeSettings() |
| 1972 | + settings.maxVersion = (3, 4) |
| 1973 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 1974 | + connection.handshakeClientCert(settings=settings) |
| 1975 | + assert connection.session.delegated_credential is not None |
| 1976 | + assert isinstance(connection.session.delegated_credential, |
| 1977 | + DelegatedCredential) |
| 1978 | + assert connection.session.delegated_credential.algorithm == SignatureScheme.ecdsa_brainpoolP256r1tls13_sha256 |
| 1979 | + testConnClient(connection) |
| 1980 | + connection.close() |
| 1981 | + |
| 1982 | + test_no += 1 |
| 1983 | + |
| 1984 | + print("Test {0} - good X.509 TLSv1.3, no DC on client side)".format(test_no)) |
| 1985 | + synchro.recv(1) |
| 1986 | + settings = HandshakeSettings() |
| 1987 | + settings.certificate_compression_receive = [] |
| 1988 | + settings.certificate_compression_send = [] |
| 1989 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 1990 | + connection = connect() |
| 1991 | + connection.handshakeClientCert(serverName=address[0], |
| 1992 | + settings=settings) |
| 1993 | + testConnClient(connection) |
| 1994 | + assert connection.server_cert_compression_algo is None |
| 1995 | + assert connection.client_cert_compression_algo is None |
| 1996 | + connection.close() |
| 1997 | + |
1913 | 1998 | print('Test {0} - good standard XMLRPC https client'.format(test_no)) |
1914 | 1999 | address = address[0], address[1]+1 |
1915 | 2000 | synchro.recv(1) |
@@ -1967,6 +2052,8 @@ def heartbeat_response_check(message): |
1967 | 2052 | print("Non-critical error: socket error trying to reach internet " |
1968 | 2053 | "server: ", e) |
1969 | 2054 |
|
| 2055 | + |
| 2056 | + |
1970 | 2057 | synchro.close() |
1971 | 2058 |
|
1972 | 2059 | if not badFault: |
@@ -2119,6 +2206,12 @@ def connect(): |
2119 | 2206 | with open(os.path.join(dir, "serverEd448Key.pem")) as f: |
2120 | 2207 | x509Ed448Key = parsePEMKey(f.read(), private=True, |
2121 | 2208 | implementations=["python"]) |
| 2209 | + with open(os.path.join(dir, "serverX509DCKey.pem")) as f: |
| 2210 | + x509DCKey = parsePEMKey(f.read(), private=True, |
| 2211 | + implementations=["python"]) |
| 2212 | + |
| 2213 | + with open(os.path.join(dir, "serverX509DCPub.pem")) as f: |
| 2214 | + X509DCPub = dePem(f.read(), "PUBLIC KEY") |
2122 | 2215 |
|
2123 | 2216 | test_no = 0 |
2124 | 2217 |
|
@@ -3655,6 +3748,106 @@ def heartbeat_response_check(message): |
3655 | 3748 |
|
3656 | 3749 | test_no +=1 |
3657 | 3750 |
|
| 3751 | + print("Test {0}-{1} - Delegated Credential test".format(test_no, test_no + 3)) |
| 3752 | + cert_alg = [(x509Chain, x509Key, SignatureScheme.rsa_pss_pss_sha256), |
| 3753 | + (x509ecdsaChain, x509ecdsaKey, SignatureScheme.ecdsa_secp256r1_sha256), |
| 3754 | + (x509Ed25519Chain, x509Ed25519Key, SignatureScheme.ed25519), |
| 3755 | + (x509ecdsaBrainpoolP256r1Chain, |
| 3756 | + x509ecdsaBrainpoolP256r1Key, |
| 3757 | + SignatureScheme.ecdsa_brainpoolP256r1tls13_sha256) |
| 3758 | + ] |
| 3759 | + for value in cert_alg: |
| 3760 | + synchro.send(b'R') |
| 3761 | + connection = connect() |
| 3762 | + |
| 3763 | + cert_chain, private_key, sig_alg = value |
| 3764 | + scheme = SignatureScheme.toRepr(sig_alg) |
| 3765 | + dc_sig_alg = SignatureScheme.rsa_pss_pss_sha256 |
| 3766 | + |
| 3767 | + cert_bytes = cert_chain.x509List[0].bytes |
| 3768 | + valid_time = int(time.time()) + DC_VALID_TIME |
| 3769 | + cred_bytes = bytearray(numberToByteArray(valid_time) + |
| 3770 | + numberToByteArray(dc_sig_alg[0]) + |
| 3771 | + numberToByteArray(dc_sig_alg[1]) + |
| 3772 | + X509DCPub) |
| 3773 | + cred = Credential(valid_time=valid_time, |
| 3774 | + dc_cert_verify_algorithm=dc_sig_alg, |
| 3775 | + subject_public_key_info=X509DCPub, |
| 3776 | + bytes=cred_bytes) |
| 3777 | + |
| 3778 | + bytes_to_sign = DelegatedCredential.compute_certificate_dc_sig_context( |
| 3779 | + cert_bytes, |
| 3780 | + cred_bytes, |
| 3781 | + sig_alg) |
| 3782 | + |
| 3783 | + if sig_alg in (SignatureScheme.ed25519, |
| 3784 | + SignatureScheme.ed448): |
| 3785 | + hashName = "intrinsic" |
| 3786 | + padType = None |
| 3787 | + saltLen = None |
| 3788 | + sig_func = private_key.hashAndSign |
| 3789 | + ver_func = private_key.hashAndVerify |
| 3790 | + elif sig_alg[1] == SignatureAlgorithm.ecdsa: |
| 3791 | + hashName = HashAlgorithm.toRepr(sig_alg[0]) |
| 3792 | + padType = None |
| 3793 | + saltLen = None |
| 3794 | + sig_func = private_key.hashAndSign |
| 3795 | + ver_func = private_key.hashAndVerify |
| 3796 | + elif sig_alg in TLS_1_3_BRAINPOOL_SIG_SCHEMES: |
| 3797 | + hashName = SignatureScheme.getHash(scheme) |
| 3798 | + padType = None |
| 3799 | + saltLen = None |
| 3800 | + sig_func = private_key.hashAndSign |
| 3801 | + ver_func = private_key.hashAndVerify |
| 3802 | + else: |
| 3803 | + padType = SignatureScheme.getPadding(scheme) |
| 3804 | + hashName = SignatureScheme.getHash(scheme) |
| 3805 | + saltLen = getattr(hashlib, hashName)().digest_size |
| 3806 | + sig_func = private_key.hashAndSign |
| 3807 | + ver_func = private_key.hashAndVerify |
| 3808 | + |
| 3809 | + signature = sig_func(bytes_to_sign, |
| 3810 | + padType, |
| 3811 | + hashName, |
| 3812 | + saltLen) |
| 3813 | + if not ver_func(signature, bytes_to_sign, |
| 3814 | + padType, |
| 3815 | + hashName, |
| 3816 | + saltLen): |
| 3817 | + raise ValueError("Delegated Credential signature failed") |
| 3818 | + |
| 3819 | + |
| 3820 | + delegated_credential = DelegatedCredential(cred=cred, |
| 3821 | + algorithm=sig_alg, |
| 3822 | + signature=signature) |
| 3823 | + |
| 3824 | + settings = HandshakeSettings() |
| 3825 | + settings.maxVersion = (3, 4) |
| 3826 | + settings.dc_sig_algs = [SignatureScheme.rsa_pss_pss_sha256] |
| 3827 | + connection.handshakeServer(certChain=cert_chain, |
| 3828 | + privateKey=None, |
| 3829 | + dc_key=x509DCKey, |
| 3830 | + del_cred=delegated_credential, |
| 3831 | + settings=settings) |
| 3832 | + assert connection.session.delegated_credential is not None |
| 3833 | + assert isinstance(connection.session.delegated_credential, |
| 3834 | + DelegatedCredential) |
| 3835 | + testConnServer(connection) |
| 3836 | + connection.close() |
| 3837 | + |
| 3838 | + test_no += 4 |
| 3839 | + |
| 3840 | + print("Test {0} - good X.509 TLSv1.3 (no DC on client side)".format(test_no)) |
| 3841 | + synchro.send(b'R') |
| 3842 | + connection = connect() |
| 3843 | + connection.handshakeServer(certChain=x509Chain, privateKey=x509Key) |
| 3844 | + assert connection.server_cert_compression_algo is None |
| 3845 | + assert connection.client_cert_compression_algo is None |
| 3846 | + testConnServer(connection) |
| 3847 | + connection.close() |
| 3848 | + |
| 3849 | + test_no += 1 |
| 3850 | + |
3658 | 3851 | print("Tests {0}-{1} - XMLRPXC server".format(test_no, test_no + 2)) |
3659 | 3852 |
|
3660 | 3853 | address = address[0], address[1]+1 |
|
0 commit comments