import json from calendar import timegm from datetime import datetime, UTC, timedelta from cryptography import x509 from cryptography.hazmat._oid import NameOID from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_pem_public_key, Encoding from dateutil.relativedelta import relativedelta from jose import jwk, jws from jose.constants import ALGORITHMS ### DEFAULTS INSTANCE_SITE_ID = '4e53a171-103b-4946-9ed8-5f4c0ee750d9' INSTANCE_KEY_RSA = """-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAyIz6i48cFn4XOK0S2GTYpLMU85xzJ1fmQmA2nC6Zod2V4BxN Xqk+9y8nvdzZVELxmC+N47ZQGV/J5cquIadx0V42F3JTryJFDuZ+7fQsNlXUX3og UQhhgvHuluhDJQSvdZAzpguS7N+gJGCGbGk1pZBYL2JtTDTWSIcWsQtD/w9DAPEk K5cHGoZkovngH1LOTkVAcyEqKxLblerMnLu3rOaDVkEcf+1l2BwvHUWTU4LI6uud CWP2em69T2EXp1qczi5IKJzc53puNfp5nXlHayrneYAdIbEAQSQg+Z40npUwNKW7 1Ue2NsG3SoGWuj3lTyEVXlLsAw0bsCDVLipMWwIDAQABAoIBAA24FyuU221ueSHK m49ro3Mg2dep/10ICYH6f8HcLjmBPwKlucucwehTtK3esPJ8SEQ9r8DA2zN6w56R aHgRsrRWQL0Gq4YMTuascRWce0NirueyvKM02SoFnGl8wGfrE7wNfhSalhWkkDMg DaujRtg2MTiMmY15z9U5gh16XjYEZipOmfmNWHLCMRnEsXV/ToZ+g7ekruKPweGD A52tG0pN/KYVaQFX+sZ7eXpd1jEl4gCSHup1SopAM3+is0DzeHiLSg9ZUN9dd8af L+SYAOCjRHXkUKcQN8a3FffoPInjy4D57dndkqRusiCtRJV3TEjO4Ld9cj/fqQZ2 kDMT0YECgYEA0iPJu73Kz3wruxGfzq2zTE8RMM/EF8OAXi/qzKer40w7KLZ4aY0c 5FUJwehipYM390/OUf0x3jiwMVk3wR6M9/L9h70zN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ngH1LOTkVAcyEqKxLblerMnLu3 rOaDVkEcf+1l2BwvHUWTU4LI6uudCWP2em69T2EXp1qczi5IKJzc53puNfp5nXlH ayrneYAdIbEAQSQg+Z40npUwNKW71Ue2NsG3SoGWuj3lTyEVXlLsAw0bsCDVLipM WwIDAQAB -----END PUBLIC KEY----- """ CLIENT_TOKEN_EXPIRE_DELTA = relativedelta(years=12) CONFIG_TOKEN_RESPONSE = { "certificateConfiguration": { "caChain": [ "-----BEGIN CERTIFICATE-----\r\nMIIF3TCCA8WgAwIBAgIUCpVszfecRrnPa3EGwPKuyWESBmMwDQYJKoZIhvcNAQELBQAwcjELMAkG\r\nA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExDzANBgNVBAoTBk52aWRpYTEnMCUGA1UECxMe\r\nTnZpZGlhIExpY2Vuc2luZyBTZXJ2aWNlIChOTFMpMRQwEgYDVQQDEwtOTFMgUm9vdCBDQTAeFw0y\r\nNDA5MjYwNzM4MTlaFw0zNDA5MjQwNzM4NDlaMHoxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxp\r\nZm9ybmlhMQ8wDQYDVQQKEwZOdmlkaWExJzAlBgNVBAsTHk52aWRpYSBMaWNlbnNpbmcgU2Vydmlj\r\nZSAoTkxTKTEcMBoGA1UEAxMTTkxTIEludGVybWVkaWF0ZSBDQTCCAiIwDQYJKoZIhvcNAQEBBQAD\r\nggIPADCCAgoCggIBAOIb5ZcYWR78WkJipEW4cOB2d3WkXhjzA9Omj0SBnA6fJad+zObguInmkgyB\r\nUC/0xMnHeEH1WQpZ0yZE1rdH0ziwPy07hmCgjMSC8iXSfV4QXoHzsQy80HSbD3dr0A5Fk9UrWdJu\r\nIlLnwqTfUjxMSqiVYbGI2JLVLDIPjnrCKgZ//vVTFWiMDQaGInDz5Qo3azHIt1Sw3u47/b88TzmK\r\ni3TMbjtAR3djlhQfJBY6nUdP8wWy2Fntx9fO7U723sp6cnGtHnbXGpon/QqxlPjT4RXXm1Q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420fd80dd0d531bf96a9103c562da51e72e82f39ba77775842ae45245ddbbb3a06f61993105ea0af045bd95fb4b6645100f57855da12f31d83152f3dddb159d93ff478fab177a8f631e2b96426af975abff2ea43bdde8728a85908405d107d9fad0fac9dc9f65d4804a18e739597376cd9f99a14724918ba3f4acbe3727b95a49faa29aa320fe74ebd355a602ac235c236709eac8123e9d715abb7d1eedb596bd2d1f99e8bb877058f76572217a21fae79871dd6f03a56c2cf84d2fede21a41143fdb0cd3555286eef8a4472511972a5ac193e51dc747699a4519f3458e15" ] } }, "configToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJOTFMgU2VydmljZSBJbnN0YW5jZSIsImF1ZCI6Ik5MUyBMaWNlbnNlZCBDbGllbnQiLCJpYXQiOjE3NDIxOTU4ODUsIm5iZiI6MTc0MjE5NTg4NSwiZXhwIjoxNzQyMTk2Nzg1LCJwcm90b2NvbF92ZXJzaW9uIjoiMi4wIiwiZF9uYW1lIjoiRExTIiwic2VydmljZV9pbnN0YW5jZV9yZWYiOiI0ZTUzYTE3MS0xMDNiLTQ5NDYtOWVkOC01ZjRjMGVlNzUwZDkiLCJzZXJ2aWNlX2luc3RhbmNlX3B1YmxpY19rZXlfY29uZmlndXJhdGlvbiI6eyJzZXJ2aWNlX2luc3RhbmNlX3B1YmxpY19rZXlfbWUiOnsibW9kIjoiYzg4Y2ZhOGI4ZjFjMTY3ZTE3MzhhZDEyZDg2NGQ4YTRiMzE0ZjM5YzczMjc1N2U2NDI2MDM2OWMyZTk5YTFkZDk1ZTAxYzRkNWVhOTNlZjcyZjI3YmRkY2Q5NTQ0MmYxOTgyZjhkZTNiNjUwMTk1ZmM5ZTVjYWFlMjFhNzcxZDE1ZTM2MTc3MjUzYWYyMjQ1MGVlNjdlZWRmNDJjMzY1NWQ0NWY3YTIwNTEwODYxODJmMWVlOTZlODQzMjUwNGFmNzU5MDMzYTYwYjkyZWNkZmEwMjQ2MDg2NmM2OTM1YTU5MDU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S0tLS1FTkQgUFVCTElDIEtFWS0tLS0tIiwia2V5X3JldGVudGlvbl9tb2RlIjoiTEFURVNUX09OTFkifX0.mZnB0NnE4WIxg0Q6zH98NSt6UYLbtbNPfWsVVJwtcn8rv9mFWmBr3vxB9BGzafY-kLo5uJztI_Ue3Q1VD5yHaMVtgrtI8K5-Ojm3d8p5hGe2qwoskQ1OXSSTE15JaGsdkTihPnZ-0r6RBRucXa-PoNWBYnvl0SbcNiU_-FAK1ugmemLW3Q9T2KZ71n8nr0-FXrJUpsxrluUEerhtDlOZmNHuCEcsU10U0ZdMPBc3iguy_psd_jR4QIDRwc6W0dp29403epDJqqkUW8c-ORi1Ny2Bk2OFNK87VBxf5GP5KxuYxlujtzf0Y1niDqnYdW1MpKl9OxRvz0E4HUEK7_JBbA" } ### VARIABLES instance_private_key = load_pem_private_key(INSTANCE_KEY_RSA.encode('utf-8'), password=None) instance_private_key_pem = instance_private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption() ) instance_public_key = load_pem_public_key(INSTANCE_KEY_PUB.encode('utf-8')) instance_public_key_pem = instance_public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) instance_public_key_exp = instance_public_key.public_numbers().e instance_public_key_mod = f'{instance_public_key.public_numbers().n:x}' # hex value without "0x" prefix ### TESTS def test_private_public_key_equals_public_key(): """ test that the public-key exported from private-key is the same as the public-key :return: """ public_key_from_private_key_as_pem = instance_private_key.public_key().public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) public_key_as_pem = instance_public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) assert public_key_as_pem == public_key_from_private_key_as_pem def test_official_cert(): """ test the right certificate :return: """ response_certificate_configuration = CONFIG_TOKEN_RESPONSE.get('certificateConfiguration') response_public_cert = response_certificate_configuration.get('publicCert').encode('utf-8') response_public_key = response_certificate_configuration.get('publicKey') response_parsed_cert = x509.load_pem_x509_certificate(response_public_cert, default_backend()) response_parsed_cert_exp = response_parsed_cert.public_key().public_numbers().e response_parsed_cert_mod = f'{response_parsed_cert.public_key().public_numbers().n:x}' # hex value without "0x" prefix assert response_parsed_cert_exp == response_public_key.get('exp') assert response_parsed_cert_mod == response_public_key.get('mod')[0] issuer = f'CN=NLS Intermediate CA,OU=Nvidia Licensing Service (NLS),O=Nvidia,ST=California,C=US' subject = f'CN={INSTANCE_SITE_ID}' assert response_parsed_cert.issuer.rfc4514_string() == issuer assert response_parsed_cert.subject.rfc4514_string() == subject def test_official_config_token_response(): """ test config-token response and test jwt-signing :return: """ response_certificate_configuration = CONFIG_TOKEN_RESPONSE.get('certificateConfiguration') response_public_cert = response_certificate_configuration.get('publicCert').encode('utf-8') jwt_decode_key = jwk.construct(response_public_cert, algorithm=ALGORITHMS.RS256) response_config_token = CONFIG_TOKEN_RESPONSE.get('configToken') payload = jws.verify(response_config_token, key=jwt_decode_key, algorithms=ALGORITHMS.RS256) payload = json.loads(payload) assert payload.get('iss') == 'NLS Service Instance' assert payload.get('aud') == 'NLS Licensed Client' assert payload.get('service_instance_ref') == INSTANCE_SITE_ID service_instance_public_key_configuration = payload.get('service_instance_public_key_configuration') service_instance_public_key_me = service_instance_public_key_configuration.get('service_instance_public_key_me') assert service_instance_public_key_me.get('mod') == instance_public_key_mod assert service_instance_public_key_me.get('exp') == instance_public_key_exp def test_our_config_token(): """ test our config-token and test jwt-signing :return: """ response_certificate_configuration = CONFIG_TOKEN_RESPONSE.get('certificateConfiguration') response_public_cert = response_certificate_configuration.get('publicCert').encode('utf-8') response_parsed_cert = x509.load_pem_x509_certificate(response_public_cert, default_backend()) jwt_decode_key = jwk.construct(response_parsed_cert, algorithm=ALGORITHMS.RS256) ### build payload cur_time = datetime.now(UTC) exp_time = cur_time + CLIENT_TOKEN_EXPIRE_DELTA payload = { "iss": "NLS Service Instance", "aud": "NLS Licensed Client", "iat": timegm(cur_time.timetuple()), "nbf": timegm(cur_time.timetuple()), "exp": timegm(exp_time.timetuple()), "protocol_version": "2.0", "d_name": "DLS", "service_instance_ref": INSTANCE_SITE_ID, "service_instance_public_key_configuration": { "service_instance_public_key_me": { "mod": hex(instance_public_key.public_numbers().n)[2:], "exp": int(instance_public_key.public_numbers().e), }, "service_instance_public_key_pem": instance_public_key_pem.decode('utf-8'), "key_retention_mode": "LATEST_ONLY" }, } # todo: maybe DLS_SI_CERTIFICATE['private_key'] # sign_key = load_key(join(dirname(__file__), 'cert\\....pem')) # sign_key = jwk.construct(sign_key.export_key().decode('utf-8'), algorithm=ALGORITHMS.RS256) sign_key = jwk.construct(instance_private_key_pem.decode('utf-8'), algorithm=ALGORITHMS.RS256) # config_token = jws.sign(payload, key=sign_key, headers=None, algorithm=ALGORITHMS.RS256) # "jwt_encode_key" has invalid signature (can't be verified with DLS_SI_CERTIFICATE['certificate']) config_token = jws.sign(payload, key=sign_key, headers=None, algorithm=ALGORITHMS.RS256) response = { "certificateConfiguration": { "caChain": CONFIG_TOKEN_RESPONSE['certificateConfiguration']['caChain'], "publicCert": CONFIG_TOKEN_RESPONSE['certificateConfiguration']['publicCert'], "publicKey": { "exp": int(response_parsed_cert.public_key().public_numbers().e), "mod": [hex(response_parsed_cert.public_key().public_numbers().n)[2:]], }, }, "configToken": config_token, } ### our_config_token = response.get('configToken') payload = jws.verify(our_config_token, key=jwt_decode_key, algorithms=ALGORITHMS.RS256) # error! payload = json.loads(payload) assert payload.get('iss') == 'NLS Service Instance' assert payload.get('aud') == 'NLS Licensed Client' assert payload.get('service_instance_ref') == INSTANCE_SITE_ID service_instance_public_key_configuration = payload.get('service_instance_public_key_configuration') service_instance_public_key_me = service_instance_public_key_configuration.get('service_instance_public_key_me') assert service_instance_public_key_me.get('mod') == instance_public_key_mod assert service_instance_public_key_me.get('exp') == instance_public_key_exp def test_our_config_token_with_our_key(): """ test our config-token and test jwt-signing :return: """ # create ca-certificate subject ca_subject = x509.Name([ x509.NameAttribute(NameOID.COUNTRY_NAME, u"DE"), x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"Hessen"), x509.NameAttribute(NameOID.LOCALITY_NAME, u"Wiesbaden"), x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Ihr Unternehmen"), x509.NameAttribute(NameOID.COMMON_NAME, u"CA Root"), ]) # create self-signed ca-certificate ca_certificate = (x509.CertificateBuilder() .subject_name(ca_subject) .issuer_name(ca_subject) .public_key(instance_private_key.public_key()) .serial_number(x509.random_serial_number()) .not_valid_before(datetime.utcnow() - timedelta(days=1)) .not_valid_after(datetime.utcnow() + timedelta(days=365 * 10)) .add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True) .sign(instance_private_key, hashes.SHA256())) # create certificate subject subject = x509.Name([ x509.NameAttribute(NameOID.COUNTRY_NAME, u'US'), x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u'California'), x509.NameAttribute(NameOID.ORGANIZATION_NAME, u'Nvidia'), x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, u'Nvidia Licensing Service (NLS)'), x509.NameAttribute(NameOID.COMMON_NAME, u"NLS Intermediate CA"), ]) issuer_name = ca_subject if ca_subject is not None else subject # create self-signed certificate certificate = (x509.CertificateBuilder() .subject_name(subject) .issuer_name(issuer_name) .public_key(instance_private_key.public_key()) .serial_number(x509.random_serial_number()) .not_valid_before(datetime.utcnow() - timedelta(days=1)) .not_valid_after(datetime.utcnow() + timedelta(days=3650)) .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=False) .sign(instance_private_key, hashes.SHA256()) ) jwt_decode_key = jwk.construct(certificate.public_bytes(encoding=Encoding.PEM), algorithm=ALGORITHMS.RS256) ### build payload cur_time = datetime.now(UTC) exp_time = cur_time + CLIENT_TOKEN_EXPIRE_DELTA payload = { "iss": "NLS Service Instance", "aud": "NLS Licensed Client", "iat": timegm(cur_time.timetuple()), "nbf": timegm(cur_time.timetuple()), "exp": timegm(exp_time.timetuple()), "protocol_version": "2.0", "d_name": "DLS", "service_instance_ref": INSTANCE_SITE_ID, "service_instance_public_key_configuration": { "service_instance_public_key_me": { "mod": hex(instance_public_key.public_numbers().n)[2:], "exp": int(instance_public_key.public_numbers().e), }, "service_instance_public_key_pem": instance_public_key_pem.decode('utf-8'), "key_retention_mode": "LATEST_ONLY" }, } sign_key = jwk.construct(instance_private_key_pem.decode('utf-8'), algorithm=ALGORITHMS.RS256) config_token = jws.sign(payload, key=sign_key, headers=None, algorithm=ALGORITHMS.RS256) response = { "certificateConfiguration": { "caChain": [ca_certificate.public_bytes(encoding=Encoding.PEM)], "publicCert": certificate.public_bytes(encoding=Encoding.PEM), "publicKey": { "exp": int(certificate.public_key().public_numbers().e), "mod": [hex(certificate.public_key().public_numbers().n)[2:]], }, }, "configToken": config_token, } ### response_config_token = response.get('configToken') payload = jws.verify(response_config_token, key=jwt_decode_key, algorithms=ALGORITHMS.RS256) # error! payload = json.loads(payload) assert payload.get('iss') == 'NLS Service Instance' assert payload.get('aud') == 'NLS Licensed Client' assert payload.get('service_instance_ref') == INSTANCE_SITE_ID service_instance_public_key_configuration = payload.get('service_instance_public_key_configuration') service_instance_public_key_me = service_instance_public_key_configuration.get('service_instance_public_key_me') assert service_instance_public_key_me.get('mod') == instance_public_key_mod assert service_instance_public_key_me.get('exp') == instance_public_key_exp