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

Annotation of src/usr.bin/openssl/pkeyutl.c, Revision 1.2

1.2     ! jsing       1: /* $OpenBSD: pkeyutl.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */
1.1       jsing       2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
                      3:  * project 2006.
                      4:  */
                      5: /* ====================================================================
                      6:  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  *
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  *
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in
                     17:  *    the documentation and/or other materials provided with the
                     18:  *    distribution.
                     19:  *
                     20:  * 3. All advertising materials mentioning features or use of this
                     21:  *    software must display the following acknowledgment:
                     22:  *    "This product includes software developed by the OpenSSL Project
                     23:  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
                     24:  *
                     25:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
                     26:  *    endorse or promote products derived from this software without
                     27:  *    prior written permission. For written permission, please contact
                     28:  *    licensing@OpenSSL.org.
                     29:  *
                     30:  * 5. Products derived from this software may not be called "OpenSSL"
                     31:  *    nor may "OpenSSL" appear in their names without prior written
                     32:  *    permission of the OpenSSL Project.
                     33:  *
                     34:  * 6. Redistributions of any form whatsoever must retain the following
                     35:  *    acknowledgment:
                     36:  *    "This product includes software developed by the OpenSSL Project
                     37:  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
                     38:  *
                     39:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
                     40:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     41:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     42:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
                     43:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     44:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     45:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     46:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     47:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     48:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     49:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                     50:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                     51:  * ====================================================================
                     52:  *
                     53:  * This product includes cryptographic software written by Eric Young
                     54:  * (eay@cryptsoft.com).  This product includes software written by Tim
                     55:  * Hudson (tjh@cryptsoft.com).
                     56:  *
                     57:  */
                     58:
                     59: #include <string.h>
                     60:
                     61: #include "apps.h"
                     62:
                     63: #include <openssl/err.h>
                     64: #include <openssl/evp.h>
                     65: #include <openssl/pem.h>
                     66:
                     67: #define KEY_PRIVKEY    1
                     68: #define KEY_PUBKEY     2
                     69: #define KEY_CERT       3
                     70:
                     71: static void usage(void);
                     72:
                     73: static EVP_PKEY_CTX *init_ctx(int *pkeysize,
                     74:     char *keyfile, int keyform, int key_type,
                     75:     char *passargin, int pkey_op, ENGINE * e);
                     76:
                     77: static int setup_peer(BIO * err, EVP_PKEY_CTX * ctx, int peerform,
                     78:     const char *file);
                     79:
                     80: static int do_keyop(EVP_PKEY_CTX * ctx, int pkey_op,
                     81:     unsigned char *out, size_t * poutlen,
                     82:     unsigned char *in, size_t inlen);
                     83:
                     84: int pkeyutl_main(int argc, char **);
                     85:
                     86: int
                     87: pkeyutl_main(int argc, char **argv)
                     88: {
                     89:        BIO *in = NULL, *out = NULL;
                     90:        char *infile = NULL, *outfile = NULL, *sigfile = NULL;
                     91:        ENGINE *e = NULL;
                     92:        int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
                     93:        int keyform = FORMAT_PEM, peerform = FORMAT_PEM;
                     94:        char badarg = 0, rev = 0;
                     95:        char hexdump = 0, asn1parse = 0;
                     96:        EVP_PKEY_CTX *ctx = NULL;
                     97:        char *passargin = NULL;
                     98:        int keysize = -1;
                     99:
                    100:        unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
                    101:        size_t buf_outlen;
                    102:        int buf_inlen = 0, siglen = -1;
                    103:
                    104:        int ret = 1, rv = -1;
                    105:
                    106:        argc--;
                    107:        argv++;
                    108:
                    109:        OpenSSL_add_all_algorithms();
                    110:
                    111:        while (argc >= 1) {
                    112:                if (!strcmp(*argv, "-in")) {
                    113:                        if (--argc < 1)
                    114:                                badarg = 1;
                    115:                        else
                    116:                                infile = *(++argv);
                    117:                } else if (!strcmp(*argv, "-out")) {
                    118:                        if (--argc < 1)
                    119:                                badarg = 1;
                    120:                        else
                    121:                                outfile = *(++argv);
                    122:                } else if (!strcmp(*argv, "-sigfile")) {
                    123:                        if (--argc < 1)
                    124:                                badarg = 1;
                    125:                        else
                    126:                                sigfile = *(++argv);
                    127:                } else if (!strcmp(*argv, "-inkey")) {
                    128:                        if (--argc < 1)
                    129:                                badarg = 1;
                    130:                        else {
                    131:                                ctx = init_ctx(&keysize,
                    132:                                    *(++argv), keyform, key_type,
                    133:                                    passargin, pkey_op, e);
                    134:                                if (!ctx) {
                    135:                                        BIO_puts(bio_err,
                    136:                                            "Error initializing context\n");
                    137:                                        ERR_print_errors(bio_err);
                    138:                                        badarg = 1;
                    139:                                }
                    140:                        }
                    141:                } else if (!strcmp(*argv, "-peerkey")) {
                    142:                        if (--argc < 1)
                    143:                                badarg = 1;
                    144:                        else if (!setup_peer(bio_err, ctx, peerform, *(++argv)))
                    145:                                badarg = 1;
                    146:                } else if (!strcmp(*argv, "-passin")) {
                    147:                        if (--argc < 1)
                    148:                                badarg = 1;
                    149:                        else
                    150:                                passargin = *(++argv);
                    151:                } else if (strcmp(*argv, "-peerform") == 0) {
                    152:                        if (--argc < 1)
                    153:                                badarg = 1;
                    154:                        else
                    155:                                peerform = str2fmt(*(++argv));
                    156:                } else if (strcmp(*argv, "-keyform") == 0) {
                    157:                        if (--argc < 1)
                    158:                                badarg = 1;
                    159:                        else
                    160:                                keyform = str2fmt(*(++argv));
                    161:                }
                    162: #ifndef OPENSSL_NO_ENGINE
                    163:                else if (!strcmp(*argv, "-engine")) {
                    164:                        if (--argc < 1)
                    165:                                badarg = 1;
                    166:                        else
                    167:                                e = setup_engine(bio_err, *(++argv), 0);
                    168:                }
                    169: #endif
                    170:                else if (!strcmp(*argv, "-pubin"))
                    171:                        key_type = KEY_PUBKEY;
                    172:                else if (!strcmp(*argv, "-certin"))
                    173:                        key_type = KEY_CERT;
                    174:                else if (!strcmp(*argv, "-asn1parse"))
                    175:                        asn1parse = 1;
                    176:                else if (!strcmp(*argv, "-hexdump"))
                    177:                        hexdump = 1;
                    178:                else if (!strcmp(*argv, "-sign"))
                    179:                        pkey_op = EVP_PKEY_OP_SIGN;
                    180:                else if (!strcmp(*argv, "-verify"))
                    181:                        pkey_op = EVP_PKEY_OP_VERIFY;
                    182:                else if (!strcmp(*argv, "-verifyrecover"))
                    183:                        pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
                    184:                else if (!strcmp(*argv, "-rev"))
                    185:                        rev = 1;
                    186:                else if (!strcmp(*argv, "-encrypt"))
                    187:                        pkey_op = EVP_PKEY_OP_ENCRYPT;
                    188:                else if (!strcmp(*argv, "-decrypt"))
                    189:                        pkey_op = EVP_PKEY_OP_DECRYPT;
                    190:                else if (!strcmp(*argv, "-derive"))
                    191:                        pkey_op = EVP_PKEY_OP_DERIVE;
                    192:                else if (strcmp(*argv, "-pkeyopt") == 0) {
                    193:                        if (--argc < 1)
                    194:                                badarg = 1;
                    195:                        else if (!ctx) {
                    196:                                BIO_puts(bio_err,
                    197:                                    "-pkeyopt command before -inkey\n");
                    198:                                badarg = 1;
                    199:                        } else if (pkey_ctrl_string(ctx, *(++argv)) <= 0) {
                    200:                                BIO_puts(bio_err, "parameter setting error\n");
                    201:                                ERR_print_errors(bio_err);
                    202:                                goto end;
                    203:                        }
                    204:                } else
                    205:                        badarg = 1;
                    206:                if (badarg) {
                    207:                        usage();
                    208:                        goto end;
                    209:                }
                    210:                argc--;
                    211:                argv++;
                    212:        }
                    213:
                    214:        if (!ctx) {
                    215:                usage();
                    216:                goto end;
                    217:        }
                    218:        if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
                    219:                BIO_puts(bio_err, "Signature file specified for non verify\n");
                    220:                goto end;
                    221:        }
                    222:        if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
                    223:                BIO_puts(bio_err, "No signature file specified for verify\n");
                    224:                goto end;
                    225:        }
                    226:
                    227:        if (pkey_op != EVP_PKEY_OP_DERIVE) {
                    228:                if (infile) {
                    229:                        if (!(in = BIO_new_file(infile, "rb"))) {
                    230:                                BIO_puts(bio_err,
                    231:                                    "Error Opening Input File\n");
                    232:                                ERR_print_errors(bio_err);
                    233:                                goto end;
                    234:                        }
                    235:                } else
                    236:                        in = BIO_new_fp(stdin, BIO_NOCLOSE);
                    237:        }
                    238:        if (outfile) {
                    239:                if (!(out = BIO_new_file(outfile, "wb"))) {
                    240:                        BIO_printf(bio_err, "Error Creating Output File\n");
                    241:                        ERR_print_errors(bio_err);
                    242:                        goto end;
                    243:                }
                    244:        } else {
                    245:                out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    246:        }
                    247:
                    248:        if (sigfile) {
                    249:                BIO *sigbio = BIO_new_file(sigfile, "rb");
                    250:                if (!sigbio) {
                    251:                        BIO_printf(bio_err, "Can't open signature file %s\n",
                    252:                            sigfile);
                    253:                        goto end;
                    254:                }
                    255:                siglen = bio_to_mem(&sig, keysize * 10, sigbio);
                    256:                BIO_free(sigbio);
                    257:                if (siglen <= 0) {
                    258:                        BIO_printf(bio_err, "Error reading signature data\n");
                    259:                        goto end;
                    260:                }
                    261:        }
                    262:        if (in) {
                    263:                /* Read the input data */
                    264:                buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
                    265:                if (buf_inlen <= 0) {
                    266:                        BIO_printf(bio_err, "Error reading input Data\n");
                    267:                        exit(1);
                    268:                }
                    269:                if (rev) {
                    270:                        size_t i;
                    271:                        unsigned char ctmp;
                    272:                        size_t l = (size_t) buf_inlen;
                    273:                        for (i = 0; i < l / 2; i++) {
                    274:                                ctmp = buf_in[i];
                    275:                                buf_in[i] = buf_in[l - 1 - i];
                    276:                                buf_in[l - 1 - i] = ctmp;
                    277:                        }
                    278:                }
                    279:        }
                    280:        if (pkey_op == EVP_PKEY_OP_VERIFY) {
                    281:                rv = EVP_PKEY_verify(ctx, sig, (size_t) siglen,
                    282:                    buf_in, (size_t) buf_inlen);
                    283:                if (rv == 0)
                    284:                        BIO_puts(out, "Signature Verification Failure\n");
                    285:                else if (rv == 1)
                    286:                        BIO_puts(out, "Signature Verified Successfully\n");
                    287:                if (rv >= 0)
                    288:                        goto end;
                    289:        } else {
                    290:                rv = do_keyop(ctx, pkey_op, NULL, (size_t *) & buf_outlen,
                    291:                    buf_in, (size_t) buf_inlen);
                    292:                if (rv > 0) {
                    293:                        buf_out = malloc(buf_outlen);
                    294:                        if (!buf_out)
                    295:                                rv = -1;
                    296:                        else
                    297:                                rv = do_keyop(ctx, pkey_op,
                    298:                                    buf_out, (size_t *) & buf_outlen,
                    299:                                    buf_in, (size_t) buf_inlen);
                    300:                }
                    301:        }
                    302:
                    303:        if (rv <= 0) {
                    304:                BIO_printf(bio_err, "Public Key operation error\n");
                    305:                ERR_print_errors(bio_err);
                    306:                goto end;
                    307:        }
                    308:        ret = 0;
                    309:        if (asn1parse) {
                    310:                if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
                    311:                        ERR_print_errors(bio_err);
                    312:        } else if (hexdump)
                    313:                BIO_dump(out, (char *) buf_out, buf_outlen);
                    314:        else
                    315:                BIO_write(out, buf_out, buf_outlen);
                    316:
                    317: end:
                    318:        if (ctx)
                    319:                EVP_PKEY_CTX_free(ctx);
                    320:        BIO_free(in);
                    321:        BIO_free_all(out);
                    322:        free(buf_in);
                    323:        free(buf_out);
                    324:        free(sig);
                    325:
                    326:        return ret;
                    327: }
                    328:
                    329: static void
                    330: usage()
                    331: {
                    332:        BIO_printf(bio_err, "Usage: pkeyutl [options]\n");
                    333:        BIO_printf(bio_err, "-in file        input file\n");
                    334:        BIO_printf(bio_err, "-out file       output file\n");
                    335:        BIO_printf(bio_err, "-sigfile file signature file (verify operation only)\n");
                    336:        BIO_printf(bio_err, "-inkey file     input key\n");
                    337:        BIO_printf(bio_err, "-keyform arg    private key format - default PEM\n");
                    338:        BIO_printf(bio_err, "-pubin          input is a public key\n");
                    339:        BIO_printf(bio_err, "-certin         input is a certificate carrying a public key\n");
                    340:        BIO_printf(bio_err, "-pkeyopt X:Y    public key options\n");
                    341:        BIO_printf(bio_err, "-sign           sign with private key\n");
                    342:        BIO_printf(bio_err, "-verify         verify with public key\n");
                    343:        BIO_printf(bio_err, "-verifyrecover  verify with public key, recover original data\n");
                    344:        BIO_printf(bio_err, "-encrypt        encrypt with public key\n");
                    345:        BIO_printf(bio_err, "-decrypt        decrypt with private key\n");
                    346:        BIO_printf(bio_err, "-derive         derive shared secret\n");
                    347:        BIO_printf(bio_err, "-hexdump        hex dump output\n");
                    348: #ifndef OPENSSL_NO_ENGINE
                    349:        BIO_printf(bio_err, "-engine e       use engine e, possibly a hardware device.\n");
                    350: #endif
                    351:        BIO_printf(bio_err, "-passin arg     pass phrase source\n");
                    352:
                    353: }
                    354:
                    355: static EVP_PKEY_CTX *
                    356: init_ctx(int *pkeysize,
                    357:     char *keyfile, int keyform, int key_type,
                    358:     char *passargin, int pkey_op, ENGINE * e)
                    359: {
                    360:        EVP_PKEY *pkey = NULL;
                    361:        EVP_PKEY_CTX *ctx = NULL;
                    362:        char *passin = NULL;
                    363:        int rv = -1;
                    364:        X509 *x;
                    365:        if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
                    366:                || (pkey_op == EVP_PKEY_OP_DERIVE))
                    367:            && (key_type != KEY_PRIVKEY)) {
                    368:                BIO_printf(bio_err, "A private key is needed for this operation\n");
                    369:                goto end;
                    370:        }
                    371:        if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
                    372:                BIO_printf(bio_err, "Error getting password\n");
                    373:                goto end;
                    374:        }
                    375:        switch (key_type) {
                    376:        case KEY_PRIVKEY:
                    377:                pkey = load_key(bio_err, keyfile, keyform, 0,
                    378:                    passin, e, "Private Key");
                    379:                break;
                    380:
                    381:        case KEY_PUBKEY:
                    382:                pkey = load_pubkey(bio_err, keyfile, keyform, 0,
                    383:                    NULL, e, "Public Key");
                    384:                break;
                    385:
                    386:        case KEY_CERT:
                    387:                x = load_cert(bio_err, keyfile, keyform,
                    388:                    NULL, e, "Certificate");
                    389:                if (x) {
                    390:                        pkey = X509_get_pubkey(x);
                    391:                        X509_free(x);
                    392:                }
                    393:                break;
                    394:
                    395:        }
                    396:
                    397:        *pkeysize = EVP_PKEY_size(pkey);
                    398:
                    399:        if (!pkey)
                    400:                goto end;
                    401:
                    402:        ctx = EVP_PKEY_CTX_new(pkey, e);
                    403:
                    404:        EVP_PKEY_free(pkey);
                    405:
                    406:        if (!ctx)
                    407:                goto end;
                    408:
                    409:        switch (pkey_op) {
                    410:        case EVP_PKEY_OP_SIGN:
                    411:                rv = EVP_PKEY_sign_init(ctx);
                    412:                break;
                    413:
                    414:        case EVP_PKEY_OP_VERIFY:
                    415:                rv = EVP_PKEY_verify_init(ctx);
                    416:                break;
                    417:
                    418:        case EVP_PKEY_OP_VERIFYRECOVER:
                    419:                rv = EVP_PKEY_verify_recover_init(ctx);
                    420:                break;
                    421:
                    422:        case EVP_PKEY_OP_ENCRYPT:
                    423:                rv = EVP_PKEY_encrypt_init(ctx);
                    424:                break;
                    425:
                    426:        case EVP_PKEY_OP_DECRYPT:
                    427:                rv = EVP_PKEY_decrypt_init(ctx);
                    428:                break;
                    429:
                    430:        case EVP_PKEY_OP_DERIVE:
                    431:                rv = EVP_PKEY_derive_init(ctx);
                    432:                break;
                    433:        }
                    434:
                    435:        if (rv <= 0) {
                    436:                EVP_PKEY_CTX_free(ctx);
                    437:                ctx = NULL;
                    438:        }
                    439: end:
                    440:
                    441:        free(passin);
                    442:
                    443:        return ctx;
                    444:
                    445:
                    446: }
                    447:
                    448: static int
                    449: setup_peer(BIO * err, EVP_PKEY_CTX * ctx, int peerform,
                    450:     const char *file)
                    451: {
                    452:        EVP_PKEY *peer = NULL;
                    453:        int ret;
                    454:        if (!ctx) {
                    455:                BIO_puts(err, "-peerkey command before -inkey\n");
                    456:                return 0;
                    457:        }
                    458:        peer = load_pubkey(bio_err, file, peerform, 0, NULL, NULL, "Peer Key");
                    459:
                    460:        if (!peer) {
                    461:                BIO_printf(bio_err, "Error reading peer key %s\n", file);
                    462:                ERR_print_errors(err);
                    463:                return 0;
                    464:        }
                    465:        ret = EVP_PKEY_derive_set_peer(ctx, peer);
                    466:
                    467:        EVP_PKEY_free(peer);
                    468:        if (ret <= 0)
                    469:                ERR_print_errors(err);
                    470:        return ret;
                    471: }
                    472:
                    473: static int
                    474: do_keyop(EVP_PKEY_CTX * ctx, int pkey_op,
                    475:     unsigned char *out, size_t * poutlen,
                    476:     unsigned char *in, size_t inlen)
                    477: {
                    478:        int rv = 0;
                    479:        switch (pkey_op) {
                    480:        case EVP_PKEY_OP_VERIFYRECOVER:
                    481:                rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
                    482:                break;
                    483:
                    484:        case EVP_PKEY_OP_SIGN:
                    485:                rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
                    486:                break;
                    487:
                    488:        case EVP_PKEY_OP_ENCRYPT:
                    489:                rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
                    490:                break;
                    491:
                    492:        case EVP_PKEY_OP_DECRYPT:
                    493:                rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
                    494:                break;
                    495:
                    496:        case EVP_PKEY_OP_DERIVE:
                    497:                rv = EVP_PKEY_derive(ctx, out, poutlen);
                    498:                break;
                    499:
                    500:        }
                    501:        return rv;
                    502: }