63e88e6762f23cc17bfba81c2b597b0b0833ad3d
[openssl.git] / apps / rsa.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_RSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <stdlib.h>
17 # include <string.h>
18 # include <time.h>
19 # include "apps.h"
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/rsa.h>
23 # include <openssl/evp.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
26 # include <openssl/bn.h>
27
28 typedef enum OPTION_choice {
29     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
31     OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
32     OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, OPT_PVK_STRONG, OPT_PVK_WEAK,
33     OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
34 } OPTION_CHOICE;
35
36 OPTIONS rsa_options[] = {
37     {"help", OPT_HELP, '-', "Display this summary"},
38     {"inform", OPT_INFORM, 'f', "Input format, one of DER NET PEM"},
39     {"outform", OPT_OUTFORM, 'f', "Output format, one of DER NET PEM PVK"},
40     {"in", OPT_IN, 's', "Input file"},
41     {"out", OPT_OUT, '>', "Output file"},
42     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
43     {"pubout", OPT_PUBOUT, '-', "Output a public key"},
44     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
45     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
46     {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
47     {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
48     {"noout", OPT_NOOUT, '-', "Don't print key out"},
49     {"text", OPT_TEXT, '-', "Print the key in text"},
50     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
51     {"check", OPT_CHECK, '-', "Verify key consistency"},
52     {"", OPT_CIPHER, '-', "Any supported cipher"},
53 # ifdef OPENSSL_NO_RC4
54     {"pvk-strong", OPT_PVK_STRONG, '-'},
55     {"pvk-weak", OPT_PVK_WEAK, '-'},
56     {"pvk-none", OPT_PVK_NONE, '-'},
57 # endif
58 # ifndef OPENSSL_NO_ENGINE
59     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
60 # endif
61     {NULL}
62 };
63
64 int rsa_main(int argc, char **argv)
65 {
66     ENGINE *e = NULL;
67     BIO *out = NULL;
68     RSA *rsa = NULL;
69     const EVP_CIPHER *enc = NULL;
70     char *infile = NULL, *outfile = NULL, *prog;
71     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
72     int i, private = 0;
73     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
74     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
75 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
76     int pvk_encr = 2;
77 #endif
78     OPTION_CHOICE o;
79
80     prog = opt_init(argc, argv, rsa_options);
81     while ((o = opt_next()) != OPT_EOF) {
82         switch (o) {
83         case OPT_EOF:
84         case OPT_ERR:
85  opthelp:
86             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
87             goto end;
88         case OPT_HELP:
89             opt_help(rsa_options);
90             ret = 0;
91             goto end;
92         case OPT_INFORM:
93             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
94                 goto opthelp;
95             break;
96         case OPT_IN:
97             infile = opt_arg();
98             break;
99         case OPT_OUTFORM:
100             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
101                 goto opthelp;
102             break;
103         case OPT_OUT:
104             outfile = opt_arg();
105             break;
106         case OPT_PASSIN:
107             passinarg = opt_arg();
108             break;
109         case OPT_PASSOUT:
110             passoutarg = opt_arg();
111             break;
112         case OPT_ENGINE:
113             e = setup_engine(opt_arg(), 0);
114             break;
115         case OPT_PUBIN:
116             pubin = 1;
117             break;
118         case OPT_PUBOUT:
119             pubout = 1;
120             break;
121         case OPT_RSAPUBKEY_IN:
122             pubin = 2;
123             break;
124         case OPT_RSAPUBKEY_OUT:
125             pubout = 2;
126             break;
127 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
128         case OPT_PVK_STRONG:
129             pvk_encr = 2;
130             break;
131         case OPT_PVK_WEAK:
132             pvk_encr = 1;
133             break;
134         case OPT_PVK_NONE:
135             pvk_encr = 0;
136             break;
137 #else
138         case OPT_PVK_STRONG:
139         case OPT_PVK_WEAK:
140         case OPT_PVK_NONE:
141             break;
142 #endif
143         case OPT_NOOUT:
144             noout = 1;
145             break;
146         case OPT_TEXT:
147             text = 1;
148             break;
149         case OPT_MODULUS:
150             modulus = 1;
151             break;
152         case OPT_CHECK:
153             check = 1;
154             break;
155         case OPT_CIPHER:
156             if (!opt_cipher(opt_unknown(), &enc))
157                 goto opthelp;
158             break;
159         }
160     }
161     argc = opt_num_rest();
162     if (argc != 0)
163         goto opthelp;
164
165     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
166
167     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
168         BIO_printf(bio_err, "Error getting passwords\n");
169         goto end;
170     }
171     if (check && pubin) {
172         BIO_printf(bio_err, "Only private keys can be checked\n");
173         goto end;
174     }
175
176     {
177         EVP_PKEY *pkey;
178
179         if (pubin) {
180             int tmpformat = -1;
181             if (pubin == 2) {
182                 if (informat == FORMAT_PEM)
183                     tmpformat = FORMAT_PEMRSA;
184                 else if (informat == FORMAT_ASN1)
185                     tmpformat = FORMAT_ASN1RSA;
186             } else
187                 tmpformat = informat;
188
189             pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
190         } else
191             pkey = load_key(infile, informat, 1, passin, e, "Private Key");
192
193         if (pkey != NULL)
194             rsa = EVP_PKEY_get1_RSA(pkey);
195         EVP_PKEY_free(pkey);
196     }
197
198     if (rsa == NULL) {
199         ERR_print_errors(bio_err);
200         goto end;
201     }
202
203     out = bio_open_owner(outfile, outformat, private);
204     if (out == NULL)
205         goto end;
206
207     if (text) {
208         assert(pubin || private);
209         if (!RSA_print(out, rsa, 0)) {
210             perror(outfile);
211             ERR_print_errors(bio_err);
212             goto end;
213         }
214     }
215
216     if (modulus) {
217         const BIGNUM *n;
218         RSA_get0_key(rsa, &n, NULL, NULL);
219         BIO_printf(out, "Modulus=");
220         BN_print(out, n);
221         BIO_printf(out, "\n");
222     }
223
224     if (check) {
225         int r = RSA_check_key(rsa);
226
227         if (r == 1)
228             BIO_printf(out, "RSA key ok\n");
229         else if (r == 0) {
230             unsigned long err;
231
232             while ((err = ERR_peek_error()) != 0 &&
233                    ERR_GET_LIB(err) == ERR_LIB_RSA &&
234                    ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
235                    ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
236                 BIO_printf(out, "RSA key error: %s\n",
237                            ERR_reason_error_string(err));
238                 ERR_get_error(); /* remove e from error stack */
239             }
240         }
241
242         /* should happen only if r == -1 */
243         if (r == -1 || ERR_peek_error() != 0) {
244             ERR_print_errors(bio_err);
245             goto end;
246         }
247     }
248
249     if (noout) {
250         ret = 0;
251         goto end;
252     }
253     BIO_printf(bio_err, "writing RSA key\n");
254     if (outformat == FORMAT_ASN1) {
255         if (pubout || pubin) {
256             if (pubout == 2)
257                 i = i2d_RSAPublicKey_bio(out, rsa);
258             else
259                 i = i2d_RSA_PUBKEY_bio(out, rsa);
260         } else {
261             assert(private);
262             i = i2d_RSAPrivateKey_bio(out, rsa);
263         }
264     }
265     else if (outformat == FORMAT_PEM) {
266         if (pubout || pubin) {
267             if (pubout == 2)
268                 i = PEM_write_bio_RSAPublicKey(out, rsa);
269             else
270                 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
271         } else {
272             assert(private);
273             i = PEM_write_bio_RSAPrivateKey(out, rsa,
274                                             enc, NULL, 0, NULL, passout);
275         }
276 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
277     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
278         EVP_PKEY *pk;
279         pk = EVP_PKEY_new();
280         EVP_PKEY_set1_RSA(pk, rsa);
281         if (outformat == FORMAT_PVK) {
282             if (pubin) {
283                 BIO_printf(bio_err, "PVK form impossible with public key input\n");
284                 EVP_PKEY_free(pk);
285                 goto end;
286             }
287             assert(private);
288             i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
289         } else if (pubin || pubout) {
290             i = i2b_PublicKey_bio(out, pk);
291         } else {
292             assert(private);
293             i = i2b_PrivateKey_bio(out, pk);
294         }
295         EVP_PKEY_free(pk);
296 # endif
297     } else {
298         BIO_printf(bio_err, "bad output format specified for outfile\n");
299         goto end;
300     }
301     if (i <= 0) {
302         BIO_printf(bio_err, "unable to write key\n");
303         ERR_print_errors(bio_err);
304     } else
305         ret = 0;
306  end:
307     BIO_free_all(out);
308     RSA_free(rsa);
309     OPENSSL_free(passin);
310     OPENSSL_free(passout);
311     return (ret);
312 }
313 #endif