Path: blob/main/crypto/openssl/demos/encode/rsa_encode.c
34889 views
/*-1* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/8#include <string.h>9#include <openssl/decoder.h>10#include <openssl/encoder.h>11#include <openssl/evp.h>1213/*14* Example showing the encoding and decoding of RSA public and private keys. A15* PEM-encoded RSA key is read in from stdin, decoded, and then re-encoded and16* output for demonstration purposes. Both public and private keys are accepted.17*18* This can be used to load RSA keys from a file or save RSA keys to a file.19*/2021/* A property query used for selecting algorithm implementations. */22static const char *propq = NULL;2324/*25* Load a PEM-encoded RSA key from a file, optionally decrypting it with a26* supplied passphrase.27*/28static EVP_PKEY *load_key(OSSL_LIB_CTX *libctx, FILE *f, const char *passphrase)29{30int ret = 0;31EVP_PKEY *pkey = NULL;32OSSL_DECODER_CTX *dctx = NULL;33int selection = 0;3435/*36* Create PEM decoder context expecting an RSA key.37*38* For raw (non-PEM-encoded) keys, change "PEM" to "DER".39*40* The selection argument here specifies whether we are willing to accept a41* public key, private key, or either. If it is set to zero, either will be42* accepted. If set to EVP_PKEY_KEYPAIR, a private key will be required, and43* if set to EVP_PKEY_PUBLIC_KEY, a public key will be required.44*/45dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, "RSA",46selection,47libctx, propq);48if (dctx == NULL) {49fprintf(stderr, "OSSL_DECODER_CTX_new_for_pkey() failed\n");50goto cleanup;51}5253/*54* Set passphrase if provided; needed to decrypt encrypted PEM files.55* If the input is not encrypted, any passphrase provided is ignored.56*57* Alternative methods for specifying passphrases exist, such as a callback58* (see OSSL_DECODER_CTX_set_passphrase_cb(3)), which may be more useful for59* interactive applications which do not know if a passphrase should be60* prompted for in advance, or for GUI applications.61*/62if (passphrase != NULL) {63if (OSSL_DECODER_CTX_set_passphrase(dctx,64(const unsigned char *)passphrase,65strlen(passphrase)) == 0) {66fprintf(stderr, "OSSL_DECODER_CTX_set_passphrase() failed\n");67goto cleanup;68}69}7071/* Do the decode, reading from file. */72if (OSSL_DECODER_from_fp(dctx, f) == 0) {73fprintf(stderr, "OSSL_DECODER_from_fp() failed\n");74goto cleanup;75}7677ret = 1;78cleanup:79OSSL_DECODER_CTX_free(dctx);8081/*82* pkey is created by OSSL_DECODER_CTX_new_for_pkey, but we83* might fail subsequently, so ensure it's properly freed84* in this case.85*/86if (ret == 0) {87EVP_PKEY_free(pkey);88pkey = NULL;89}9091return pkey;92}9394/*95* Store an RSA public or private key to a file using PEM encoding.96*97* If a passphrase is supplied, the file is encrypted, otherwise98* it is unencrypted.99*/100static int store_key(EVP_PKEY *pkey, FILE *f, const char *passphrase)101{102int ret = 0;103int selection;104OSSL_ENCODER_CTX *ectx = NULL;105106/*107* Create a PEM encoder context.108*109* For raw (non-PEM-encoded) output, change "PEM" to "DER".110*111* The selection argument controls whether the private key is exported112* (EVP_PKEY_KEYPAIR), or only the public key (EVP_PKEY_PUBLIC_KEY). The113* former will fail if we only have a public key.114*115* Note that unlike the decode API, you cannot specify zero here.116*117* Purely for the sake of demonstration, here we choose to export the whole118* key if a passphrase is provided and the public key otherwise.119*/120selection = (passphrase != NULL)121? EVP_PKEY_KEYPAIR122: EVP_PKEY_PUBLIC_KEY;123124ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", NULL, propq);125if (ectx == NULL) {126fprintf(stderr, "OSSL_ENCODER_CTX_new_for_pkey() failed\n");127goto cleanup;128}129130/*131* Set passphrase if provided; the encoded output will then be encrypted132* using the passphrase.133*134* Alternative methods for specifying passphrases exist, such as a callback135* (see OSSL_ENCODER_CTX_set_passphrase_cb(3), just as for OSSL_DECODER_CTX;136* however you are less likely to need them as you presumably know whether137* encryption is desired in advance.138*139* Note that specifying a passphrase alone is not enough to cause the140* key to be encrypted. You must set both a cipher and a passphrase.141*/142if (passphrase != NULL) {143/* Set cipher. AES-128-CBC is a reasonable default. */144if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-128-CBC", propq) == 0) {145fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");146goto cleanup;147}148149/* Set passphrase. */150if (OSSL_ENCODER_CTX_set_passphrase(ectx,151(const unsigned char *)passphrase,152strlen(passphrase)) == 0) {153fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");154goto cleanup;155}156}157158/* Do the encode, writing to the given file. */159if (OSSL_ENCODER_to_fp(ectx, f) == 0) {160fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");161goto cleanup;162}163164ret = 1;165cleanup:166OSSL_ENCODER_CTX_free(ectx);167return ret;168}169170int main(int argc, char **argv)171{172int ret = EXIT_FAILURE;173OSSL_LIB_CTX *libctx = NULL;174EVP_PKEY *pkey = NULL;175const char *passphrase_in = NULL, *passphrase_out = NULL;176177/* usage: rsa_encode <passphrase-in> <passphrase-out> */178if (argc > 1 && argv[1][0])179passphrase_in = argv[1];180181if (argc > 2 && argv[2][0])182passphrase_out = argv[2];183184/* Decode PEM key from stdin and then PEM encode it to stdout. */185pkey = load_key(libctx, stdin, passphrase_in);186if (pkey == NULL) {187fprintf(stderr, "Failed to decode key\n");188goto cleanup;189}190191if (store_key(pkey, stdout, passphrase_out) == 0) {192fprintf(stderr, "Failed to encode key\n");193goto cleanup;194}195196ret = EXIT_SUCCESS;197cleanup:198EVP_PKEY_free(pkey);199OSSL_LIB_CTX_free(libctx);200return ret;201}202203204