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
)) {
186 * This routine will take care of calling fclose() for us.
188 ret
= loadrsakey_main(fp
, key
, FALSE
, NULL
, passphrase
, &error
);
193 * Otherwise, we have nothing. Return empty-handed.
195 error
= "not an SSH-1 RSA file";
199 if ((ret
!= 1) && errorstr
)
205 * See whether an RSA key is encrypted. Return its comment field as
208 int rsakey_encrypted(const Filename
*filename
, char **comment
)
213 fp
= f_open(*filename
, "rb");
215 return 0; /* doesn't even exist */
218 * Read the first line of the file and see if it's a v1 private
221 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
224 * This routine will take care of calling fclose() for us.
226 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
, &dummy
);
229 return 0; /* wasn't the right kind of file */
233 * Return a malloc'ed chunk of memory containing the public blob of
234 * an RSA key, as given in the agent protocol (modulus bits,
235 * exponent, modulus).
237 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
,
238 const char **errorstr
)
244 const char *error
= NULL
;
246 /* Default return if we fail. */
251 fp
= f_open(*filename
, "rb");
253 error
= "can't open file";
258 * Read the first line of the file and see if it's a v1 private
261 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
262 memset(&key
, 0, sizeof(key
));
263 if (loadrsakey_main(fp
, &key
, TRUE
, NULL
, NULL
, &error
)) {
264 *blob
= rsa_public_blob(&key
, bloblen
);
269 error
= "not an SSH-1 RSA file";
275 if ((ret
!= 1) && errorstr
)
281 * Save an RSA key file. Return nonzero on success.
283 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
285 unsigned char buf
[16384];
286 unsigned char keybuf
[16];
287 struct MD5Context md5c
;
288 unsigned char *p
, *estart
;
292 * Write the initial signature.
295 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
296 p
+= sizeof(rsa_signature
);
299 * One byte giving encryption type, and one reserved (zero)
302 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
307 * An ordinary SSH 1 public key consists of: a uint32
308 * containing the bit count, then two bignums containing the
309 * modulus and exponent respectively.
311 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
313 p
+= ssh1_write_bignum(p
, key
->modulus
);
314 p
+= ssh1_write_bignum(p
, key
->exponent
);
317 * A string containing the comment field.
320 PUT_32BIT(p
, strlen(key
->comment
));
322 memcpy(p
, key
->comment
, strlen(key
->comment
));
323 p
+= strlen(key
->comment
);
330 * The encrypted portion starts here.
335 * Two bytes, then the same two bytes repeated.
337 *p
++ = random_byte();
338 *p
++ = random_byte();
344 * Four more bignums: the decryption exponent, then iqmp, then
347 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
348 p
+= ssh1_write_bignum(p
, key
->iqmp
);
349 p
+= ssh1_write_bignum(p
, key
->q
);
350 p
+= ssh1_write_bignum(p
, key
->p
);
353 * Now write zeros until the encrypted portion is a multiple of
356 while ((p
- estart
) % 8)
360 * Now encrypt the encrypted portion.
364 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
365 MD5Final(keybuf
, &md5c
);
366 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
367 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
371 * Done. Write the result to the file.
373 fp
= f_open(*filename
, "wb");
375 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
376 ret
= ret
&& (fclose(fp
) == 0);
382 /* ----------------------------------------------------------------------
383 * SSH2 private key load/store functions.
387 * PuTTY's own format for SSH2 keys is as follows:
389 * The file is text. Lines are terminated by CRLF, although CR-only
390 * and LF-only are tolerated on input.
392 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
393 * algorithm ("ssh-dss", "ssh-rsa" etc).
395 * The next line says "Encryption: " plus an encryption type.
396 * Currently the only supported encryption types are "aes256-cbc"
399 * The next line says "Comment: " plus the comment string.
401 * Next there is a line saying "Public-Lines: " plus a number N.
402 * The following N lines contain a base64 encoding of the public
403 * part of the key. This is encoded as the standard SSH2 public key
404 * blob (with no initial length): so for RSA, for example, it will
411 * Next, there is a line saying "Private-Lines: " plus a number N,
412 * and then N lines containing the (potentially encrypted) private
413 * part of the key. For the key type "ssh-rsa", this will be
416 * mpint private_exponent
417 * mpint p (the larger of the two primes)
418 * mpint q (the smaller prime)
419 * mpint iqmp (the inverse of q modulo p)
420 * data padding (to reach a multiple of the cipher block size)
422 * And for "ssh-dss", it will be composed of
424 * mpint x (the private key parameter)
425 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
427 * Finally, there is a line saying "Private-MAC: " plus a hex
428 * representation of a HMAC-SHA-1 of:
430 * string name of algorithm ("ssh-dss", "ssh-rsa")
431 * string encryption type
434 * string private-plaintext (the plaintext version of the
435 * private part, including the final
438 * The key to the MAC is itself a SHA-1 hash of:
440 * data "putty-private-key-file-mac-key"
443 * (An empty passphrase is used for unencrypted keys.)
445 * If the key is encrypted, the encryption key is derived from the
446 * passphrase by means of a succession of SHA-1 hashes. Each hash
449 * uint32 sequence-number
452 * where the sequence-number increases from zero. As many of these
453 * hashes are used as necessary.
455 * For backwards compatibility with snapshots between 0.51 and
456 * 0.52, we also support the older key file format, which begins
457 * with "PuTTY-User-Key-File-1" (version number differs). In this
458 * format the Private-MAC: field only covers the private-plaintext
459 * field and nothing else (and without the 4-byte string length on
460 * the front too). Moreover, for RSA keys the Private-MAC: field
461 * can be replaced with a Private-Hash: field which is a plain
462 * SHA-1 hash instead of an HMAC. This is not allowable in DSA
463 * keys. (Yes, the old format was a mess. Guess why it changed :-)
466 static int read_header(FILE * fp
, char *header
)
473 if (c
== '\n' || c
== '\r' || c
== EOF
)
474 return 0; /* failure */
480 return 1; /* success! */
483 return 0; /* failure */
487 return 0; /* failure */
490 static char *read_body(FILE * fp
)
498 text
= snewn(size
, char);
504 if (c
== '\r' || c
== '\n') {
506 if (c
!= '\r' && c
!= '\n' && c
!= EOF
)
514 if (len
+ 1 > size
) {
516 text
= sresize(text
, size
, char);
523 int base64_decode_atom(char *atom
, unsigned char *out
)
530 for (i
= 0; i
< 4; i
++) {
532 if (c
>= 'A' && c
<= 'Z')
534 else if (c
>= 'a' && c
<= 'z')
536 else if (c
>= '0' && c
<= '9')
545 return 0; /* invalid atom */
549 if (vals
[0] == -1 || vals
[1] == -1)
551 if (vals
[2] == -1 && vals
[3] != -1)
556 else if (vals
[2] != -1)
561 word
= ((vals
[0] << 18) |
562 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
563 out
[0] = (word
>> 16) & 0xFF;
565 out
[1] = (word
>> 8) & 0xFF;
567 out
[2] = word
& 0xFF;
571 static unsigned char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
578 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
579 blob
= snewn(48 * nlines
, unsigned char);
581 for (i
= 0; i
< nlines
; i
++) {
582 line
= read_body(fp
);
587 linelen
= strlen(line
);
588 if (linelen
% 4 != 0 || linelen
> 64) {
593 for (j
= 0; j
< linelen
; j
+= 4) {
594 k
= base64_decode_atom(line
+ j
, blob
+ len
);
609 * Magic error return value for when the passphrase is wrong.
611 struct ssh2_userkey ssh2_wrong_passphrase
= {
615 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
616 char *passphrase
, const char **errorstr
)
619 char header
[40], *b
, *encryption
, *comment
, *mac
;
620 const struct ssh_signkey
*alg
;
621 struct ssh2_userkey
*ret
;
622 int cipher
, cipherblk
;
623 unsigned char *public_blob
, *private_blob
;
624 int public_blob_len
, private_blob_len
;
625 int i
, is_mac
, old_fmt
;
626 int passlen
= passphrase ?
strlen(passphrase
) : 0;
627 const char *error
= NULL
;
629 ret
= NULL
; /* return NULL for most errors */
630 encryption
= comment
= mac
= NULL
;
631 public_blob
= private_blob
= NULL
;
633 fp
= f_open(*filename
, "rb");
635 error
= "can't open file";
639 /* Read the first header line which contains the key type. */
640 if (!read_header(fp
, header
))
642 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
644 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
645 /* this is an old key file; warn and then continue */
646 old_keyfile_warning();
649 error
= "not a PuTTY SSH-2 private key";
652 error
= "file format error";
653 if ((b
= read_body(fp
)) == NULL
)
655 /* Select key algorithm structure. */
656 if (!strcmp(b
, "ssh-rsa"))
658 else if (!strcmp(b
, "ssh-dss"))
666 /* Read the Encryption header line. */
667 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
669 if ((encryption
= read_body(fp
)) == NULL
)
671 if (!strcmp(encryption
, "aes256-cbc")) {
674 } else if (!strcmp(encryption
, "none")) {
682 /* Read the Comment header line. */
683 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
685 if ((comment
= read_body(fp
)) == NULL
)
688 /* Read the Public-Lines header line and the public blob. */
689 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
691 if ((b
= read_body(fp
)) == NULL
)
695 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
698 /* Read the Private-Lines header line and the Private blob. */
699 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
701 if ((b
= read_body(fp
)) == NULL
)
705 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
708 /* Read the Private-MAC or Private-Hash header line. */
709 if (!read_header(fp
, header
))
711 if (0 == strcmp(header
, "Private-MAC")) {
712 if ((mac
= read_body(fp
)) == NULL
)
715 } else if (0 == strcmp(header
, "Private-Hash") &&
716 alg
== &ssh_rsa
&& old_fmt
) {
717 if ((mac
= read_body(fp
)) == NULL
)
727 * Decrypt the private blob.
730 unsigned char key
[40];
735 if (private_blob_len
% cipherblk
)
739 SHA_Bytes(&s
, "\0\0\0\0", 4);
740 SHA_Bytes(&s
, passphrase
, passlen
);
741 SHA_Final(&s
, key
+ 0);
743 SHA_Bytes(&s
, "\0\0\0\1", 4);
744 SHA_Bytes(&s
, passphrase
, passlen
);
745 SHA_Final(&s
, key
+ 20);
746 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
754 unsigned char binary
[20];
755 unsigned char *macdata
;
760 /* MAC (or hash) only covers the private blob. */
761 macdata
= private_blob
;
762 maclen
= private_blob_len
;
766 int namelen
= strlen(alg
->name
);
767 int enclen
= strlen(encryption
);
768 int commlen
= strlen(comment
);
769 maclen
= (4 + namelen
+
772 4 + public_blob_len
+
773 4 + private_blob_len
);
774 macdata
= snewn(maclen
, unsigned char);
776 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
777 DO_STR(alg
->name
, namelen
);
778 DO_STR(encryption
, enclen
);
779 DO_STR(comment
, commlen
);
780 DO_STR(public_blob
, public_blob_len
);
781 DO_STR(private_blob
, private_blob_len
);
788 unsigned char mackey
[20];
789 char header
[] = "putty-private-key-file-mac-key";
792 SHA_Bytes(&s
, header
, sizeof(header
)-1);
793 if (cipher
&& passphrase
)
794 SHA_Bytes(&s
, passphrase
, passlen
);
795 SHA_Final(&s
, mackey
);
797 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
799 memset(mackey
, 0, sizeof(mackey
));
800 memset(&s
, 0, sizeof(s
));
802 SHA_Simple(macdata
, maclen
, binary
);
806 memset(macdata
, 0, maclen
);
810 for (i
= 0; i
< 20; i
++)
811 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
813 if (strcmp(mac
, realmac
)) {
814 /* An incorrect MAC is an unconditional Error if the key is
815 * unencrypted. Otherwise, it means Wrong Passphrase. */
817 ret
= SSH2_WRONG_PASSPHRASE
;
819 error
= "MAC failed";
828 * Create and return the key.
830 ret
= snew(struct ssh2_userkey
);
832 ret
->comment
= comment
;
833 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
834 private_blob
, private_blob_len
);
839 error
= "createkey failed";
870 char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
871 int *pub_blob_len
, const char **errorstr
)
875 const struct ssh_signkey
*alg
;
876 unsigned char *public_blob
;
879 const char *error
= NULL
;
883 fp
= f_open(*filename
, "rb");
885 error
= "can't open file";
889 /* Read the first header line which contains the key type. */
890 if (!read_header(fp
, header
)
891 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
892 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
893 error
= "not a PuTTY SSH-2 private key";
896 error
= "file format error";
897 if ((b
= read_body(fp
)) == NULL
)
899 /* Select key algorithm structure. Currently only ssh-rsa. */
900 if (!strcmp(b
, "ssh-rsa"))
902 else if (!strcmp(b
, "ssh-dss"))
910 /* Read the Encryption header line. */
911 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
913 if ((b
= read_body(fp
)) == NULL
)
915 sfree(b
); /* we don't care */
917 /* Read the Comment header line. */
918 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
920 if ((b
= read_body(fp
)) == NULL
)
922 sfree(b
); /* we don't care */
924 /* Read the Public-Lines header line and the public blob. */
925 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
927 if ((b
= read_body(fp
)) == NULL
)
931 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
936 *pub_blob_len
= public_blob_len
;
938 *algorithm
= alg
->name
;
939 return (char *)public_blob
;
954 int ssh2_userkey_encrypted(const Filename
*filename
, char **commentptr
)
957 char header
[40], *b
, *comment
;
963 fp
= f_open(*filename
, "rb");
966 if (!read_header(fp
, header
)
967 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
968 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
972 if ((b
= read_body(fp
)) == NULL
) {
976 sfree(b
); /* we don't care about key type here */
977 /* Read the Encryption header line. */
978 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
982 if ((b
= read_body(fp
)) == NULL
) {
987 /* Read the Comment header line. */
988 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
993 if ((comment
= read_body(fp
)) == NULL
) {
1000 *commentptr
= comment
;
1003 if (!strcmp(b
, "aes256-cbc"))
1011 int base64_lines(int datalen
)
1013 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
1014 return (datalen
+ 47) / 48;
1017 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
1023 while (datalen
> 0) {
1024 n
= (datalen
< 3 ? datalen
: 3);
1025 base64_encode_atom(data
, n
, out
);
1028 for (i
= 0; i
< 4; i
++) {
1029 if (linelen
>= cpl
) {
1040 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
1044 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
1045 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
1050 unsigned char priv_mac
[20];
1053 * Fetch the key component blobs.
1055 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
1056 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
1057 if (!pub_blob
|| !priv_blob
) {
1064 * Determine encryption details, and encrypt the private blob.
1067 cipherstr
= "aes256-cbc";
1073 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1074 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1075 priv_blob_encrypted
= snewn(priv_encrypted_len
, unsigned char);
1076 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1077 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1078 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1079 * too easy a known-plaintext attack on the last block. */
1080 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1081 assert(priv_encrypted_len
- priv_blob_len
< 20);
1082 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1083 priv_encrypted_len
- priv_blob_len
);
1085 /* Now create the MAC. */
1087 unsigned char *macdata
;
1090 int namelen
= strlen(key
->alg
->name
);
1091 int enclen
= strlen(cipherstr
);
1092 int commlen
= strlen(key
->comment
);
1094 unsigned char mackey
[20];
1095 char header
[] = "putty-private-key-file-mac-key";
1097 maclen
= (4 + namelen
+
1101 4 + priv_encrypted_len
);
1102 macdata
= snewn(maclen
, unsigned char);
1104 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1105 DO_STR(key
->alg
->name
, namelen
);
1106 DO_STR(cipherstr
, enclen
);
1107 DO_STR(key
->comment
, commlen
);
1108 DO_STR(pub_blob
, pub_blob_len
);
1109 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1112 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1114 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1115 SHA_Final(&s
, mackey
);
1116 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1117 memset(macdata
, 0, maclen
);
1119 memset(mackey
, 0, sizeof(mackey
));
1120 memset(&s
, 0, sizeof(s
));
1124 unsigned char key
[40];
1127 passlen
= strlen(passphrase
);
1130 SHA_Bytes(&s
, "\0\0\0\0", 4);
1131 SHA_Bytes(&s
, passphrase
, passlen
);
1132 SHA_Final(&s
, key
+ 0);
1134 SHA_Bytes(&s
, "\0\0\0\1", 4);
1135 SHA_Bytes(&s
, passphrase
, passlen
);
1136 SHA_Final(&s
, key
+ 20);
1137 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1138 priv_encrypted_len
);
1140 memset(key
, 0, sizeof(key
));
1141 memset(&s
, 0, sizeof(s
));
1144 fp
= f_open(*filename
, "w");
1147 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1148 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1149 fprintf(fp
, "Comment: %s\n", key
->comment
);
1150 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1151 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1152 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1153 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1154 fprintf(fp
, "Private-MAC: ");
1155 for (i
= 0; i
< 20; i
++)
1156 fprintf(fp
, "%02x", priv_mac
[i
]);
1161 memset(priv_blob
, 0, priv_blob_len
);
1163 sfree(priv_blob_encrypted
);
1167 /* ----------------------------------------------------------------------
1168 * A function to determine the type of a private key file. Returns
1169 * 0 on failure, 1 or 2 on success.
1171 int key_type(const Filename
*filename
)
1175 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1176 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1177 const char openssh_sig
[] = "-----BEGIN ";
1180 fp
= f_open(*filename
, "r");
1182 return SSH_KEYTYPE_UNOPENABLE
;
1183 i
= fread(buf
, 1, sizeof(buf
), fp
);
1186 return SSH_KEYTYPE_UNOPENABLE
;
1188 return SSH_KEYTYPE_UNKNOWN
;
1189 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1190 return SSH_KEYTYPE_SSH1
;
1191 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1192 return SSH_KEYTYPE_SSH2
;
1193 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1194 return SSH_KEYTYPE_OPENSSH
;
1195 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1196 return SSH_KEYTYPE_SSHCOM
;
1197 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1201 * Convert the type word to a string, for `wrong type' error
1204 char *key_type_to_str(int type
)
1207 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1208 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1209 case SSH_KEYTYPE_SSH1
: return "SSH1 private key"; break;
1210 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH2 private key"; break;
1211 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH2 private key"; break;
1212 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH2 private key"; break;
1213 default: return "INTERNAL ERROR"; break;