def test_export_keystore(app): from lemur.plugins.base import plugins p = plugins.get('java-keystore-jks') options = [ {'name': 'passphrase', 'value': 'hunter2'}, {'name': 'alias', 'value': 'AzureDiamond'}, ] chain = INTERMEDIATE_CERT_STR + '\n' + ROOTCA_CERT_STR with pytest.raises(Exception): p.export(INTERNAL_CERTIFICATE_A_STR, chain, '', options) ext, password, raw = p.export(SAN_CERT_STR, chain, SAN_CERT_KEY, options) assert ext == 'jks' assert password == 'hunter2' assert isinstance(raw, bytes) ks = KeyStore.loads(raw, password) assert ks.store_type == 'jks' # JKS lower-cases alias strings assert ks.entries.keys() == {'azurediamond'} entry = ks.entries['azurediamond'] assert isinstance(entry, PrivateKeyEntry) assert len(entry.cert_chain) == 3 # Cert and chain were provided
def test_export_keystore(app): from lemur.plugins.base import plugins p = plugins.get("java-keystore-jks") options = [ { "name": "passphrase", "value": "hunter2" }, { "name": "alias", "value": "AzureDiamond" }, ] chain = INTERMEDIATE_CERT_STR + "\n" + ROOTCA_CERT_STR with pytest.raises(Exception): p.export(INTERNAL_CERTIFICATE_A_STR, chain, "", options) ext, password, raw = p.export(SAN_CERT_STR, chain, SAN_CERT_KEY, options) assert ext == "jks" assert password == "hunter2" assert isinstance(raw, bytes) ks = KeyStore.loads(raw, password) assert ks.store_type == "jks" # JKS lower-cases alias strings assert ks.entries.keys() == {"azurediamond"} entry = ks.entries["azurediamond"] assert isinstance(entry, PrivateKeyEntry) assert len(entry.cert_chain) == 3 # Cert and chain were provided
def test_export_truststore(app): from lemur.plugins.base import plugins p = plugins.get("java-truststore-jks") options = [ { "name": "passphrase", "value": "hunter2" }, { "name": "alias", "value": "AzureDiamond" }, ] chain = INTERMEDIATE_CERT_STR + "\n" + ROOTCA_CERT_STR ext, password, raw = p.export(SAN_CERT_STR, chain, SAN_CERT_KEY, options) assert ext == "jks" assert password == "hunter2" assert isinstance(raw, bytes) ks = KeyStore.loads(raw, "hunter2") assert ks.store_type == "jks" # JKS lower-cases alias strings assert ks.entries.keys() == { "azurediamond_cert", "azurediamond_cert_1", "azurediamond_cert_2", } assert isinstance(ks.entries["azurediamond_cert"], TrustedCertEntry)
def create_truststore(cert, chain, alias, passphrase): entries = [] for idx, cert_bytes in enumerate(cert_chain_as_der(cert, chain)): # The original cert gets name <ALIAS>_cert, first chain element is <ALIAS>_cert_1, etc. cert_alias = alias + "_cert" + ("_{}".format(idx) if idx else "") entries.append(TrustedCertEntry.new(cert_alias, cert_bytes)) return KeyStore.new("jks", entries).saves(passphrase)
def create_truststore(cert, chain, alias, passphrase): entries = [] for idx, cert_bytes in enumerate(cert_chain_as_der(cert, chain)): # The original cert gets name <ALIAS>_cert, first chain element is <ALIAS>_cert_1, etc. cert_alias = alias + '_cert' + ('_{}'.format(idx) if idx else '') entries.append(TrustedCertEntry.new(cert_alias, cert_bytes)) return KeyStore.new('jks', entries).saves(passphrase)
def get_private_key_from_jks(jks, alias, key_store_password): from jks import KeyStore key = next(_key for _key in KeyStore.load(jks, key_store_password).private_keys if _key.alias == alias) # Get the certificate and chain from the key with: key.cert_chain # Or all the certificates with: KeyStore.load(jks, key_store_password).certs return get_pem(key.pkey, 'RSA PRIVATE KEY')
def create_keystore(cert, chain, key, alias, passphrase): certs_bytes = cert_chain_as_der(cert, chain) key_bytes = parse_private_key(key).private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) entry = PrivateKeyEntry.new(alias, certs_bytes, key_bytes) return KeyStore.new('jks', [entry]).saves(passphrase)
def create_keystore(cert, chain, key, alias, passphrase): certs_bytes = cert_chain_as_der(cert, chain) key_bytes = parse_private_key(key).private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) entry = PrivateKeyEntry.new(alias, certs_bytes, key_bytes) return KeyStore.new('jks', [entry]).saves(passphrase)
def test_export_truststore_defaults(app): from lemur.plugins.base import plugins p = plugins.get('java-truststore-jks') options = [] ext, password, raw = p.export(INTERNAL_CERTIFICATE_A_STR, '', '', options) assert ext == 'jks' assert isinstance(password, str) assert isinstance(raw, bytes) ks = KeyStore.loads(raw, password) assert ks.store_type == 'jks' # JKS lower-cases alias strings assert ks.entries.keys() == {'acommonname_cert'} assert isinstance(ks.entries['acommonname_cert'], TrustedCertEntry)
def test_export_truststore(app): from lemur.plugins.base import plugins p = plugins.get('java-truststore-jks') options = [ {'name': 'passphrase', 'value': 'hunter2'}, {'name': 'alias', 'value': 'AzureDiamond'}, ] chain = INTERMEDIATE_CERT_STR + '\n' + ROOTCA_CERT_STR ext, password, raw = p.export(SAN_CERT_STR, chain, SAN_CERT_KEY, options) assert ext == 'jks' assert password == 'hunter2' assert isinstance(raw, bytes) ks = KeyStore.loads(raw, 'hunter2') assert ks.store_type == 'jks' # JKS lower-cases alias strings assert ks.entries.keys() == {'azurediamond_cert', 'azurediamond_cert_1', 'azurediamond_cert_2'} assert isinstance(ks.entries['azurediamond_cert'], TrustedCertEntry)
def test_export_keystore_defaults(app): from lemur.plugins.base import plugins p = plugins.get('java-keystore-jks') options = [] with pytest.raises(Exception): p.export(INTERNAL_CERTIFICATE_A_STR, '', '', options) ext, password, raw = p.export(SAN_CERT_STR, '', SAN_CERT_KEY, options) assert ext == 'jks' assert isinstance(password, str) assert isinstance(raw, bytes) ks = KeyStore.loads(raw, password) assert ks.store_type == 'jks' assert ks.entries.keys() == {'san.example.org'} entry = ks.entries['san.example.org'] assert isinstance(entry, PrivateKeyEntry) assert len(entry.cert_chain) == 1 # Only cert itself, no chain was provided