def _parsePKCS8(bytes): s = ASN1_Node(str(bytes)) root = s.root() version_node = s.first_child(root) version = bytestr_to_int(s.get_value_of_type(version_node, 'INTEGER')) if version != 0: raise SyntaxError("Unrecognized PKCS8 version") rsaOID_node = s.next_node(version_node) ii = s.first_child(rsaOID_node) rsaOID = decode_OID(s.get_value_of_type(ii, 'OBJECT IDENTIFIER')) if rsaOID != '1.2.840.113549.1.1.1': raise SyntaxError("Unrecognized AlgorithmIdentifier") privkey_node = s.next_node(rsaOID_node) value = s.get_value_of_type(privkey_node, 'OCTET STRING') return _parseASN1PrivateKey(value)
def _parseASN1PrivateKey(s): s = ASN1_Node(s) root = s.root() version_node = s.first_child(root) version = bytestr_to_int(s.get_value_of_type(version_node, 'INTEGER')) if version != 0: raise SyntaxError("Unrecognized RSAPrivateKey version") n = s.next_node(version_node) e = s.next_node(n) d = s.next_node(e) p = s.next_node(d) q = s.next_node(p) dP = s.next_node(q) dQ = s.next_node(dP) qInv = s.next_node(dQ) return map(lambda x: bytesToNumber(s.get_value_of_type(x, 'INTEGER')), [n, e, d, p, q, dP, dQ, qInv])
def _parseSSLeay(bytes): return _parseASN1PrivateKey(ASN1_Node(str(bytes)))