encode ( private_key_info, ifNotEmpty = True ) pke. setComponentByName ( 'privateKey', key ) pke. setComponentByName ( 'privateKeyAlgorithm', a ) private_key_info. setComponentByName ( 'parameters', ' \x05\x00 ' ) private_key_info. setComponentByName ( 'version', 'v1' ) a = AlgorithmIdentifier () a. _algorithm_oid = RSA_ENCRYPTION_OID # We must encode it to pkcs8 private_key_info = rfc5208. pkey_pkcs8 = key elif key_format = 'rsa_raw' : pke. lower (), cert_chain = cert_chain ) if key_format = 'pkcs8' : private_key_info = decoder. append (( 'X.509', cert )) pke = cls ( timestamp = timestamp, # Alias must be lower case or it will corrupt the keystore for Java Keytool and Keytool Explorer alias = alias. time ()) * 1000 cert_chain = for cert in certs : cert_chain. :raises UnsupportedKeyFormatException: If the key format is unsupported. :returns: A loaded :class:`PrivateKeyEntry` instance, ready to be placed in a keystore. :param str key_format: The format of the provided private key. :param str key: A byte string containing the private key in the format specified in the key_format parameter (default pkcs8). The first one should be the one belonging to the private key, the others the chain (in correct order). :param str alias: The alias for the Private Key Entry :param list certs: An list of certificates, as byte strings. def new ( cls, alias, certs, key, key_format = 'pkcs8' ): """ Helper function to create a new PrivateKeyEntry. pack ( 0xCECECECE ) SIGNATURE_WHITENING = b "Mighty Aphrodite" pack ( 0xFEEDFEED ) MAGIC_NUMBER_JCEKS = b4. join ( str ( x ) for x in _version_info_ if str ( x )) MAGIC_NUMBER_JKS = b4. util import * try : from StringIO import StringIO as BytesIO # python 2 except ImportError : from io import BytesIO # python3 _version_info_ = ( 20, 0, 1, 'dev' ) _version_ = ".". """ from _future_ import print_function import struct import ctypes import hashlib import javaobj import time from import encoder, decoder from pyasn1_modules import rfc5208 from pyasn1_modules.rfc2459 import AlgorithmIdentifier from pyasn1.type import univ, namedtype from. We use bytearray() for this purpose in both Python2 and Python3, this will return individual elements as an 'int'. You can't do 'int' operations on a 'str' and vice-versa, so we need some form of common data type. str returns a 'str' in Python2 and an 'int' in Python3. Things get slightly more complicated when we need to manipulate individual bytes from a byte string. The libraries we interact with are written to expect these natural types in their respective Py2/P圓 versions, so this works well. Python2.6 aliases the 'bytes' type to 'str', so we can universally write bytes(.) or b"" to get each version's natural byte string representation. Notes on Python2/3 compatibility: Whereever possible, we rely on the 'natural' byte string representation of each Python version, i.e. Use in conjunction with PyOpenSSL to translate to PEM, or load private key and certs directly into openssl structs and wrap sockets. # vim: set et ai ts=4 sts=4 sw=4: """JKS/JCEKS file format decoder.
0 Comments
Leave a Reply. |