[BACK]Return to dsa.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/dsa.c, Revision 1.18

1.18    ! tb          1: /* $OpenBSD: dsa.c,v 1.17 2022/11/11 17:07:38 joshua 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>       /* for OPENSSL_NO_DSA */
                     60:
                     61: #include <stdio.h>
                     62: #include <stdlib.h>
                     63: #include <time.h>
                     64: #include <string.h>
                     65:
                     66: #include "apps.h"
                     67:
                     68: #include <openssl/bio.h>
                     69: #include <openssl/bn.h>
                     70: #include <openssl/dsa.h>
                     71: #include <openssl/err.h>
                     72: #include <openssl/evp.h>
                     73: #include <openssl/pem.h>
                     74: #include <openssl/x509.h>
                     75:
1.3       doug       76: static struct {
                     77:        const EVP_CIPHER *enc;
                     78:        char *infile;
                     79:        int informat;
                     80:        int modulus;
                     81:        int noout;
                     82:        char *outfile;
                     83:        int outformat;
                     84:        char *passargin;
                     85:        char *passargout;
                     86:        int pubin;
                     87:        int pubout;
                     88:        int pvk_encr;
                     89:        int text;
1.18    ! tb         90: } cfg;
1.3       doug       91:
                     92: static int
                     93: dsa_opt_enc(int argc, char **argv, int *argsused)
                     94: {
                     95:        char *name = argv[0];
                     96:
                     97:        if (*name++ != '-')
                     98:                return (1);
                     99:
1.18    ! tb        100:        if ((cfg.enc = EVP_get_cipherbyname(name)) != NULL) {
1.3       doug      101:                *argsused = 1;
                    102:                return (0);
                    103:        }
                    104:
                    105:        return (1);
                    106: }
                    107:
1.15      guenther  108: static const struct option dsa_options[] = {
1.3       doug      109:        {
                    110:                .name = "in",
                    111:                .argname = "file",
                    112:                .desc = "Input file (default stdin)",
                    113:                .type = OPTION_ARG,
1.18    ! tb        114:                .opt.arg = &cfg.infile,
1.3       doug      115:        },
                    116:        {
                    117:                .name = "inform",
                    118:                .argname = "format",
                    119:                .desc = "Input format (PEM (default) or any other supported"
                    120:                    " format)",
                    121:                .type = OPTION_ARG_FORMAT,
1.18    ! tb        122:                .opt.value = &cfg.informat,
1.3       doug      123:        },
                    124:        {
1.14      inoguchi  125:                .name = "modulus",
                    126:                .desc = "Print the DSA public value",
                    127:                .type = OPTION_FLAG,
1.18    ! tb        128:                .opt.flag = &cfg.modulus,
1.14      inoguchi  129:        },
                    130:        {
1.3       doug      131:                .name = "noout",
                    132:                .desc = "No output",
                    133:                .type = OPTION_FLAG,
1.18    ! tb        134:                .opt.flag = &cfg.noout,
1.3       doug      135:        },
                    136:        {
                    137:                .name = "out",
                    138:                .argname = "file",
                    139:                .desc = "Output file (default stdout)",
                    140:                .type = OPTION_ARG,
1.18    ! tb        141:                .opt.arg = &cfg.outfile,
1.3       doug      142:        },
                    143:        {
                    144:                .name = "outform",
                    145:                .argname = "format",
                    146:                .desc = "Output format (DER, MSBLOB, PEM (default) or PVK)",
                    147:                .type = OPTION_ARG_FORMAT,
1.18    ! tb        148:                .opt.value = &cfg.outformat,
1.3       doug      149:        },
                    150:        {
                    151:                .name = "passin",
                    152:                .argname = "source",
                    153:                .desc = "Input file passphrase source",
                    154:                .type = OPTION_ARG,
1.18    ! tb        155:                .opt.arg = &cfg.passargin,
1.3       doug      156:        },
                    157:        {
                    158:                .name = "passout",
                    159:                .argname = "source",
                    160:                .desc = "Output file passphrase source",
                    161:                .type = OPTION_ARG,
1.18    ! tb        162:                .opt.arg = &cfg.passargout,
1.3       doug      163:        },
                    164:        {
                    165:                .name = "pubin",
                    166:                .desc = "Read a public key from the input file instead of"
                    167:                    " private key",
                    168:                .type = OPTION_FLAG,
1.18    ! tb        169:                .opt.flag = &cfg.pubin,
1.3       doug      170:        },
                    171:        {
                    172:                .name = "pubout",
                    173:                .desc = "Output a public key instead of private key",
                    174:                .type = OPTION_FLAG,
1.18    ! tb        175:                .opt.flag = &cfg.pubout,
1.3       doug      176:        },
                    177:        {
                    178:                .name = "pvk-none",
                    179:                .desc = "PVK encryption level",
                    180:                .type = OPTION_VALUE,
                    181:                .value = 0,
1.18    ! tb        182:                .opt.value = &cfg.pvk_encr,
1.3       doug      183:        },
                    184:        {
                    185:                .name = "pvk-strong",
                    186:                .desc = "PVK encryption level (default)",
                    187:                .type = OPTION_VALUE,
                    188:                .value = 2,
1.18    ! tb        189:                .opt.value = &cfg.pvk_encr,
1.3       doug      190:        },
                    191:        {
                    192:                .name = "pvk-weak",
                    193:                .desc = "PVK encryption level",
                    194:                .type = OPTION_VALUE,
                    195:                .value = 1,
1.18    ! tb        196:                .opt.value = &cfg.pvk_encr,
1.3       doug      197:        },
                    198:        {
                    199:                .name = "text",
                    200:                .desc = "Print the key in text form",
                    201:                .type = OPTION_FLAG,
1.18    ! tb        202:                .opt.flag = &cfg.text,
1.3       doug      203:        },
                    204:        {
                    205:                .name = NULL,
                    206:                .type = OPTION_ARGV_FUNC,
                    207:                .opt.argvfunc = dsa_opt_enc,
                    208:        },
                    209:        { NULL },
                    210: };
                    211:
                    212: static void
                    213: dsa_usage(void)
                    214: {
1.13      inoguchi  215:        int n = 0;
                    216:
1.3       doug      217:        fprintf(stderr,
1.14      inoguchi  218:            "usage: dsa [-in file] [-inform format] [-modulus] [-noout]\n"
1.3       doug      219:            "    [-out file] [-outform format] [-passin src] [-passout src]\n"
                    220:            "    [-pubin] [-pubout] [-pvk-none | -pvk-strong | -pvk-weak]\n"
                    221:            "    [-text] [-ciphername]\n\n");
                    222:        options_usage(dsa_options);
                    223:        fprintf(stderr, "\n");
                    224:
                    225:        fprintf(stderr, "Valid ciphername values:\n\n");
1.13      inoguchi  226:        OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_cipher, &n);
1.3       doug      227:        fprintf(stderr, "\n");
                    228: }
1.1       jsing     229:
                    230: int
                    231: dsa_main(int argc, char **argv)
                    232: {
                    233:        int ret = 1;
                    234:        DSA *dsa = NULL;
1.3       doug      235:        int i;
1.1       jsing     236:        BIO *in = NULL, *out = NULL;
                    237:        char *passin = NULL, *passout = NULL;
1.6       doug      238:
1.17      joshua    239:        if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
                    240:                perror("pledge");
                    241:                exit(1);
1.6       doug      242:        }
1.1       jsing     243:
1.18    ! tb        244:        memset(&cfg, 0, sizeof(cfg));
1.1       jsing     245:
1.18    ! tb        246:        cfg.pvk_encr = 2;
        !           247:        cfg.informat = FORMAT_PEM;
        !           248:        cfg.outformat = FORMAT_PEM;
1.1       jsing     249:
1.3       doug      250:        if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) {
                    251:                dsa_usage();
1.1       jsing     252:                goto end;
                    253:        }
                    254:
1.18    ! tb        255:        if (!app_passwd(bio_err, cfg.passargin, cfg.passargout,
1.3       doug      256:            &passin, &passout)) {
1.1       jsing     257:                BIO_printf(bio_err, "Error getting passwords\n");
                    258:                goto end;
                    259:        }
1.3       doug      260:
1.1       jsing     261:        in = BIO_new(BIO_s_file());
                    262:        out = BIO_new(BIO_s_file());
1.3       doug      263:        if (in == NULL || out == NULL) {
1.1       jsing     264:                ERR_print_errors(bio_err);
                    265:                goto end;
                    266:        }
1.18    ! tb        267:        if (cfg.infile == NULL)
1.1       jsing     268:                BIO_set_fp(in, stdin, BIO_NOCLOSE);
                    269:        else {
1.18    ! tb        270:                if (BIO_read_filename(in, cfg.infile) <= 0) {
        !           271:                        perror(cfg.infile);
1.1       jsing     272:                        goto end;
                    273:                }
                    274:        }
                    275:
                    276:        BIO_printf(bio_err, "read DSA key\n");
                    277:
                    278:        {
                    279:                EVP_PKEY *pkey;
                    280:
1.18    ! tb        281:                if (cfg.pubin)
        !           282:                        pkey = load_pubkey(bio_err, cfg.infile,
        !           283:                            cfg.informat, 1, passin, "Public Key");
1.1       jsing     284:                else
1.18    ! tb        285:                        pkey = load_key(bio_err, cfg.infile,
        !           286:                            cfg.informat, 1, passin, "Private Key");
1.1       jsing     287:
                    288:                if (pkey) {
                    289:                        dsa = EVP_PKEY_get1_DSA(pkey);
                    290:                        EVP_PKEY_free(pkey);
                    291:                }
                    292:        }
                    293:        if (dsa == NULL) {
                    294:                BIO_printf(bio_err, "unable to load Key\n");
                    295:                ERR_print_errors(bio_err);
                    296:                goto end;
                    297:        }
1.18    ! tb        298:        if (cfg.outfile == NULL) {
1.1       jsing     299:                BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    300:        } else {
1.18    ! tb        301:                if (BIO_write_filename(out, cfg.outfile) <= 0) {
        !           302:                        perror(cfg.outfile);
1.1       jsing     303:                        goto end;
                    304:                }
                    305:        }
                    306:
1.18    ! tb        307:        if (cfg.text) {
1.1       jsing     308:                if (!DSA_print(out, dsa, 0)) {
1.18    ! tb        309:                        perror(cfg.outfile);
1.1       jsing     310:                        ERR_print_errors(bio_err);
                    311:                        goto end;
                    312:                }
                    313:        }
1.18    ! tb        314:        if (cfg.modulus) {
1.1       jsing     315:                fprintf(stdout, "Public Key=");
1.16      tb        316:                BN_print(out, DSA_get0_pub_key(dsa));
1.1       jsing     317:                fprintf(stdout, "\n");
                    318:        }
1.18    ! tb        319:        if (cfg.noout)
1.1       jsing     320:                goto end;
                    321:        BIO_printf(bio_err, "writing DSA key\n");
1.18    ! tb        322:        if (cfg.outformat == FORMAT_ASN1) {
        !           323:                if (cfg.pubin || cfg.pubout)
1.1       jsing     324:                        i = i2d_DSA_PUBKEY_bio(out, dsa);
                    325:                else
                    326:                        i = i2d_DSAPrivateKey_bio(out, dsa);
1.18    ! tb        327:        } else if (cfg.outformat == FORMAT_PEM) {
        !           328:                if (cfg.pubin || cfg.pubout)
1.1       jsing     329:                        i = PEM_write_bio_DSA_PUBKEY(out, dsa);
                    330:                else
1.18    ! tb        331:                        i = PEM_write_bio_DSAPrivateKey(out, dsa, cfg.enc,
1.1       jsing     332:                            NULL, 0, NULL, passout);
                    333: #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
1.18    ! tb        334:        } else if (cfg.outformat == FORMAT_MSBLOB ||
        !           335:            cfg.outformat == FORMAT_PVK) {
1.1       jsing     336:                EVP_PKEY *pk;
                    337:                pk = EVP_PKEY_new();
                    338:                EVP_PKEY_set1_DSA(pk, dsa);
1.18    ! tb        339:                if (cfg.outformat == FORMAT_PVK)
        !           340:                        i = i2b_PVK_bio(out, pk, cfg.pvk_encr, 0,
1.3       doug      341:                            passout);
1.18    ! tb        342:                else if (cfg.pubin || cfg.pubout)
1.1       jsing     343:                        i = i2b_PublicKey_bio(out, pk);
                    344:                else
                    345:                        i = i2b_PrivateKey_bio(out, pk);
                    346:                EVP_PKEY_free(pk);
                    347: #endif
                    348:        } else {
                    349:                BIO_printf(bio_err, "bad output format specified for outfile\n");
                    350:                goto end;
                    351:        }
                    352:        if (i <= 0) {
                    353:                BIO_printf(bio_err, "unable to write private key\n");
                    354:                ERR_print_errors(bio_err);
                    355:        } else
                    356:                ret = 0;
1.11      jsing     357:  end:
1.1       jsing     358:        BIO_free(in);
1.10      jsing     359:        BIO_free_all(out);
                    360:        DSA_free(dsa);
1.1       jsing     361:        free(passin);
                    362:        free(passout);
                    363:
                    364:        return (ret);
                    365: }