Path: blob/main/crypto/openssl/demos/encode/ec_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 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)) == 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 a EC 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/*144* Set cipher. Let's use AES-256-CBC, because it is145* more quantum resistant.146*/147if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-256-CBC", propq) == 0) {148fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");149goto cleanup;150}151152/* Set passphrase. */153if (OSSL_ENCODER_CTX_set_passphrase(ectx,154(const unsigned char *)passphrase,155strlen(passphrase)) == 0) {156fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");157goto cleanup;158}159}160161/* Do the encode, writing to the given file. */162if (OSSL_ENCODER_to_fp(ectx, f) == 0) {163fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");164goto cleanup;165}166167ret = 1;168cleanup:169OSSL_ENCODER_CTX_free(ectx);170return ret;171}172173int main(int argc, char **argv)174{175int ret = EXIT_FAILURE;176OSSL_LIB_CTX *libctx = NULL;177EVP_PKEY *pkey = NULL;178const char *passphrase_in = NULL, *passphrase_out = NULL;179180/* usage: ec_encode <passphrase-in> <passphrase-out> */181if (argc > 1 && argv[1][0])182passphrase_in = argv[1];183184if (argc > 2 && argv[2][0])185passphrase_out = argv[2];186187/* Decode PEM key from stdin and then PEM encode it to stdout. */188pkey = load_key(libctx, stdin, passphrase_in);189if (pkey == NULL) {190fprintf(stderr, "Failed to decode key\n");191goto cleanup;192}193194if (store_key(pkey, stdout, passphrase_out) == 0) {195fprintf(stderr, "Failed to encode key\n");196goto cleanup;197}198199ret = EXIT_SUCCESS;200cleanup:201EVP_PKEY_free(pkey);202OSSL_LIB_CTX_free(libctx);203return ret;204}205206207