Annotation of src/usr.bin/openssl/rsa.c, Revision 1.11
1.11 ! inoguchi 1: /* $OpenBSD: rsa.c,v 1.10 2018/02/07 05:47:55 jsing Exp $ */
1.1 jsing 2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3: * All rights reserved.
4: *
5: * This package is an SSL implementation written
6: * by Eric Young (eay@cryptsoft.com).
7: * The implementation was written so as to conform with Netscapes SSL.
8: *
9: * This library is free for commercial and non-commercial use as long as
10: * the following conditions are aheared to. The following conditions
11: * apply to all code found in this distribution, be it the RC4, RSA,
12: * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13: * included with this distribution is covered by the same copyright terms
14: * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15: *
16: * Copyright remains Eric Young's, and as such any Copyright notices in
17: * the code are not to be removed.
18: * If this package is used in a product, Eric Young should be given attribution
19: * as the author of the parts of the library used.
20: * This can be in the form of a textual message at program startup or
21: * in documentation (online or textual) provided with the package.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
31: * 3. All advertising materials mentioning features or use of this software
32: * must display the following acknowledgement:
33: * "This product includes cryptographic software written by
34: * Eric Young (eay@cryptsoft.com)"
35: * The word 'cryptographic' can be left out if the rouines from the library
36: * being used are not cryptographic related :-).
37: * 4. If you include any Windows specific code (or a derivative thereof) from
38: * the apps directory (application code) you must include an acknowledgement:
39: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40: *
41: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51: * SUCH DAMAGE.
52: *
53: * The licence and distribution terms for any publically available version or
54: * derivative of this code cannot be changed. i.e. this code cannot simply be
55: * copied and put under another distribution licence
56: * [including the GNU Public Licence.]
57: */
58:
59: #include <openssl/opensslconf.h>
60:
1.11 ! inoguchi 61: #include <ctype.h>
1.1 jsing 62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <time.h>
66:
67: #include "apps.h"
1.4 deraadt 68: #include "progs.h"
1.1 jsing 69:
70: #include <openssl/bio.h>
71: #include <openssl/bn.h>
72: #include <openssl/err.h>
73: #include <openssl/evp.h>
74: #include <openssl/pem.h>
75: #include <openssl/rsa.h>
76: #include <openssl/x509.h>
77:
1.3 doug 78: static struct {
79: int check;
80: const EVP_CIPHER *enc;
81: char *infile;
82: int informat;
83: int modulus;
84: int noout;
85: char *outfile;
86: int outformat;
87: char *passargin;
88: char *passargout;
89: int pubin;
90: int pubout;
91: int pvk_encr;
92: int sgckey;
93: int text;
94: } rsa_config;
95:
96: static int
97: rsa_opt_cipher(int argc, char **argv, int *argsused)
98: {
99: char *name = argv[0];
100:
101: if (*name++ != '-')
102: return (1);
103:
104: if ((rsa_config.enc = EVP_get_cipherbyname(name)) == NULL) {
105: fprintf(stderr, "Invalid cipher '%s'\n", name);
106: return (1);
107: }
108:
109: *argsused = 1;
110: return (0);
111: }
112:
113: static struct option rsa_options[] = {
114: {
115: .name = "check",
116: .desc = "Check consistency of RSA private key",
117: .type = OPTION_FLAG,
118: .opt.flag = &rsa_config.check,
119: },
120: {
121: .name = "in",
122: .argname = "file",
123: .desc = "Input file (default stdin)",
124: .type = OPTION_ARG,
125: .opt.arg = &rsa_config.infile,
126: },
127: {
128: .name = "inform",
129: .argname = "format",
130: .desc = "Input format (DER, NET or PEM (default))",
131: .type = OPTION_ARG_FORMAT,
132: .opt.value = &rsa_config.informat,
133: },
134: {
135: .name = "modulus",
136: .desc = "Print the RSA key modulus",
137: .type = OPTION_FLAG,
138: .opt.flag = &rsa_config.modulus,
139: },
140: {
141: .name = "noout",
142: .desc = "Do not print encoded version of the key",
143: .type = OPTION_FLAG,
144: .opt.flag = &rsa_config.noout,
145: },
146: {
147: .name = "out",
148: .argname = "file",
149: .desc = "Output file (default stdout)",
150: .type = OPTION_ARG,
151: .opt.arg = &rsa_config.outfile,
152: },
153: {
154: .name = "outform",
155: .argname = "format",
156: .desc = "Output format (DER, NET or PEM (default PEM))",
157: .type = OPTION_ARG_FORMAT,
158: .opt.value = &rsa_config.outformat,
159: },
160: {
161: .name = "passin",
162: .argname = "src",
163: .desc = "Input file passphrase source",
164: .type = OPTION_ARG,
165: .opt.arg = &rsa_config.passargin,
166: },
167: {
168: .name = "passout",
169: .argname = "src",
170: .desc = "Output file passphrase source",
171: .type = OPTION_ARG,
172: .opt.arg = &rsa_config.passargout,
173: },
174: {
175: .name = "pubin",
176: .desc = "Expect a public key (default private key)",
177: .type = OPTION_VALUE,
178: .value = 1,
179: .opt.value = &rsa_config.pubin,
180: },
181: {
182: .name = "pubout",
183: .desc = "Output a public key (default private key)",
184: .type = OPTION_VALUE,
185: .value = 1,
186: .opt.value = &rsa_config.pubout,
187: },
188: {
189: .name = "pvk-none",
190: .type = OPTION_VALUE,
191: .value = 0,
192: .opt.value = &rsa_config.pvk_encr,
193: },
194: {
195: .name = "pvk-strong",
196: .type = OPTION_VALUE,
197: .value = 2,
198: .opt.value = &rsa_config.pvk_encr,
199: },
200: {
201: .name = "pvk-weak",
202: .type = OPTION_VALUE,
203: .value = 1,
204: .opt.value = &rsa_config.pvk_encr,
205: },
206: {
207: .name = "RSAPublicKey_in",
208: .type = OPTION_VALUE,
209: .value = 2,
210: .opt.value = &rsa_config.pubin,
211: },
212: {
213: .name = "RSAPublicKey_out",
214: .type = OPTION_VALUE,
215: .value = 2,
216: .opt.value = &rsa_config.pubout,
217: },
218: {
219: .name = "sgckey",
220: .desc = "Use modified NET algorithm for IIS and SGC keys",
221: .type = OPTION_FLAG,
222: .opt.flag = &rsa_config.sgckey,
223: },
224: {
225: .name = "text",
226: .desc = "Print in plain text in addition to encoded",
227: .type = OPTION_FLAG,
228: .opt.flag = &rsa_config.text,
229: },
230: {
231: .name = NULL,
232: .type = OPTION_ARGV_FUNC,
233: .opt.argvfunc = rsa_opt_cipher,
234: },
235: { NULL }
236: };
237:
238: static void
239: show_ciphers(const OBJ_NAME *name, void *arg)
240: {
241: static int n;
1.11 ! inoguchi 242:
! 243: if (!islower((unsigned char)*name->name))
! 244: return;
1.3 doug 245:
246: fprintf(stderr, " -%-24s%s", name->name, (++n % 3 ? "" : "\n"));
247: }
1.1 jsing 248:
1.3 doug 249: static void
250: rsa_usage()
251: {
252: fprintf(stderr,
1.5 bcook 253: "usage: rsa [-ciphername] [-check] [-in file] "
1.3 doug 254: "[-inform fmt]\n"
255: " [-modulus] [-noout] [-out file] [-outform fmt] "
256: "[-passin src]\n"
257: " [-passout src] [-pubin] [-pubout] [-sgckey] [-text]\n\n");
258: options_usage(rsa_options);
259: fprintf(stderr, "\n");
260:
261: fprintf(stderr, "Valid ciphername values:\n\n");
262: OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_ciphers, NULL);
263: fprintf(stderr, "\n");
264: }
1.1 jsing 265:
266: int
267: rsa_main(int argc, char **argv)
268: {
269: int ret = 1;
270: RSA *rsa = NULL;
1.3 doug 271: int i;
1.1 jsing 272: BIO *out = NULL;
273: char *passin = NULL, *passout = NULL;
1.6 doug 274:
275: if (single_execution) {
1.9 deraadt 276: if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
1.6 doug 277: perror("pledge");
1.8 doug 278: exit(1);
279: }
1.6 doug 280: }
1.1 jsing 281:
1.3 doug 282: memset(&rsa_config, 0, sizeof(rsa_config));
283: rsa_config.pvk_encr = 2;
284: rsa_config.informat = FORMAT_PEM;
285: rsa_config.outformat = FORMAT_PEM;
1.1 jsing 286:
1.3 doug 287: if (options_parse(argc, argv, rsa_options, NULL, NULL) != 0) {
288: rsa_usage();
1.1 jsing 289: goto end;
290: }
291:
1.3 doug 292: if (!app_passwd(bio_err, rsa_config.passargin, rsa_config.passargout,
293: &passin, &passout)) {
1.1 jsing 294: BIO_printf(bio_err, "Error getting passwords\n");
295: goto end;
296: }
1.3 doug 297: if (rsa_config.check && rsa_config.pubin) {
1.1 jsing 298: BIO_printf(bio_err, "Only private keys can be checked\n");
299: goto end;
300: }
301: out = BIO_new(BIO_s_file());
302:
303: {
304: EVP_PKEY *pkey;
305:
1.3 doug 306: if (rsa_config.pubin) {
1.1 jsing 307: int tmpformat = -1;
1.3 doug 308: if (rsa_config.pubin == 2) {
309: if (rsa_config.informat == FORMAT_PEM)
1.1 jsing 310: tmpformat = FORMAT_PEMRSA;
1.3 doug 311: else if (rsa_config.informat == FORMAT_ASN1)
1.1 jsing 312: tmpformat = FORMAT_ASN1RSA;
1.3 doug 313: } else if (rsa_config.informat == FORMAT_NETSCAPE &&
314: rsa_config.sgckey)
1.1 jsing 315: tmpformat = FORMAT_IISSGC;
316: else
1.3 doug 317: tmpformat = rsa_config.informat;
1.1 jsing 318:
1.3 doug 319: pkey = load_pubkey(bio_err, rsa_config.infile,
1.5 bcook 320: tmpformat, 1, passin, "Public Key");
1.1 jsing 321: } else
1.3 doug 322: pkey = load_key(bio_err, rsa_config.infile,
323: (rsa_config.informat == FORMAT_NETSCAPE &&
324: rsa_config.sgckey ? FORMAT_IISSGC :
1.5 bcook 325: rsa_config.informat), 1, passin, "Private Key");
1.1 jsing 326:
327: if (pkey != NULL)
328: rsa = EVP_PKEY_get1_RSA(pkey);
329: EVP_PKEY_free(pkey);
330: }
331:
332: if (rsa == NULL) {
333: ERR_print_errors(bio_err);
334: goto end;
335: }
1.3 doug 336: if (rsa_config.outfile == NULL) {
1.1 jsing 337: BIO_set_fp(out, stdout, BIO_NOCLOSE);
338: } else {
1.3 doug 339: if (BIO_write_filename(out, rsa_config.outfile) <= 0) {
340: perror(rsa_config.outfile);
1.1 jsing 341: goto end;
342: }
343: }
344:
1.3 doug 345: if (rsa_config.text)
1.1 jsing 346: if (!RSA_print(out, rsa, 0)) {
1.3 doug 347: perror(rsa_config.outfile);
1.1 jsing 348: ERR_print_errors(bio_err);
349: goto end;
350: }
1.3 doug 351: if (rsa_config.modulus) {
1.1 jsing 352: BIO_printf(out, "Modulus=");
353: BN_print(out, rsa->n);
354: BIO_printf(out, "\n");
355: }
1.3 doug 356: if (rsa_config.check) {
1.1 jsing 357: int r = RSA_check_key(rsa);
358:
359: if (r == 1)
360: BIO_printf(out, "RSA key ok\n");
361: else if (r == 0) {
362: unsigned long err;
363:
364: while ((err = ERR_peek_error()) != 0 &&
365: ERR_GET_LIB(err) == ERR_LIB_RSA &&
366: ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
367: ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
1.3 doug 368: BIO_printf(out, "RSA key error: %s\n",
369: ERR_reason_error_string(err));
1.1 jsing 370: ERR_get_error(); /* remove e from error
371: * stack */
372: }
373: }
374: if (r == -1 || ERR_peek_error() != 0) { /* should happen only if
375: * r == -1 */
376: ERR_print_errors(bio_err);
377: goto end;
378: }
379: }
1.3 doug 380: if (rsa_config.noout) {
1.1 jsing 381: ret = 0;
382: goto end;
383: }
384: BIO_printf(bio_err, "writing RSA key\n");
1.3 doug 385: if (rsa_config.outformat == FORMAT_ASN1) {
386: if (rsa_config.pubout || rsa_config.pubin) {
387: if (rsa_config.pubout == 2)
1.1 jsing 388: i = i2d_RSAPublicKey_bio(out, rsa);
389: else
390: i = i2d_RSA_PUBKEY_bio(out, rsa);
391: } else
392: i = i2d_RSAPrivateKey_bio(out, rsa);
393: }
394: #ifndef OPENSSL_NO_RC4
1.3 doug 395: else if (rsa_config.outformat == FORMAT_NETSCAPE) {
1.1 jsing 396: unsigned char *p, *pp;
397: int size;
398:
399: i = 1;
1.3 doug 400: size = i2d_RSA_NET(rsa, NULL, NULL, rsa_config.sgckey);
1.1 jsing 401: if ((p = malloc(size)) == NULL) {
402: BIO_printf(bio_err, "Memory allocation failure\n");
403: goto end;
404: }
405: pp = p;
1.3 doug 406: i2d_RSA_NET(rsa, &p, NULL, rsa_config.sgckey);
1.1 jsing 407: BIO_write(out, (char *) pp, size);
408: free(pp);
409: }
410: #endif
1.3 doug 411: else if (rsa_config.outformat == FORMAT_PEM) {
412: if (rsa_config.pubout || rsa_config.pubin) {
413: if (rsa_config.pubout == 2)
1.1 jsing 414: i = PEM_write_bio_RSAPublicKey(out, rsa);
415: else
416: i = PEM_write_bio_RSA_PUBKEY(out, rsa);
417: } else
418: i = PEM_write_bio_RSAPrivateKey(out, rsa,
1.3 doug 419: rsa_config.enc, NULL, 0, NULL, passout);
1.1 jsing 420: #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
1.3 doug 421: } else if (rsa_config.outformat == FORMAT_MSBLOB ||
422: rsa_config.outformat == FORMAT_PVK) {
1.1 jsing 423: EVP_PKEY *pk;
424: pk = EVP_PKEY_new();
425: EVP_PKEY_set1_RSA(pk, rsa);
1.3 doug 426: if (rsa_config.outformat == FORMAT_PVK)
427: i = i2b_PVK_bio(out, pk, rsa_config.pvk_encr, 0,
428: passout);
429: else if (rsa_config.pubin || rsa_config.pubout)
1.1 jsing 430: i = i2b_PublicKey_bio(out, pk);
431: else
432: i = i2b_PrivateKey_bio(out, pk);
433: EVP_PKEY_free(pk);
434: #endif
435: } else {
1.3 doug 436: BIO_printf(bio_err,
437: "bad output format specified for outfile\n");
1.1 jsing 438: goto end;
439: }
440: if (i <= 0) {
441: BIO_printf(bio_err, "unable to write key\n");
442: ERR_print_errors(bio_err);
443: } else
444: ret = 0;
1.3 doug 445:
1.10 jsing 446: end:
1.3 doug 447: BIO_free_all(out);
448: RSA_free(rsa);
1.1 jsing 449: free(passin);
450: free(passout);
451:
452: return (ret);
453: }