xref: /netbsd-src/crypto/external/bsd/openssl/dist/demos/encode/ec_encode.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*-
2*b0d17251Schristos  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos #include <string.h>
10*b0d17251Schristos #include <openssl/decoder.h>
11*b0d17251Schristos #include <openssl/encoder.h>
12*b0d17251Schristos #include <openssl/evp.h>
13*b0d17251Schristos 
14*b0d17251Schristos /*
15*b0d17251Schristos  * Example showing the encoding and decoding of EC public and private keys. A
16*b0d17251Schristos  * PEM-encoded EC key is read in from stdin, decoded, and then re-encoded and
17*b0d17251Schristos  * output for demonstration purposes. Both public and private keys are accepted.
18*b0d17251Schristos  *
19*b0d17251Schristos  * This can be used to load EC keys from a file or save EC keys to a file.
20*b0d17251Schristos  */
21*b0d17251Schristos 
22*b0d17251Schristos /* A property query used for selecting algorithm implementations. */
23*b0d17251Schristos static const char *propq = NULL;
24*b0d17251Schristos 
25*b0d17251Schristos /*
26*b0d17251Schristos  * Load a PEM-encoded EC key from a file, optionally decrypting it with a
27*b0d17251Schristos  * supplied passphrase.
28*b0d17251Schristos  */
load_key(OSSL_LIB_CTX * libctx,FILE * f,const char * passphrase)29*b0d17251Schristos static EVP_PKEY *load_key(OSSL_LIB_CTX *libctx, FILE *f, const char *passphrase)
30*b0d17251Schristos {
31*b0d17251Schristos     int rv = 0;
32*b0d17251Schristos     EVP_PKEY *pkey = NULL;
33*b0d17251Schristos     OSSL_DECODER_CTX *dctx = NULL;
34*b0d17251Schristos     int selection = 0;
35*b0d17251Schristos 
36*b0d17251Schristos     /*
37*b0d17251Schristos      * Create PEM decoder context expecting an EC key.
38*b0d17251Schristos      *
39*b0d17251Schristos      * For raw (non-PEM-encoded) keys, change "PEM" to "DER".
40*b0d17251Schristos      *
41*b0d17251Schristos      * The selection argument here specifies whether we are willing to accept a
42*b0d17251Schristos      * public key, private key, or either. If it is set to zero, either will be
43*b0d17251Schristos      * accepted. If set to EVP_PKEY_KEYPAIR, a private key will be required, and
44*b0d17251Schristos      * if set to EVP_PKEY_PUBLIC_KEY, a public key will be required.
45*b0d17251Schristos      */
46*b0d17251Schristos     dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, "EC",
47*b0d17251Schristos                                          selection,
48*b0d17251Schristos                                          libctx, propq);
49*b0d17251Schristos     if (dctx == NULL) {
50*b0d17251Schristos         fprintf(stderr, "OSSL_DECODER_CTX_new_for_pkey() failed\n");
51*b0d17251Schristos         goto cleanup;
52*b0d17251Schristos     }
53*b0d17251Schristos 
54*b0d17251Schristos     /*
55*b0d17251Schristos      * Set passphrase if provided; needed to decrypt encrypted PEM files.
56*b0d17251Schristos      * If the input is not encrypted, any passphrase provided is ignored.
57*b0d17251Schristos      *
58*b0d17251Schristos      * Alternative methods for specifying passphrases exist, such as a callback
59*b0d17251Schristos      * (see OSSL_DECODER_CTX_set_passphrase_cb(3)), which may be more useful for
60*b0d17251Schristos      * interactive applications which do not know if a passphrase should be
61*b0d17251Schristos      * prompted for in advance, or for GUI applications.
62*b0d17251Schristos      */
63*b0d17251Schristos     if (passphrase != NULL) {
64*b0d17251Schristos         if (OSSL_DECODER_CTX_set_passphrase(dctx,
65*b0d17251Schristos                                             (const unsigned char *)passphrase,
66*b0d17251Schristos                                             strlen(passphrase)) == 0) {
67*b0d17251Schristos             fprintf(stderr, "OSSL_DECODER_CTX_set_passphrase() failed\n");
68*b0d17251Schristos             goto cleanup;
69*b0d17251Schristos         }
70*b0d17251Schristos     }
71*b0d17251Schristos 
72*b0d17251Schristos     /* Do the decode, reading from file. */
73*b0d17251Schristos     if (OSSL_DECODER_from_fp(dctx, f) == 0) {
74*b0d17251Schristos         fprintf(stderr, "OSSL_DECODER_from_fp() failed\n");
75*b0d17251Schristos         goto cleanup;
76*b0d17251Schristos     }
77*b0d17251Schristos 
78*b0d17251Schristos     rv = 1;
79*b0d17251Schristos cleanup:
80*b0d17251Schristos     OSSL_DECODER_CTX_free(dctx);
81*b0d17251Schristos 
82*b0d17251Schristos     /*
83*b0d17251Schristos      * pkey is created by OSSL_DECODER_CTX_new_for_pkey, but we
84*b0d17251Schristos      * might fail subsequently, so ensure it's properly freed
85*b0d17251Schristos      * in this case.
86*b0d17251Schristos      */
87*b0d17251Schristos     if (rv == 0) {
88*b0d17251Schristos         EVP_PKEY_free(pkey);
89*b0d17251Schristos         pkey = NULL;
90*b0d17251Schristos     }
91*b0d17251Schristos 
92*b0d17251Schristos     return pkey;
93*b0d17251Schristos }
94*b0d17251Schristos 
95*b0d17251Schristos /*
96*b0d17251Schristos  * Store a EC public or private key to a file using PEM encoding.
97*b0d17251Schristos  *
98*b0d17251Schristos  * If a passphrase is supplied, the file is encrypted, otherwise
99*b0d17251Schristos  * it is unencrypted.
100*b0d17251Schristos  */
store_key(EVP_PKEY * pkey,FILE * f,const char * passphrase)101*b0d17251Schristos static int store_key(EVP_PKEY *pkey, FILE *f, const char *passphrase)
102*b0d17251Schristos {
103*b0d17251Schristos     int rv = 0;
104*b0d17251Schristos     int selection;
105*b0d17251Schristos     OSSL_ENCODER_CTX *ectx = NULL;
106*b0d17251Schristos 
107*b0d17251Schristos     /*
108*b0d17251Schristos      * Create a PEM encoder context.
109*b0d17251Schristos      *
110*b0d17251Schristos      * For raw (non-PEM-encoded) output, change "PEM" to "DER".
111*b0d17251Schristos      *
112*b0d17251Schristos      * The selection argument controls whether the private key is exported
113*b0d17251Schristos      * (EVP_PKEY_KEYPAIR), or only the public key (EVP_PKEY_PUBLIC_KEY). The
114*b0d17251Schristos      * former will fail if we only have a public key.
115*b0d17251Schristos      *
116*b0d17251Schristos      * Note that unlike the decode API, you cannot specify zero here.
117*b0d17251Schristos      *
118*b0d17251Schristos      * Purely for the sake of demonstration, here we choose to export the whole
119*b0d17251Schristos      * key if a passphrase is provided and the public key otherwise.
120*b0d17251Schristos      */
121*b0d17251Schristos     selection = (passphrase != NULL)
122*b0d17251Schristos         ? EVP_PKEY_KEYPAIR
123*b0d17251Schristos         : EVP_PKEY_PUBLIC_KEY;
124*b0d17251Schristos 
125*b0d17251Schristos     ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", NULL, propq);
126*b0d17251Schristos     if (ectx == NULL) {
127*b0d17251Schristos         fprintf(stderr, "OSSL_ENCODER_CTX_new_for_pkey() failed\n");
128*b0d17251Schristos         goto cleanup;
129*b0d17251Schristos     }
130*b0d17251Schristos 
131*b0d17251Schristos     /*
132*b0d17251Schristos      * Set passphrase if provided; the encoded output will then be encrypted
133*b0d17251Schristos      * using the passphrase.
134*b0d17251Schristos      *
135*b0d17251Schristos      * Alternative methods for specifying passphrases exist, such as a callback
136*b0d17251Schristos      * (see OSSL_ENCODER_CTX_set_passphrase_cb(3), just as for OSSL_DECODER_CTX;
137*b0d17251Schristos      * however you are less likely to need them as you presumably know whether
138*b0d17251Schristos      * encryption is desired in advance.
139*b0d17251Schristos      *
140*b0d17251Schristos      * Note that specifying a passphrase alone is not enough to cause the
141*b0d17251Schristos      * key to be encrypted. You must set both a cipher and a passphrase.
142*b0d17251Schristos      */
143*b0d17251Schristos     if (passphrase != NULL) {
144*b0d17251Schristos         /*
145*b0d17251Schristos          * Set cipher. Let's use AES-256-CBC, because it is
146*b0d17251Schristos          * more quantum resistant.
147*b0d17251Schristos          */
148*b0d17251Schristos         if (OSSL_ENCODER_CTX_set_cipher(ectx, "AES-256-CBC", propq) == 0) {
149*b0d17251Schristos             fprintf(stderr, "OSSL_ENCODER_CTX_set_cipher() failed\n");
150*b0d17251Schristos             goto cleanup;
151*b0d17251Schristos         }
152*b0d17251Schristos 
153*b0d17251Schristos         /* Set passphrase. */
154*b0d17251Schristos         if (OSSL_ENCODER_CTX_set_passphrase(ectx,
155*b0d17251Schristos                                             (const unsigned char *)passphrase,
156*b0d17251Schristos                                             strlen(passphrase)) == 0) {
157*b0d17251Schristos             fprintf(stderr, "OSSL_ENCODER_CTX_set_passphrase() failed\n");
158*b0d17251Schristos             goto cleanup;
159*b0d17251Schristos         }
160*b0d17251Schristos     }
161*b0d17251Schristos 
162*b0d17251Schristos     /* Do the encode, writing to the given file. */
163*b0d17251Schristos     if (OSSL_ENCODER_to_fp(ectx, f) == 0) {
164*b0d17251Schristos         fprintf(stderr, "OSSL_ENCODER_to_fp() failed\n");
165*b0d17251Schristos         goto cleanup;
166*b0d17251Schristos     }
167*b0d17251Schristos 
168*b0d17251Schristos     rv = 1;
169*b0d17251Schristos cleanup:
170*b0d17251Schristos     OSSL_ENCODER_CTX_free(ectx);
171*b0d17251Schristos     return rv;
172*b0d17251Schristos }
173*b0d17251Schristos 
main(int argc,char ** argv)174*b0d17251Schristos int main(int argc, char **argv)
175*b0d17251Schristos {
176*b0d17251Schristos     int rv = 1;
177*b0d17251Schristos     OSSL_LIB_CTX *libctx = NULL;
178*b0d17251Schristos     EVP_PKEY *pkey = NULL;
179*b0d17251Schristos     const char *passphrase_in = NULL, *passphrase_out = NULL;
180*b0d17251Schristos 
181*b0d17251Schristos     /* usage: ec_encode <passphrase-in> <passphrase-out> */
182*b0d17251Schristos     if (argc > 1 && argv[1][0])
183*b0d17251Schristos         passphrase_in = argv[1];
184*b0d17251Schristos 
185*b0d17251Schristos     if (argc > 2 && argv[2][0])
186*b0d17251Schristos         passphrase_out = argv[2];
187*b0d17251Schristos 
188*b0d17251Schristos     /* Decode PEM key from stdin and then PEM encode it to stdout. */
189*b0d17251Schristos     pkey = load_key(libctx, stdin, passphrase_in);
190*b0d17251Schristos     if (pkey == NULL) {
191*b0d17251Schristos         fprintf(stderr, "Failed to decode key\n");
192*b0d17251Schristos         goto cleanup;
193*b0d17251Schristos     }
194*b0d17251Schristos 
195*b0d17251Schristos     if (store_key(pkey, stdout, passphrase_out) == 0) {
196*b0d17251Schristos         fprintf(stderr, "Failed to encode key\n");
197*b0d17251Schristos         goto cleanup;
198*b0d17251Schristos     }
199*b0d17251Schristos 
200*b0d17251Schristos     rv = 0;
201*b0d17251Schristos cleanup:
202*b0d17251Schristos     EVP_PKEY_free(pkey);
203*b0d17251Schristos     OSSL_LIB_CTX_free(libctx);
204*b0d17251Schristos     return rv;
205*b0d17251Schristos }
206