Reputation: 33
I'm using pyOpenSSL and I have a PKCS12 object and with the get_privateKey()
and get_publicKey()
methods one can obtain the RSA Private/Public key objects. Is there a way from these objects to extract the RSA key parameters (p, q, dp, dq, qinv)?
Upvotes: 1
Views: 794
Reputation: 969
You can use ASN1 parser to get the key parameters:
from OpenSSL.crypto import dump_privatekey, FILETYPE_ASN1
from Crypto.Util.asn1 import DerSequence
private_key_der = DerSequence()
# private_key is obtained from PKCS12 object using get_privateKey()
private_key_der.decode(dump_privatekey(FILETYPE_ASN1, private_key))
The private_key_der
will contain the key parameters according to the following der structure
RSAPrivateKey ::= SEQUENCE {
version Version,
modulus INTEGER, -- n
publicExponent INTEGER, -- e
privateExponent INTEGER, -- d
prime1 INTEGER, -- p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1)
exponent2 INTEGER, -- d mod (q-1)
coefficient INTEGER, -- (inverse of q) mod p
otherPrimeInfos OtherPrimeInfos OPTIONAL
}
which in python will be equivalent to:
private_key_der[0] # version
private_key_der[1] # modulus
private_key_der[2] # publicExponent
private_key_der[3] # privateExponent
private_key_der[4] # prime1
private_key_der[5] # prime2
private_key_der[6] # exponent1
private_key_der[7] # exponent2
private_key_der[8] # coefficient
private_key_der[9] # otherPrimeInfos
Upvotes: 2