Is there a way to print out the DH parameters from DH struct in OpenSSL?
1 Answer
User dave_thompson_085 basically gave you the answer in his comment. For the version that you are using, the function i2d_DHparams_fp() is not available, you will have to use i2d_DHparams() to get a buffer of unsigned chars and then write that to a file. Something like this.
/* Just an example */
DH *dh = DH_get_2048_256();
unsigned char *der = NULL;
/* On success, len contains the length of the buffer */
int len = i2d_DHparams(dh, &der);
/* Open file, write bytes to it, close file */
FILE *fder = fopen("dhparams.der", "wb");
fwrite(der, len, 1, fder);
fclose(fder);
/* Done with the buffer */
OPENSSL_free(der);
Error checking has to be added.
You can check that it worked by using the asn1parse tool, like this:
$ openssl asn1parse -inform der -in dhparams.der
0:d=0 hl=4 l= 521 cons: SEQUENCE
4:d=1 hl=4 l= 257 prim: INTEGER :87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597
265:d=1 hl=4 l= 256 prim: INTEGER :3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659
DHparams_print[_fp]prints labelled textual values, which is not PEM.PEM_write[_bio]_DHparamsprints/writes PEM. 'print' normally means human-readable and DER is not human-readable, so we say 'writes'.i2d_DHparams{,_fp,_bio}writes DER to a memory buffer, stdioFILE*or OpenSSLBIO*(which can in turn be a file, socket, memory buffer, or something else) in the same way as thei2droutines for all other OpenSSL types. In 1.0.2 up the PEM and i2d routines haveDHxparamsversions which use X9.42 format instead of PKCS3 format. To be clear, these are params and not keys.