# Path: tlslite/utils/cryptomath.py
# def getRandomBytes(howMany):
# def bytesToNumber(bytes):
# def numberToBytes(n, howManyBytes=None):
# def bytesToBase64(bytes):
# def base64ToBytes(s):
# def numberToBase64(n):
# def base64ToNumber(s):
# def stringToNumber(s):
# def numberToString(s, howManyBytes=None):
# def base64ToString(s):
# def stringToBase64(s):
# def mpiToNumber(mpi): #mpi is an openssl-format bignum string
# def numberToMPI(n):
# def numBytes(n):
# def getRandomNumber(low, high):
# def gcd(a,b):
# def lcm(a, b):
# def invMod(a, b):
#     def powMod(base, power, modulus):
#     def powMod(base, power, modulus):
# def makeSieve(n):
# def isPrime(n, iterations=5, display=False):
# def getRandomPrime(bits, display=False):
# def getRandomSafePrime(bits, display=False):
# 
# Path: tlslite/x509.py
# class X509:
#     """This class represents an X.509 certificate.
# 
#     @type bytes: L{bytearray} of unsigned bytes
#     @ivar bytes: The DER-encoded ASN.1 certificate
# 
#     @type publicKey: L{tlslite.utils.rsakey.RSAKey}
#     @ivar publicKey: The subject public key from the certificate.
# 
#     @type subject: L{bytearray} of unsigned bytes
#     @ivar subject: The DER-encoded ASN.1 subject distinguished name.
#     """
# 
#     def __init__(self):
#         self.bytes = createByteArraySequence([])
#         self.publicKey = None
#         self.subject = None
# 
#     def parse(self, s):
#         """Parse a PEM-encoded X.509 certificate.
# 
#         @type s: str
#         @param s: A PEM-encoded X.509 certificate (i.e. a base64-encoded
#         certificate wrapped with "-----BEGIN CERTIFICATE-----" and
#         "-----END CERTIFICATE-----" tags).
#         """
# 
#         bytes = dePem(s, "CERTIFICATE")
#         self.parseBinary(bytes)
#         return self
# 
#     def parseBinary(self, bytes):
#         """Parse a DER-encoded X.509 certificate.
# 
#         @type bytes: str or L{bytearray} of unsigned bytes
#         @param bytes: A DER-encoded X.509 certificate.
#         """
# 
#         if isinstance(bytes, type("")):
#             bytes = stringToBytes(bytes)
# 
#         self.bytes = bytes
#         p = ASN1Parser(bytes)
# 
#         #Get the tbsCertificate
#         tbsCertificateP = p.getChild(0)
# 
#         #Is the optional version field present?
#         #This determines which index the key is at.
#         if tbsCertificateP.value[0]==0xA0:
#             subjectPublicKeyInfoIndex = 6
#         else:
#             subjectPublicKeyInfoIndex = 5
# 
#         #Get the subject
#         self.subject = tbsCertificateP.getChildBytes(\
#                            subjectPublicKeyInfoIndex - 1)
# 
#         #Get the subjectPublicKeyInfo
#         subjectPublicKeyInfoP = tbsCertificateP.getChild(\
#                                     subjectPublicKeyInfoIndex)
# 
#         #Get the algorithm
#         algorithmP = subjectPublicKeyInfoP.getChild(0)
#         rsaOID = algorithmP.value
#         if list(rsaOID) != [6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0]:
#             raise SyntaxError("Unrecognized AlgorithmIdentifier")
# 
#         #Get the subjectPublicKey
#         subjectPublicKeyP = subjectPublicKeyInfoP.getChild(1)
# 
#         #Adjust for BIT STRING encapsulation
#         if (subjectPublicKeyP.value[0] !=0):
#             raise SyntaxError()
#         subjectPublicKeyP = ASN1Parser(subjectPublicKeyP.value[1:])
# 
#         #Get the modulus and exponent
#         modulusP = subjectPublicKeyP.getChild(0)
#         publicExponentP = subjectPublicKeyP.getChild(1)
# 
#         #Decode them into numbers
#         n = bytesToNumber(modulusP.value)
#         e = bytesToNumber(publicExponentP.value)
# 
#         #Create a public key instance
#         self.publicKey = _createPublicRSAKey(n, e)
# 
#     def getFingerprint(self):
#         """Get the hex-encoded fingerprint of this certificate.
# 
#         @rtype: str
#         @return: A hex-encoded fingerprint.
#         """
#         return sha1(bytesToString(self.bytes)).hexdigest()
# 
#     def writeBytes(self):
#         return self.bytes


from .utils import cryptomath
from .utils.tackwrapper import *
from .utils.pem import *
from .x509 import X509
class X509CertChain:
    """This class represents a chain of X.509 certificates.

    @type x509List: list
    @ivar x509List: A list of L{tlslite.x509.X509} instances,
    starting with the end-entity certificate and with every
    subsequent certificate certifying the previous.
    """

    def __init__(self, x509List=None):
        """Create a new X509CertChain.

        @type x509List: list
        @param x509List: A list of L{tlslite.x509.X509} instances,
        starting with the end-entity certificate and with every
        subsequent certificate certifying the previous.
        """
        if x509List:
            self.x509List = x509List
        else:
            self.x509List = []

    def parsePemList(self, s):
        """Parse a string containing a sequence of PEM certs.

        Raise a SyntaxError if input is malformed.
        """
        x509List = []
        bList = dePemList(s, "CERTIFICATE")
        for b in bList:

# Path: tlslite/utils/cryptomath.py
# def getRandomBytes(howMany):
# def bytesToNumber(bytes):
# def numberToBytes(n, howManyBytes=None):
# def bytesToBase64(bytes):
# def base64ToBytes(s):
# def numberToBase64(n):
# def base64ToNumber(s):
# def stringToNumber(s):
# def numberToString(s, howManyBytes=None):
# def base64ToString(s):
# def stringToBase64(s):
# def mpiToNumber(mpi): #mpi is an openssl-format bignum string
# def numberToMPI(n):
# def numBytes(n):
# def getRandomNumber(low, high):
# def gcd(a,b):
# def lcm(a, b):
# def invMod(a, b):
#     def powMod(base, power, modulus):
#     def powMod(base, power, modulus):
# def makeSieve(n):
# def isPrime(n, iterations=5, display=False):
# def getRandomPrime(bits, display=False):
# def getRandomSafePrime(bits, display=False):
# 
# Path: tlslite/x509.py
# class X509:
#     """This class represents an X.509 certificate.
# 
#     @type bytes: L{bytearray} of unsigned bytes
#     @ivar bytes: The DER-encoded ASN.1 certificate
# 
#     @type publicKey: L{tlslite.utils.rsakey.RSAKey}
#     @ivar publicKey: The subject public key from the certificate.
# 
#     @type subject: L{bytearray} of unsigned bytes
#     @ivar subject: The DER-encoded ASN.1 subject distinguished name.
#     """
# 
#     def __init__(self):
#         self.bytes = createByteArraySequence([])
#         self.publicKey = None
#         self.subject = None
# 
#     def parse(self, s):
#         """Parse a PEM-encoded X.509 certificate.
# 
#         @type s: str
#         @param s: A PEM-encoded X.509 certificate (i.e. a base64-encoded
#         certificate wrapped with "-----BEGIN CERTIFICATE-----" and
#         "-----END CERTIFICATE-----" tags).
#         """
# 
#         bytes = dePem(s, "CERTIFICATE")
#         self.parseBinary(bytes)
#         return self
# 
#     def parseBinary(self, bytes):
#         """Parse a DER-encoded X.509 certificate.
# 
#         @type bytes: str or L{bytearray} of unsigned bytes
#         @param bytes: A DER-encoded X.509 certificate.
#         """
# 
#         if isinstance(bytes, type("")):
#             bytes = stringToBytes(bytes)
# 
#         self.bytes = bytes
#         p = ASN1Parser(bytes)
# 
#         #Get the tbsCertificate
#         tbsCertificateP = p.getChild(0)
# 
#         #Is the optional version field present?
#         #This determines which index the key is at.
#         if tbsCertificateP.value[0]==0xA0:
#             subjectPublicKeyInfoIndex = 6
#         else:
#             subjectPublicKeyInfoIndex = 5
# 
#         #Get the subject
#         self.subject = tbsCertificateP.getChildBytes(\
#                            subjectPublicKeyInfoIndex - 1)
# 
#         #Get the subjectPublicKeyInfo
#         subjectPublicKeyInfoP = tbsCertificateP.getChild(\
#                                     subjectPublicKeyInfoIndex)
# 
#         #Get the algorithm
#         algorithmP = subjectPublicKeyInfoP.getChild(0)
#         rsaOID = algorithmP.value
#         if list(rsaOID) != [6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0]:
#             raise SyntaxError("Unrecognized AlgorithmIdentifier")
# 
#         #Get the subjectPublicKey
#         subjectPublicKeyP = subjectPublicKeyInfoP.getChild(1)
# 
#         #Adjust for BIT STRING encapsulation
#         if (subjectPublicKeyP.value[0] !=0):
#             raise SyntaxError()
#         subjectPublicKeyP = ASN1Parser(subjectPublicKeyP.value[1:])
# 
#         #Get the modulus and exponent
#         modulusP = subjectPublicKeyP.getChild(0)
#         publicExponentP = subjectPublicKeyP.getChild(1)
# 
#         #Decode them into numbers
#         n = bytesToNumber(modulusP.value)
#         e = bytesToNumber(publicExponentP.value)
# 
#         #Create a public key instance
#         self.publicKey = _createPublicRSAKey(n, e)
# 
#     def getFingerprint(self):
#         """Get the hex-encoded fingerprint of this certificate.
# 
#         @rtype: str
#         @return: A hex-encoded fingerprint.
#         """
#         return sha1(bytesToString(self.bytes)).hexdigest()
# 
#     def writeBytes(self):
#         return self.bytes


# Path: tlslite/x509certchain.py
from .utils import cryptomath
from .utils.tackwrapper import *
from .utils.pem import *
from .x509 import X509

class X509CertChain:
    """This class represents a chain of X.509 certificates.

    @type x509List: list
    @ivar x509List: A list of L{tlslite.x509.X509} instances,
    starting with the end-entity certificate and with every
    subsequent certificate certifying the previous.
    """

    def __init__(self, x509List=None):
        """Create a new X509CertChain.

        @type x509List: list
        @param x509List: A list of L{tlslite.x509.X509} instances,
        starting with the end-entity certificate and with every
        subsequent certificate certifying the previous.
        """
        if x509List:
            self.x509List = x509List
        else:
            self.x509List = []

    def parsePemList(self, s):
        """Parse a string containing a sequence of PEM certs.

        Raise a SyntaxError if input is malformed.
        """
        x509List = []
        bList = dePemList(s, "CERTIFICATE")
        for b in bList:

            x509 = X509()