Path: blob/main/crypto/openssl/demos/encode/ec_encode.c
104183 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 EC public and private keys. A15* PEM-encoded EC 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 EC keys from a file or save EC 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 EC 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 EC 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, "EC",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 a EC 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/*145* Set cipher. Let's use AES-256-CBC, because it is146* more quantum resistant.147*/148if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-256-CBC", propq) == 0) {149fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");150goto cleanup;151}152153/* Set passphrase. */154if (OSSL_ENCODER_CTX_set_passphrase(ectx,155(const unsigned char *)passphrase,156strlen(passphrase))157== 0) {158fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");159goto cleanup;160}161}162163/* Do the encode, writing to the given file. */164if (OSSL_ENCODER_to_fp(ectx, f) == 0) {165fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");166goto cleanup;167}168169ret = 1;170cleanup:171OSSL_ENCODER_CTX_free(ectx);172return ret;173}174175int main(int argc, char **argv)176{177int ret = EXIT_FAILURE;178OSSL_LIB_CTX *libctx = NULL;179EVP_PKEY *pkey = NULL;180const char *passphrase_in = NULL, *passphrase_out = NULL;181182/* usage: ec_encode <passphrase-in> <passphrase-out> */183if (argc > 1 && argv[1][0])184passphrase_in = argv[1];185186if (argc > 2 && argv[2][0])187passphrase_out = argv[2];188189/* Decode PEM key from stdin and then PEM encode it to stdout. */190pkey = load_key(libctx, stdin, passphrase_in);191if (pkey == NULL) {192fprintf(stderr, "Failed to decode key\n");193goto cleanup;194}195196if (store_key(pkey, stdout, passphrase_out) == 0) {197fprintf(stderr, "Failed to encode key\n");198goto cleanup;199}200201ret = EXIT_SUCCESS;202cleanup:203EVP_PKEY_free(pkey);204OSSL_LIB_CTX_free(libctx);205return ret;206}207208209