2 * Generic SSH public-key handling operations. In particular,
3 * reading of SSH public-key files, and also the generic `sign'
4 * operation for ssh2 (which checks the type of the key and
5 * dispatches to the appropriate key-type specific function).
16 #define PUT_32BIT(cp, value) do { \
18 (cp)[2] = (value) >> 8; \
19 (cp)[1] = (value) >> 16; \
20 (cp)[0] = (value) >> 24; } while (0)
22 #define GET_32BIT(cp) \
23 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
24 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
25 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
26 ((unsigned long)(unsigned char)(cp)[3]))
28 #define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
30 #define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\
31 (x)-'a'<26 ? (x)-'a'+26 :\
32 (x)-'0'<10 ? (x)-'0'+52 :\
36 static int loadrsakey_main(FILE * fp
, struct RSAKey
*key
, int pub_only
,
37 char **commentptr
, char *passphrase
,
40 unsigned char buf
[16384];
41 unsigned char keybuf
[16];
45 struct MD5Context md5c
;
50 /* Slurp the whole file (minus the header) into a buffer. */
51 len
= fread(buf
, 1, sizeof(buf
), fp
);
53 if (len
< 0 || len
== sizeof(buf
)) {
54 *error
= "error reading file";
55 goto end
; /* file too big or not read */
59 *error
= "file format error";
62 * A zero byte. (The signature includes a terminating NUL.)
64 if (len
- i
< 1 || buf
[i
] != 0)
68 /* One byte giving encryption type, and one reserved uint32. */
72 if (ciphertype
!= 0 && ciphertype
!= SSH_CIPHER_3DES
)
76 goto end
; /* reserved field not present */
77 if (buf
[i
] != 0 || buf
[i
+ 1] != 0 || buf
[i
+ 2] != 0
78 || buf
[i
+ 3] != 0) goto end
; /* reserved field nonzero, panic! */
81 /* Now the serious stuff. An ordinary SSH 1 public key. */
82 i
+= makekey(buf
+ i
, key
, NULL
, 1);
84 goto end
; /* overran */
91 /* Next, the comment field. */
92 j
= GET_32BIT(buf
+ i
);
96 comment
= snewn(j
+ 1, char);
98 memcpy(comment
, buf
+ i
, j
);
103 *commentptr
= comment
;
105 key
->comment
= comment
;
107 ret
= ciphertype
!= 0;
113 * Decrypt remainder of buffer.
117 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
118 MD5Final(keybuf
, &md5c
);
119 des3_decrypt_pubkey(keybuf
, buf
+ i
, (len
- i
+ 7) & ~7);
120 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
124 * We are now in the secret part of the key. The first four
125 * bytes should be of the form a, b, a, b.
129 if (buf
[i
] != buf
[i
+ 2] || buf
[i
+ 1] != buf
[i
+ 3]) {
130 *error
= "wrong passphrase";
137 * After that, we have one further bignum which is our
138 * decryption exponent, and then the three auxiliary values
141 i
+= makeprivate(buf
+ i
, key
);
144 i
+= ssh1_read_bignum(buf
+ i
, &key
->iqmp
);
147 i
+= ssh1_read_bignum(buf
+ i
, &key
->q
);
150 i
+= ssh1_read_bignum(buf
+ i
, &key
->p
);
154 if (!rsa_verify(key
)) {
155 *error
= "rsa_verify failed";
162 memset(buf
, 0, sizeof(buf
)); /* burn the evidence */
166 int loadrsakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
,
167 const char **errorstr
)
172 const char *error
= NULL
;
174 fp
= f_open(*filename
, "rb");
176 error
= "can't open file";
181 * Read the first line of the file and see if it's a v1 private
184 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
185 ret
= loadrsakey_main(fp
, key
, FALSE
, NULL
, passphrase
, &error
);
190 * Otherwise, we have nothing. Return empty-handed.
193 error
= "not an SSH-1 RSA file";
196 if ((ret
!= 1) && errorstr
)
202 * See whether an RSA key is encrypted. Return its comment field as
205 int rsakey_encrypted(const Filename
*filename
, char **comment
)
210 fp
= f_open(*filename
, "rb");
212 return 0; /* doesn't even exist */
215 * Read the first line of the file and see if it's a v1 private
218 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
220 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
, &dummy
);
223 return 0; /* wasn't the right kind of file */
227 * Return a malloc'ed chunk of memory containing the public blob of
228 * an RSA key, as given in the agent protocol (modulus bits,
229 * exponent, modulus).
231 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
,
232 const char **errorstr
)
238 const char *error
= NULL
;
240 /* Default return if we fail. */
245 fp
= f_open(*filename
, "rb");
247 error
= "can't open file";
252 * Read the first line of the file and see if it's a v1 private
255 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
256 memset(&key
, 0, sizeof(key
));
257 if (loadrsakey_main(fp
, &key
, TRUE
, NULL
, NULL
, &error
)) {
258 *blob
= rsa_public_blob(&key
, bloblen
);
263 error
= "not an SSH-1 RSA file";
268 if ((ret
!= 1) && errorstr
)
274 * Save an RSA key file. Return nonzero on success.
276 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
278 unsigned char buf
[16384];
279 unsigned char keybuf
[16];
280 struct MD5Context md5c
;
281 unsigned char *p
, *estart
;
285 * Write the initial signature.
288 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
289 p
+= sizeof(rsa_signature
);
292 * One byte giving encryption type, and one reserved (zero)
295 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
300 * An ordinary SSH 1 public key consists of: a uint32
301 * containing the bit count, then two bignums containing the
302 * modulus and exponent respectively.
304 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
306 p
+= ssh1_write_bignum(p
, key
->modulus
);
307 p
+= ssh1_write_bignum(p
, key
->exponent
);
310 * A string containing the comment field.
313 PUT_32BIT(p
, strlen(key
->comment
));
315 memcpy(p
, key
->comment
, strlen(key
->comment
));
316 p
+= strlen(key
->comment
);
323 * The encrypted portion starts here.
328 * Two bytes, then the same two bytes repeated.
330 *p
++ = random_byte();
331 *p
++ = random_byte();
337 * Four more bignums: the decryption exponent, then iqmp, then
340 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
341 p
+= ssh1_write_bignum(p
, key
->iqmp
);
342 p
+= ssh1_write_bignum(p
, key
->q
);
343 p
+= ssh1_write_bignum(p
, key
->p
);
346 * Now write zeros until the encrypted portion is a multiple of
349 while ((p
- estart
) % 8)
353 * Now encrypt the encrypted portion.
357 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
358 MD5Final(keybuf
, &md5c
);
359 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
360 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
364 * Done. Write the result to the file.
366 fp
= f_open(*filename
, "wb");
368 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
369 ret
= ret
&& (fclose(fp
) == 0);
375 /* ----------------------------------------------------------------------
376 * SSH2 private key load/store functions.
380 * PuTTY's own format for SSH2 keys is as follows:
382 * The file is text. Lines are terminated by CRLF, although CR-only
383 * and LF-only are tolerated on input.
385 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
386 * algorithm ("ssh-dss", "ssh-rsa" etc).
388 * The next line says "Encryption: " plus an encryption type.
389 * Currently the only supported encryption types are "aes256-cbc"
392 * The next line says "Comment: " plus the comment string.
394 * Next there is a line saying "Public-Lines: " plus a number N.
395 * The following N lines contain a base64 encoding of the public
396 * part of the key. This is encoded as the standard SSH2 public key
397 * blob (with no initial length): so for RSA, for example, it will
404 * Next, there is a line saying "Private-Lines: " plus a number N,
405 * and then N lines containing the (potentially encrypted) private
406 * part of the key. For the key type "ssh-rsa", this will be
409 * mpint private_exponent
410 * mpint p (the larger of the two primes)
411 * mpint q (the smaller prime)
412 * mpint iqmp (the inverse of q modulo p)
413 * data padding (to reach a multiple of the cipher block size)
415 * And for "ssh-dss", it will be composed of
417 * mpint x (the private key parameter)
418 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
420 * Finally, there is a line saying "Private-MAC: " plus a hex
421 * representation of a HMAC-SHA-1 of:
423 * string name of algorithm ("ssh-dss", "ssh-rsa")
424 * string encryption type
427 * string private-plaintext (the plaintext version of the
428 * private part, including the final
431 * The key to the MAC is itself a SHA-1 hash of:
433 * data "putty-private-key-file-mac-key"
436 * (An empty passphrase is used for unencrypted keys.)
438 * If the key is encrypted, the encryption key is derived from the
439 * passphrase by means of a succession of SHA-1 hashes. Each hash
442 * uint32 sequence-number
445 * where the sequence-number increases from zero. As many of these
446 * hashes are used as necessary.
448 * For backwards compatibility with snapshots between 0.51 and
449 * 0.52, we also support the older key file format, which begins
450 * with "PuTTY-User-Key-File-1" (version number differs). In this
451 * format the Private-MAC: field only covers the private-plaintext
452 * field and nothing else (and without the 4-byte string length on
453 * the front too). Moreover, for RSA keys the Private-MAC: field
454 * can be replaced with a Private-Hash: field which is a plain
455 * SHA-1 hash instead of an HMAC. This is not allowable in DSA
456 * keys. (Yes, the old format was a mess. Guess why it changed :-)
459 static int read_header(FILE * fp
, char *header
)
466 if (c
== '\n' || c
== '\r' || c
== EOF
)
467 return 0; /* failure */
473 return 1; /* success! */
476 return 0; /* failure */
480 return 0; /* failure */
483 static char *read_body(FILE * fp
)
491 text
= snewn(size
, char);
497 if (c
== '\r' || c
== '\n') {
499 if (c
!= '\r' && c
!= '\n' && c
!= EOF
)
507 if (len
+ 1 > size
) {
509 text
= sresize(text
, size
, char);
516 int base64_decode_atom(char *atom
, unsigned char *out
)
523 for (i
= 0; i
< 4; i
++) {
525 if (c
>= 'A' && c
<= 'Z')
527 else if (c
>= 'a' && c
<= 'z')
529 else if (c
>= '0' && c
<= '9')
538 return 0; /* invalid atom */
542 if (vals
[0] == -1 || vals
[1] == -1)
544 if (vals
[2] == -1 && vals
[3] != -1)
549 else if (vals
[2] != -1)
554 word
= ((vals
[0] << 18) |
555 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
556 out
[0] = (word
>> 16) & 0xFF;
558 out
[1] = (word
>> 8) & 0xFF;
560 out
[2] = word
& 0xFF;
564 static unsigned char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
571 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
572 blob
= snewn(48 * nlines
, unsigned char);
574 for (i
= 0; i
< nlines
; i
++) {
575 line
= read_body(fp
);
580 linelen
= strlen(line
);
581 if (linelen
% 4 != 0 || linelen
> 64) {
586 for (j
= 0; j
< linelen
; j
+= 4) {
587 k
= base64_decode_atom(line
+ j
, blob
+ len
);
602 * Magic error return value for when the passphrase is wrong.
604 struct ssh2_userkey ssh2_wrong_passphrase
= {
608 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
609 char *passphrase
, const char **errorstr
)
612 char header
[40], *b
, *encryption
, *comment
, *mac
;
613 const struct ssh_signkey
*alg
;
614 struct ssh2_userkey
*ret
;
615 int cipher
, cipherblk
;
616 unsigned char *public_blob
, *private_blob
;
617 int public_blob_len
, private_blob_len
;
618 int i
, is_mac
, old_fmt
;
619 int passlen
= passphrase ?
strlen(passphrase
) : 0;
620 const char *error
= NULL
;
622 ret
= NULL
; /* return NULL for most errors */
623 encryption
= comment
= mac
= NULL
;
624 public_blob
= private_blob
= NULL
;
626 fp
= f_open(*filename
, "rb");
628 error
= "can't open file";
632 /* Read the first header line which contains the key type. */
633 if (!read_header(fp
, header
))
635 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
637 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
638 /* this is an old key file; warn and then continue */
639 old_keyfile_warning();
642 error
= "not a PuTTY SSH-2 private key";
645 error
= "file format error";
646 if ((b
= read_body(fp
)) == NULL
)
648 /* Select key algorithm structure. */
649 if (!strcmp(b
, "ssh-rsa"))
651 else if (!strcmp(b
, "ssh-dss"))
659 /* Read the Encryption header line. */
660 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
662 if ((encryption
= read_body(fp
)) == NULL
)
664 if (!strcmp(encryption
, "aes256-cbc")) {
667 } else if (!strcmp(encryption
, "none")) {
675 /* Read the Comment header line. */
676 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
678 if ((comment
= read_body(fp
)) == NULL
)
681 /* Read the Public-Lines header line and the public blob. */
682 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
684 if ((b
= read_body(fp
)) == NULL
)
688 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
691 /* Read the Private-Lines header line and the Private blob. */
692 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
694 if ((b
= read_body(fp
)) == NULL
)
698 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
701 /* Read the Private-MAC or Private-Hash header line. */
702 if (!read_header(fp
, header
))
704 if (0 == strcmp(header
, "Private-MAC")) {
705 if ((mac
= read_body(fp
)) == NULL
)
708 } else if (0 == strcmp(header
, "Private-Hash") &&
709 alg
== &ssh_rsa
&& old_fmt
) {
710 if ((mac
= read_body(fp
)) == NULL
)
720 * Decrypt the private blob.
723 unsigned char key
[40];
728 if (private_blob_len
% cipherblk
)
732 SHA_Bytes(&s
, "\0\0\0\0", 4);
733 SHA_Bytes(&s
, passphrase
, passlen
);
734 SHA_Final(&s
, key
+ 0);
736 SHA_Bytes(&s
, "\0\0\0\1", 4);
737 SHA_Bytes(&s
, passphrase
, passlen
);
738 SHA_Final(&s
, key
+ 20);
739 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
747 unsigned char binary
[20];
748 unsigned char *macdata
;
753 /* MAC (or hash) only covers the private blob. */
754 macdata
= private_blob
;
755 maclen
= private_blob_len
;
759 int namelen
= strlen(alg
->name
);
760 int enclen
= strlen(encryption
);
761 int commlen
= strlen(comment
);
762 maclen
= (4 + namelen
+
765 4 + public_blob_len
+
766 4 + private_blob_len
);
767 macdata
= snewn(maclen
, unsigned char);
769 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
770 DO_STR(alg
->name
, namelen
);
771 DO_STR(encryption
, enclen
);
772 DO_STR(comment
, commlen
);
773 DO_STR(public_blob
, public_blob_len
);
774 DO_STR(private_blob
, private_blob_len
);
781 unsigned char mackey
[20];
782 char header
[] = "putty-private-key-file-mac-key";
785 SHA_Bytes(&s
, header
, sizeof(header
)-1);
786 if (cipher
&& passphrase
)
787 SHA_Bytes(&s
, passphrase
, passlen
);
788 SHA_Final(&s
, mackey
);
790 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
792 memset(mackey
, 0, sizeof(mackey
));
793 memset(&s
, 0, sizeof(s
));
795 SHA_Simple(macdata
, maclen
, binary
);
799 memset(macdata
, 0, maclen
);
803 for (i
= 0; i
< 20; i
++)
804 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
806 if (strcmp(mac
, realmac
)) {
807 /* An incorrect MAC is an unconditional Error if the key is
808 * unencrypted. Otherwise, it means Wrong Passphrase. */
810 ret
= SSH2_WRONG_PASSPHRASE
;
812 error
= "MAC failed";
821 * Create and return the key.
823 ret
= snew(struct ssh2_userkey
);
825 ret
->comment
= comment
;
826 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
827 private_blob
, private_blob_len
);
832 error
= "createkey failed";
863 char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
864 int *pub_blob_len
, const char **errorstr
)
868 const struct ssh_signkey
*alg
;
869 unsigned char *public_blob
;
872 const char *error
= NULL
;
876 fp
= f_open(*filename
, "rb");
878 error
= "can't open file";
882 /* Read the first header line which contains the key type. */
883 if (!read_header(fp
, header
)
884 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
885 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
886 error
= "not a PuTTY SSH-2 private key";
889 error
= "file format error";
890 if ((b
= read_body(fp
)) == NULL
)
892 /* Select key algorithm structure. Currently only ssh-rsa. */
893 if (!strcmp(b
, "ssh-rsa"))
895 else if (!strcmp(b
, "ssh-dss"))
903 /* Read the Encryption header line. */
904 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
906 if ((b
= read_body(fp
)) == NULL
)
908 sfree(b
); /* we don't care */
910 /* Read the Comment header line. */
911 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
913 if ((b
= read_body(fp
)) == NULL
)
915 sfree(b
); /* we don't care */
917 /* Read the Public-Lines header line and the public blob. */
918 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
920 if ((b
= read_body(fp
)) == NULL
)
924 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
929 *pub_blob_len
= public_blob_len
;
931 *algorithm
= alg
->name
;
932 return (char *)public_blob
;
947 int ssh2_userkey_encrypted(const Filename
*filename
, char **commentptr
)
950 char header
[40], *b
, *comment
;
956 fp
= f_open(*filename
, "rb");
959 if (!read_header(fp
, header
)
960 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
961 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
965 if ((b
= read_body(fp
)) == NULL
) {
969 sfree(b
); /* we don't care about key type here */
970 /* Read the Encryption header line. */
971 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
975 if ((b
= read_body(fp
)) == NULL
) {
980 /* Read the Comment header line. */
981 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
986 if ((comment
= read_body(fp
)) == NULL
) {
993 *commentptr
= comment
;
996 if (!strcmp(b
, "aes256-cbc"))
1004 int base64_lines(int datalen
)
1006 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
1007 return (datalen
+ 47) / 48;
1010 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
1016 while (datalen
> 0) {
1017 n
= (datalen
< 3 ? datalen
: 3);
1018 base64_encode_atom(data
, n
, out
);
1021 for (i
= 0; i
< 4; i
++) {
1022 if (linelen
>= cpl
) {
1033 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
1037 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
1038 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
1043 unsigned char priv_mac
[20];
1046 * Fetch the key component blobs.
1048 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
1049 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
1050 if (!pub_blob
|| !priv_blob
) {
1057 * Determine encryption details, and encrypt the private blob.
1060 cipherstr
= "aes256-cbc";
1066 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1067 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1068 priv_blob_encrypted
= snewn(priv_encrypted_len
, unsigned char);
1069 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1070 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1071 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1072 * too easy a known-plaintext attack on the last block. */
1073 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1074 assert(priv_encrypted_len
- priv_blob_len
< 20);
1075 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1076 priv_encrypted_len
- priv_blob_len
);
1078 /* Now create the MAC. */
1080 unsigned char *macdata
;
1083 int namelen
= strlen(key
->alg
->name
);
1084 int enclen
= strlen(cipherstr
);
1085 int commlen
= strlen(key
->comment
);
1087 unsigned char mackey
[20];
1088 char header
[] = "putty-private-key-file-mac-key";
1090 maclen
= (4 + namelen
+
1094 4 + priv_encrypted_len
);
1095 macdata
= snewn(maclen
, unsigned char);
1097 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1098 DO_STR(key
->alg
->name
, namelen
);
1099 DO_STR(cipherstr
, enclen
);
1100 DO_STR(key
->comment
, commlen
);
1101 DO_STR(pub_blob
, pub_blob_len
);
1102 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1105 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1107 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1108 SHA_Final(&s
, mackey
);
1109 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1110 memset(macdata
, 0, maclen
);
1112 memset(mackey
, 0, sizeof(mackey
));
1113 memset(&s
, 0, sizeof(s
));
1117 unsigned char key
[40];
1120 passlen
= strlen(passphrase
);
1123 SHA_Bytes(&s
, "\0\0\0\0", 4);
1124 SHA_Bytes(&s
, passphrase
, passlen
);
1125 SHA_Final(&s
, key
+ 0);
1127 SHA_Bytes(&s
, "\0\0\0\1", 4);
1128 SHA_Bytes(&s
, passphrase
, passlen
);
1129 SHA_Final(&s
, key
+ 20);
1130 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1131 priv_encrypted_len
);
1133 memset(key
, 0, sizeof(key
));
1134 memset(&s
, 0, sizeof(s
));
1137 fp
= f_open(*filename
, "w");
1140 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1141 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1142 fprintf(fp
, "Comment: %s\n", key
->comment
);
1143 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1144 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1145 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1146 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1147 fprintf(fp
, "Private-MAC: ");
1148 for (i
= 0; i
< 20; i
++)
1149 fprintf(fp
, "%02x", priv_mac
[i
]);
1154 memset(priv_blob
, 0, priv_blob_len
);
1156 sfree(priv_blob_encrypted
);
1160 /* ----------------------------------------------------------------------
1161 * A function to determine the type of a private key file. Returns
1162 * 0 on failure, 1 or 2 on success.
1164 int key_type(const Filename
*filename
)
1168 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1169 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1170 const char openssh_sig
[] = "-----BEGIN ";
1173 fp
= f_open(*filename
, "r");
1175 return SSH_KEYTYPE_UNOPENABLE
;
1176 i
= fread(buf
, 1, sizeof(buf
), fp
);
1179 return SSH_KEYTYPE_UNOPENABLE
;
1181 return SSH_KEYTYPE_UNKNOWN
;
1182 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1183 return SSH_KEYTYPE_SSH1
;
1184 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1185 return SSH_KEYTYPE_SSH2
;
1186 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1187 return SSH_KEYTYPE_OPENSSH
;
1188 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1189 return SSH_KEYTYPE_SSHCOM
;
1190 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1194 * Convert the type word to a string, for `wrong type' error
1197 char *key_type_to_str(int type
)
1200 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1201 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1202 case SSH_KEYTYPE_SSH1
: return "SSH1 private key"; break;
1203 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH2 private key"; break;
1204 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH2 private key"; break;
1205 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH2 private key"; break;
1206 default: return "INTERNAL ERROR"; break;