|
| 1 | + |
| 2 | + |
| 3 | +from .python_rsakey import Python_RSAKey |
| 4 | +from .pem import dePem, pemSniff |
| 5 | +from .asn1parser import ASN1Parser |
| 6 | +from .cryptomath import bytesToNumber |
| 7 | + |
| 8 | + |
| 9 | +class Python_Key(object): |
| 10 | + """ |
| 11 | + Generic methods for parsing private keys from files. |
| 12 | +
|
| 13 | + Handles both RSA and ECDSA keys, irrespective of file format. |
| 14 | + """ |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def parsePEM(s, passwordCallback=None): |
| 18 | + """Parse a string containing a PEM-encoded <privateKey>.""" |
| 19 | + |
| 20 | + if pemSniff(s, "PRIVATE KEY"): |
| 21 | + bytes = dePem(s, "PRIVATE KEY") |
| 22 | + return Python_Key._parse_pkcs8(bytes) |
| 23 | + elif pemSniff(s, "RSA PRIVATE KEY"): |
| 24 | + bytes = dePem(s, "RSA PRIVATE KEY") |
| 25 | + return Python_Key._parse_ssleay(bytes) |
| 26 | + elif pemSniff(s, "DSA PRIVATE KEY"): |
| 27 | + raise SyntaxError("DSA private key files unsupported") |
| 28 | + elif pemSniff(s, "EC PRIVATE KEY"): |
| 29 | + raise SyntaxError("ECDSA private key files unsupported") |
| 30 | + elif pemSniff(s, "PUBLIC KEY"): |
| 31 | + bytes = dePem(s, "PUBLIC KEY") |
| 32 | + return Python_Key._parse_public_key(bytes) |
| 33 | + else: |
| 34 | + raise SyntaxError("Not a PEM private key file") |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def _parse_public_key(bytes): |
| 38 | + # public keys are encoded as the subject_public_key_info objects |
| 39 | + spk_info = ASN1Parser(bytes) |
| 40 | + |
| 41 | + # first element of the SEQUENCE is the AlgorithmIdentifier |
| 42 | + alg_id = spk_info.getChild(0) |
| 43 | + |
| 44 | + # AlgId has two elements, the OID of the algorithm and parameters |
| 45 | + # parameters generally have to be NULL, with exception of RSA-PSS |
| 46 | + |
| 47 | + alg_oid = alg_id.getChild(0) |
| 48 | + |
| 49 | + if list(alg_oid.value) != [42, 134, 72, 134, 247, 13, 1, 1, 1]: |
| 50 | + raise SyntaxError("Only RSA Public keys supported") |
| 51 | + |
| 52 | + subject_public_key = ASN1Parser( |
| 53 | + ASN1Parser(spk_info.getChildBytes(1)).value[1:]) |
| 54 | + |
| 55 | + modulus = subject_public_key.getChild(0) |
| 56 | + exponent = subject_public_key.getChild(1) |
| 57 | + |
| 58 | + n = bytesToNumber(modulus.value) |
| 59 | + e = bytesToNumber(exponent.value) |
| 60 | + |
| 61 | + return Python_RSAKey(n, e) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def _parse_pkcs8(bytes): |
| 65 | + parser = ASN1Parser(bytes) |
| 66 | + |
| 67 | + # first element in PrivateKeyInfo is an INTEGER |
| 68 | + version = parser.getChild(0).value |
| 69 | + if bytesToNumber(version) != 0: |
| 70 | + raise SyntaxError("Unrecognized PKCS8 version") |
| 71 | + |
| 72 | + # second element in PrivateKeyInfo is a SEQUENCE of type |
| 73 | + # AlgorithmIdentifier |
| 74 | + alg_ident = parser.getChild(1) |
| 75 | + seq_len = alg_ident.getChildCount() |
| 76 | + # first item of AlgorithmIdentifier is an OBJECT (OID) |
| 77 | + oid = alg_ident.getChild(0) |
| 78 | + if list(oid.value) == [42, 134, 72, 134, 247, 13, 1, 1, 1]: |
| 79 | + key_type = "rsa" |
| 80 | + elif list(oid.value) == [42, 134, 72, 134, 247, 13, 1, 1, 10]: |
| 81 | + key_type = "rsa-pss" |
| 82 | + else: |
| 83 | + raise SyntaxError("Unrecognized AlgorithmIdentifier: {0}" |
| 84 | + .format(list(oid.value))) |
| 85 | + # second item of AlgorithmIdentifier are parameters (defined by |
| 86 | + # above algorithm) |
| 87 | + if key_type == "rsa": |
| 88 | + if seq_len != 2: |
| 89 | + raise SyntaxError("Missing parameters for RSA algorithm ID") |
| 90 | + parameters = alg_ident.getChild(1) |
| 91 | + if parameters.value != bytearray(0): |
| 92 | + raise SyntaxError("RSA parameters are not NULL") |
| 93 | + else: # rsa-pss |
| 94 | + pass # ignore parameters - don't apply restrictions |
| 95 | + |
| 96 | + if seq_len > 2: |
| 97 | + raise SyntaxError("Invalid encoding of AlgorithmIdentifier") |
| 98 | + |
| 99 | + #Get the privateKey |
| 100 | + private_key_parser = parser.getChild(2) |
| 101 | + |
| 102 | + #Adjust for OCTET STRING encapsulation |
| 103 | + private_key_parser = ASN1Parser(private_key_parser.value) |
| 104 | + |
| 105 | + return Python_Key._parse_asn1_private_key(private_key_parser) |
| 106 | + |
| 107 | + @staticmethod |
| 108 | + def _parse_ssleay(data): |
| 109 | + """ |
| 110 | + Parse binary structure of the old SSLeay file format used by OpenSSL. |
| 111 | +
|
| 112 | + For RSA keys. |
| 113 | + """ |
| 114 | + private_key_parser = ASN1Parser(data) |
| 115 | + return Python_Key._parse_asn1_private_key(private_key_parser) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _parse_asn1_private_key(private_key_parser): |
| 119 | + version = private_key_parser.getChild(0).value[0] |
| 120 | + if version != 0: |
| 121 | + raise SyntaxError("Unrecognized RSAPrivateKey version") |
| 122 | + n = bytesToNumber(private_key_parser.getChild(1).value) |
| 123 | + e = bytesToNumber(private_key_parser.getChild(2).value) |
| 124 | + d = bytesToNumber(private_key_parser.getChild(3).value) |
| 125 | + p = bytesToNumber(private_key_parser.getChild(4).value) |
| 126 | + q = bytesToNumber(private_key_parser.getChild(5).value) |
| 127 | + dP = bytesToNumber(private_key_parser.getChild(6).value) |
| 128 | + dQ = bytesToNumber(private_key_parser.getChild(7).value) |
| 129 | + qInv = bytesToNumber(private_key_parser.getChild(8).value) |
| 130 | + return Python_RSAKey(n, e, d, p, q, dP, dQ, qInv) |
0 commit comments