2 * Generic SSH public-key handling operations. In particular,
\r
3 * reading of SSH public-key files, and also the generic `sign'
\r
4 * operation for SSH-2 (which checks the type of the key and
\r
5 * dispatches to the appropriate key-type specific function).
\r
16 #define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
\r
18 #define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\
\r
19 (x)-'a'<26 ? (x)-'a'+26 :\
\r
20 (x)-'0'<10 ? (x)-'0'+52 :\
\r
24 static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
\r
25 char **commentptr, char *passphrase,
\r
28 unsigned char buf[16384];
\r
29 unsigned char keybuf[16];
\r
31 int i, j, ciphertype;
\r
33 struct MD5Context md5c;
\r
38 /* Slurp the whole file (minus the header) into a buffer. */
\r
39 len = fread(buf, 1, sizeof(buf), fp);
\r
41 if (len < 0 || len == sizeof(buf)) {
\r
42 *error = "error reading file";
\r
43 goto end; /* file too big or not read */
\r
47 *error = "file format error";
\r
50 * A zero byte. (The signature includes a terminating NUL.)
\r
52 if (len - i < 1 || buf[i] != 0)
\r
56 /* One byte giving encryption type, and one reserved uint32. */
\r
59 ciphertype = buf[i];
\r
60 if (ciphertype != 0 && ciphertype != SSH_CIPHER_3DES)
\r
64 goto end; /* reserved field not present */
\r
65 if (buf[i] != 0 || buf[i + 1] != 0 || buf[i + 2] != 0
\r
66 || buf[i + 3] != 0) goto end; /* reserved field nonzero, panic! */
\r
69 /* Now the serious stuff. An ordinary SSH-1 public key. */
\r
70 i += makekey(buf + i, len, key, NULL, 1);
\r
72 goto end; /* overran */
\r
74 /* Next, the comment field. */
\r
75 j = GET_32BIT(buf + i);
\r
79 comment = snewn(j + 1, char);
\r
81 memcpy(comment, buf + i, j);
\r
86 *commentptr = dupstr(comment);
\r
88 key->comment = comment;
\r
98 ret = ciphertype != 0;
\r
104 * Decrypt remainder of buffer.
\r
108 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
\r
109 MD5Final(keybuf, &md5c);
\r
110 des3_decrypt_pubkey(keybuf, buf + i, (len - i + 7) & ~7);
\r
111 memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */
\r
115 * We are now in the secret part of the key. The first four
\r
116 * bytes should be of the form a, b, a, b.
\r
120 if (buf[i] != buf[i + 2] || buf[i + 1] != buf[i + 3]) {
\r
121 *error = "wrong passphrase";
\r
128 * After that, we have one further bignum which is our
\r
129 * decryption exponent, and then the three auxiliary values
\r
132 j = makeprivate(buf + i, len - i, key);
\r
133 if (j < 0) goto end;
\r
135 j = ssh1_read_bignum(buf + i, len - i, &key->iqmp);
\r
136 if (j < 0) goto end;
\r
138 j = ssh1_read_bignum(buf + i, len - i, &key->q);
\r
139 if (j < 0) goto end;
\r
141 j = ssh1_read_bignum(buf + i, len - i, &key->p);
\r
142 if (j < 0) goto end;
\r
145 if (!rsa_verify(key)) {
\r
146 *error = "rsa_verify failed";
\r
153 memset(buf, 0, sizeof(buf)); /* burn the evidence */
\r
157 int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,
\r
158 const char **errorstr)
\r
163 const char *error = NULL;
\r
165 fp = f_open(*filename, "rb", FALSE);
\r
167 error = "can't open file";
\r
172 * Read the first line of the file and see if it's a v1 private
\r
175 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
177 * This routine will take care of calling fclose() for us.
\r
179 ret = loadrsakey_main(fp, key, FALSE, NULL, passphrase, &error);
\r
185 * Otherwise, we have nothing. Return empty-handed.
\r
187 error = "not an SSH-1 RSA file";
\r
192 if ((ret != 1) && errorstr)
\r
198 * See whether an RSA key is encrypted. Return its comment field as
\r
201 int rsakey_encrypted(const Filename *filename, char **comment)
\r
206 fp = f_open(*filename, "rb", FALSE);
\r
208 return 0; /* doesn't even exist */
\r
211 * Read the first line of the file and see if it's a v1 private
\r
214 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
217 * This routine will take care of calling fclose() for us.
\r
219 return loadrsakey_main(fp, NULL, FALSE, comment, NULL, &dummy);
\r
222 return 0; /* wasn't the right kind of file */
\r
226 * Return a malloc'ed chunk of memory containing the public blob of
\r
227 * an RSA key, as given in the agent protocol (modulus bits,
\r
228 * exponent, modulus).
\r
230 int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
\r
231 char **commentptr, const char **errorstr)
\r
237 const char *error = NULL;
\r
239 /* Default return if we fail. */
\r
244 fp = f_open(*filename, "rb", FALSE);
\r
246 error = "can't open file";
\r
251 * Read the first line of the file and see if it's a v1 private
\r
254 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
255 memset(&key, 0, sizeof(key));
\r
256 if (loadrsakey_main(fp, &key, TRUE, commentptr, NULL, &error)) {
\r
257 *blob = rsa_public_blob(&key, bloblen);
\r
263 error = "not an SSH-1 RSA file";
\r
269 if ((ret != 1) && errorstr)
\r
275 * Save an RSA key file. Return nonzero on success.
\r
277 int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)
\r
279 unsigned char buf[16384];
\r
280 unsigned char keybuf[16];
\r
281 struct MD5Context md5c;
\r
282 unsigned char *p, *estart;
\r
286 * Write the initial signature.
\r
289 memcpy(p, rsa_signature, sizeof(rsa_signature));
\r
290 p += sizeof(rsa_signature);
\r
293 * One byte giving encryption type, and one reserved (zero)
\r
296 *p++ = (passphrase ? SSH_CIPHER_3DES : 0);
\r
301 * An ordinary SSH-1 public key consists of: a uint32
\r
302 * containing the bit count, then two bignums containing the
\r
303 * modulus and exponent respectively.
\r
305 PUT_32BIT(p, bignum_bitcount(key->modulus));
\r
307 p += ssh1_write_bignum(p, key->modulus);
\r
308 p += ssh1_write_bignum(p, key->exponent);
\r
311 * A string containing the comment field.
\r
313 if (key->comment) {
\r
314 PUT_32BIT(p, strlen(key->comment));
\r
316 memcpy(p, key->comment, strlen(key->comment));
\r
317 p += strlen(key->comment);
\r
324 * The encrypted portion starts here.
\r
329 * Two bytes, then the same two bytes repeated.
\r
331 *p++ = random_byte();
\r
332 *p++ = random_byte();
\r
338 * Four more bignums: the decryption exponent, then iqmp, then
\r
341 p += ssh1_write_bignum(p, key->private_exponent);
\r
342 p += ssh1_write_bignum(p, key->iqmp);
\r
343 p += ssh1_write_bignum(p, key->q);
\r
344 p += ssh1_write_bignum(p, key->p);
\r
347 * Now write zeros until the encrypted portion is a multiple of
\r
350 while ((p - estart) % 8)
\r
354 * Now encrypt the encrypted portion.
\r
358 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
\r
359 MD5Final(keybuf, &md5c);
\r
360 des3_encrypt_pubkey(keybuf, estart, p - estart);
\r
361 memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */
\r
365 * Done. Write the result to the file.
\r
367 fp = f_open(*filename, "wb", TRUE);
\r
369 int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));
\r
377 /* ----------------------------------------------------------------------
\r
378 * SSH-2 private key load/store functions.
\r
382 * PuTTY's own format for SSH-2 keys is as follows:
\r
384 * The file is text. Lines are terminated by CRLF, although CR-only
\r
385 * and LF-only are tolerated on input.
\r
387 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
\r
388 * algorithm ("ssh-dss", "ssh-rsa" etc).
\r
390 * The next line says "Encryption: " plus an encryption type.
\r
391 * Currently the only supported encryption types are "aes256-cbc"
\r
394 * The next line says "Comment: " plus the comment string.
\r
396 * Next there is a line saying "Public-Lines: " plus a number N.
\r
397 * The following N lines contain a base64 encoding of the public
\r
398 * part of the key. This is encoded as the standard SSH-2 public key
\r
399 * blob (with no initial length): so for RSA, for example, it will
\r
406 * Next, there is a line saying "Private-Lines: " plus a number N,
\r
407 * and then N lines containing the (potentially encrypted) private
\r
408 * part of the key. For the key type "ssh-rsa", this will be
\r
411 * mpint private_exponent
\r
412 * mpint p (the larger of the two primes)
\r
413 * mpint q (the smaller prime)
\r
414 * mpint iqmp (the inverse of q modulo p)
\r
415 * data padding (to reach a multiple of the cipher block size)
\r
417 * And for "ssh-dss", it will be composed of
\r
419 * mpint x (the private key parameter)
\r
420 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
\r
422 * Finally, there is a line saying "Private-MAC: " plus a hex
\r
423 * representation of a HMAC-SHA-1 of:
\r
425 * string name of algorithm ("ssh-dss", "ssh-rsa")
\r
426 * string encryption type
\r
428 * string public-blob
\r
429 * string private-plaintext (the plaintext version of the
\r
430 * private part, including the final
\r
433 * The key to the MAC is itself a SHA-1 hash of:
\r
435 * data "putty-private-key-file-mac-key"
\r
438 * (An empty passphrase is used for unencrypted keys.)
\r
440 * If the key is encrypted, the encryption key is derived from the
\r
441 * passphrase by means of a succession of SHA-1 hashes. Each hash
\r
444 * uint32 sequence-number
\r
447 * where the sequence-number increases from zero. As many of these
\r
448 * hashes are used as necessary.
\r
450 * For backwards compatibility with snapshots between 0.51 and
\r
451 * 0.52, we also support the older key file format, which begins
\r
452 * with "PuTTY-User-Key-File-1" (version number differs). In this
\r
453 * format the Private-MAC: field only covers the private-plaintext
\r
454 * field and nothing else (and without the 4-byte string length on
\r
455 * the front too). Moreover, the Private-MAC: field can be replaced
\r
456 * with a Private-Hash: field which is a plain SHA-1 hash instead of
\r
457 * an HMAC (this was generated for unencrypted keys).
\r
460 static int read_header(FILE * fp, char *header)
\r
467 if (c == '\n' || c == '\r' || c == EOF)
\r
468 return 0; /* failure */
\r
474 return 1; /* success! */
\r
477 return 0; /* failure */
\r
481 return 0; /* failure */
\r
484 static char *read_body(FILE * fp)
\r
492 text = snewn(size, char);
\r
498 if (c == '\r' || c == '\n') {
\r
500 if (c != '\r' && c != '\n' && c != EOF)
\r
508 if (len + 1 >= size) {
\r
510 text = sresize(text, size, char);
\r
517 int base64_decode_atom(char *atom, unsigned char *out)
\r
524 for (i = 0; i < 4; i++) {
\r
526 if (c >= 'A' && c <= 'Z')
\r
528 else if (c >= 'a' && c <= 'z')
\r
530 else if (c >= '0' && c <= '9')
\r
539 return 0; /* invalid atom */
\r
543 if (vals[0] == -1 || vals[1] == -1)
\r
545 if (vals[2] == -1 && vals[3] != -1)
\r
550 else if (vals[2] != -1)
\r
555 word = ((vals[0] << 18) |
\r
556 (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
\r
557 out[0] = (word >> 16) & 0xFF;
\r
559 out[1] = (word >> 8) & 0xFF;
\r
561 out[2] = word & 0xFF;
\r
565 static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
\r
567 unsigned char *blob;
\r
572 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
\r
573 blob = snewn(48 * nlines, unsigned char);
\r
575 for (i = 0; i < nlines; i++) {
\r
576 line = read_body(fp);
\r
581 linelen = strlen(line);
\r
582 if (linelen % 4 != 0 || linelen > 64) {
\r
587 for (j = 0; j < linelen; j += 4) {
\r
588 k = base64_decode_atom(line + j, blob + len);
\r
603 * Magic error return value for when the passphrase is wrong.
\r
605 struct ssh2_userkey ssh2_wrong_passphrase = {
\r
609 const struct ssh_signkey *find_pubkey_alg(const char *name)
\r
611 if (!strcmp(name, "ssh-rsa"))
\r
613 else if (!strcmp(name, "ssh-dss"))
\r
619 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
\r
620 char *passphrase, const char **errorstr)
\r
623 char header[40], *b, *encryption, *comment, *mac;
\r
624 const struct ssh_signkey *alg;
\r
625 struct ssh2_userkey *ret;
\r
626 int cipher, cipherblk;
\r
627 unsigned char *public_blob, *private_blob;
\r
628 int public_blob_len, private_blob_len;
\r
629 int i, is_mac, old_fmt;
\r
630 int passlen = passphrase ? strlen(passphrase) : 0;
\r
631 const char *error = NULL;
\r
633 ret = NULL; /* return NULL for most errors */
\r
634 encryption = comment = mac = NULL;
\r
635 public_blob = private_blob = NULL;
\r
637 fp = f_open(*filename, "rb", FALSE);
\r
639 error = "can't open file";
\r
643 /* Read the first header line which contains the key type. */
\r
644 if (!read_header(fp, header))
\r
646 if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {
\r
648 } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {
\r
649 /* this is an old key file; warn and then continue */
\r
650 old_keyfile_warning();
\r
653 error = "not a PuTTY SSH-2 private key";
\r
656 error = "file format error";
\r
657 if ((b = read_body(fp)) == NULL)
\r
659 /* Select key algorithm structure. */
\r
660 alg = find_pubkey_alg(b);
\r
667 /* Read the Encryption header line. */
\r
668 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
670 if ((encryption = read_body(fp)) == NULL)
\r
672 if (!strcmp(encryption, "aes256-cbc")) {
\r
675 } else if (!strcmp(encryption, "none")) {
\r
683 /* Read the Comment header line. */
\r
684 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
686 if ((comment = read_body(fp)) == NULL)
\r
689 /* Read the Public-Lines header line and the public blob. */
\r
690 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
692 if ((b = read_body(fp)) == NULL)
\r
696 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
699 /* Read the Private-Lines header line and the Private blob. */
\r
700 if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))
\r
702 if ((b = read_body(fp)) == NULL)
\r
706 if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)
\r
709 /* Read the Private-MAC or Private-Hash header line. */
\r
710 if (!read_header(fp, header))
\r
712 if (0 == strcmp(header, "Private-MAC")) {
\r
713 if ((mac = read_body(fp)) == NULL)
\r
716 } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {
\r
717 if ((mac = read_body(fp)) == NULL)
\r
727 * Decrypt the private blob.
\r
730 unsigned char key[40];
\r
735 if (private_blob_len % cipherblk)
\r
739 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
740 SHA_Bytes(&s, passphrase, passlen);
\r
741 SHA_Final(&s, key + 0);
\r
743 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
744 SHA_Bytes(&s, passphrase, passlen);
\r
745 SHA_Final(&s, key + 20);
\r
746 aes256_decrypt_pubkey(key, private_blob, private_blob_len);
\r
754 unsigned char binary[20];
\r
755 unsigned char *macdata;
\r
760 /* MAC (or hash) only covers the private blob. */
\r
761 macdata = private_blob;
\r
762 maclen = private_blob_len;
\r
766 int namelen = strlen(alg->name);
\r
767 int enclen = strlen(encryption);
\r
768 int commlen = strlen(comment);
\r
769 maclen = (4 + namelen +
\r
772 4 + public_blob_len +
\r
773 4 + private_blob_len);
\r
774 macdata = snewn(maclen, unsigned char);
\r
776 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
777 DO_STR(alg->name, namelen);
\r
778 DO_STR(encryption, enclen);
\r
779 DO_STR(comment, commlen);
\r
780 DO_STR(public_blob, public_blob_len);
\r
781 DO_STR(private_blob, private_blob_len);
\r
788 unsigned char mackey[20];
\r
789 char header[] = "putty-private-key-file-mac-key";
\r
792 SHA_Bytes(&s, header, sizeof(header)-1);
\r
793 if (cipher && passphrase)
\r
794 SHA_Bytes(&s, passphrase, passlen);
\r
795 SHA_Final(&s, mackey);
\r
797 hmac_sha1_simple(mackey, 20, macdata, maclen, binary);
\r
799 memset(mackey, 0, sizeof(mackey));
\r
800 memset(&s, 0, sizeof(s));
\r
802 SHA_Simple(macdata, maclen, binary);
\r
805 if (free_macdata) {
\r
806 memset(macdata, 0, maclen);
\r
810 for (i = 0; i < 20; i++)
\r
811 sprintf(realmac + 2 * i, "%02x", binary[i]);
\r
813 if (strcmp(mac, realmac)) {
\r
814 /* An incorrect MAC is an unconditional Error if the key is
\r
815 * unencrypted. Otherwise, it means Wrong Passphrase. */
\r
817 error = "wrong passphrase";
\r
818 ret = SSH2_WRONG_PASSPHRASE;
\r
820 error = "MAC failed";
\r
829 * Create and return the key.
\r
831 ret = snew(struct ssh2_userkey);
\r
833 ret->comment = comment;
\r
834 ret->data = alg->createkey(public_blob, public_blob_len,
\r
835 private_blob, private_blob_len);
\r
837 sfree(ret->comment);
\r
840 error = "createkey failed";
\r
843 sfree(public_blob);
\r
844 sfree(private_blob);
\r
851 * Error processing.
\r
863 sfree(public_blob);
\r
865 sfree(private_blob);
\r
871 unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
\r
872 int *pub_blob_len, char **commentptr,
\r
873 const char **errorstr)
\r
876 char header[40], *b;
\r
877 const struct ssh_signkey *alg;
\r
878 unsigned char *public_blob;
\r
879 int public_blob_len;
\r
881 const char *error = NULL;
\r
884 public_blob = NULL;
\r
886 fp = f_open(*filename, "rb", FALSE);
\r
888 error = "can't open file";
\r
892 /* Read the first header line which contains the key type. */
\r
893 if (!read_header(fp, header)
\r
894 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
895 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
896 error = "not a PuTTY SSH-2 private key";
\r
899 error = "file format error";
\r
900 if ((b = read_body(fp)) == NULL)
\r
902 /* Select key algorithm structure. */
\r
903 alg = find_pubkey_alg(b);
\r
910 /* Read the Encryption header line. */
\r
911 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
913 if ((b = read_body(fp)) == NULL)
\r
915 sfree(b); /* we don't care */
\r
917 /* Read the Comment header line. */
\r
918 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
920 if ((comment = read_body(fp)) == NULL)
\r
924 *commentptr = comment;
\r
928 /* Read the Public-Lines header line and the public blob. */
\r
929 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
931 if ((b = read_body(fp)) == NULL)
\r
935 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
940 *pub_blob_len = public_blob_len;
\r
942 *algorithm = alg->name;
\r
943 return public_blob;
\r
946 * Error processing.
\r
952 sfree(public_blob);
\r
958 int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
\r
961 char header[40], *b, *comment;
\r
965 *commentptr = NULL;
\r
967 fp = f_open(*filename, "rb", FALSE);
\r
970 if (!read_header(fp, header)
\r
971 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
972 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
976 if ((b = read_body(fp)) == NULL) {
\r
980 sfree(b); /* we don't care about key type here */
\r
981 /* Read the Encryption header line. */
\r
982 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
\r
986 if ((b = read_body(fp)) == NULL) {
\r
991 /* Read the Comment header line. */
\r
992 if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
\r
997 if ((comment = read_body(fp)) == NULL) {
\r
1004 *commentptr = comment;
\r
1007 if (!strcmp(b, "aes256-cbc"))
\r
1015 int base64_lines(int datalen)
\r
1017 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
\r
1018 return (datalen + 47) / 48;
\r
1021 void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)
\r
1027 while (datalen > 0) {
\r
1028 n = (datalen < 3 ? datalen : 3);
\r
1029 base64_encode_atom(data, n, out);
\r
1032 for (i = 0; i < 4; i++) {
\r
1033 if (linelen >= cpl) {
\r
1037 fputc(out[i], fp);
\r
1044 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
\r
1048 unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
\r
1049 int pub_blob_len, priv_blob_len, priv_encrypted_len;
\r
1054 unsigned char priv_mac[20];
\r
1057 * Fetch the key component blobs.
\r
1059 pub_blob = key->alg->public_blob(key->data, &pub_blob_len);
\r
1060 priv_blob = key->alg->private_blob(key->data, &priv_blob_len);
\r
1061 if (!pub_blob || !priv_blob) {
\r
1068 * Determine encryption details, and encrypt the private blob.
\r
1071 cipherstr = "aes256-cbc";
\r
1074 cipherstr = "none";
\r
1077 priv_encrypted_len = priv_blob_len + cipherblk - 1;
\r
1078 priv_encrypted_len -= priv_encrypted_len % cipherblk;
\r
1079 priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
\r
1080 memset(priv_blob_encrypted, 0, priv_encrypted_len);
\r
1081 memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
\r
1082 /* Create padding based on the SHA hash of the unpadded blob. This prevents
\r
1083 * too easy a known-plaintext attack on the last block. */
\r
1084 SHA_Simple(priv_blob, priv_blob_len, priv_mac);
\r
1085 assert(priv_encrypted_len - priv_blob_len < 20);
\r
1086 memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,
\r
1087 priv_encrypted_len - priv_blob_len);
\r
1089 /* Now create the MAC. */
\r
1091 unsigned char *macdata;
\r
1094 int namelen = strlen(key->alg->name);
\r
1095 int enclen = strlen(cipherstr);
\r
1096 int commlen = strlen(key->comment);
\r
1098 unsigned char mackey[20];
\r
1099 char header[] = "putty-private-key-file-mac-key";
\r
1101 maclen = (4 + namelen +
\r
1104 4 + pub_blob_len +
\r
1105 4 + priv_encrypted_len);
\r
1106 macdata = snewn(maclen, unsigned char);
\r
1108 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
1109 DO_STR(key->alg->name, namelen);
\r
1110 DO_STR(cipherstr, enclen);
\r
1111 DO_STR(key->comment, commlen);
\r
1112 DO_STR(pub_blob, pub_blob_len);
\r
1113 DO_STR(priv_blob_encrypted, priv_encrypted_len);
\r
1116 SHA_Bytes(&s, header, sizeof(header)-1);
\r
1118 SHA_Bytes(&s, passphrase, strlen(passphrase));
\r
1119 SHA_Final(&s, mackey);
\r
1120 hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);
\r
1121 memset(macdata, 0, maclen);
\r
1123 memset(mackey, 0, sizeof(mackey));
\r
1124 memset(&s, 0, sizeof(s));
\r
1128 unsigned char key[40];
\r
1131 passlen = strlen(passphrase);
\r
1134 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
1135 SHA_Bytes(&s, passphrase, passlen);
\r
1136 SHA_Final(&s, key + 0);
\r
1138 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
1139 SHA_Bytes(&s, passphrase, passlen);
\r
1140 SHA_Final(&s, key + 20);
\r
1141 aes256_encrypt_pubkey(key, priv_blob_encrypted,
\r
1142 priv_encrypted_len);
\r
1144 memset(key, 0, sizeof(key));
\r
1145 memset(&s, 0, sizeof(s));
\r
1148 fp = f_open(*filename, "w", TRUE);
\r
1151 fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);
\r
1152 fprintf(fp, "Encryption: %s\n", cipherstr);
\r
1153 fprintf(fp, "Comment: %s\n", key->comment);
\r
1154 fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));
\r
1155 base64_encode(fp, pub_blob, pub_blob_len, 64);
\r
1156 fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
\r
1157 base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);
\r
1158 fprintf(fp, "Private-MAC: ");
\r
1159 for (i = 0; i < 20; i++)
\r
1160 fprintf(fp, "%02x", priv_mac[i]);
\r
1161 fprintf(fp, "\n");
\r
1165 memset(priv_blob, 0, priv_blob_len);
\r
1167 sfree(priv_blob_encrypted);
\r
1171 /* ----------------------------------------------------------------------
\r
1172 * A function to determine the type of a private key file. Returns
\r
1173 * 0 on failure, 1 or 2 on success.
\r
1175 int key_type(const Filename *filename)
\r
1179 const char putty2_sig[] = "PuTTY-User-Key-File-";
\r
1180 const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
\r
1181 const char openssh_sig[] = "-----BEGIN ";
\r
1184 fp = f_open(*filename, "r", FALSE);
\r
1186 return SSH_KEYTYPE_UNOPENABLE;
\r
1187 i = fread(buf, 1, sizeof(buf), fp);
\r
1190 return SSH_KEYTYPE_UNOPENABLE;
\r
1192 return SSH_KEYTYPE_UNKNOWN;
\r
1193 if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))
\r
1194 return SSH_KEYTYPE_SSH1;
\r
1195 if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))
\r
1196 return SSH_KEYTYPE_SSH2;
\r
1197 if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))
\r
1198 return SSH_KEYTYPE_OPENSSH;
\r
1199 if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))
\r
1200 return SSH_KEYTYPE_SSHCOM;
\r
1201 return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */
\r
1205 * Convert the type word to a string, for `wrong type' error
\r
1208 char *key_type_to_str(int type)
\r
1211 case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;
\r
1212 case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;
\r
1213 case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;
\r
1214 case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;
\r
1215 case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;
\r
1216 case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;
\r
1217 default: return "INTERNAL ERROR"; break;
\r