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 j = makekey(buf + i, len - i, key, NULL, 1);
\r
72 goto end; /* overran */
\r
75 /* Next, the comment field. */
\r
76 j = toint(GET_32BIT(buf + i));
\r
78 if (j < 0 || len - i < j)
\r
80 comment = snewn(j + 1, char);
\r
82 memcpy(comment, buf + i, j);
\r
87 *commentptr = dupstr(comment);
\r
89 key->comment = comment;
\r
99 ret = ciphertype != 0;
\r
105 * Decrypt remainder of buffer.
\r
109 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
\r
110 MD5Final(keybuf, &md5c);
\r
111 des3_decrypt_pubkey(keybuf, buf + i, (len - i + 7) & ~7);
\r
112 smemclr(keybuf, sizeof(keybuf)); /* burn the evidence */
\r
116 * We are now in the secret part of the key. The first four
\r
117 * bytes should be of the form a, b, a, b.
\r
121 if (buf[i] != buf[i + 2] || buf[i + 1] != buf[i + 3]) {
\r
122 *error = "wrong passphrase";
\r
129 * After that, we have one further bignum which is our
\r
130 * decryption exponent, and then the three auxiliary values
\r
133 j = makeprivate(buf + i, len - i, key);
\r
134 if (j < 0) goto end;
\r
136 j = ssh1_read_bignum(buf + i, len - i, &key->iqmp);
\r
137 if (j < 0) goto end;
\r
139 j = ssh1_read_bignum(buf + i, len - i, &key->q);
\r
140 if (j < 0) goto end;
\r
142 j = ssh1_read_bignum(buf + i, len - i, &key->p);
\r
143 if (j < 0) goto end;
\r
146 if (!rsa_verify(key)) {
\r
147 *error = "rsa_verify failed";
\r
154 smemclr(buf, sizeof(buf)); /* burn the evidence */
\r
158 int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,
\r
159 const char **errorstr)
\r
164 const char *error = NULL;
\r
166 fp = f_open(filename, "rb", FALSE);
\r
168 error = "can't open file";
\r
173 * Read the first line of the file and see if it's a v1 private
\r
176 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
178 * This routine will take care of calling fclose() for us.
\r
180 ret = loadrsakey_main(fp, key, FALSE, NULL, passphrase, &error);
\r
186 * Otherwise, we have nothing. Return empty-handed.
\r
188 error = "not an SSH-1 RSA file";
\r
193 if ((ret != 1) && errorstr)
\r
199 * See whether an RSA key is encrypted. Return its comment field as
\r
202 int rsakey_encrypted(const Filename *filename, char **comment)
\r
207 fp = f_open(filename, "rb", FALSE);
\r
209 return 0; /* doesn't even exist */
\r
212 * Read the first line of the file and see if it's a v1 private
\r
215 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
218 * This routine will take care of calling fclose() for us.
\r
220 return loadrsakey_main(fp, NULL, FALSE, comment, NULL, &dummy);
\r
223 return 0; /* wasn't the right kind of file */
\r
227 * Return a malloc'ed chunk of memory containing the public blob of
\r
228 * an RSA key, as given in the agent protocol (modulus bits,
\r
229 * exponent, modulus).
\r
231 int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
\r
232 char **commentptr, const char **errorstr)
\r
238 const char *error = NULL;
\r
240 /* Default return if we fail. */
\r
245 fp = f_open(filename, "rb", FALSE);
\r
247 error = "can't open file";
\r
252 * Read the first line of the file and see if it's a v1 private
\r
255 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
\r
256 memset(&key, 0, sizeof(key));
\r
257 if (loadrsakey_main(fp, &key, TRUE, commentptr, NULL, &error)) {
\r
258 *blob = rsa_public_blob(&key, bloblen);
\r
262 fp = NULL; /* loadrsakey_main unconditionally closes fp */
\r
264 error = "not an SSH-1 RSA file";
\r
270 if ((ret != 1) && errorstr)
\r
276 * Save an RSA key file. Return nonzero on success.
\r
278 int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)
\r
280 unsigned char buf[16384];
\r
281 unsigned char keybuf[16];
\r
282 struct MD5Context md5c;
\r
283 unsigned char *p, *estart;
\r
287 * Write the initial signature.
\r
290 memcpy(p, rsa_signature, sizeof(rsa_signature));
\r
291 p += sizeof(rsa_signature);
\r
294 * One byte giving encryption type, and one reserved (zero)
\r
297 *p++ = (passphrase ? SSH_CIPHER_3DES : 0);
\r
302 * An ordinary SSH-1 public key consists of: a uint32
\r
303 * containing the bit count, then two bignums containing the
\r
304 * modulus and exponent respectively.
\r
306 PUT_32BIT(p, bignum_bitcount(key->modulus));
\r
308 p += ssh1_write_bignum(p, key->modulus);
\r
309 p += ssh1_write_bignum(p, key->exponent);
\r
312 * A string containing the comment field.
\r
314 if (key->comment) {
\r
315 PUT_32BIT(p, strlen(key->comment));
\r
317 memcpy(p, key->comment, strlen(key->comment));
\r
318 p += strlen(key->comment);
\r
325 * The encrypted portion starts here.
\r
330 * Two bytes, then the same two bytes repeated.
\r
332 *p++ = random_byte();
\r
333 *p++ = random_byte();
\r
339 * Four more bignums: the decryption exponent, then iqmp, then
\r
342 p += ssh1_write_bignum(p, key->private_exponent);
\r
343 p += ssh1_write_bignum(p, key->iqmp);
\r
344 p += ssh1_write_bignum(p, key->q);
\r
345 p += ssh1_write_bignum(p, key->p);
\r
348 * Now write zeros until the encrypted portion is a multiple of
\r
351 while ((p - estart) % 8)
\r
355 * Now encrypt the encrypted portion.
\r
359 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
\r
360 MD5Final(keybuf, &md5c);
\r
361 des3_encrypt_pubkey(keybuf, estart, p - estart);
\r
362 smemclr(keybuf, sizeof(keybuf)); /* burn the evidence */
\r
366 * Done. Write the result to the file.
\r
368 fp = f_open(filename, "wb", TRUE);
\r
370 int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));
\r
378 /* ----------------------------------------------------------------------
\r
379 * SSH-2 private key load/store functions.
\r
383 * PuTTY's own format for SSH-2 keys is as follows:
\r
385 * The file is text. Lines are terminated by CRLF, although CR-only
\r
386 * and LF-only are tolerated on input.
\r
388 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
\r
389 * algorithm ("ssh-dss", "ssh-rsa" etc).
\r
391 * The next line says "Encryption: " plus an encryption type.
\r
392 * Currently the only supported encryption types are "aes256-cbc"
\r
395 * The next line says "Comment: " plus the comment string.
\r
397 * Next there is a line saying "Public-Lines: " plus a number N.
\r
398 * The following N lines contain a base64 encoding of the public
\r
399 * part of the key. This is encoded as the standard SSH-2 public key
\r
400 * blob (with no initial length): so for RSA, for example, it will
\r
407 * Next, there is a line saying "Private-Lines: " plus a number N,
\r
408 * and then N lines containing the (potentially encrypted) private
\r
409 * part of the key. For the key type "ssh-rsa", this will be
\r
412 * mpint private_exponent
\r
413 * mpint p (the larger of the two primes)
\r
414 * mpint q (the smaller prime)
\r
415 * mpint iqmp (the inverse of q modulo p)
\r
416 * data padding (to reach a multiple of the cipher block size)
\r
418 * And for "ssh-dss", it will be composed of
\r
420 * mpint x (the private key parameter)
\r
421 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
\r
423 * Finally, there is a line saying "Private-MAC: " plus a hex
\r
424 * representation of a HMAC-SHA-1 of:
\r
426 * string name of algorithm ("ssh-dss", "ssh-rsa")
\r
427 * string encryption type
\r
429 * string public-blob
\r
430 * string private-plaintext (the plaintext version of the
\r
431 * private part, including the final
\r
434 * The key to the MAC is itself a SHA-1 hash of:
\r
436 * data "putty-private-key-file-mac-key"
\r
439 * (An empty passphrase is used for unencrypted keys.)
\r
441 * If the key is encrypted, the encryption key is derived from the
\r
442 * passphrase by means of a succession of SHA-1 hashes. Each hash
\r
445 * uint32 sequence-number
\r
448 * where the sequence-number increases from zero. As many of these
\r
449 * hashes are used as necessary.
\r
451 * For backwards compatibility with snapshots between 0.51 and
\r
452 * 0.52, we also support the older key file format, which begins
\r
453 * with "PuTTY-User-Key-File-1" (version number differs). In this
\r
454 * format the Private-MAC: field only covers the private-plaintext
\r
455 * field and nothing else (and without the 4-byte string length on
\r
456 * the front too). Moreover, the Private-MAC: field can be replaced
\r
457 * with a Private-Hash: field which is a plain SHA-1 hash instead of
\r
458 * an HMAC (this was generated for unencrypted keys).
\r
461 static int read_header(FILE * fp, char *header)
\r
468 if (c == '\n' || c == '\r' || c == EOF)
\r
469 return 0; /* failure */
\r
475 return 1; /* success! */
\r
478 return 0; /* failure */
\r
482 return 0; /* failure */
\r
485 static char *read_body(FILE * fp)
\r
493 text = snewn(size, char);
\r
499 if (c == '\r' || c == '\n' || c == EOF) {
\r
502 if (c != '\r' && c != '\n')
\r
507 if (len + 1 >= size) {
\r
509 text = sresize(text, size, char);
\r
516 static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
\r
518 unsigned char *blob;
\r
523 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
\r
524 blob = snewn(48 * nlines, unsigned char);
\r
526 for (i = 0; i < nlines; i++) {
\r
527 line = read_body(fp);
\r
532 linelen = strlen(line);
\r
533 if (linelen % 4 != 0 || linelen > 64) {
\r
538 for (j = 0; j < linelen; j += 4) {
\r
539 k = base64_decode_atom(line + j, blob + len);
\r
554 * Magic error return value for when the passphrase is wrong.
\r
556 struct ssh2_userkey ssh2_wrong_passphrase = {
\r
560 const struct ssh_signkey *find_pubkey_alg(const char *name)
\r
562 if (!strcmp(name, "ssh-rsa"))
\r
564 else if (!strcmp(name, "ssh-dss"))
\r
570 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
\r
571 char *passphrase, const char **errorstr)
\r
574 char header[40], *b, *encryption, *comment, *mac;
\r
575 const struct ssh_signkey *alg;
\r
576 struct ssh2_userkey *ret;
\r
577 int cipher, cipherblk;
\r
578 unsigned char *public_blob, *private_blob;
\r
579 int public_blob_len, private_blob_len;
\r
580 int i, is_mac, old_fmt;
\r
581 int passlen = passphrase ? strlen(passphrase) : 0;
\r
582 const char *error = NULL;
\r
584 ret = NULL; /* return NULL for most errors */
\r
585 encryption = comment = mac = NULL;
\r
586 public_blob = private_blob = NULL;
\r
588 fp = f_open(filename, "rb", FALSE);
\r
590 error = "can't open file";
\r
594 /* Read the first header line which contains the key type. */
\r
595 if (!read_header(fp, header))
\r
597 if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {
\r
599 } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {
\r
600 /* this is an old key file; warn and then continue */
\r
601 old_keyfile_warning();
\r
603 } else if (0 == strncmp(header, "PuTTY-User-Key-File-", 20)) {
\r
604 /* this is a key file FROM THE FUTURE; refuse it, but with a
\r
605 * more specific error message than the generic one below */
\r
606 error = "PuTTY key format too new";
\r
609 error = "not a PuTTY SSH-2 private key";
\r
612 error = "file format error";
\r
613 if ((b = read_body(fp)) == NULL)
\r
615 /* Select key algorithm structure. */
\r
616 alg = find_pubkey_alg(b);
\r
623 /* Read the Encryption header line. */
\r
624 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
626 if ((encryption = read_body(fp)) == NULL)
\r
628 if (!strcmp(encryption, "aes256-cbc")) {
\r
631 } else if (!strcmp(encryption, "none")) {
\r
638 /* Read the Comment header line. */
\r
639 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
641 if ((comment = read_body(fp)) == NULL)
\r
644 /* Read the Public-Lines header line and the public blob. */
\r
645 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
647 if ((b = read_body(fp)) == NULL)
\r
651 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
654 /* Read the Private-Lines header line and the Private blob. */
\r
655 if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))
\r
657 if ((b = read_body(fp)) == NULL)
\r
661 if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)
\r
664 /* Read the Private-MAC or Private-Hash header line. */
\r
665 if (!read_header(fp, header))
\r
667 if (0 == strcmp(header, "Private-MAC")) {
\r
668 if ((mac = read_body(fp)) == NULL)
\r
671 } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {
\r
672 if ((mac = read_body(fp)) == NULL)
\r
682 * Decrypt the private blob.
\r
685 unsigned char key[40];
\r
690 if (private_blob_len % cipherblk)
\r
694 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
695 SHA_Bytes(&s, passphrase, passlen);
\r
696 SHA_Final(&s, key + 0);
\r
698 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
699 SHA_Bytes(&s, passphrase, passlen);
\r
700 SHA_Final(&s, key + 20);
\r
701 aes256_decrypt_pubkey(key, private_blob, private_blob_len);
\r
709 unsigned char binary[20];
\r
710 unsigned char *macdata;
\r
715 /* MAC (or hash) only covers the private blob. */
\r
716 macdata = private_blob;
\r
717 maclen = private_blob_len;
\r
721 int namelen = strlen(alg->name);
\r
722 int enclen = strlen(encryption);
\r
723 int commlen = strlen(comment);
\r
724 maclen = (4 + namelen +
\r
727 4 + public_blob_len +
\r
728 4 + private_blob_len);
\r
729 macdata = snewn(maclen, unsigned char);
\r
731 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
732 DO_STR(alg->name, namelen);
\r
733 DO_STR(encryption, enclen);
\r
734 DO_STR(comment, commlen);
\r
735 DO_STR(public_blob, public_blob_len);
\r
736 DO_STR(private_blob, private_blob_len);
\r
743 unsigned char mackey[20];
\r
744 char header[] = "putty-private-key-file-mac-key";
\r
747 SHA_Bytes(&s, header, sizeof(header)-1);
\r
748 if (cipher && passphrase)
\r
749 SHA_Bytes(&s, passphrase, passlen);
\r
750 SHA_Final(&s, mackey);
\r
752 hmac_sha1_simple(mackey, 20, macdata, maclen, binary);
\r
754 smemclr(mackey, sizeof(mackey));
\r
755 smemclr(&s, sizeof(s));
\r
757 SHA_Simple(macdata, maclen, binary);
\r
760 if (free_macdata) {
\r
761 smemclr(macdata, maclen);
\r
765 for (i = 0; i < 20; i++)
\r
766 sprintf(realmac + 2 * i, "%02x", binary[i]);
\r
768 if (strcmp(mac, realmac)) {
\r
769 /* An incorrect MAC is an unconditional Error if the key is
\r
770 * unencrypted. Otherwise, it means Wrong Passphrase. */
\r
772 error = "wrong passphrase";
\r
773 ret = SSH2_WRONG_PASSPHRASE;
\r
775 error = "MAC failed";
\r
785 * Create and return the key.
\r
787 ret = snew(struct ssh2_userkey);
\r
789 ret->comment = comment;
\r
790 ret->data = alg->createkey(public_blob, public_blob_len,
\r
791 private_blob, private_blob_len);
\r
795 error = "createkey failed";
\r
798 sfree(public_blob);
\r
799 smemclr(private_blob, private_blob_len);
\r
800 sfree(private_blob);
\r
807 * Error processing.
\r
819 sfree(public_blob);
\r
820 if (private_blob) {
\r
821 smemclr(private_blob, private_blob_len);
\r
822 sfree(private_blob);
\r
829 unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
\r
830 int *pub_blob_len, char **commentptr,
\r
831 const char **errorstr)
\r
834 char header[40], *b;
\r
835 const struct ssh_signkey *alg;
\r
836 unsigned char *public_blob;
\r
837 int public_blob_len;
\r
839 const char *error = NULL;
\r
840 char *comment = NULL;
\r
842 public_blob = NULL;
\r
844 fp = f_open(filename, "rb", FALSE);
\r
846 error = "can't open file";
\r
850 /* Read the first header line which contains the key type. */
\r
851 if (!read_header(fp, header)
\r
852 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
853 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
854 if (0 == strncmp(header, "PuTTY-User-Key-File-", 20))
\r
855 error = "PuTTY key format too new";
\r
857 error = "not a PuTTY SSH-2 private key";
\r
860 error = "file format error";
\r
861 if ((b = read_body(fp)) == NULL)
\r
863 /* Select key algorithm structure. */
\r
864 alg = find_pubkey_alg(b);
\r
870 /* Read the Encryption header line. */
\r
871 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
873 if ((b = read_body(fp)) == NULL)
\r
875 sfree(b); /* we don't care */
\r
877 /* Read the Comment header line. */
\r
878 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
880 if ((comment = read_body(fp)) == NULL)
\r
884 *commentptr = comment;
\r
888 /* Read the Public-Lines header line and the public blob. */
\r
889 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
891 if ((b = read_body(fp)) == NULL)
\r
895 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
900 *pub_blob_len = public_blob_len;
\r
902 *algorithm = alg->name;
\r
903 return public_blob;
\r
906 * Error processing.
\r
912 sfree(public_blob);
\r
915 if (comment && commentptr) {
\r
917 *commentptr = NULL;
\r
922 int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
\r
925 char header[40], *b, *comment;
\r
929 *commentptr = NULL;
\r
931 fp = f_open(filename, "rb", FALSE);
\r
934 if (!read_header(fp, header)
\r
935 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
936 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
940 if ((b = read_body(fp)) == NULL) {
\r
944 sfree(b); /* we don't care about key type here */
\r
945 /* Read the Encryption header line. */
\r
946 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
\r
950 if ((b = read_body(fp)) == NULL) {
\r
955 /* Read the Comment header line. */
\r
956 if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
\r
961 if ((comment = read_body(fp)) == NULL) {
\r
968 *commentptr = comment;
\r
973 if (!strcmp(b, "aes256-cbc"))
\r
981 int base64_lines(int datalen)
\r
983 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
\r
984 return (datalen + 47) / 48;
\r
987 void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)
\r
993 while (datalen > 0) {
\r
994 n = (datalen < 3 ? datalen : 3);
\r
995 base64_encode_atom(data, n, out);
\r
998 for (i = 0; i < 4; i++) {
\r
999 if (linelen >= cpl) {
\r
1003 fputc(out[i], fp);
\r
1010 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
\r
1014 unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
\r
1015 int pub_blob_len, priv_blob_len, priv_encrypted_len;
\r
1020 unsigned char priv_mac[20];
\r
1023 * Fetch the key component blobs.
\r
1025 pub_blob = key->alg->public_blob(key->data, &pub_blob_len);
\r
1026 priv_blob = key->alg->private_blob(key->data, &priv_blob_len);
\r
1027 if (!pub_blob || !priv_blob) {
\r
1034 * Determine encryption details, and encrypt the private blob.
\r
1037 cipherstr = "aes256-cbc";
\r
1040 cipherstr = "none";
\r
1043 priv_encrypted_len = priv_blob_len + cipherblk - 1;
\r
1044 priv_encrypted_len -= priv_encrypted_len % cipherblk;
\r
1045 priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
\r
1046 memset(priv_blob_encrypted, 0, priv_encrypted_len);
\r
1047 memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
\r
1048 /* Create padding based on the SHA hash of the unpadded blob. This prevents
\r
1049 * too easy a known-plaintext attack on the last block. */
\r
1050 SHA_Simple(priv_blob, priv_blob_len, priv_mac);
\r
1051 assert(priv_encrypted_len - priv_blob_len < 20);
\r
1052 memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,
\r
1053 priv_encrypted_len - priv_blob_len);
\r
1055 /* Now create the MAC. */
\r
1057 unsigned char *macdata;
\r
1060 int namelen = strlen(key->alg->name);
\r
1061 int enclen = strlen(cipherstr);
\r
1062 int commlen = strlen(key->comment);
\r
1064 unsigned char mackey[20];
\r
1065 char header[] = "putty-private-key-file-mac-key";
\r
1067 maclen = (4 + namelen +
\r
1070 4 + pub_blob_len +
\r
1071 4 + priv_encrypted_len);
\r
1072 macdata = snewn(maclen, unsigned char);
\r
1074 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
1075 DO_STR(key->alg->name, namelen);
\r
1076 DO_STR(cipherstr, enclen);
\r
1077 DO_STR(key->comment, commlen);
\r
1078 DO_STR(pub_blob, pub_blob_len);
\r
1079 DO_STR(priv_blob_encrypted, priv_encrypted_len);
\r
1082 SHA_Bytes(&s, header, sizeof(header)-1);
\r
1084 SHA_Bytes(&s, passphrase, strlen(passphrase));
\r
1085 SHA_Final(&s, mackey);
\r
1086 hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);
\r
1087 smemclr(macdata, maclen);
\r
1089 smemclr(mackey, sizeof(mackey));
\r
1090 smemclr(&s, sizeof(s));
\r
1094 unsigned char key[40];
\r
1097 passlen = strlen(passphrase);
\r
1100 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
1101 SHA_Bytes(&s, passphrase, passlen);
\r
1102 SHA_Final(&s, key + 0);
\r
1104 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
1105 SHA_Bytes(&s, passphrase, passlen);
\r
1106 SHA_Final(&s, key + 20);
\r
1107 aes256_encrypt_pubkey(key, priv_blob_encrypted,
\r
1108 priv_encrypted_len);
\r
1110 smemclr(key, sizeof(key));
\r
1111 smemclr(&s, sizeof(s));
\r
1114 fp = f_open(filename, "w", TRUE);
\r
1117 smemclr(priv_blob, priv_blob_len);
\r
1119 smemclr(priv_blob_encrypted, priv_blob_len);
\r
1120 sfree(priv_blob_encrypted);
\r
1123 fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);
\r
1124 fprintf(fp, "Encryption: %s\n", cipherstr);
\r
1125 fprintf(fp, "Comment: %s\n", key->comment);
\r
1126 fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));
\r
1127 base64_encode(fp, pub_blob, pub_blob_len, 64);
\r
1128 fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
\r
1129 base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);
\r
1130 fprintf(fp, "Private-MAC: ");
\r
1131 for (i = 0; i < 20; i++)
\r
1132 fprintf(fp, "%02x", priv_mac[i]);
\r
1133 fprintf(fp, "\n");
\r
1137 smemclr(priv_blob, priv_blob_len);
\r
1139 smemclr(priv_blob_encrypted, priv_blob_len);
\r
1140 sfree(priv_blob_encrypted);
\r
1144 /* ----------------------------------------------------------------------
\r
1145 * A function to determine the type of a private key file. Returns
\r
1146 * 0 on failure, 1 or 2 on success.
\r
1148 int key_type(const Filename *filename)
\r
1152 const char putty2_sig[] = "PuTTY-User-Key-File-";
\r
1153 const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
\r
1154 const char openssh_sig[] = "-----BEGIN ";
\r
1157 fp = f_open(filename, "r", FALSE);
\r
1159 return SSH_KEYTYPE_UNOPENABLE;
\r
1160 i = fread(buf, 1, sizeof(buf), fp);
\r
1163 return SSH_KEYTYPE_UNOPENABLE;
\r
1165 return SSH_KEYTYPE_UNKNOWN;
\r
1166 if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))
\r
1167 return SSH_KEYTYPE_SSH1;
\r
1168 if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))
\r
1169 return SSH_KEYTYPE_SSH2;
\r
1170 if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))
\r
1171 return SSH_KEYTYPE_OPENSSH;
\r
1172 if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))
\r
1173 return SSH_KEYTYPE_SSHCOM;
\r
1174 return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */
\r
1178 * Convert the type word to a string, for `wrong type' error
\r
1181 char *key_type_to_str(int type)
\r
1184 case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;
\r
1185 case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;
\r
1186 case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;
\r
1187 case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;
\r
1188 case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;
\r
1189 case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;
\r
1190 default: return "INTERNAL ERROR"; break;
\r