openssl's d2i_X509 complains with "wrong tag"
Tried to parse a DER key from a minecraft server auth protocol.
openssl asn1parse -inform DER
correctly parses the key, but when I try to use d2i_X509
function from within C to parse this data, I receive null, and the error stack looks like this:
140508081342272:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:crypto/asn1/tasn_dec.c:1149:
140508081342272:error:0D06C03A:asn1 encoding routines:asn1_d2i_ex_primitive:nested asn1 error:crypto/asn1/tasn_dec.c:713:
140508081342272:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:646:Field=serialNumber, Type=X509_CINF
140508081342272:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:crypto/asn1/tasn_dec.c:646:Field=cert_info, Type=X509
This is how I call d2i_X509
:
X509 *key_struct = d2i_X509(NULL, byte_data, byte_data_length);
if(key_struct == NULL) {
ERR_print_errors_fp(stderr);
}
OpenSSL returns this:
openssl asn1parse -inform DER < out.bin
0:d=0 hl=3 l= 159 cons: SEQUENCE
3:d=1 hl=2 l= 13 cons: SEQUENCE
5:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
16:d=2 hl=2 l= 0 prim: NULL
18:d=1 hl=3 l= 141 prim: BIT STRING
What you have there isn't an X.509 structure (certificate) it's a SubjectPublicKeyInfo
that is just defined in X.509 RFC that seems like an RSA public key. I'd suggest using another openssl
function that is made to parse this structure d2i_PUBKEY()
documented here: https://www.openssl.org/docs/man1.1.0/man3/i2d_PUBKEY_bio.html