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, 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 int base64_decode_atom(char *atom, unsigned char *out)
\r
523 for (i = 0; i < 4; i++) {
\r
525 if (c >= 'A' && c <= 'Z')
\r
527 else if (c >= 'a' && c <= 'z')
\r
529 else if (c >= '0' && c <= '9')
\r
538 return 0; /* invalid atom */
\r
542 if (vals[0] == -1 || vals[1] == -1)
\r
544 if (vals[2] == -1 && vals[3] != -1)
\r
549 else if (vals[2] != -1)
\r
554 word = ((vals[0] << 18) |
\r
555 (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
\r
556 out[0] = (word >> 16) & 0xFF;
\r
558 out[1] = (word >> 8) & 0xFF;
\r
560 out[2] = word & 0xFF;
\r
564 static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
\r
566 unsigned char *blob;
\r
571 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
\r
572 blob = snewn(48 * nlines, unsigned char);
\r
574 for (i = 0; i < nlines; i++) {
\r
575 line = read_body(fp);
\r
580 linelen = strlen(line);
\r
581 if (linelen % 4 != 0 || linelen > 64) {
\r
586 for (j = 0; j < linelen; j += 4) {
\r
587 k = base64_decode_atom(line + j, blob + len);
\r
602 * Magic error return value for when the passphrase is wrong.
\r
604 struct ssh2_userkey ssh2_wrong_passphrase = {
\r
608 const struct ssh_signkey *find_pubkey_alg(const char *name)
\r
610 if (!strcmp(name, "ssh-rsa"))
\r
612 else if (!strcmp(name, "ssh-dss"))
\r
618 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
\r
619 char *passphrase, const char **errorstr)
\r
622 char header[40], *b, *encryption, *comment, *mac;
\r
623 const struct ssh_signkey *alg;
\r
624 struct ssh2_userkey *ret;
\r
625 int cipher, cipherblk;
\r
626 unsigned char *public_blob, *private_blob;
\r
627 int public_blob_len, private_blob_len;
\r
628 int i, is_mac, old_fmt;
\r
629 int passlen = passphrase ? strlen(passphrase) : 0;
\r
630 const char *error = NULL;
\r
632 ret = NULL; /* return NULL for most errors */
\r
633 encryption = comment = mac = NULL;
\r
634 public_blob = private_blob = NULL;
\r
636 fp = f_open(filename, "rb", FALSE);
\r
638 error = "can't open file";
\r
642 /* Read the first header line which contains the key type. */
\r
643 if (!read_header(fp, header))
\r
645 if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {
\r
647 } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {
\r
648 /* this is an old key file; warn and then continue */
\r
649 old_keyfile_warning();
\r
651 } else if (0 == strncmp(header, "PuTTY-User-Key-File-", 20)) {
\r
652 /* this is a key file FROM THE FUTURE; refuse it, but with a
\r
653 * more specific error message than the generic one below */
\r
654 error = "PuTTY key format too new";
\r
657 error = "not a PuTTY SSH-2 private key";
\r
660 error = "file format error";
\r
661 if ((b = read_body(fp)) == NULL)
\r
663 /* Select key algorithm structure. */
\r
664 alg = find_pubkey_alg(b);
\r
671 /* Read the Encryption header line. */
\r
672 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
674 if ((encryption = read_body(fp)) == NULL)
\r
676 if (!strcmp(encryption, "aes256-cbc")) {
\r
679 } else if (!strcmp(encryption, "none")) {
\r
686 /* Read the Comment header line. */
\r
687 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
689 if ((comment = read_body(fp)) == NULL)
\r
692 /* Read the Public-Lines header line and the public blob. */
\r
693 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
695 if ((b = read_body(fp)) == NULL)
\r
699 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
702 /* Read the Private-Lines header line and the Private blob. */
\r
703 if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))
\r
705 if ((b = read_body(fp)) == NULL)
\r
709 if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)
\r
712 /* Read the Private-MAC or Private-Hash header line. */
\r
713 if (!read_header(fp, header))
\r
715 if (0 == strcmp(header, "Private-MAC")) {
\r
716 if ((mac = read_body(fp)) == NULL)
\r
719 } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {
\r
720 if ((mac = read_body(fp)) == NULL)
\r
730 * Decrypt the private blob.
\r
733 unsigned char key[40];
\r
738 if (private_blob_len % cipherblk)
\r
742 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
743 SHA_Bytes(&s, passphrase, passlen);
\r
744 SHA_Final(&s, key + 0);
\r
746 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
747 SHA_Bytes(&s, passphrase, passlen);
\r
748 SHA_Final(&s, key + 20);
\r
749 aes256_decrypt_pubkey(key, private_blob, private_blob_len);
\r
757 unsigned char binary[20];
\r
758 unsigned char *macdata;
\r
763 /* MAC (or hash) only covers the private blob. */
\r
764 macdata = private_blob;
\r
765 maclen = private_blob_len;
\r
769 int namelen = strlen(alg->name);
\r
770 int enclen = strlen(encryption);
\r
771 int commlen = strlen(comment);
\r
772 maclen = (4 + namelen +
\r
775 4 + public_blob_len +
\r
776 4 + private_blob_len);
\r
777 macdata = snewn(maclen, unsigned char);
\r
779 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
780 DO_STR(alg->name, namelen);
\r
781 DO_STR(encryption, enclen);
\r
782 DO_STR(comment, commlen);
\r
783 DO_STR(public_blob, public_blob_len);
\r
784 DO_STR(private_blob, private_blob_len);
\r
791 unsigned char mackey[20];
\r
792 char header[] = "putty-private-key-file-mac-key";
\r
795 SHA_Bytes(&s, header, sizeof(header)-1);
\r
796 if (cipher && passphrase)
\r
797 SHA_Bytes(&s, passphrase, passlen);
\r
798 SHA_Final(&s, mackey);
\r
800 hmac_sha1_simple(mackey, 20, macdata, maclen, binary);
\r
802 smemclr(mackey, sizeof(mackey));
\r
803 smemclr(&s, sizeof(s));
\r
805 SHA_Simple(macdata, maclen, binary);
\r
808 if (free_macdata) {
\r
809 smemclr(macdata, maclen);
\r
813 for (i = 0; i < 20; i++)
\r
814 sprintf(realmac + 2 * i, "%02x", binary[i]);
\r
816 if (strcmp(mac, realmac)) {
\r
817 /* An incorrect MAC is an unconditional Error if the key is
\r
818 * unencrypted. Otherwise, it means Wrong Passphrase. */
\r
820 error = "wrong passphrase";
\r
821 ret = SSH2_WRONG_PASSPHRASE;
\r
823 error = "MAC failed";
\r
832 * Create and return the key.
\r
834 ret = snew(struct ssh2_userkey);
\r
836 ret->comment = comment;
\r
837 ret->data = alg->createkey(public_blob, public_blob_len,
\r
838 private_blob, private_blob_len);
\r
840 sfree(ret->comment);
\r
843 error = "createkey failed";
\r
846 sfree(public_blob);
\r
847 sfree(private_blob);
\r
854 * Error processing.
\r
866 sfree(public_blob);
\r
868 sfree(private_blob);
\r
874 unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
\r
875 int *pub_blob_len, char **commentptr,
\r
876 const char **errorstr)
\r
879 char header[40], *b;
\r
880 const struct ssh_signkey *alg;
\r
881 unsigned char *public_blob;
\r
882 int public_blob_len;
\r
884 const char *error = NULL;
\r
887 public_blob = NULL;
\r
889 fp = f_open(filename, "rb", FALSE);
\r
891 error = "can't open file";
\r
895 /* Read the first header line which contains the key type. */
\r
896 if (!read_header(fp, header)
\r
897 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
898 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
899 if (0 == strncmp(header, "PuTTY-User-Key-File-", 20))
\r
900 error = "PuTTY key format too new";
\r
902 error = "not a PuTTY SSH-2 private key";
\r
905 error = "file format error";
\r
906 if ((b = read_body(fp)) == NULL)
\r
908 /* Select key algorithm structure. */
\r
909 alg = find_pubkey_alg(b);
\r
916 /* Read the Encryption header line. */
\r
917 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
\r
919 if ((b = read_body(fp)) == NULL)
\r
921 sfree(b); /* we don't care */
\r
923 /* Read the Comment header line. */
\r
924 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
\r
926 if ((comment = read_body(fp)) == NULL)
\r
930 *commentptr = comment;
\r
934 /* Read the Public-Lines header line and the public blob. */
\r
935 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
\r
937 if ((b = read_body(fp)) == NULL)
\r
941 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
\r
946 *pub_blob_len = public_blob_len;
\r
948 *algorithm = alg->name;
\r
949 return public_blob;
\r
952 * Error processing.
\r
958 sfree(public_blob);
\r
964 int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
\r
967 char header[40], *b, *comment;
\r
971 *commentptr = NULL;
\r
973 fp = f_open(filename, "rb", FALSE);
\r
976 if (!read_header(fp, header)
\r
977 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
\r
978 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
\r
982 if ((b = read_body(fp)) == NULL) {
\r
986 sfree(b); /* we don't care about key type here */
\r
987 /* Read the Encryption header line. */
\r
988 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
\r
992 if ((b = read_body(fp)) == NULL) {
\r
997 /* Read the Comment header line. */
\r
998 if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
\r
1003 if ((comment = read_body(fp)) == NULL) {
\r
1010 *commentptr = comment;
\r
1015 if (!strcmp(b, "aes256-cbc"))
\r
1023 int base64_lines(int datalen)
\r
1025 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
\r
1026 return (datalen + 47) / 48;
\r
1029 void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)
\r
1035 while (datalen > 0) {
\r
1036 n = (datalen < 3 ? datalen : 3);
\r
1037 base64_encode_atom(data, n, out);
\r
1040 for (i = 0; i < 4; i++) {
\r
1041 if (linelen >= cpl) {
\r
1045 fputc(out[i], fp);
\r
1052 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
\r
1056 unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
\r
1057 int pub_blob_len, priv_blob_len, priv_encrypted_len;
\r
1062 unsigned char priv_mac[20];
\r
1065 * Fetch the key component blobs.
\r
1067 pub_blob = key->alg->public_blob(key->data, &pub_blob_len);
\r
1068 priv_blob = key->alg->private_blob(key->data, &priv_blob_len);
\r
1069 if (!pub_blob || !priv_blob) {
\r
1076 * Determine encryption details, and encrypt the private blob.
\r
1079 cipherstr = "aes256-cbc";
\r
1082 cipherstr = "none";
\r
1085 priv_encrypted_len = priv_blob_len + cipherblk - 1;
\r
1086 priv_encrypted_len -= priv_encrypted_len % cipherblk;
\r
1087 priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
\r
1088 memset(priv_blob_encrypted, 0, priv_encrypted_len);
\r
1089 memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
\r
1090 /* Create padding based on the SHA hash of the unpadded blob. This prevents
\r
1091 * too easy a known-plaintext attack on the last block. */
\r
1092 SHA_Simple(priv_blob, priv_blob_len, priv_mac);
\r
1093 assert(priv_encrypted_len - priv_blob_len < 20);
\r
1094 memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,
\r
1095 priv_encrypted_len - priv_blob_len);
\r
1097 /* Now create the MAC. */
\r
1099 unsigned char *macdata;
\r
1102 int namelen = strlen(key->alg->name);
\r
1103 int enclen = strlen(cipherstr);
\r
1104 int commlen = strlen(key->comment);
\r
1106 unsigned char mackey[20];
\r
1107 char header[] = "putty-private-key-file-mac-key";
\r
1109 maclen = (4 + namelen +
\r
1112 4 + pub_blob_len +
\r
1113 4 + priv_encrypted_len);
\r
1114 macdata = snewn(maclen, unsigned char);
\r
1116 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
\r
1117 DO_STR(key->alg->name, namelen);
\r
1118 DO_STR(cipherstr, enclen);
\r
1119 DO_STR(key->comment, commlen);
\r
1120 DO_STR(pub_blob, pub_blob_len);
\r
1121 DO_STR(priv_blob_encrypted, priv_encrypted_len);
\r
1124 SHA_Bytes(&s, header, sizeof(header)-1);
\r
1126 SHA_Bytes(&s, passphrase, strlen(passphrase));
\r
1127 SHA_Final(&s, mackey);
\r
1128 hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);
\r
1129 smemclr(macdata, maclen);
\r
1131 smemclr(mackey, sizeof(mackey));
\r
1132 smemclr(&s, sizeof(s));
\r
1136 unsigned char key[40];
\r
1139 passlen = strlen(passphrase);
\r
1142 SHA_Bytes(&s, "\0\0\0\0", 4);
\r
1143 SHA_Bytes(&s, passphrase, passlen);
\r
1144 SHA_Final(&s, key + 0);
\r
1146 SHA_Bytes(&s, "\0\0\0\1", 4);
\r
1147 SHA_Bytes(&s, passphrase, passlen);
\r
1148 SHA_Final(&s, key + 20);
\r
1149 aes256_encrypt_pubkey(key, priv_blob_encrypted,
\r
1150 priv_encrypted_len);
\r
1152 smemclr(key, sizeof(key));
\r
1153 smemclr(&s, sizeof(s));
\r
1156 fp = f_open(filename, "w", TRUE);
\r
1159 fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);
\r
1160 fprintf(fp, "Encryption: %s\n", cipherstr);
\r
1161 fprintf(fp, "Comment: %s\n", key->comment);
\r
1162 fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));
\r
1163 base64_encode(fp, pub_blob, pub_blob_len, 64);
\r
1164 fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
\r
1165 base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);
\r
1166 fprintf(fp, "Private-MAC: ");
\r
1167 for (i = 0; i < 20; i++)
\r
1168 fprintf(fp, "%02x", priv_mac[i]);
\r
1169 fprintf(fp, "\n");
\r
1173 smemclr(priv_blob, priv_blob_len);
\r
1175 sfree(priv_blob_encrypted);
\r
1179 /* ----------------------------------------------------------------------
\r
1180 * A function to determine the type of a private key file. Returns
\r
1181 * 0 on failure, 1 or 2 on success.
\r
1183 int key_type(const Filename *filename)
\r
1187 const char putty2_sig[] = "PuTTY-User-Key-File-";
\r
1188 const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
\r
1189 const char openssh_sig[] = "-----BEGIN ";
\r
1192 fp = f_open(filename, "r", FALSE);
\r
1194 return SSH_KEYTYPE_UNOPENABLE;
\r
1195 i = fread(buf, 1, sizeof(buf), fp);
\r
1198 return SSH_KEYTYPE_UNOPENABLE;
\r
1200 return SSH_KEYTYPE_UNKNOWN;
\r
1201 if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))
\r
1202 return SSH_KEYTYPE_SSH1;
\r
1203 if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))
\r
1204 return SSH_KEYTYPE_SSH2;
\r
1205 if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))
\r
1206 return SSH_KEYTYPE_OPENSSH;
\r
1207 if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))
\r
1208 return SSH_KEYTYPE_SSHCOM;
\r
1209 return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */
\r
1213 * Convert the type word to a string, for `wrong type' error
\r
1216 char *key_type_to_str(int type)
\r
1219 case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;
\r
1220 case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;
\r
1221 case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;
\r
1222 case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;
\r
1223 case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;
\r
1224 case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;
\r
1225 default: return "INTERNAL ERROR"; break;
\r