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

1.1       deraadt     1: /*
1.9     ! deraadt     2:  *
        !             3:  * authfile.c
        !             4:  *
        !             5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
        !             6:  *
        !             7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
        !             8:  *                    All rights reserved
        !             9:  *
        !            10:  * Created: Mon Mar 27 03:52:05 1995 ylo
        !            11:  *
        !            12:  * This file contains functions for reading and writing identity files, and
        !            13:  * for reading the passphrase from the user.
        !            14:  *
        !            15:  */
1.1       deraadt    16:
                     17: #include "includes.h"
1.9     ! deraadt    18: RCSID("$Id: authfile.c,v 1.8 1999/11/23 22:25:52 markus Exp $");
1.1       deraadt    19:
1.3       provos     20: #include <ssl/bn.h>
1.1       deraadt    21: #include "xmalloc.h"
                     22: #include "buffer.h"
                     23: #include "bufaux.h"
                     24: #include "cipher.h"
                     25: #include "ssh.h"
                     26:
                     27: /* Version identification string for identity files. */
                     28: #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
                     29:
                     30: /* Saves the authentication (private) key in a file, encrypting it with
                     31:    passphrase.  The identification of the file (lowest 64 bits of n)
                     32:    will precede the key to provide identification of the key without
                     33:    needing a passphrase. */
                     34:
1.3       provos     35: int
                     36: save_private_key(const char *filename, const char *passphrase,
                     37:                 RSA *key, const char *comment)
1.1       deraadt    38: {
1.8       markus     39:        Buffer buffer, encrypted;
                     40:        char buf[100], *cp;
                     41:        int f, i;
                     42:        CipherContext cipher;
                     43:        int cipher_type;
                     44:        u_int32_t rand;
                     45:
                     46:        /* If the passphrase is empty, use SSH_CIPHER_NONE to ease
                     47:           converting to another cipher; otherwise use
                     48:           SSH_AUTHFILE_CIPHER. */
                     49:        if (strcmp(passphrase, "") == 0)
                     50:                cipher_type = SSH_CIPHER_NONE;
                     51:        else
                     52:                cipher_type = SSH_AUTHFILE_CIPHER;
                     53:
                     54:        /* This buffer is used to built the secret part of the private key. */
                     55:        buffer_init(&buffer);
                     56:
                     57:        /* Put checkbytes for checking passphrase validity. */
                     58:        rand = arc4random();
                     59:        buf[0] = rand & 0xff;
                     60:        buf[1] = (rand >> 8) & 0xff;
                     61:        buf[2] = buf[0];
                     62:        buf[3] = buf[1];
                     63:        buffer_append(&buffer, buf, 4);
                     64:
                     65:        /* Store the private key (n and e will not be stored because they
                     66:           will be stored in plain text, and storing them also in
                     67:           encrypted format would just give known plaintext). */
                     68:        buffer_put_bignum(&buffer, key->d);
                     69:        buffer_put_bignum(&buffer, key->iqmp);
                     70:        buffer_put_bignum(&buffer, key->q);     /* reverse from SSL p */
                     71:        buffer_put_bignum(&buffer, key->p);     /* reverse from SSL q */
                     72:
                     73:        /* Pad the part to be encrypted until its size is a multiple of 8. */
                     74:        while (buffer_len(&buffer) % 8 != 0)
                     75:                buffer_put_char(&buffer, 0);
                     76:
                     77:        /* This buffer will be used to contain the data in the file. */
                     78:        buffer_init(&encrypted);
                     79:
                     80:        /* First store keyfile id string. */
                     81:        cp = AUTHFILE_ID_STRING;
                     82:        for (i = 0; cp[i]; i++)
                     83:                buffer_put_char(&encrypted, cp[i]);
                     84:        buffer_put_char(&encrypted, 0);
                     85:
                     86:        /* Store cipher type. */
                     87:        buffer_put_char(&encrypted, cipher_type);
                     88:        buffer_put_int(&encrypted, 0);  /* For future extension */
                     89:
                     90:        /* Store public key.  This will be in plain text. */
                     91:        buffer_put_int(&encrypted, BN_num_bits(key->n));
                     92:        buffer_put_bignum(&encrypted, key->n);
                     93:        buffer_put_bignum(&encrypted, key->e);
                     94:        buffer_put_string(&encrypted, comment, strlen(comment));
                     95:
                     96:        /* Allocate space for the private part of the key in the buffer. */
                     97:        buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
                     98:
                     99:        cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
                    100:        cipher_encrypt(&cipher, (unsigned char *) cp,
                    101:                       (unsigned char *) buffer_ptr(&buffer),
                    102:                       buffer_len(&buffer));
                    103:        memset(&cipher, 0, sizeof(cipher));
                    104:
                    105:        /* Destroy temporary data. */
                    106:        memset(buf, 0, sizeof(buf));
                    107:        buffer_free(&buffer);
                    108:
                    109:        /* Write to a file. */
                    110:        f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
                    111:        if (f < 0)
                    112:                return 0;
                    113:
                    114:        if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
                    115:            buffer_len(&encrypted)) {
                    116:                debug("Write to key file %.200s failed: %.100s", filename,
                    117:                      strerror(errno));
                    118:                buffer_free(&encrypted);
                    119:                close(f);
                    120:                remove(filename);
                    121:                return 0;
                    122:        }
                    123:        close(f);
                    124:        buffer_free(&encrypted);
                    125:        return 1;
1.1       deraadt   126: }
                    127:
                    128: /* Loads the public part of the key file.  Returns 0 if an error
                    129:    was encountered (the file does not exist or is not readable), and
                    130:    non-zero otherwise. */
                    131:
1.3       provos    132: int
1.8       markus    133: load_public_key(const char *filename, RSA * pub,
1.3       provos    134:                char **comment_return)
1.1       deraadt   135: {
1.8       markus    136:        int f, i;
                    137:        off_t len;
                    138:        Buffer buffer;
                    139:        char *cp;
                    140:
                    141:        /* Read data from the file into the buffer. */
                    142:        f = open(filename, O_RDONLY);
                    143:        if (f < 0)
                    144:                return 0;
                    145:
                    146:        len = lseek(f, (off_t) 0, SEEK_END);
                    147:        lseek(f, (off_t) 0, SEEK_SET);
                    148:
                    149:        buffer_init(&buffer);
                    150:        buffer_append_space(&buffer, &cp, len);
                    151:
                    152:        if (read(f, cp, (size_t) len) != (size_t) len) {
                    153:                debug("Read from key file %.200s failed: %.100s", filename,
                    154:                      strerror(errno));
                    155:                buffer_free(&buffer);
                    156:                close(f);
                    157:                return 0;
                    158:        }
                    159:        close(f);
                    160:
                    161:        /* Check that it is at least big enought to contain the ID string. */
                    162:        if (len < strlen(AUTHFILE_ID_STRING) + 1) {
                    163:                debug("Bad key file %.200s.", filename);
                    164:                buffer_free(&buffer);
                    165:                return 0;
                    166:        }
                    167:        /* Make sure it begins with the id string.  Consume the id string
                    168:           from the buffer. */
                    169:        for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
                    170:                if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
                    171:                        debug("Bad key file %.200s.", filename);
                    172:                        buffer_free(&buffer);
                    173:                        return 0;
                    174:                }
                    175:        /* Skip cipher type and reserved data. */
                    176:        (void) buffer_get_char(&buffer);        /* cipher type */
                    177:        (void) buffer_get_int(&buffer);         /* reserved */
                    178:
                    179:        /* Read the public key from the buffer. */
                    180:        buffer_get_int(&buffer);
                    181:        pub->n = BN_new();
                    182:        buffer_get_bignum(&buffer, pub->n);
                    183:        pub->e = BN_new();
                    184:        buffer_get_bignum(&buffer, pub->e);
                    185:        if (comment_return)
                    186:                *comment_return = buffer_get_string(&buffer, NULL);
                    187:        /* The encrypted private part is not parsed by this function. */
                    188:
1.1       deraadt   189:        buffer_free(&buffer);
                    190:
1.8       markus    191:        return 1;
1.1       deraadt   192: }
                    193:
                    194: /* Loads the private key from the file.  Returns 0 if an error is encountered
                    195:    (file does not exist or is not readable, or passphrase is bad).
                    196:    This initializes the private key. */
                    197:
1.3       provos    198: int
                    199: load_private_key(const char *filename, const char *passphrase,
1.8       markus    200:                 RSA * prv, char **comment_return)
1.1       deraadt   201: {
1.8       markus    202:        int f, i, check1, check2, cipher_type;
                    203:        off_t len;
                    204:        Buffer buffer, decrypted;
                    205:        char *cp;
                    206:        CipherContext cipher;
                    207:        BN_CTX *ctx;
                    208:        BIGNUM *aux;
                    209:        struct stat st;
                    210:
                    211:        /* Read the file into the buffer. */
                    212:        f = open(filename, O_RDONLY);
                    213:        if (f < 0)
                    214:                return 0;
                    215:
                    216:        /* We assume we are called under uid of the owner of the file */
                    217:        if (fstat(f, &st) < 0 ||
                    218:            (st.st_uid != 0 && st.st_uid != getuid()) ||
                    219:            (st.st_mode & 077) != 0) {
                    220:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    221:                error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
                    222:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    223:                error("Bad ownership or mode(0%3.3o) for '%s'.",
                    224:                      st.st_mode & 0777, filename);
                    225:                error("It is recommended that your private key files are NOT accessible by others.");
                    226:                return 0;
                    227:        }
                    228:        len = lseek(f, (off_t) 0, SEEK_END);
                    229:        lseek(f, (off_t) 0, SEEK_SET);
                    230:
                    231:        buffer_init(&buffer);
                    232:        buffer_append_space(&buffer, &cp, len);
                    233:
                    234:        if (read(f, cp, (size_t) len) != (size_t) len) {
                    235:                debug("Read from key file %.200s failed: %.100s", filename,
                    236:                      strerror(errno));
                    237:                buffer_free(&buffer);
                    238:                close(f);
                    239:                return 0;
                    240:        }
                    241:        close(f);
                    242:
                    243:        /* Check that it is at least big enought to contain the ID string. */
                    244:        if (len < strlen(AUTHFILE_ID_STRING) + 1) {
                    245:                debug("Bad key file %.200s.", filename);
                    246:                buffer_free(&buffer);
                    247:                return 0;
                    248:        }
                    249:        /* Make sure it begins with the id string.  Consume the id string
                    250:           from the buffer. */
                    251:        for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
                    252:                if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
                    253:                        debug("Bad key file %.200s.", filename);
                    254:                        buffer_free(&buffer);
                    255:                        return 0;
                    256:                }
                    257:        /* Read cipher type. */
                    258:        cipher_type = buffer_get_char(&buffer);
                    259:        (void) buffer_get_int(&buffer); /* Reserved data. */
                    260:
                    261:        /* Read the public key from the buffer. */
                    262:        buffer_get_int(&buffer);
                    263:        prv->n = BN_new();
                    264:        buffer_get_bignum(&buffer, prv->n);
                    265:        prv->e = BN_new();
                    266:        buffer_get_bignum(&buffer, prv->e);
                    267:        if (comment_return)
                    268:                *comment_return = buffer_get_string(&buffer, NULL);
                    269:        else
                    270:                xfree(buffer_get_string(&buffer, NULL));
                    271:
                    272:        /* Check that it is a supported cipher. */
                    273:        if (((cipher_mask() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
                    274:             (1 << cipher_type)) == 0) {
                    275:                debug("Unsupported cipher %.100s used in key file %.200s.",
                    276:                      cipher_name(cipher_type), filename);
                    277:                buffer_free(&buffer);
                    278:                goto fail;
                    279:        }
                    280:        /* Initialize space for decrypted data. */
                    281:        buffer_init(&decrypted);
                    282:        buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
                    283:
                    284:        /* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
                    285:        cipher_set_key_string(&cipher, cipher_type, passphrase, 0);
                    286:        cipher_decrypt(&cipher, (unsigned char *) cp,
                    287:                       (unsigned char *) buffer_ptr(&buffer),
                    288:                       buffer_len(&buffer));
                    289:
1.1       deraadt   290:        buffer_free(&buffer);
                    291:
1.8       markus    292:        check1 = buffer_get_char(&decrypted);
                    293:        check2 = buffer_get_char(&decrypted);
                    294:        if (check1 != buffer_get_char(&decrypted) ||
                    295:            check2 != buffer_get_char(&decrypted)) {
                    296:                if (strcmp(passphrase, "") != 0)
                    297:                        debug("Bad passphrase supplied for key file %.200s.", filename);
                    298:                /* Bad passphrase. */
                    299:                buffer_free(&decrypted);
                    300: fail:
                    301:                BN_clear_free(prv->n);
                    302:                BN_clear_free(prv->e);
                    303:                if (comment_return)
                    304:                        xfree(*comment_return);
                    305:                return 0;
                    306:        }
                    307:        /* Read the rest of the private key. */
                    308:        prv->d = BN_new();
                    309:        buffer_get_bignum(&decrypted, prv->d);
                    310:        prv->iqmp = BN_new();
                    311:        buffer_get_bignum(&decrypted, prv->iqmp);       /* u */
                    312:        /* in SSL and SSH p and q are exchanged */
                    313:        prv->q = BN_new();
                    314:        buffer_get_bignum(&decrypted, prv->q);          /* p */
                    315:        prv->p = BN_new();
                    316:        buffer_get_bignum(&decrypted, prv->p);          /* q */
                    317:
                    318:        ctx = BN_CTX_new();
                    319:        aux = BN_new();
                    320:
                    321:        BN_sub(aux, prv->q, BN_value_one());
                    322:        prv->dmq1 = BN_new();
                    323:        BN_mod(prv->dmq1, prv->d, aux, ctx);
                    324:
                    325:        BN_sub(aux, prv->p, BN_value_one());
                    326:        prv->dmp1 = BN_new();
                    327:        BN_mod(prv->dmp1, prv->d, aux, ctx);
                    328:
                    329:        BN_clear_free(aux);
                    330:        BN_CTX_free(ctx);
                    331:
                    332:        buffer_free(&decrypted);
1.1       deraadt   333:
1.8       markus    334:        return 1;
1.1       deraadt   335: }