[openssl-users] i2d and d2i fucntions

Dave Thompson dthompson at prinpay.com
Thu Feb 12 21:06:34 UTC 2015

> From: openssl-users On Behalf Of Rajeswari K
> Sent: Thursday, February 12, 2015 00:40

> I have a query on d2i_PUBKEY() and i2d_PUBKEY().

> i have a EC public key in form of character buffer. 
> Have inputted this character buffer to d2i_PUBKEY() and got EVP_PKEY format EC key. 

To be exact, a byte (or even more exact octet) buffer. In C 
(and C++ and ObjC) it's type 'char[]' or better 'unsigned char[]',
but the values do not and often cannot represent *characters&.

> Now i tried to input this EVP_PKEY to i2d_PUBKEY() to compare will i get 
> exactly same data which i gave as input to d2i_PUBKEY().

> But i see that the outputs are completely different.

> i2d_PUBKEY() is leaving lots of 0's at the o/p buffer. 

> 0
> 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
> 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
> FD  1   10  DF  AB  12  34  CD  0   6F  0   0   0   0   1   83
> F   8B  AF  D8  D  ................................................

You must be doing something wrong. Probably the most common is,
are you looking at the beginning of the buffer? Remember that after 
calling i2d_whatever, the pointer you give it is moved to point 
*after* the encoded data, at unused and often junk memory.

If that's not it, reduce your code to the minimum that shows the 
problem, post it, and identify the version and build you are using.

> My goal is, to get complete EC public key in form of asn1 der 
> encoded from EC_KEY structure.

> I tried to use i2d_EC_PUBKEY() and i20_ECPublickey(). <snip>

Note that PUBKEY is the X.509 SPKI format: it contains an 
AlgorithmIdentifier identifying the algorithm and the curve,
*and* the public key value (a point) embedded in a bitstring,
all combined into an ASN.1 structure and DER encoded.

i2o_ECPublicKey (letter o not zero) uses a special non-ASN1 
non-DER encoding that contains *only* the point. 

More information about the openssl-users mailing list