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

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