2 * Generic SSH public-key handling operations. In particular,
3 * reading of SSH public-key files, and also the generic `sign'
4 * operation for SSH-2 (which checks the type of the key and
5 * dispatches to the appropriate key-type specific function).
16 #define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
18 #define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\
19 (x)-'a'<26 ? (x)-'a'+26 :\
20 (x)-'0'<10 ? (x)-'0'+52 :\
24 static int loadrsakey_main(FILE * fp
, struct RSAKey
*key
, int pub_only
,
25 char **commentptr
, char *passphrase
,
28 unsigned char buf
[16384];
29 unsigned char keybuf
[16];
33 struct MD5Context md5c
;
38 /* Slurp the whole file (minus the header) into a buffer. */
39 len
= fread(buf
, 1, sizeof(buf
), fp
);
41 if (len
< 0 || len
== sizeof(buf
)) {
42 *error
= "error reading file";
43 goto end
; /* file too big or not read */
47 *error
= "file format error";
50 * A zero byte. (The signature includes a terminating NUL.)
52 if (len
- i
< 1 || buf
[i
] != 0)
56 /* One byte giving encryption type, and one reserved uint32. */
60 if (ciphertype
!= 0 && ciphertype
!= SSH_CIPHER_3DES
)
64 goto end
; /* reserved field not present */
65 if (buf
[i
] != 0 || buf
[i
+ 1] != 0 || buf
[i
+ 2] != 0
66 || buf
[i
+ 3] != 0) goto end
; /* reserved field nonzero, panic! */
69 /* Now the serious stuff. An ordinary SSH-1 public key. */
70 i
+= makekey(buf
+ i
, len
, key
, NULL
, 1);
72 goto end
; /* overran */
74 /* Next, the comment field. */
75 j
= GET_32BIT(buf
+ i
);
79 comment
= snewn(j
+ 1, char);
81 memcpy(comment
, buf
+ i
, j
);
86 *commentptr
= dupstr(comment
);
88 key
->comment
= comment
;
98 ret
= ciphertype
!= 0;
104 * Decrypt remainder of buffer.
108 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
109 MD5Final(keybuf
, &md5c
);
110 des3_decrypt_pubkey(keybuf
, buf
+ i
, (len
- i
+ 7) & ~7);
111 smemclr(keybuf
, sizeof(keybuf
)); /* burn the evidence */
115 * We are now in the secret part of the key. The first four
116 * bytes should be of the form a, b, a, b.
120 if (buf
[i
] != buf
[i
+ 2] || buf
[i
+ 1] != buf
[i
+ 3]) {
121 *error
= "wrong passphrase";
128 * After that, we have one further bignum which is our
129 * decryption exponent, and then the three auxiliary values
132 j
= makeprivate(buf
+ i
, len
- i
, key
);
135 j
= ssh1_read_bignum(buf
+ i
, len
- i
, &key
->iqmp
);
138 j
= ssh1_read_bignum(buf
+ i
, len
- i
, &key
->q
);
141 j
= ssh1_read_bignum(buf
+ i
, len
- i
, &key
->p
);
145 if (!rsa_verify(key
)) {
146 *error
= "rsa_verify failed";
153 smemclr(buf
, sizeof(buf
)); /* burn the evidence */
157 int loadrsakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
,
158 const char **errorstr
)
163 const char *error
= NULL
;
165 fp
= f_open(filename
, "rb", FALSE
);
167 error
= "can't open file";
172 * Read the first line of the file and see if it's a v1 private
175 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
177 * This routine will take care of calling fclose() for us.
179 ret
= loadrsakey_main(fp
, key
, FALSE
, NULL
, passphrase
, &error
);
185 * Otherwise, we have nothing. Return empty-handed.
187 error
= "not an SSH-1 RSA file";
192 if ((ret
!= 1) && errorstr
)
198 * See whether an RSA key is encrypted. Return its comment field as
201 int rsakey_encrypted(const Filename
*filename
, char **comment
)
206 fp
= f_open(filename
, "rb", FALSE
);
208 return 0; /* doesn't even exist */
211 * Read the first line of the file and see if it's a v1 private
214 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
217 * This routine will take care of calling fclose() for us.
219 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
, &dummy
);
222 return 0; /* wasn't the right kind of file */
226 * Return a malloc'ed chunk of memory containing the public blob of
227 * an RSA key, as given in the agent protocol (modulus bits,
228 * exponent, modulus).
230 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
,
231 char **commentptr
, const char **errorstr
)
237 const char *error
= NULL
;
239 /* Default return if we fail. */
244 fp
= f_open(filename
, "rb", FALSE
);
246 error
= "can't open file";
251 * Read the first line of the file and see if it's a v1 private
254 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
255 memset(&key
, 0, sizeof(key
));
256 if (loadrsakey_main(fp
, &key
, TRUE
, commentptr
, NULL
, &error
)) {
257 *blob
= rsa_public_blob(&key
, bloblen
);
263 error
= "not an SSH-1 RSA file";
269 if ((ret
!= 1) && errorstr
)
275 * Save an RSA key file. Return nonzero on success.
277 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
279 unsigned char buf
[16384];
280 unsigned char keybuf
[16];
281 struct MD5Context md5c
;
282 unsigned char *p
, *estart
;
286 * Write the initial signature.
289 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
290 p
+= sizeof(rsa_signature
);
293 * One byte giving encryption type, and one reserved (zero)
296 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
301 * An ordinary SSH-1 public key consists of: a uint32
302 * containing the bit count, then two bignums containing the
303 * modulus and exponent respectively.
305 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
307 p
+= ssh1_write_bignum(p
, key
->modulus
);
308 p
+= ssh1_write_bignum(p
, key
->exponent
);
311 * A string containing the comment field.
314 PUT_32BIT(p
, strlen(key
->comment
));
316 memcpy(p
, key
->comment
, strlen(key
->comment
));
317 p
+= strlen(key
->comment
);
324 * The encrypted portion starts here.
329 * Two bytes, then the same two bytes repeated.
331 *p
++ = random_byte();
332 *p
++ = random_byte();
338 * Four more bignums: the decryption exponent, then iqmp, then
341 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
342 p
+= ssh1_write_bignum(p
, key
->iqmp
);
343 p
+= ssh1_write_bignum(p
, key
->q
);
344 p
+= ssh1_write_bignum(p
, key
->p
);
347 * Now write zeros until the encrypted portion is a multiple of
350 while ((p
- estart
) % 8)
354 * Now encrypt the encrypted portion.
358 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
359 MD5Final(keybuf
, &md5c
);
360 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
361 smemclr(keybuf
, sizeof(keybuf
)); /* burn the evidence */
365 * Done. Write the result to the file.
367 fp
= f_open(filename
, "wb", TRUE
);
369 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
377 /* ----------------------------------------------------------------------
378 * SSH-2 private key load/store functions.
382 * PuTTY's own format for SSH-2 keys is as follows:
384 * The file is text. Lines are terminated by CRLF, although CR-only
385 * and LF-only are tolerated on input.
387 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
388 * algorithm ("ssh-dss", "ssh-rsa" etc).
390 * The next line says "Encryption: " plus an encryption type.
391 * Currently the only supported encryption types are "aes256-cbc"
394 * The next line says "Comment: " plus the comment string.
396 * Next there is a line saying "Public-Lines: " plus a number N.
397 * The following N lines contain a base64 encoding of the public
398 * part of the key. This is encoded as the standard SSH-2 public key
399 * blob (with no initial length): so for RSA, for example, it will
406 * Next, there is a line saying "Private-Lines: " plus a number N,
407 * and then N lines containing the (potentially encrypted) private
408 * part of the key. For the key type "ssh-rsa", this will be
411 * mpint private_exponent
412 * mpint p (the larger of the two primes)
413 * mpint q (the smaller prime)
414 * mpint iqmp (the inverse of q modulo p)
415 * data padding (to reach a multiple of the cipher block size)
417 * And for "ssh-dss", it will be composed of
419 * mpint x (the private key parameter)
420 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
422 * Finally, there is a line saying "Private-MAC: " plus a hex
423 * representation of a HMAC-SHA-1 of:
425 * string name of algorithm ("ssh-dss", "ssh-rsa")
426 * string encryption type
429 * string private-plaintext (the plaintext version of the
430 * private part, including the final
433 * The key to the MAC is itself a SHA-1 hash of:
435 * data "putty-private-key-file-mac-key"
438 * (An empty passphrase is used for unencrypted keys.)
440 * If the key is encrypted, the encryption key is derived from the
441 * passphrase by means of a succession of SHA-1 hashes. Each hash
444 * uint32 sequence-number
447 * where the sequence-number increases from zero. As many of these
448 * hashes are used as necessary.
450 * For backwards compatibility with snapshots between 0.51 and
451 * 0.52, we also support the older key file format, which begins
452 * with "PuTTY-User-Key-File-1" (version number differs). In this
453 * format the Private-MAC: field only covers the private-plaintext
454 * field and nothing else (and without the 4-byte string length on
455 * the front too). Moreover, the Private-MAC: field can be replaced
456 * with a Private-Hash: field which is a plain SHA-1 hash instead of
457 * an HMAC (this was generated for unencrypted keys).
460 static int read_header(FILE * fp
, char *header
)
467 if (c
== '\n' || c
== '\r' || c
== EOF
)
468 return 0; /* failure */
474 return 1; /* success! */
477 return 0; /* failure */
481 return 0; /* failure */
484 static char *read_body(FILE * fp
)
492 text
= snewn(size
, char);
498 if (c
== '\r' || c
== '\n' || c
== EOF
) {
501 if (c
!= '\r' && c
!= '\n')
506 if (len
+ 1 >= size
) {
508 text
= sresize(text
, size
, char);
515 int base64_decode_atom(char *atom
, unsigned char *out
)
522 for (i
= 0; i
< 4; i
++) {
524 if (c
>= 'A' && c
<= 'Z')
526 else if (c
>= 'a' && c
<= 'z')
528 else if (c
>= '0' && c
<= '9')
537 return 0; /* invalid atom */
541 if (vals
[0] == -1 || vals
[1] == -1)
543 if (vals
[2] == -1 && vals
[3] != -1)
548 else if (vals
[2] != -1)
553 word
= ((vals
[0] << 18) |
554 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
555 out
[0] = (word
>> 16) & 0xFF;
557 out
[1] = (word
>> 8) & 0xFF;
559 out
[2] = word
& 0xFF;
563 static unsigned char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
570 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
571 blob
= snewn(48 * nlines
, unsigned char);
573 for (i
= 0; i
< nlines
; i
++) {
574 line
= read_body(fp
);
579 linelen
= strlen(line
);
580 if (linelen
% 4 != 0 || linelen
> 64) {
585 for (j
= 0; j
< linelen
; j
+= 4) {
586 k
= base64_decode_atom(line
+ j
, blob
+ len
);
601 * Magic error return value for when the passphrase is wrong.
603 struct ssh2_userkey ssh2_wrong_passphrase
= {
607 const struct ssh_signkey
*find_pubkey_alg(const char *name
)
609 if (!strcmp(name
, "ssh-rsa"))
611 else if (!strcmp(name
, "ssh-dss"))
617 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
618 char *passphrase
, const char **errorstr
)
621 char header
[40], *b
, *encryption
, *comment
, *mac
;
622 const struct ssh_signkey
*alg
;
623 struct ssh2_userkey
*ret
;
624 int cipher
, cipherblk
;
625 unsigned char *public_blob
, *private_blob
;
626 int public_blob_len
, private_blob_len
;
627 int i
, is_mac
, old_fmt
;
628 int passlen
= passphrase ?
strlen(passphrase
) : 0;
629 const char *error
= NULL
;
631 ret
= NULL
; /* return NULL for most errors */
632 encryption
= comment
= mac
= NULL
;
633 public_blob
= private_blob
= NULL
;
635 fp
= f_open(filename
, "rb", FALSE
);
637 error
= "can't open file";
641 /* Read the first header line which contains the key type. */
642 if (!read_header(fp
, header
))
644 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
646 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
647 /* this is an old key file; warn and then continue */
648 old_keyfile_warning();
650 } else if (0 == strncmp(header
, "PuTTY-User-Key-File-", 20)) {
651 /* this is a key file FROM THE FUTURE; refuse it, but with a
652 * more specific error message than the generic one below */
653 error
= "PuTTY key format too new";
656 error
= "not a PuTTY SSH-2 private key";
659 error
= "file format error";
660 if ((b
= read_body(fp
)) == NULL
)
662 /* Select key algorithm structure. */
663 alg
= find_pubkey_alg(b
);
670 /* Read the Encryption header line. */
671 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
673 if ((encryption
= read_body(fp
)) == NULL
)
675 if (!strcmp(encryption
, "aes256-cbc")) {
678 } else if (!strcmp(encryption
, "none")) {
686 /* Read the Comment header line. */
687 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
689 if ((comment
= read_body(fp
)) == NULL
)
692 /* Read the Public-Lines header line and the public blob. */
693 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
695 if ((b
= read_body(fp
)) == NULL
)
699 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
702 /* Read the Private-Lines header line and the Private blob. */
703 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
705 if ((b
= read_body(fp
)) == NULL
)
709 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
712 /* Read the Private-MAC or Private-Hash header line. */
713 if (!read_header(fp
, header
))
715 if (0 == strcmp(header
, "Private-MAC")) {
716 if ((mac
= read_body(fp
)) == NULL
)
719 } else if (0 == strcmp(header
, "Private-Hash") && old_fmt
) {
720 if ((mac
= read_body(fp
)) == NULL
)
730 * Decrypt the private blob.
733 unsigned char key
[40];
738 if (private_blob_len
% cipherblk
)
742 SHA_Bytes(&s
, "\0\0\0\0", 4);
743 SHA_Bytes(&s
, passphrase
, passlen
);
744 SHA_Final(&s
, key
+ 0);
746 SHA_Bytes(&s
, "\0\0\0\1", 4);
747 SHA_Bytes(&s
, passphrase
, passlen
);
748 SHA_Final(&s
, key
+ 20);
749 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
757 unsigned char binary
[20];
758 unsigned char *macdata
;
763 /* MAC (or hash) only covers the private blob. */
764 macdata
= private_blob
;
765 maclen
= private_blob_len
;
769 int namelen
= strlen(alg
->name
);
770 int enclen
= strlen(encryption
);
771 int commlen
= strlen(comment
);
772 maclen
= (4 + namelen
+
775 4 + public_blob_len
+
776 4 + private_blob_len
);
777 macdata
= snewn(maclen
, unsigned char);
779 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
780 DO_STR(alg
->name
, namelen
);
781 DO_STR(encryption
, enclen
);
782 DO_STR(comment
, commlen
);
783 DO_STR(public_blob
, public_blob_len
);
784 DO_STR(private_blob
, private_blob_len
);
791 unsigned char mackey
[20];
792 char header
[] = "putty-private-key-file-mac-key";
795 SHA_Bytes(&s
, header
, sizeof(header
)-1);
796 if (cipher
&& passphrase
)
797 SHA_Bytes(&s
, passphrase
, passlen
);
798 SHA_Final(&s
, mackey
);
800 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
802 smemclr(mackey
, sizeof(mackey
));
803 smemclr(&s
, sizeof(s
));
805 SHA_Simple(macdata
, maclen
, binary
);
809 smemclr(macdata
, maclen
);
813 for (i
= 0; i
< 20; i
++)
814 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
816 if (strcmp(mac
, realmac
)) {
817 /* An incorrect MAC is an unconditional Error if the key is
818 * unencrypted. Otherwise, it means Wrong Passphrase. */
820 error
= "wrong passphrase";
821 ret
= SSH2_WRONG_PASSPHRASE
;
823 error
= "MAC failed";
832 * Create and return the key.
834 ret
= snew(struct ssh2_userkey
);
836 ret
->comment
= comment
;
837 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
838 private_blob
, private_blob_len
);
843 error
= "createkey failed";
874 unsigned char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
875 int *pub_blob_len
, char **commentptr
,
876 const char **errorstr
)
880 const struct ssh_signkey
*alg
;
881 unsigned char *public_blob
;
884 const char *error
= NULL
;
889 fp
= f_open(filename
, "rb", FALSE
);
891 error
= "can't open file";
895 /* Read the first header line which contains the key type. */
896 if (!read_header(fp
, header
)
897 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
898 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
899 if (0 == strncmp(header
, "PuTTY-User-Key-File-", 20))
900 error
= "PuTTY key format too new";
902 error
= "not a PuTTY SSH-2 private key";
905 error
= "file format error";
906 if ((b
= read_body(fp
)) == NULL
)
908 /* Select key algorithm structure. */
909 alg
= find_pubkey_alg(b
);
916 /* Read the Encryption header line. */
917 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
919 if ((b
= read_body(fp
)) == NULL
)
921 sfree(b
); /* we don't care */
923 /* Read the Comment header line. */
924 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
926 if ((comment
= read_body(fp
)) == NULL
)
930 *commentptr
= comment
;
934 /* Read the Public-Lines header line and the public blob. */
935 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
937 if ((b
= read_body(fp
)) == NULL
)
941 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
946 *pub_blob_len
= public_blob_len
;
948 *algorithm
= alg
->name
;
964 int ssh2_userkey_encrypted(const Filename
*filename
, char **commentptr
)
967 char header
[40], *b
, *comment
;
973 fp
= f_open(filename
, "rb", FALSE
);
976 if (!read_header(fp
, header
)
977 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
978 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
982 if ((b
= read_body(fp
)) == NULL
) {
986 sfree(b
); /* we don't care about key type here */
987 /* Read the Encryption header line. */
988 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
992 if ((b
= read_body(fp
)) == NULL
) {
997 /* Read the Comment header line. */
998 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
1003 if ((comment
= read_body(fp
)) == NULL
) {
1010 *commentptr
= comment
;
1013 if (!strcmp(b
, "aes256-cbc"))
1021 int base64_lines(int datalen
)
1023 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
1024 return (datalen
+ 47) / 48;
1027 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
1033 while (datalen
> 0) {
1034 n
= (datalen
< 3 ? datalen
: 3);
1035 base64_encode_atom(data
, n
, out
);
1038 for (i
= 0; i
< 4; i
++) {
1039 if (linelen
>= cpl
) {
1050 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
1054 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
1055 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
1060 unsigned char priv_mac
[20];
1063 * Fetch the key component blobs.
1065 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
1066 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
1067 if (!pub_blob
|| !priv_blob
) {
1074 * Determine encryption details, and encrypt the private blob.
1077 cipherstr
= "aes256-cbc";
1083 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1084 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1085 priv_blob_encrypted
= snewn(priv_encrypted_len
, unsigned char);
1086 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1087 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1088 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1089 * too easy a known-plaintext attack on the last block. */
1090 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1091 assert(priv_encrypted_len
- priv_blob_len
< 20);
1092 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1093 priv_encrypted_len
- priv_blob_len
);
1095 /* Now create the MAC. */
1097 unsigned char *macdata
;
1100 int namelen
= strlen(key
->alg
->name
);
1101 int enclen
= strlen(cipherstr
);
1102 int commlen
= strlen(key
->comment
);
1104 unsigned char mackey
[20];
1105 char header
[] = "putty-private-key-file-mac-key";
1107 maclen
= (4 + namelen
+
1111 4 + priv_encrypted_len
);
1112 macdata
= snewn(maclen
, unsigned char);
1114 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1115 DO_STR(key
->alg
->name
, namelen
);
1116 DO_STR(cipherstr
, enclen
);
1117 DO_STR(key
->comment
, commlen
);
1118 DO_STR(pub_blob
, pub_blob_len
);
1119 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1122 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1124 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1125 SHA_Final(&s
, mackey
);
1126 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1127 smemclr(macdata
, maclen
);
1129 smemclr(mackey
, sizeof(mackey
));
1130 smemclr(&s
, sizeof(s
));
1134 unsigned char key
[40];
1137 passlen
= strlen(passphrase
);
1140 SHA_Bytes(&s
, "\0\0\0\0", 4);
1141 SHA_Bytes(&s
, passphrase
, passlen
);
1142 SHA_Final(&s
, key
+ 0);
1144 SHA_Bytes(&s
, "\0\0\0\1", 4);
1145 SHA_Bytes(&s
, passphrase
, passlen
);
1146 SHA_Final(&s
, key
+ 20);
1147 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1148 priv_encrypted_len
);
1150 smemclr(key
, sizeof(key
));
1151 smemclr(&s
, sizeof(s
));
1154 fp
= f_open(filename
, "w", TRUE
);
1157 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1158 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1159 fprintf(fp
, "Comment: %s\n", key
->comment
);
1160 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1161 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1162 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1163 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1164 fprintf(fp
, "Private-MAC: ");
1165 for (i
= 0; i
< 20; i
++)
1166 fprintf(fp
, "%02x", priv_mac
[i
]);
1171 smemclr(priv_blob
, priv_blob_len
);
1173 sfree(priv_blob_encrypted
);
1177 /* ----------------------------------------------------------------------
1178 * A function to determine the type of a private key file. Returns
1179 * 0 on failure, 1 or 2 on success.
1181 int key_type(const Filename
*filename
)
1185 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1186 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1187 const char openssh_sig
[] = "-----BEGIN ";
1190 fp
= f_open(filename
, "r", FALSE
);
1192 return SSH_KEYTYPE_UNOPENABLE
;
1193 i
= fread(buf
, 1, sizeof(buf
), fp
);
1196 return SSH_KEYTYPE_UNOPENABLE
;
1198 return SSH_KEYTYPE_UNKNOWN
;
1199 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1200 return SSH_KEYTYPE_SSH1
;
1201 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1202 return SSH_KEYTYPE_SSH2
;
1203 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1204 return SSH_KEYTYPE_OPENSSH
;
1205 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1206 return SSH_KEYTYPE_SSHCOM
;
1207 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1211 * Convert the type word to a string, for `wrong type' error
1214 char *key_type_to_str(int type
)
1217 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1218 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1219 case SSH_KEYTYPE_SSH1
: return "SSH-1 private key"; break;
1220 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH-2 private key"; break;
1221 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH-2 private key"; break;
1222 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH-2 private key"; break;
1223 default: return "INTERNAL ERROR"; break;