I'm trying to parse RSA (private) keys using asn1c. I based the asn1 module on https://www.rfc-editor.org/rfc/rfc3447 and it looks as following (i tried to only use the private and public key part):
-- ===================
-- Main structures
-- ===================
RSAPrivateKey DEFINITIONS ::=
BEGIN
--
-- Representation of RSA private key with information for the CRT
-- algorithm.
--
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e
}
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
}
Version ::= INTEGER { two-prime(0), multi(1) }
(CONSTRAINED BY {
-- version must be multi if otherPrimeInfos present --
})
OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
OtherPrimeInfo ::= SEQUENCE {
prime INTEGER, -- ri
exponent INTEGER, -- di
coefficient INTEGER -- ti
}
END -- PKCS1Definitions
However when i compile the module and try to parse .der private key using the following, it ends with RC_FAIL.
RSAPrivateKey_t *rsa_p_key;
rsa_p_key= (RSAPrivateKey_t*)calloc(1, sizeof *rsa_p_key);
asn_dec_rval_t rval = ber_decode(
0,
&asn_DEF_RSAPrivateKey,
(void**)&rsa_p_key,
buffer, // buffer containing key (unsigned char*)
buffer_len); // buffer length (amount of read bytes)
I've been trying to find the error in asn1 module, but with no luck. Using openssl to print out the .der file, it seems to match the RSAPrivateKey. I also tested for erronous file reading, but buffer matches and binary read mode is used.