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

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