Updating to openssl v3 replacing PEM_write_bio_RSAPublicKey #24331
-
Hi I have been working on converting an application to the new openssl 3+ api and I am currently replacing PEM_write_bio_RSAPublicKey. with PEM_write_bio_PUBKEY(stringBIO, pkey); However one of the tests for compatability with the old version test that the output of a known private key produces the public key in both the old format and new. PEM_write_bio_PUBKEY(stringBIO, pkey); produces the correct public key in the -----BEGIN PUBLIC KEY----- PEM format. Is there a way in openssl3 API to produce it in -----BEGIN RSA PUBLIC KEY----- format? Tim |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can use encoders to define the output format. ...
OSSL_ENCODER_CTX *ctx = NULL;
const char *pubkey_out = "rsa.pub.pkcs1";
...
fp_out = fopen(pubkey_out, "w");
if (fp_out == NULL) {
fprintf(stderr, "Failed to open public key to write\n");
// handle errors and exit
}
ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
"PEM", "PKCS1", NULL);
if (ctx == NULL || !OSSL_ENCODER_CTX_get_num_encoders(ctx)) {
fprintf(stderr, "Failed to get an encoder context\n");
// handle errors and exit
}
fprintf(stdout, "Public key PKCS1 write to %s %s\n", pubkey_out,
OSSL_ENCODER_to_fp(ctx, fp_out) ? "success" : "failed");
...
OSSL_ENCODER_CTX_free(ctx);
... This should generate output in the desired format $ grep KEY rsa.pub.pkcs1
-----BEGIN RSA PUBLIC KEY-----
-----END RSA PUBLIC KEY----- References |
Beta Was this translation helpful? Give feedback.
-
Thanks again I had previously found the decoder function and wondered why there was not one to do what I wanted. This worked great |
Beta Was this translation helpful? Give feedback.
You can use encoders to define the output format.
Example: