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
, len
, 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 j
= makeprivate(buf
+ i
, len
- i
, key
);
144 j
= ssh1_read_bignum(buf
+ i
, len
- i
, &key
->iqmp
);
147 j
= ssh1_read_bignum(buf
+ i
, len
- i
, &key
->q
);
150 j
= ssh1_read_bignum(buf
+ i
, len
- 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
);
194 * Otherwise, we have nothing. Return empty-handed.
196 error
= "not an SSH-1 RSA file";
201 if ((ret
!= 1) && errorstr
)
207 * See whether an RSA key is encrypted. Return its comment field as
210 int rsakey_encrypted(const Filename
*filename
, char **comment
)
215 fp
= f_open(*filename
, "rb");
217 return 0; /* doesn't even exist */
220 * Read the first line of the file and see if it's a v1 private
223 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
226 * This routine will take care of calling fclose() for us.
228 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
, &dummy
);
231 return 0; /* wasn't the right kind of file */
235 * Return a malloc'ed chunk of memory containing the public blob of
236 * an RSA key, as given in the agent protocol (modulus bits,
237 * exponent, modulus).
239 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
,
240 const char **errorstr
)
246 const char *error
= NULL
;
248 /* Default return if we fail. */
253 fp
= f_open(*filename
, "rb");
255 error
= "can't open file";
260 * Read the first line of the file and see if it's a v1 private
263 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
264 memset(&key
, 0, sizeof(key
));
265 if (loadrsakey_main(fp
, &key
, TRUE
, NULL
, NULL
, &error
)) {
266 *blob
= rsa_public_blob(&key
, bloblen
);
272 error
= "not an SSH-1 RSA file";
278 if ((ret
!= 1) && errorstr
)
284 * Save an RSA key file. Return nonzero on success.
286 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
288 unsigned char buf
[16384];
289 unsigned char keybuf
[16];
290 struct MD5Context md5c
;
291 unsigned char *p
, *estart
;
295 * Write the initial signature.
298 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
299 p
+= sizeof(rsa_signature
);
302 * One byte giving encryption type, and one reserved (zero)
305 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
310 * An ordinary SSH 1 public key consists of: a uint32
311 * containing the bit count, then two bignums containing the
312 * modulus and exponent respectively.
314 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
316 p
+= ssh1_write_bignum(p
, key
->modulus
);
317 p
+= ssh1_write_bignum(p
, key
->exponent
);
320 * A string containing the comment field.
323 PUT_32BIT(p
, strlen(key
->comment
));
325 memcpy(p
, key
->comment
, strlen(key
->comment
));
326 p
+= strlen(key
->comment
);
333 * The encrypted portion starts here.
338 * Two bytes, then the same two bytes repeated.
340 *p
++ = random_byte();
341 *p
++ = random_byte();
347 * Four more bignums: the decryption exponent, then iqmp, then
350 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
351 p
+= ssh1_write_bignum(p
, key
->iqmp
);
352 p
+= ssh1_write_bignum(p
, key
->q
);
353 p
+= ssh1_write_bignum(p
, key
->p
);
356 * Now write zeros until the encrypted portion is a multiple of
359 while ((p
- estart
) % 8)
363 * Now encrypt the encrypted portion.
367 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
368 MD5Final(keybuf
, &md5c
);
369 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
370 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
374 * Done. Write the result to the file.
376 fp
= f_open(*filename
, "wb");
378 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
386 /* ----------------------------------------------------------------------
387 * SSH2 private key load/store functions.
391 * PuTTY's own format for SSH2 keys is as follows:
393 * The file is text. Lines are terminated by CRLF, although CR-only
394 * and LF-only are tolerated on input.
396 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
397 * algorithm ("ssh-dss", "ssh-rsa" etc).
399 * The next line says "Encryption: " plus an encryption type.
400 * Currently the only supported encryption types are "aes256-cbc"
403 * The next line says "Comment: " plus the comment string.
405 * Next there is a line saying "Public-Lines: " plus a number N.
406 * The following N lines contain a base64 encoding of the public
407 * part of the key. This is encoded as the standard SSH2 public key
408 * blob (with no initial length): so for RSA, for example, it will
415 * Next, there is a line saying "Private-Lines: " plus a number N,
416 * and then N lines containing the (potentially encrypted) private
417 * part of the key. For the key type "ssh-rsa", this will be
420 * mpint private_exponent
421 * mpint p (the larger of the two primes)
422 * mpint q (the smaller prime)
423 * mpint iqmp (the inverse of q modulo p)
424 * data padding (to reach a multiple of the cipher block size)
426 * And for "ssh-dss", it will be composed of
428 * mpint x (the private key parameter)
429 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
431 * Finally, there is a line saying "Private-MAC: " plus a hex
432 * representation of a HMAC-SHA-1 of:
434 * string name of algorithm ("ssh-dss", "ssh-rsa")
435 * string encryption type
438 * string private-plaintext (the plaintext version of the
439 * private part, including the final
442 * The key to the MAC is itself a SHA-1 hash of:
444 * data "putty-private-key-file-mac-key"
447 * (An empty passphrase is used for unencrypted keys.)
449 * If the key is encrypted, the encryption key is derived from the
450 * passphrase by means of a succession of SHA-1 hashes. Each hash
453 * uint32 sequence-number
456 * where the sequence-number increases from zero. As many of these
457 * hashes are used as necessary.
459 * For backwards compatibility with snapshots between 0.51 and
460 * 0.52, we also support the older key file format, which begins
461 * with "PuTTY-User-Key-File-1" (version number differs). In this
462 * format the Private-MAC: field only covers the private-plaintext
463 * field and nothing else (and without the 4-byte string length on
464 * the front too). Moreover, the Private-MAC: field can be replaced
465 * with a Private-Hash: field which is a plain SHA-1 hash instead of
466 * an HMAC (this was generated for unencrypted keys).
469 static int read_header(FILE * fp
, char *header
)
476 if (c
== '\n' || c
== '\r' || c
== EOF
)
477 return 0; /* failure */
483 return 1; /* success! */
486 return 0; /* failure */
490 return 0; /* failure */
493 static char *read_body(FILE * fp
)
501 text
= snewn(size
, char);
507 if (c
== '\r' || c
== '\n') {
509 if (c
!= '\r' && c
!= '\n' && c
!= EOF
)
517 if (len
+ 1 > size
) {
519 text
= sresize(text
, size
, char);
526 int base64_decode_atom(char *atom
, unsigned char *out
)
533 for (i
= 0; i
< 4; i
++) {
535 if (c
>= 'A' && c
<= 'Z')
537 else if (c
>= 'a' && c
<= 'z')
539 else if (c
>= '0' && c
<= '9')
548 return 0; /* invalid atom */
552 if (vals
[0] == -1 || vals
[1] == -1)
554 if (vals
[2] == -1 && vals
[3] != -1)
559 else if (vals
[2] != -1)
564 word
= ((vals
[0] << 18) |
565 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
566 out
[0] = (word
>> 16) & 0xFF;
568 out
[1] = (word
>> 8) & 0xFF;
570 out
[2] = word
& 0xFF;
574 static unsigned char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
581 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
582 blob
= snewn(48 * nlines
, unsigned char);
584 for (i
= 0; i
< nlines
; i
++) {
585 line
= read_body(fp
);
590 linelen
= strlen(line
);
591 if (linelen
% 4 != 0 || linelen
> 64) {
596 for (j
= 0; j
< linelen
; j
+= 4) {
597 k
= base64_decode_atom(line
+ j
, blob
+ len
);
612 * Magic error return value for when the passphrase is wrong.
614 struct ssh2_userkey ssh2_wrong_passphrase
= {
618 const struct ssh_signkey
*find_pubkey_alg(const char *name
)
620 if (!strcmp(name
, "ssh-rsa"))
622 else if (!strcmp(name
, "ssh-dss"))
628 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
629 char *passphrase
, const char **errorstr
)
632 char header
[40], *b
, *encryption
, *comment
, *mac
;
633 const struct ssh_signkey
*alg
;
634 struct ssh2_userkey
*ret
;
635 int cipher
, cipherblk
;
636 unsigned char *public_blob
, *private_blob
;
637 int public_blob_len
, private_blob_len
;
638 int i
, is_mac
, old_fmt
;
639 int passlen
= passphrase ?
strlen(passphrase
) : 0;
640 const char *error
= NULL
;
642 ret
= NULL
; /* return NULL for most errors */
643 encryption
= comment
= mac
= NULL
;
644 public_blob
= private_blob
= NULL
;
646 fp
= f_open(*filename
, "rb");
648 error
= "can't open file";
652 /* Read the first header line which contains the key type. */
653 if (!read_header(fp
, header
))
655 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
657 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
658 /* this is an old key file; warn and then continue */
659 old_keyfile_warning();
662 error
= "not a PuTTY SSH-2 private key";
665 error
= "file format error";
666 if ((b
= read_body(fp
)) == NULL
)
668 /* Select key algorithm structure. */
669 alg
= find_pubkey_alg(b
);
676 /* Read the Encryption header line. */
677 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
679 if ((encryption
= read_body(fp
)) == NULL
)
681 if (!strcmp(encryption
, "aes256-cbc")) {
684 } else if (!strcmp(encryption
, "none")) {
692 /* Read the Comment header line. */
693 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
695 if ((comment
= read_body(fp
)) == NULL
)
698 /* Read the Public-Lines header line and the public blob. */
699 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
701 if ((b
= read_body(fp
)) == NULL
)
705 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
708 /* Read the Private-Lines header line and the Private blob. */
709 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
711 if ((b
= read_body(fp
)) == NULL
)
715 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
718 /* Read the Private-MAC or Private-Hash header line. */
719 if (!read_header(fp
, header
))
721 if (0 == strcmp(header
, "Private-MAC")) {
722 if ((mac
= read_body(fp
)) == NULL
)
725 } else if (0 == strcmp(header
, "Private-Hash") && old_fmt
) {
726 if ((mac
= read_body(fp
)) == NULL
)
736 * Decrypt the private blob.
739 unsigned char key
[40];
744 if (private_blob_len
% cipherblk
)
748 SHA_Bytes(&s
, "\0\0\0\0", 4);
749 SHA_Bytes(&s
, passphrase
, passlen
);
750 SHA_Final(&s
, key
+ 0);
752 SHA_Bytes(&s
, "\0\0\0\1", 4);
753 SHA_Bytes(&s
, passphrase
, passlen
);
754 SHA_Final(&s
, key
+ 20);
755 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
763 unsigned char binary
[20];
764 unsigned char *macdata
;
769 /* MAC (or hash) only covers the private blob. */
770 macdata
= private_blob
;
771 maclen
= private_blob_len
;
775 int namelen
= strlen(alg
->name
);
776 int enclen
= strlen(encryption
);
777 int commlen
= strlen(comment
);
778 maclen
= (4 + namelen
+
781 4 + public_blob_len
+
782 4 + private_blob_len
);
783 macdata
= snewn(maclen
, unsigned char);
785 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
786 DO_STR(alg
->name
, namelen
);
787 DO_STR(encryption
, enclen
);
788 DO_STR(comment
, commlen
);
789 DO_STR(public_blob
, public_blob_len
);
790 DO_STR(private_blob
, private_blob_len
);
797 unsigned char mackey
[20];
798 char header
[] = "putty-private-key-file-mac-key";
801 SHA_Bytes(&s
, header
, sizeof(header
)-1);
802 if (cipher
&& passphrase
)
803 SHA_Bytes(&s
, passphrase
, passlen
);
804 SHA_Final(&s
, mackey
);
806 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
808 memset(mackey
, 0, sizeof(mackey
));
809 memset(&s
, 0, sizeof(s
));
811 SHA_Simple(macdata
, maclen
, binary
);
815 memset(macdata
, 0, maclen
);
819 for (i
= 0; i
< 20; i
++)
820 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
822 if (strcmp(mac
, realmac
)) {
823 /* An incorrect MAC is an unconditional Error if the key is
824 * unencrypted. Otherwise, it means Wrong Passphrase. */
826 error
= "wrong passphrase";
827 ret
= SSH2_WRONG_PASSPHRASE
;
829 error
= "MAC failed";
838 * Create and return the key.
840 ret
= snew(struct ssh2_userkey
);
842 ret
->comment
= comment
;
843 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
844 private_blob
, private_blob_len
);
849 error
= "createkey failed";
880 char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
881 int *pub_blob_len
, const char **errorstr
)
885 const struct ssh_signkey
*alg
;
886 unsigned char *public_blob
;
889 const char *error
= NULL
;
893 fp
= f_open(*filename
, "rb");
895 error
= "can't open file";
899 /* Read the first header line which contains the key type. */
900 if (!read_header(fp
, header
)
901 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
902 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
903 error
= "not a PuTTY SSH-2 private key";
906 error
= "file format error";
907 if ((b
= read_body(fp
)) == NULL
)
909 /* Select key algorithm structure. Currently only ssh-rsa. */
910 alg
= find_pubkey_alg(b
);
917 /* Read the Encryption header line. */
918 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
920 if ((b
= read_body(fp
)) == NULL
)
922 sfree(b
); /* we don't care */
924 /* Read the Comment header line. */
925 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
927 if ((b
= read_body(fp
)) == NULL
)
929 sfree(b
); /* we don't care */
931 /* Read the Public-Lines header line and the public blob. */
932 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
934 if ((b
= read_body(fp
)) == NULL
)
938 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
943 *pub_blob_len
= public_blob_len
;
945 *algorithm
= alg
->name
;
946 return (char *)public_blob
;
961 int ssh2_userkey_encrypted(const Filename
*filename
, char **commentptr
)
964 char header
[40], *b
, *comment
;
970 fp
= f_open(*filename
, "rb");
973 if (!read_header(fp
, header
)
974 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
975 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
979 if ((b
= read_body(fp
)) == NULL
) {
983 sfree(b
); /* we don't care about key type here */
984 /* Read the Encryption header line. */
985 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
989 if ((b
= read_body(fp
)) == NULL
) {
994 /* Read the Comment header line. */
995 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
1000 if ((comment
= read_body(fp
)) == NULL
) {
1007 *commentptr
= comment
;
1010 if (!strcmp(b
, "aes256-cbc"))
1018 int base64_lines(int datalen
)
1020 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
1021 return (datalen
+ 47) / 48;
1024 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
1030 while (datalen
> 0) {
1031 n
= (datalen
< 3 ? datalen
: 3);
1032 base64_encode_atom(data
, n
, out
);
1035 for (i
= 0; i
< 4; i
++) {
1036 if (linelen
>= cpl
) {
1047 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
1051 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
1052 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
1057 unsigned char priv_mac
[20];
1060 * Fetch the key component blobs.
1062 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
1063 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
1064 if (!pub_blob
|| !priv_blob
) {
1071 * Determine encryption details, and encrypt the private blob.
1074 cipherstr
= "aes256-cbc";
1080 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1081 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1082 priv_blob_encrypted
= snewn(priv_encrypted_len
, unsigned char);
1083 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1084 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1085 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1086 * too easy a known-plaintext attack on the last block. */
1087 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1088 assert(priv_encrypted_len
- priv_blob_len
< 20);
1089 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1090 priv_encrypted_len
- priv_blob_len
);
1092 /* Now create the MAC. */
1094 unsigned char *macdata
;
1097 int namelen
= strlen(key
->alg
->name
);
1098 int enclen
= strlen(cipherstr
);
1099 int commlen
= strlen(key
->comment
);
1101 unsigned char mackey
[20];
1102 char header
[] = "putty-private-key-file-mac-key";
1104 maclen
= (4 + namelen
+
1108 4 + priv_encrypted_len
);
1109 macdata
= snewn(maclen
, unsigned char);
1111 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1112 DO_STR(key
->alg
->name
, namelen
);
1113 DO_STR(cipherstr
, enclen
);
1114 DO_STR(key
->comment
, commlen
);
1115 DO_STR(pub_blob
, pub_blob_len
);
1116 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1119 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1121 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1122 SHA_Final(&s
, mackey
);
1123 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1124 memset(macdata
, 0, maclen
);
1126 memset(mackey
, 0, sizeof(mackey
));
1127 memset(&s
, 0, sizeof(s
));
1131 unsigned char key
[40];
1134 passlen
= strlen(passphrase
);
1137 SHA_Bytes(&s
, "\0\0\0\0", 4);
1138 SHA_Bytes(&s
, passphrase
, passlen
);
1139 SHA_Final(&s
, key
+ 0);
1141 SHA_Bytes(&s
, "\0\0\0\1", 4);
1142 SHA_Bytes(&s
, passphrase
, passlen
);
1143 SHA_Final(&s
, key
+ 20);
1144 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1145 priv_encrypted_len
);
1147 memset(key
, 0, sizeof(key
));
1148 memset(&s
, 0, sizeof(s
));
1151 fp
= f_open(*filename
, "w");
1154 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1155 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1156 fprintf(fp
, "Comment: %s\n", key
->comment
);
1157 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1158 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1159 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1160 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1161 fprintf(fp
, "Private-MAC: ");
1162 for (i
= 0; i
< 20; i
++)
1163 fprintf(fp
, "%02x", priv_mac
[i
]);
1168 memset(priv_blob
, 0, priv_blob_len
);
1170 sfree(priv_blob_encrypted
);
1174 /* ----------------------------------------------------------------------
1175 * A function to determine the type of a private key file. Returns
1176 * 0 on failure, 1 or 2 on success.
1178 int key_type(const Filename
*filename
)
1182 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1183 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1184 const char openssh_sig
[] = "-----BEGIN ";
1187 fp
= f_open(*filename
, "r");
1189 return SSH_KEYTYPE_UNOPENABLE
;
1190 i
= fread(buf
, 1, sizeof(buf
), fp
);
1193 return SSH_KEYTYPE_UNOPENABLE
;
1195 return SSH_KEYTYPE_UNKNOWN
;
1196 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1197 return SSH_KEYTYPE_SSH1
;
1198 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1199 return SSH_KEYTYPE_SSH2
;
1200 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1201 return SSH_KEYTYPE_OPENSSH
;
1202 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1203 return SSH_KEYTYPE_SSHCOM
;
1204 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1208 * Convert the type word to a string, for `wrong type' error
1211 char *key_type_to_str(int type
)
1214 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1215 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1216 case SSH_KEYTYPE_SSH1
: return "SSH1 private key"; break;
1217 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH2 private key"; break;
1218 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH2 private key"; break;
1219 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH2 private key"; break;
1220 default: return "INTERNAL ERROR"; break;