Example #1
0
def _markers(pem_marker):
    """
    Returns the start and end PEM markers, as bytes.
    """

    if not is_bytes(pem_marker):
        pem_marker = pem_marker.encode('ascii')

    return (b('-----BEGIN ') + pem_marker + b('-----'),
            b('-----END ') + pem_marker + b('-----'))
Example #2
0
def _markers(pem_marker):
    """
    Returns the start and end PEM markers
    """

    if is_bytes(pem_marker):
        pem_marker = pem_marker.decode('utf-8')

    return (b('-----BEGIN %s-----' % pem_marker),
            b('-----END %s-----' % pem_marker))
Example #3
0
    def test_decoding_failure(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        if is_bytes(a):
            a = ord(a)
        encrypted = encrypted[:5] + byte(a + 1) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
Example #4
0
 def test_byte(self):
     for i in range(256):
         byt = byte(i)
         self.assertTrue(is_bytes(byt))
         self.assertEqual(ord(byt), i)
Example #5
0
 def test_bytes_private(self):
     key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
     self.assertTrue(is_bytes(key.save_pkcs1(format='DER')))
     self.assertTrue(is_bytes(key.save_pkcs1(format='PEM')))
Example #6
0
 def test_bytes_public(self):
     key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii'))
     self.assertTrue(is_bytes(key.save_pkcs1(format='DER')))
     self.assertTrue(is_bytes(key.save_pkcs1(format='PEM')))
Example #7
0
def load_pem(contents, pem_marker):
    """Loads a PEM file.

    :param contents: the contents of the file to interpret
    :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
        when your file has '-----BEGIN RSA PRIVATE KEY-----' and
        '-----END RSA PRIVATE KEY-----' markers.

    :return: the base64-decoded content between the start and end markers.

    @raise ValueError: when the content is invalid, for example when the start
        marker cannot be found.

    """

    # We want bytes, not text. If it's text, it can be converted to ASCII bytes.
    if not is_bytes(contents):
        contents = contents.encode('ascii')

    (pem_start, pem_end) = _markers(pem_marker)

    pem_lines = []
    in_pem_part = False

    for line in contents.splitlines():
        line = line.strip()

        # Skip empty lines
        if not line:
            continue

        # Handle start marker
        if line == pem_start:
            if in_pem_part:
                raise ValueError('Seen start marker "%s" twice' % pem_start)

            in_pem_part = True
            continue

        # Skip stuff before first marker
        if not in_pem_part:
            continue

        # Handle end marker
        if in_pem_part and line == pem_end:
            in_pem_part = False
            break

        # Load fields
        if b(':') in line:
            continue

        pem_lines.append(line)

    # Do some sanity checks
    if not pem_lines:
        raise ValueError('No PEM start marker "%s" found' % pem_start)

    if in_pem_part:
        raise ValueError('No PEM end marker "%s" found' % pem_end)

    # Base64-decode the contents
    pem = b('').join(pem_lines)
    return base64.standard_b64decode(pem)
Example #8
0
def _markers(pem_marker):
    if is_bytes(pem_marker):
        pem_marker = pem_marker.decode('utf-8')
    return (b('-----BEGIN %s-----' % pem_marker), b('-----END %s-----' % pem_marker))