[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.74

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