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

1.1       deraadt     1: /*
                      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: */
                     16:
                     17: #include "includes.h"
1.3     ! provos     18: RCSID("$Id: authfile.c,v 1.2 1999/05/04 11:58:28 bg 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: {
                     39:   Buffer buffer, encrypted;
                     40:   char buf[100], *cp;
                     41:   int f, i;
                     42:   CipherContext cipher;
                     43:   int cipher_type;
1.3     ! provos     44:   u_int32_t rand;
1.1       deraadt    45:
                     46:   /* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting to
                     47:      another cipher; otherwise use SSH_AUTHFILE_CIPHER. */
                     48:   if (strcmp(passphrase, "") == 0)
                     49:     cipher_type = SSH_CIPHER_NONE;
                     50:   else
                     51:     cipher_type = SSH_AUTHFILE_CIPHER;
                     52:
                     53:   /* This buffer is used to built the secret part of the private key. */
                     54:   buffer_init(&buffer);
                     55:
                     56:   /* Put checkbytes for checking passphrase validity. */
1.3     ! provos     57:   rand = arc4random();
        !            58:   buf[0] = rand & 0xff;
        !            59:   buf[1] = (rand >> 8) & 0xff;
1.1       deraadt    60:   buf[2] = buf[0];
                     61:   buf[3] = buf[1];
                     62:   buffer_append(&buffer, buf, 4);
                     63:
                     64:   /* Store the private key (n and e will not be stored because they will
                     65:      be stored in plain text, and storing them also in encrypted format
                     66:      would just give known plaintext). */
1.3     ! provos     67:   buffer_put_bignum(&buffer, key->d);
        !            68:   buffer_put_bignum(&buffer, key->iqmp);
        !            69:   buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
        !            70:   buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
1.1       deraadt    71:
                     72:   /* Pad the part to be encrypted until its size is a multiple of 8. */
                     73:   while (buffer_len(&buffer) % 8 != 0)
                     74:     buffer_put_char(&buffer, 0);
                     75:
                     76:   /* This buffer will be used to contain the data in the file. */
                     77:   buffer_init(&encrypted);
                     78:
                     79:   /* First store keyfile id string. */
                     80:   cp = AUTHFILE_ID_STRING;
                     81:   for (i = 0; cp[i]; i++)
                     82:     buffer_put_char(&encrypted, cp[i]);
                     83:   buffer_put_char(&encrypted, 0);
                     84:
                     85:   /* Store cipher type. */
                     86:   buffer_put_char(&encrypted, cipher_type);
                     87:   buffer_put_int(&encrypted, 0);  /* For future extension */
                     88:
                     89:   /* Store public key.  This will be in plain text. */
1.3     ! provos     90:   buffer_put_int(&encrypted, BN_num_bits(key->n));
        !            91:   buffer_put_bignum(&encrypted, key->n);
        !            92:   buffer_put_bignum(&encrypted, key->e);
1.1       deraadt    93:   buffer_put_string(&encrypted, comment, strlen(comment));
                     94:
                     95:   /* Allocate space for the private part of the key in the buffer. */
                     96:   buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
                     97:
                     98:   cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
                     99:   cipher_encrypt(&cipher, (unsigned char *)cp,
                    100:                 (unsigned char *)buffer_ptr(&buffer),
                    101:                 buffer_len(&buffer));
                    102:   memset(&cipher, 0, sizeof(cipher));
                    103:
                    104:   /* Destroy temporary data. */
                    105:   memset(buf, 0, sizeof(buf));
                    106:   buffer_free(&buffer);
                    107:
                    108:   /* Write to a file. */
                    109:   f = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0600);
                    110:   if (f < 0)
                    111:     return 0;
                    112:
                    113:   if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
                    114:       buffer_len(&encrypted))
                    115:     {
                    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;
                    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
        !           133: load_public_key(const char *filename, RSA *pub,
        !           134:                char **comment_return)
1.1       deraadt   135: {
                    136:   int f, i;
                    137:   unsigned long 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)0L, 2);
                    147:   lseek(f, (off_t)0L, 0);
                    148:
                    149:   buffer_init(&buffer);
                    150:   buffer_append_space(&buffer, &cp, len);
                    151:
                    152:   if (read(f, cp, len) != len)
                    153:     {
                    154:       debug("Read from key file %.200s failed: %.100s", filename,
                    155:            strerror(errno));
                    156:       buffer_free(&buffer);
                    157:       close(f);
                    158:       return 0;
                    159:     }
                    160:   close(f);
                    161:
                    162:   /* Check that it is at least big enought to contain the ID string. */
                    163:   if (len < strlen(AUTHFILE_ID_STRING) + 1)
                    164:     {
                    165:       debug("Bad key file %.200s.", filename);
                    166:       buffer_free(&buffer);
                    167:       return 0;
                    168:     }
                    169:
                    170:   /* Make sure it begins with the id string.  Consume the id string from
                    171:      the buffer. */
                    172:   for (i = 0; i < (unsigned int)strlen(AUTHFILE_ID_STRING) + 1; i++)
                    173:     if (buffer_get_char(&buffer) != (unsigned char)AUTHFILE_ID_STRING[i])
                    174:       {
                    175:        debug("Bad key file %.200s.", filename);
                    176:        buffer_free(&buffer);
                    177:        return 0;
                    178:       }
                    179:
                    180:   /* Skip cipher type and reserved data. */
                    181:   (void)buffer_get_char(&buffer); /* cipher type */
                    182:   (void)buffer_get_int(&buffer); /* reserved */
                    183:
                    184:   /* Read the public key from the buffer. */
1.3     ! provos    185:   buffer_get_int(&buffer);
        !           186:   pub->n = BN_new();
        !           187:   buffer_get_bignum(&buffer, pub->n);
        !           188:   pub->e = BN_new();
        !           189:   buffer_get_bignum(&buffer, pub->e);
1.1       deraadt   190:   if (comment_return)
                    191:     *comment_return = buffer_get_string(&buffer, NULL);
                    192:   /* The encrypted private part is not parsed by this function. */
                    193:
                    194:   buffer_free(&buffer);
                    195:
                    196:   return 1;
                    197: }
                    198:
                    199: /* Loads the private key from the file.  Returns 0 if an error is encountered
                    200:    (file does not exist or is not readable, or passphrase is bad).
                    201:    This initializes the private key. */
                    202:
1.3     ! provos    203: int
        !           204: load_private_key(const char *filename, const char *passphrase,
        !           205:                 RSA *prv, char **comment_return)
1.1       deraadt   206: {
                    207:   int f, i, check1, check2, cipher_type;
                    208:   unsigned long len;
                    209:   Buffer buffer, decrypted;
                    210:   char *cp;
                    211:   CipherContext cipher;
1.3     ! provos    212:   BN_CTX *ctx;
        !           213:   BIGNUM *aux;
1.1       deraadt   214:
                    215:   /* Read the file into the buffer. */
                    216:   f = open(filename, O_RDONLY);
                    217:   if (f < 0)
                    218:     return 0;
                    219:
                    220:   len = lseek(f, (off_t)0L, 2);
                    221:   lseek(f, (off_t)0L, 0);
                    222:
                    223:   buffer_init(&buffer);
                    224:   buffer_append_space(&buffer, &cp, len);
                    225:
                    226:   if (read(f, cp, len) != len)
                    227:     {
                    228:       debug("Read from key file %.200s failed: %.100s", filename,
                    229:            strerror(errno));
                    230:       buffer_free(&buffer);
                    231:       close(f);
                    232:       return 0;
                    233:     }
                    234:   close(f);
                    235:
                    236:   /* Check that it is at least big enought to contain the ID string. */
                    237:   if (len < strlen(AUTHFILE_ID_STRING) + 1)
                    238:     {
                    239:       debug("Bad key file %.200s.", filename);
                    240:       buffer_free(&buffer);
                    241:       return 0;
                    242:     }
                    243:
                    244:   /* Make sure it begins with the id string.  Consume the id string from
                    245:      the buffer. */
                    246:   for (i = 0; i < (unsigned int)strlen(AUTHFILE_ID_STRING) + 1; i++)
                    247:     if (buffer_get_char(&buffer) != (unsigned char)AUTHFILE_ID_STRING[i])
                    248:       {
                    249:        debug("Bad key file %.200s.", filename);
                    250:        buffer_free(&buffer);
                    251:        return 0;
                    252:       }
                    253:
                    254:   /* Read cipher type. */
                    255:   cipher_type = buffer_get_char(&buffer);
                    256:   (void)buffer_get_int(&buffer);  /* Reserved data. */
                    257:
                    258:   /* Read the public key from the buffer. */
1.3     ! provos    259:   buffer_get_int(&buffer);
        !           260:   prv->n = BN_new();
        !           261:   buffer_get_bignum(&buffer, prv->n);
        !           262:   prv->e = BN_new();
        !           263:   buffer_get_bignum(&buffer, prv->e);
1.1       deraadt   264:   if (comment_return)
                    265:     *comment_return = buffer_get_string(&buffer, NULL);
                    266:   else
                    267:     xfree(buffer_get_string(&buffer, NULL));
                    268:
                    269:   /* Check that it is a supported cipher. */
                    270:   if ((cipher_mask() & (1 << cipher_type)) == 0)
                    271:     {
                    272:       debug("Unsupported cipher %.100s used in key file %.200s.",
                    273:            cipher_name(cipher_type), filename);
                    274:       buffer_free(&buffer);
                    275:       goto fail;
                    276:     }
                    277:
                    278:   /* Initialize space for decrypted data. */
                    279:   buffer_init(&decrypted);
                    280:   buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
                    281:
                    282:   /* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
                    283:   cipher_set_key_string(&cipher, cipher_type, passphrase, 0);
                    284:   cipher_decrypt(&cipher, (unsigned char *)cp,
                    285:                 (unsigned char *)buffer_ptr(&buffer),
                    286:                 buffer_len(&buffer));
                    287:
                    288:   buffer_free(&buffer);
                    289:
                    290:   check1 = buffer_get_char(&decrypted);
                    291:   check2 = buffer_get_char(&decrypted);
                    292:   if (check1 != buffer_get_char(&decrypted) ||
                    293:       check2 != buffer_get_char(&decrypted))
                    294:     {
                    295:       if (strcmp(passphrase, "") != 0)
                    296:        debug("Bad passphrase supplied for key file %.200s.", filename);
                    297:       /* Bad passphrase. */
                    298:       buffer_free(&decrypted);
                    299:     fail:
1.3     ! provos    300:       BN_clear_free(prv->n);
        !           301:       BN_clear_free(prv->e);
1.1       deraadt   302:       if (comment_return)
                    303:        xfree(*comment_return);
                    304:       return 0;
                    305:     }
                    306:
                    307:   /* Read the rest of the private key. */
1.3     ! provos    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);
1.1       deraadt   331:
                    332:   buffer_free(&decrypted);
                    333:
                    334:   return 1;
                    335: }