Path: blob/main/crypto/openssl/demos/encode/rsa_encode.c
104839 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))66== 0) {67fprintf(stderr, "OSSL_DECODER_CTX_set_passphrase() failed\n");68goto cleanup;69}70}7172/* Do the decode, reading from file. */73if (OSSL_DECODER_from_fp(dctx, f) == 0) {74fprintf(stderr, "OSSL_DECODER_from_fp() failed\n");75goto cleanup;76}7778ret = 1;79cleanup:80OSSL_DECODER_CTX_free(dctx);8182/*83* pkey is created by OSSL_DECODER_CTX_new_for_pkey, but we84* might fail subsequently, so ensure it's properly freed85* in this case.86*/87if (ret == 0) {88EVP_PKEY_free(pkey);89pkey = NULL;90}9192return pkey;93}9495/*96* Store an RSA public or private key to a file using PEM encoding.97*98* If a passphrase is supplied, the file is encrypted, otherwise99* it is unencrypted.100*/101static int store_key(EVP_PKEY *pkey, FILE *f, const char *passphrase)102{103int ret = 0;104int selection;105OSSL_ENCODER_CTX *ectx = NULL;106107/*108* Create a PEM encoder context.109*110* For raw (non-PEM-encoded) output, change "PEM" to "DER".111*112* The selection argument controls whether the private key is exported113* (EVP_PKEY_KEYPAIR), or only the public key (EVP_PKEY_PUBLIC_KEY). The114* former will fail if we only have a public key.115*116* Note that unlike the decode API, you cannot specify zero here.117*118* Purely for the sake of demonstration, here we choose to export the whole119* key if a passphrase is provided and the public key otherwise.120*/121selection = (passphrase != NULL)122? EVP_PKEY_KEYPAIR123: EVP_PKEY_PUBLIC_KEY;124125ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", NULL, propq);126if (ectx == NULL) {127fprintf(stderr, "OSSL_ENCODER_CTX_new_for_pkey() failed\n");128goto cleanup;129}130131/*132* Set passphrase if provided; the encoded output will then be encrypted133* using the passphrase.134*135* Alternative methods for specifying passphrases exist, such as a callback136* (see OSSL_ENCODER_CTX_set_passphrase_cb(3), just as for OSSL_DECODER_CTX;137* however you are less likely to need them as you presumably know whether138* encryption is desired in advance.139*140* Note that specifying a passphrase alone is not enough to cause the141* key to be encrypted. You must set both a cipher and a passphrase.142*/143if (passphrase != NULL) {144/* Set cipher. AES-128-CBC is a reasonable default. */145if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-128-CBC", propq) == 0) {146fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");147goto cleanup;148}149150/* Set passphrase. */151if (OSSL_ENCODER_CTX_set_passphrase(ectx,152(const unsigned char *)passphrase,153strlen(passphrase))154== 0) {155fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");156goto cleanup;157}158}159160/* Do the encode, writing to the given file. */161if (OSSL_ENCODER_to_fp(ectx, f) == 0) {162fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");163goto cleanup;164}165166ret = 1;167cleanup:168OSSL_ENCODER_CTX_free(ectx);169return ret;170}171172int main(int argc, char **argv)173{174int ret = EXIT_FAILURE;175OSSL_LIB_CTX *libctx = NULL;176EVP_PKEY *pkey = NULL;177const char *passphrase_in = NULL, *passphrase_out = NULL;178179/* usage: rsa_encode <passphrase-in> <passphrase-out> */180if (argc > 1 && argv[1][0])181passphrase_in = argv[1];182183if (argc > 2 && argv[2][0])184passphrase_out = argv[2];185186/* Decode PEM key from stdin and then PEM encode it to stdout. */187pkey = load_key(libctx, stdin, passphrase_in);188if (pkey == NULL) {189fprintf(stderr, "Failed to decode key\n");190goto cleanup;191}192193if (store_key(pkey, stdout, passphrase_out) == 0) {194fprintf(stderr, "Failed to encode key\n");195goto cleanup;196}197198ret = EXIT_SUCCESS;199cleanup:200EVP_PKEY_free(pkey);201OSSL_LIB_CTX_free(libctx);202return ret;203}204205206