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

Annotation of src/usr.bin/ssh/authfile.c, Revision 1.58.2.1

1.1       deraadt     1: /*
1.9       deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * This file contains functions for reading and writing identity files, and
                      6:  * for reading the passphrase from the user.
1.14      markus      7:  *
1.19      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
                     14:  *
                     15:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                     16:  *
                     17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.9       deraadt    36:  */
1.1       deraadt    37:
                     38: #include "includes.h"
1.58.2.1! brad       39: RCSID("$OpenBSD: authfile.c,v 1.60 2004/12/11 01:48:56 dtucker Exp $");
1.1       deraadt    40:
1.21      markus     41: #include <openssl/err.h>
1.25      markus     42: #include <openssl/evp.h>
1.15      markus     43: #include <openssl/pem.h>
                     44:
1.25      markus     45: #include "cipher.h"
1.1       deraadt    46: #include "xmalloc.h"
                     47: #include "buffer.h"
                     48: #include "bufaux.h"
1.25      markus     49: #include "key.h"
1.1       deraadt    50: #include "ssh.h"
1.25      markus     51: #include "log.h"
1.27      itojun     52: #include "authfile.h"
1.44      markus     53: #include "rsa.h"
1.58.2.1! brad       54: #include "misc.h"
1.1       deraadt    55:
1.29      markus     56: /* Version identification string for SSH v1 identity files. */
1.26      stevesk    57: static const char authfile_id_string[] =
                     58:     "SSH PRIVATE KEY FILE FORMAT 1.1\n";
1.1       deraadt    59:
1.10      markus     60: /*
                     61:  * Saves the authentication (private) key in a file, encrypting it with
                     62:  * passphrase.  The identification of the file (lowest 64 bits of n) will
                     63:  * precede the key to provide identification of the key without needing a
                     64:  * passphrase.
                     65:  */
1.1       deraadt    66:
1.37      itojun     67: static int
1.29      markus     68: key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
                     69:     const char *comment)
1.1       deraadt    70: {
1.8       markus     71:        Buffer buffer, encrypted;
1.45      stevesk    72:        u_char buf[100], *cp;
1.46      markus     73:        int fd, i, cipher_num;
1.20      markus     74:        CipherContext ciphercontext;
                     75:        Cipher *cipher;
1.57      avsm       76:        u_int32_t rnd;
1.8       markus     77:
1.10      markus     78:        /*
                     79:         * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
                     80:         * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
                     81:         */
1.46      markus     82:        cipher_num = (strcmp(passphrase, "") == 0) ?
                     83:            SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
                     84:        if ((cipher = cipher_by_number(cipher_num)) == NULL)
1.20      markus     85:                fatal("save_private_key_rsa: bad cipher");
1.8       markus     86:
                     87:        /* This buffer is used to built the secret part of the private key. */
                     88:        buffer_init(&buffer);
                     89:
                     90:        /* Put checkbytes for checking passphrase validity. */
1.57      avsm       91:        rnd = arc4random();
                     92:        buf[0] = rnd & 0xff;
                     93:        buf[1] = (rnd >> 8) & 0xff;
1.8       markus     94:        buf[2] = buf[0];
                     95:        buf[3] = buf[1];
                     96:        buffer_append(&buffer, buf, 4);
                     97:
1.10      markus     98:        /*
                     99:         * Store the private key (n and e will not be stored because they
                    100:         * will be stored in plain text, and storing them also in encrypted
                    101:         * format would just give known plaintext).
                    102:         */
1.29      markus    103:        buffer_put_bignum(&buffer, key->rsa->d);
                    104:        buffer_put_bignum(&buffer, key->rsa->iqmp);
                    105:        buffer_put_bignum(&buffer, key->rsa->q);        /* reverse from SSL p */
                    106:        buffer_put_bignum(&buffer, key->rsa->p);        /* reverse from SSL q */
1.8       markus    107:
                    108:        /* Pad the part to be encrypted until its size is a multiple of 8. */
                    109:        while (buffer_len(&buffer) % 8 != 0)
                    110:                buffer_put_char(&buffer, 0);
                    111:
                    112:        /* This buffer will be used to contain the data in the file. */
                    113:        buffer_init(&encrypted);
                    114:
                    115:        /* First store keyfile id string. */
1.26      stevesk   116:        for (i = 0; authfile_id_string[i]; i++)
                    117:                buffer_put_char(&encrypted, authfile_id_string[i]);
1.8       markus    118:        buffer_put_char(&encrypted, 0);
                    119:
                    120:        /* Store cipher type. */
1.46      markus    121:        buffer_put_char(&encrypted, cipher_num);
1.8       markus    122:        buffer_put_int(&encrypted, 0);  /* For future extension */
                    123:
                    124:        /* Store public key.  This will be in plain text. */
1.29      markus    125:        buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
                    126:        buffer_put_bignum(&encrypted, key->rsa->n);
                    127:        buffer_put_bignum(&encrypted, key->rsa->e);
1.36      markus    128:        buffer_put_cstring(&encrypted, comment);
1.8       markus    129:
                    130:        /* Allocate space for the private part of the key in the buffer. */
1.42      stevesk   131:        cp = buffer_append_space(&encrypted, buffer_len(&buffer));
1.8       markus    132:
1.46      markus    133:        cipher_set_key_string(&ciphercontext, cipher, passphrase,
                    134:            CIPHER_ENCRYPT);
                    135:        cipher_crypt(&ciphercontext, cp,
1.45      stevesk   136:            buffer_ptr(&buffer), buffer_len(&buffer));
1.46      markus    137:        cipher_cleanup(&ciphercontext);
1.20      markus    138:        memset(&ciphercontext, 0, sizeof(ciphercontext));
1.8       markus    139:
                    140:        /* Destroy temporary data. */
                    141:        memset(buf, 0, sizeof(buf));
                    142:        buffer_free(&buffer);
                    143:
1.11      deraadt   144:        fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1.31      markus    145:        if (fd < 0) {
                    146:                error("open %s failed: %s.", filename, strerror(errno));
1.55      markus    147:                buffer_free(&encrypted);
1.8       markus    148:                return 0;
1.31      markus    149:        }
1.11      deraadt   150:        if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
1.8       markus    151:            buffer_len(&encrypted)) {
1.31      markus    152:                error("write to key file %s failed: %s", filename,
1.41      deraadt   153:                    strerror(errno));
1.8       markus    154:                buffer_free(&encrypted);
1.11      deraadt   155:                close(fd);
1.22      markus    156:                unlink(filename);
1.8       markus    157:                return 0;
                    158:        }
1.11      deraadt   159:        close(fd);
1.8       markus    160:        buffer_free(&encrypted);
                    161:        return 1;
1.1       deraadt   162: }
                    163:
1.29      markus    164: /* save SSH v2 key in OpenSSL PEM format */
1.37      itojun    165: static int
1.29      markus    166: key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
                    167:     const char *comment)
1.15      markus    168: {
                    169:        FILE *fp;
                    170:        int fd;
1.21      markus    171:        int success = 0;
                    172:        int len = strlen(_passphrase);
1.47      markus    173:        u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
1.48      markus    174:        const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
1.15      markus    175:
                    176:        if (len > 0 && len <= 4) {
1.31      markus    177:                error("passphrase too short: have %d bytes, need > 4", len);
1.15      markus    178:                return 0;
                    179:        }
                    180:        fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
                    181:        if (fd < 0) {
1.31      markus    182:                error("open %s failed: %s.", filename, strerror(errno));
1.15      markus    183:                return 0;
                    184:        }
                    185:        fp = fdopen(fd, "w");
                    186:        if (fp == NULL ) {
1.31      markus    187:                error("fdopen %s failed: %s.", filename, strerror(errno));
1.15      markus    188:                close(fd);
                    189:                return 0;
                    190:        }
1.21      markus    191:        switch (key->type) {
1.30      markus    192:        case KEY_DSA:
                    193:                success = PEM_write_DSAPrivateKey(fp, key->dsa,
                    194:                    cipher, passphrase, len, NULL, NULL);
                    195:                break;
                    196:        case KEY_RSA:
                    197:                success = PEM_write_RSAPrivateKey(fp, key->rsa,
                    198:                    cipher, passphrase, len, NULL, NULL);
                    199:                break;
1.15      markus    200:        }
                    201:        fclose(fp);
                    202:        return success;
                    203: }
                    204:
                    205: int
1.29      markus    206: key_save_private(Key *key, const char *filename, const char *passphrase,
1.15      markus    207:     const char *comment)
                    208: {
                    209:        switch (key->type) {
1.21      markus    210:        case KEY_RSA1:
1.29      markus    211:                return key_save_private_rsa1(key, filename, passphrase,
                    212:                    comment);
1.15      markus    213:                break;
                    214:        case KEY_DSA:
1.21      markus    215:        case KEY_RSA:
1.29      markus    216:                return key_save_private_pem(key, filename, passphrase,
                    217:                    comment);
1.15      markus    218:                break;
                    219:        default:
                    220:                break;
                    221:        }
1.31      markus    222:        error("key_save_private: cannot save key type %d", key->type);
1.15      markus    223:        return 0;
                    224: }
                    225:
1.10      markus    226: /*
1.29      markus    227:  * Loads the public part of the ssh v1 key file.  Returns NULL if an error was
                    228:  * encountered (the file does not exist or is not readable), and the key
1.10      markus    229:  * otherwise.
                    230:  */
1.1       deraadt   231:
1.37      itojun    232: static Key *
1.29      markus    233: key_load_public_rsa1(int fd, const char *filename, char **commentp)
1.1       deraadt   234: {
1.8       markus    235:        Buffer buffer;
1.29      markus    236:        Key *pub;
1.51      fgsch     237:        struct stat st;
1.8       markus    238:        char *cp;
1.29      markus    239:        int i;
1.56      deraadt   240:        size_t len;
1.8       markus    241:
1.51      fgsch     242:        if (fstat(fd, &st) < 0) {
                    243:                error("fstat for key file %.200s failed: %.100s",
                    244:                    filename, strerror(errno));
                    245:                return NULL;
                    246:        }
1.58      djm       247:        if (st.st_size > 1*1024*1024) {
                    248:                error("key file %.200s too large", filename);
                    249:                return NULL;
                    250:        }
1.56      deraadt   251:        len = (size_t)st.st_size;               /* truncated */
1.8       markus    252:
                    253:        buffer_init(&buffer);
1.42      stevesk   254:        cp = buffer_append_space(&buffer, len);
1.8       markus    255:
1.11      deraadt   256:        if (read(fd, cp, (size_t) len) != (size_t) len) {
1.8       markus    257:                debug("Read from key file %.200s failed: %.100s", filename,
1.15      markus    258:                    strerror(errno));
1.8       markus    259:                buffer_free(&buffer);
1.29      markus    260:                return NULL;
1.8       markus    261:        }
                    262:
1.26      stevesk   263:        /* Check that it is at least big enough to contain the ID string. */
                    264:        if (len < sizeof(authfile_id_string)) {
1.39      markus    265:                debug3("Not a RSA1 key file %.200s.", filename);
1.8       markus    266:                buffer_free(&buffer);
1.29      markus    267:                return NULL;
1.8       markus    268:        }
1.10      markus    269:        /*
                    270:         * Make sure it begins with the id string.  Consume the id string
                    271:         * from the buffer.
                    272:         */
1.26      stevesk   273:        for (i = 0; i < sizeof(authfile_id_string); i++)
                    274:                if (buffer_get_char(&buffer) != authfile_id_string[i]) {
1.39      markus    275:                        debug3("Not a RSA1 key file %.200s.", filename);
1.8       markus    276:                        buffer_free(&buffer);
1.29      markus    277:                        return NULL;
1.8       markus    278:                }
                    279:        /* Skip cipher type and reserved data. */
                    280:        (void) buffer_get_char(&buffer);        /* cipher type */
                    281:        (void) buffer_get_int(&buffer);         /* reserved */
                    282:
                    283:        /* Read the public key from the buffer. */
1.50      markus    284:        (void) buffer_get_int(&buffer);
1.29      markus    285:        pub = key_new(KEY_RSA1);
                    286:        buffer_get_bignum(&buffer, pub->rsa->n);
                    287:        buffer_get_bignum(&buffer, pub->rsa->e);
                    288:        if (commentp)
                    289:                *commentp = buffer_get_string(&buffer, NULL);
1.8       markus    290:        /* The encrypted private part is not parsed by this function. */
                    291:
1.1       deraadt   292:        buffer_free(&buffer);
1.29      markus    293:        return pub;
1.1       deraadt   294: }
                    295:
1.29      markus    296: /* load public key from private-key file, works only for SSH v1 */
                    297: Key *
                    298: key_load_public_type(int type, const char *filename, char **commentp)
1.15      markus    299: {
1.29      markus    300:        Key *pub;
                    301:        int fd;
                    302:
                    303:        if (type == KEY_RSA1) {
                    304:                fd = open(filename, O_RDONLY);
                    305:                if (fd < 0)
                    306:                        return NULL;
                    307:                pub = key_load_public_rsa1(fd, filename, commentp);
                    308:                close(fd);
                    309:                return pub;
1.15      markus    310:        }
1.29      markus    311:        return NULL;
1.15      markus    312: }
                    313:
1.10      markus    314: /*
                    315:  * Loads the private key from the file.  Returns 0 if an error is encountered
                    316:  * (file does not exist or is not readable, or passphrase is bad). This
                    317:  * initializes the private key.
                    318:  * Assumes we are called under uid of the owner of the file.
                    319:  */
1.1       deraadt   320:
1.37      itojun    321: static Key *
1.29      markus    322: key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
                    323:     char **commentp)
1.1       deraadt   324: {
1.15      markus    325:        int i, check1, check2, cipher_type;
1.56      deraadt   326:        size_t len;
1.8       markus    327:        Buffer buffer, decrypted;
1.45      stevesk   328:        u_char *cp;
1.20      markus    329:        CipherContext ciphercontext;
                    330:        Cipher *cipher;
1.29      markus    331:        Key *prv = NULL;
1.51      fgsch     332:        struct stat st;
1.8       markus    333:
1.51      fgsch     334:        if (fstat(fd, &st) < 0) {
                    335:                error("fstat for key file %.200s failed: %.100s",
                    336:                    filename, strerror(errno));
                    337:                close(fd);
                    338:                return NULL;
                    339:        }
1.56      deraadt   340:        if (st.st_size > 1*1024*1024) {
1.58      djm       341:                error("key file %.200s too large", filename);
1.56      deraadt   342:                close(fd);
                    343:                return (NULL);
                    344:        }
                    345:        len = (size_t)st.st_size;               /* truncated */
1.8       markus    346:
                    347:        buffer_init(&buffer);
1.42      stevesk   348:        cp = buffer_append_space(&buffer, len);
1.8       markus    349:
1.11      deraadt   350:        if (read(fd, cp, (size_t) len) != (size_t) len) {
1.8       markus    351:                debug("Read from key file %.200s failed: %.100s", filename,
1.21      markus    352:                    strerror(errno));
1.8       markus    353:                buffer_free(&buffer);
1.11      deraadt   354:                close(fd);
1.29      markus    355:                return NULL;
1.8       markus    356:        }
                    357:
1.26      stevesk   358:        /* Check that it is at least big enough to contain the ID string. */
                    359:        if (len < sizeof(authfile_id_string)) {
1.39      markus    360:                debug3("Not a RSA1 key file %.200s.", filename);
1.8       markus    361:                buffer_free(&buffer);
1.28      deraadt   362:                close(fd);
1.29      markus    363:                return NULL;
1.8       markus    364:        }
1.10      markus    365:        /*
                    366:         * Make sure it begins with the id string.  Consume the id string
                    367:         * from the buffer.
                    368:         */
1.26      stevesk   369:        for (i = 0; i < sizeof(authfile_id_string); i++)
                    370:                if (buffer_get_char(&buffer) != authfile_id_string[i]) {
1.39      markus    371:                        debug3("Not a RSA1 key file %.200s.", filename);
1.8       markus    372:                        buffer_free(&buffer);
1.28      deraadt   373:                        close(fd);
1.29      markus    374:                        return NULL;
1.8       markus    375:                }
1.28      deraadt   376:
1.8       markus    377:        /* Read cipher type. */
                    378:        cipher_type = buffer_get_char(&buffer);
                    379:        (void) buffer_get_int(&buffer); /* Reserved data. */
                    380:
                    381:        /* Read the public key from the buffer. */
1.50      markus    382:        (void) buffer_get_int(&buffer);
1.29      markus    383:        prv = key_new_private(KEY_RSA1);
                    384:
                    385:        buffer_get_bignum(&buffer, prv->rsa->n);
                    386:        buffer_get_bignum(&buffer, prv->rsa->e);
                    387:        if (commentp)
                    388:                *commentp = buffer_get_string(&buffer, NULL);
1.8       markus    389:        else
                    390:                xfree(buffer_get_string(&buffer, NULL));
                    391:
                    392:        /* Check that it is a supported cipher. */
1.20      markus    393:        cipher = cipher_by_number(cipher_type);
                    394:        if (cipher == NULL) {
                    395:                debug("Unsupported cipher %d used in key file %.200s.",
                    396:                    cipher_type, filename);
1.8       markus    397:                buffer_free(&buffer);
                    398:                goto fail;
                    399:        }
                    400:        /* Initialize space for decrypted data. */
                    401:        buffer_init(&decrypted);
1.42      stevesk   402:        cp = buffer_append_space(&decrypted, buffer_len(&buffer));
1.8       markus    403:
                    404:        /* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
1.46      markus    405:        cipher_set_key_string(&ciphercontext, cipher, passphrase,
                    406:            CIPHER_DECRYPT);
                    407:        cipher_crypt(&ciphercontext, cp,
1.45      stevesk   408:            buffer_ptr(&buffer), buffer_len(&buffer));
1.46      markus    409:        cipher_cleanup(&ciphercontext);
1.20      markus    410:        memset(&ciphercontext, 0, sizeof(ciphercontext));
1.1       deraadt   411:        buffer_free(&buffer);
                    412:
1.8       markus    413:        check1 = buffer_get_char(&decrypted);
                    414:        check2 = buffer_get_char(&decrypted);
                    415:        if (check1 != buffer_get_char(&decrypted) ||
                    416:            check2 != buffer_get_char(&decrypted)) {
                    417:                if (strcmp(passphrase, "") != 0)
1.29      markus    418:                        debug("Bad passphrase supplied for key file %.200s.",
                    419:                            filename);
1.8       markus    420:                /* Bad passphrase. */
                    421:                buffer_free(&decrypted);
1.29      markus    422:                goto fail;
1.8       markus    423:        }
                    424:        /* Read the rest of the private key. */
1.29      markus    425:        buffer_get_bignum(&decrypted, prv->rsa->d);
                    426:        buffer_get_bignum(&decrypted, prv->rsa->iqmp);          /* u */
                    427:        /* in SSL and SSH v1 p and q are exchanged */
                    428:        buffer_get_bignum(&decrypted, prv->rsa->q);             /* p */
                    429:        buffer_get_bignum(&decrypted, prv->rsa->p);             /* q */
1.8       markus    430:
1.29      markus    431:        /* calculate p-1 and q-1 */
1.43      markus    432:        rsa_generate_additional_parameters(prv->rsa);
1.8       markus    433:
                    434:        buffer_free(&decrypted);
1.52      markus    435:
                    436:        /* enable blinding */
                    437:        if (RSA_blinding_on(prv->rsa, NULL) != 1) {
                    438:                error("key_load_private_rsa1: RSA_blinding_on failed");
                    439:                goto fail;
                    440:        }
1.28      deraadt   441:        close(fd);
1.29      markus    442:        return prv;
                    443:
                    444: fail:
                    445:        if (commentp)
                    446:                xfree(*commentp);
                    447:        close(fd);
                    448:        key_free(prv);
                    449:        return NULL;
1.15      markus    450: }
                    451:
1.49      markus    452: Key *
1.29      markus    453: key_load_private_pem(int fd, int type, const char *passphrase,
                    454:     char **commentp)
1.15      markus    455: {
                    456:        FILE *fp;
1.21      markus    457:        EVP_PKEY *pk = NULL;
1.29      markus    458:        Key *prv = NULL;
1.21      markus    459:        char *name = "<no key>";
1.15      markus    460:
                    461:        fp = fdopen(fd, "r");
                    462:        if (fp == NULL) {
1.31      markus    463:                error("fdopen failed: %s", strerror(errno));
1.28      deraadt   464:                close(fd);
1.29      markus    465:                return NULL;
1.15      markus    466:        }
1.21      markus    467:        pk = PEM_read_PrivateKey(fp, NULL, NULL, (char *)passphrase);
                    468:        if (pk == NULL) {
1.32      markus    469:                debug("PEM_read_PrivateKey failed");
1.21      markus    470:                (void)ERR_get_error();
1.29      markus    471:        } else if (pk->type == EVP_PKEY_RSA &&
1.41      deraadt   472:            (type == KEY_UNSPEC||type==KEY_RSA)) {
1.29      markus    473:                prv = key_new(KEY_UNSPEC);
                    474:                prv->rsa = EVP_PKEY_get1_RSA(pk);
                    475:                prv->type = KEY_RSA;
                    476:                name = "rsa w/o comment";
1.21      markus    477: #ifdef DEBUG_PK
1.29      markus    478:                RSA_print_fp(stderr, prv->rsa, 8);
1.21      markus    479: #endif
1.52      markus    480:                if (RSA_blinding_on(prv->rsa, NULL) != 1) {
                    481:                        error("key_load_private_pem: RSA_blinding_on failed");
                    482:                        key_free(prv);
                    483:                        prv = NULL;
                    484:                }
1.29      markus    485:        } else if (pk->type == EVP_PKEY_DSA &&
1.41      deraadt   486:            (type == KEY_UNSPEC||type==KEY_DSA)) {
1.29      markus    487:                prv = key_new(KEY_UNSPEC);
                    488:                prv->dsa = EVP_PKEY_get1_DSA(pk);
                    489:                prv->type = KEY_DSA;
                    490:                name = "dsa w/o comment";
1.21      markus    491: #ifdef DEBUG_PK
1.29      markus    492:                DSA_print_fp(stderr, prv->dsa, 8);
1.21      markus    493: #endif
1.15      markus    494:        } else {
1.21      markus    495:                error("PEM_read_PrivateKey: mismatch or "
                    496:                    "unknown EVP_PKEY save_type %d", pk->save_type);
1.15      markus    497:        }
                    498:        fclose(fp);
1.21      markus    499:        if (pk != NULL)
                    500:                EVP_PKEY_free(pk);
1.29      markus    501:        if (prv != NULL && commentp)
                    502:                *commentp = xstrdup(name);
                    503:        debug("read PEM private key done: type %s",
                    504:            prv ? key_type(prv) : "<unknown>");
                    505:        return prv;
1.15      markus    506: }
                    507:
1.37      itojun    508: static int
1.29      markus    509: key_perm_ok(int fd, const char *filename)
1.15      markus    510: {
                    511:        struct stat st;
                    512:
1.38      markus    513:        if (fstat(fd, &st) < 0)
                    514:                return 0;
                    515:        /*
                    516:         * if a key owned by the user is accessed, then we check the
                    517:         * permissions of the file. if the key owned by a different user,
                    518:         * then we don't care.
                    519:         */
                    520:        if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
1.15      markus    521:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    522:                error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
                    523:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.38      markus    524:                error("Permissions 0%3.3o for '%s' are too open.",
1.54      djm       525:                    (u_int)st.st_mode & 0777, filename);
1.15      markus    526:                error("It is recommended that your private key files are NOT accessible by others.");
1.29      markus    527:                error("This private key will be ignored.");
1.15      markus    528:                return 0;
                    529:        }
1.29      markus    530:        return 1;
                    531: }
                    532:
                    533: Key *
                    534: key_load_private_type(int type, const char *filename, const char *passphrase,
                    535:     char **commentp)
                    536: {
                    537:        int fd;
                    538:
                    539:        fd = open(filename, O_RDONLY);
                    540:        if (fd < 0)
                    541:                return NULL;
                    542:        if (!key_perm_ok(fd, filename)) {
1.31      markus    543:                error("bad permissions: ignore key: %s", filename);
1.29      markus    544:                close(fd);
                    545:                return NULL;
                    546:        }
                    547:        switch (type) {
1.21      markus    548:        case KEY_RSA1:
1.29      markus    549:                return key_load_private_rsa1(fd, filename, passphrase,
                    550:                    commentp);
                    551:                /* closes fd */
1.15      markus    552:                break;
                    553:        case KEY_DSA:
1.21      markus    554:        case KEY_RSA:
                    555:        case KEY_UNSPEC:
1.29      markus    556:                return key_load_private_pem(fd, type, passphrase, commentp);
                    557:                /* closes fd */
1.28      deraadt   558:                break;
1.15      markus    559:        default:
1.28      deraadt   560:                close(fd);
1.15      markus    561:                break;
                    562:        }
1.29      markus    563:        return NULL;
                    564: }
                    565:
                    566: Key *
                    567: key_load_private(const char *filename, const char *passphrase,
                    568:     char **commentp)
                    569: {
1.34      markus    570:        Key *pub, *prv;
1.29      markus    571:        int fd;
                    572:
                    573:        fd = open(filename, O_RDONLY);
                    574:        if (fd < 0)
                    575:                return NULL;
                    576:        if (!key_perm_ok(fd, filename)) {
1.31      markus    577:                error("bad permissions: ignore key: %s", filename);
1.29      markus    578:                close(fd);
                    579:                return NULL;
                    580:        }
                    581:        pub = key_load_public_rsa1(fd, filename, commentp);
                    582:        lseek(fd, (off_t) 0, SEEK_SET);         /* rewind */
                    583:        if (pub == NULL) {
                    584:                /* closes fd */
1.34      markus    585:                prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
                    586:                /* use the filename as a comment for PEM */
                    587:                if (commentp && prv)
1.35      markus    588:                        *commentp = xstrdup(filename);
1.29      markus    589:        } else {
                    590:                /* it's a SSH v1 key if the public key part is readable */
                    591:                key_free(pub);
                    592:                /* closes fd */
1.34      markus    593:                prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
1.29      markus    594:        }
1.34      markus    595:        return prv;
1.18      markus    596: }
                    597:
1.37      itojun    598: static int
1.29      markus    599: key_try_load_public(Key *k, const char *filename, char **commentp)
1.18      markus    600: {
                    601:        FILE *f;
1.58.2.1! brad      602:        char line[SSH_MAX_PUBKEY_BYTES];
1.18      markus    603:        char *cp;
1.58.2.1! brad      604:        u_long linenum = 0;
1.18      markus    605:
                    606:        f = fopen(filename, "r");
                    607:        if (f != NULL) {
1.58.2.1! brad      608:                while (read_keyfile_line(f, filename, line, sizeof(line),
        !           609:                            &linenum) != -1) {
1.18      markus    610:                        cp = line;
1.40      deraadt   611:                        switch (*cp) {
1.18      markus    612:                        case '#':
                    613:                        case '\n':
                    614:                        case '\0':
                    615:                                continue;
                    616:                        }
                    617:                        /* Skip leading whitespace. */
                    618:                        for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
                    619:                                ;
                    620:                        if (*cp) {
1.21      markus    621:                                if (key_read(k, &cp) == 1) {
1.18      markus    622:                                        if (commentp)
                    623:                                                *commentp=xstrdup(filename);
                    624:                                        fclose(f);
                    625:                                        return 1;
                    626:                                }
                    627:                        }
                    628:                }
                    629:                fclose(f);
                    630:        }
                    631:        return 0;
                    632: }
                    633:
1.29      markus    634: /* load public key from ssh v1 private or any pubkey file */
                    635: Key *
                    636: key_load_public(const char *filename, char **commentp)
1.18      markus    637: {
1.29      markus    638:        Key *pub;
                    639:        char file[MAXPATHLEN];
1.18      markus    640:
1.53      markus    641:        /* try rsa1 private key */
1.29      markus    642:        pub = key_load_public_type(KEY_RSA1, filename, commentp);
                    643:        if (pub != NULL)
                    644:                return pub;
1.53      markus    645:
                    646:        /* try rsa1 public key */
                    647:        pub = key_new(KEY_RSA1);
                    648:        if (key_try_load_public(pub, filename, commentp) == 1)
                    649:                return pub;
                    650:        key_free(pub);
                    651:
                    652:        /* try ssh2 public key */
1.29      markus    653:        pub = key_new(KEY_UNSPEC);
                    654:        if (key_try_load_public(pub, filename, commentp) == 1)
                    655:                return pub;
                    656:        if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
                    657:            (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
                    658:            (key_try_load_public(pub, file, commentp) == 1))
                    659:                return pub;
                    660:        key_free(pub);
                    661:        return NULL;
1.1       deraadt   662: }