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).
15 #define PUT_32BIT(cp, value) do { \
17 (cp)[2] = (value) >> 8; \
18 (cp)[1] = (value) >> 16; \
19 (cp)[0] = (value) >> 24; } while (0)
21 #define GET_32BIT(cp) \
22 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
23 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
24 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
25 ((unsigned long)(unsigned char)(cp)[3]))
27 #define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
29 #define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\
30 (x)-'a'<26 ? (x)-'a'+26 :\
31 (x)-'0'<10 ? (x)-'0'+52 :\
35 static int loadrsakey_main(FILE * fp
, struct RSAKey
*key
, int pub_only
,
36 char **commentptr
, char *passphrase
)
38 unsigned char buf
[16384];
39 unsigned char keybuf
[16];
43 struct MD5Context md5c
;
46 /* Slurp the whole file (minus the header) into a buffer. */
47 len
= fread(buf
, 1, sizeof(buf
), fp
);
49 if (len
< 0 || len
== sizeof(buf
))
50 goto end
; /* file too big or not read */
55 * A zero byte. (The signature includes a terminating NUL.)
57 if (len
- i
< 1 || buf
[i
] != 0)
61 /* One byte giving encryption type, and one reserved uint32. */
65 if (ciphertype
!= 0 && ciphertype
!= SSH_CIPHER_3DES
)
69 goto end
; /* reserved field not present */
70 if (buf
[i
] != 0 || buf
[i
+ 1] != 0 || buf
[i
+ 2] != 0
71 || buf
[i
+ 3] != 0) goto end
; /* reserved field nonzero, panic! */
74 /* Now the serious stuff. An ordinary SSH 1 public key. */
75 i
+= makekey(buf
+ i
, key
, NULL
, 1);
77 goto end
; /* overran */
84 /* Next, the comment field. */
85 j
= GET_32BIT(buf
+ i
);
89 comment
= smalloc(j
+ 1);
91 memcpy(comment
, buf
+ i
, j
);
96 *commentptr
= comment
;
98 key
->comment
= comment
;
100 return ciphertype
!= 0;
104 * Decrypt remainder of buffer.
108 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
109 MD5Final(keybuf
, &md5c
);
110 des3_decrypt_pubkey(keybuf
, buf
+ i
, (len
- i
+ 7) & ~7);
111 memset(keybuf
, 0, 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]) {
127 * After that, we have one further bignum which is our
128 * decryption exponent, and then the three auxiliary values
131 i
+= makeprivate(buf
+ i
, key
);
134 i
+= ssh1_read_bignum(buf
+ i
, &key
->iqmp
);
137 i
+= ssh1_read_bignum(buf
+ i
, &key
->q
);
140 i
+= ssh1_read_bignum(buf
+ i
, &key
->p
);
144 if (!rsa_verify(key
)) {
151 memset(buf
, 0, sizeof(buf
)); /* burn the evidence */
155 int loadrsakey(char *filename
, struct RSAKey
*key
, char *passphrase
)
158 unsigned char buf
[64];
160 fp
= fopen(filename
, "rb");
162 return 0; /* doesn't even exist */
165 * Read the first line of the file and see if it's a v1 private
168 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
169 return loadrsakey_main(fp
, key
, FALSE
, NULL
, passphrase
);
173 * Otherwise, we have nothing. Return empty-handed.
180 * See whether an RSA key is encrypted. Return its comment field as
183 int rsakey_encrypted(char *filename
, char **comment
)
186 unsigned char buf
[64];
188 fp
= fopen(filename
, "rb");
190 return 0; /* doesn't even exist */
193 * Read the first line of the file and see if it's a v1 private
196 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
197 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
);
200 return 0; /* wasn't the right kind of file */
204 * Return a malloc'ed chunk of memory containing the public blob of
205 * an RSA key, as given in the agent protocol (modulus bits,
206 * exponent, modulus).
208 int rsakey_pubblob(char *filename
, void **blob
, int *bloblen
)
211 unsigned char buf
[64];
215 /* Default return if we fail. */
220 fp
= fopen(filename
, "rb");
222 return 0; /* doesn't even exist */
225 * Read the first line of the file and see if it's a v1 private
228 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
229 memset(&key
, 0, sizeof(key
));
230 if (loadrsakey_main(fp
, &key
, TRUE
, NULL
, NULL
)) {
231 *blob
= rsa_public_blob(&key
, bloblen
);
241 * Save an RSA key file. Return nonzero on success.
243 int saversakey(char *filename
, struct RSAKey
*key
, char *passphrase
)
245 unsigned char buf
[16384];
246 unsigned char keybuf
[16];
247 struct MD5Context md5c
;
248 unsigned char *p
, *estart
;
252 * Write the initial signature.
255 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
256 p
+= sizeof(rsa_signature
);
259 * One byte giving encryption type, and one reserved (zero)
262 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
267 * An ordinary SSH 1 public key consists of: a uint32
268 * containing the bit count, then two bignums containing the
269 * modulus and exponent respectively.
271 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
273 p
+= ssh1_write_bignum(p
, key
->modulus
);
274 p
+= ssh1_write_bignum(p
, key
->exponent
);
277 * A string containing the comment field.
280 PUT_32BIT(p
, strlen(key
->comment
));
282 memcpy(p
, key
->comment
, strlen(key
->comment
));
283 p
+= strlen(key
->comment
);
290 * The encrypted portion starts here.
295 * Two bytes, then the same two bytes repeated.
297 *p
++ = random_byte();
298 *p
++ = random_byte();
304 * Four more bignums: the decryption exponent, then iqmp, then
307 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
308 p
+= ssh1_write_bignum(p
, key
->iqmp
);
309 p
+= ssh1_write_bignum(p
, key
->q
);
310 p
+= ssh1_write_bignum(p
, key
->p
);
313 * Now write zeros until the encrypted portion is a multiple of
316 while ((p
- estart
) % 8)
320 * Now encrypt the encrypted portion.
324 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
325 MD5Final(keybuf
, &md5c
);
326 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
327 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
331 * Done. Write the result to the file.
333 fp
= fopen(filename
, "wb");
335 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
336 ret
= ret
&& (fclose(fp
) == 0);
342 /* ----------------------------------------------------------------------
343 * SSH2 private key load/store functions.
347 * PuTTY's own format for SSH2 keys is as follows:
349 * The file is text. Lines are terminated by CRLF, although CR-only
350 * and LF-only are tolerated on input.
352 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
353 * algorithm ("ssh-dss", "ssh-rsa" etc).
355 * The next line says "Encryption: " plus an encryption type.
356 * Currently the only supported encryption types are "aes256-cbc"
359 * The next line says "Comment: " plus the comment string.
361 * Next there is a line saying "Public-Lines: " plus a number N.
362 * The following N lines contain a base64 encoding of the public
363 * part of the key. This is encoded as the standard SSH2 public key
364 * blob (with no initial length): so for RSA, for example, it will
371 * Next, there is a line saying "Private-Lines: " plus a number N,
372 * and then N lines containing the (potentially encrypted) private
373 * part of the key. For the key type "ssh-rsa", this will be
376 * mpint private_exponent
377 * mpint p (the larger of the two primes)
378 * mpint q (the smaller prime)
379 * mpint iqmp (the inverse of q modulo p)
380 * data padding (to reach a multiple of the cipher block size)
382 * And for "ssh-dss", it will be composed of
384 * mpint x (the private key parameter)
385 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
387 * Finally, there is a line saying "Private-MAC: " plus a hex
388 * representation of a HMAC-SHA-1 of:
390 * string name of algorithm ("ssh-dss", "ssh-rsa")
391 * string encryption type
394 * string private-plaintext (the plaintext version of the
395 * private part, including the final
398 * The key to the MAC is itself a SHA-1 hash of:
400 * data "putty-private-key-file-mac-key"
403 * Encrypted keys should have a MAC, whereas unencrypted ones must
406 * If the key is encrypted, the encryption key is derived from the
407 * passphrase by means of a succession of SHA-1 hashes. Each hash
410 * uint32 sequence-number
413 * where the sequence-number increases from zero. As many of these
414 * hashes are used as necessary.
416 * For backwards compatibility with snapshots between 0.51 and
417 * 0.52, we also support the older key file format, which begins
418 * with "PuTTY-User-Key-File-1" (version number differs). In this
419 * format the Private-MAC: field only covers the private-plaintext
420 * field and nothing else (and without the 4-byte string length on
421 * the front too). Moreover, for RSA keys the Private-MAC: field
422 * can be replaced with a Private-Hash: field which is a plain
423 * SHA-1 hash instead of an HMAC. This is not allowable in DSA
424 * keys. (Yes, the old format was a mess. Guess why it changed :-)
427 static int read_header(FILE * fp
, char *header
)
434 if (c
== '\n' || c
== '\r' || c
== EOF
)
435 return 0; /* failure */
441 return 1; /* success! */
444 return 0; /* failure */
448 return 0; /* failure */
451 static char *read_body(FILE * fp
)
459 text
= smalloc(size
);
465 if (c
== '\r' || c
== '\n') {
467 if (c
!= '\r' && c
!= '\n' && c
!= EOF
)
475 if (len
+ 1 > size
) {
477 text
= srealloc(text
, size
);
484 int base64_decode_atom(char *atom
, unsigned char *out
)
491 for (i
= 0; i
< 4; i
++) {
493 if (c
>= 'A' && c
<= 'Z')
495 else if (c
>= 'a' && c
<= 'z')
497 else if (c
>= '0' && c
<= '9')
506 return 0; /* invalid atom */
510 if (vals
[0] == -1 || vals
[1] == -1)
512 if (vals
[2] == -1 && vals
[3] != -1)
517 else if (vals
[2] != -1)
522 word
= ((vals
[0] << 18) |
523 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
524 out
[0] = (word
>> 16) & 0xFF;
526 out
[1] = (word
>> 8) & 0xFF;
528 out
[2] = word
& 0xFF;
532 static char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
539 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
540 blob
= smalloc(48 * nlines
);
542 for (i
= 0; i
< nlines
; i
++) {
543 line
= read_body(fp
);
548 linelen
= strlen(line
);
549 if (linelen
% 4 != 0 || linelen
> 64) {
554 for (j
= 0; j
< linelen
; j
+= 4) {
555 k
= base64_decode_atom(line
+ j
, blob
+ len
);
570 * Magic error return value for when the passphrase is wrong.
572 struct ssh2_userkey ssh2_wrong_passphrase
= {
576 struct ssh2_userkey
*ssh2_load_userkey(char *filename
, char *passphrase
)
579 char header
[40], *b
, *encryption
, *comment
, *mac
;
580 const struct ssh_signkey
*alg
;
581 struct ssh2_userkey
*ret
;
582 int cipher
, cipherblk
;
583 unsigned char *public_blob
, *private_blob
;
584 int public_blob_len
, private_blob_len
;
585 int i
, is_mac
, old_fmt
;
586 int passlen
= passphrase ?
strlen(passphrase
) : 0;
588 ret
= NULL
; /* return NULL for most errors */
589 comment
= mac
= NULL
;
590 public_blob
= private_blob
= NULL
;
592 fp
= fopen(filename
, "rb");
596 /* Read the first header line which contains the key type. */
597 if (!read_header(fp
, header
))
599 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
601 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
602 /* this is an old key file; warn and then continue */
603 old_keyfile_warning();
607 if ((b
= read_body(fp
)) == NULL
)
609 /* Select key algorithm structure. */
610 if (!strcmp(b
, "ssh-rsa"))
612 else if (!strcmp(b
, "ssh-dss"))
620 /* Read the Encryption header line. */
621 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
623 if ((encryption
= read_body(fp
)) == NULL
)
625 if (!strcmp(encryption
, "aes256-cbc")) {
628 } else if (!strcmp(encryption
, "none")) {
636 /* Read the Comment header line. */
637 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
639 if ((comment
= read_body(fp
)) == NULL
)
642 /* Read the Public-Lines header line and the public blob. */
643 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
645 if ((b
= read_body(fp
)) == NULL
)
649 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
652 /* Read the Private-Lines header line and the Private blob. */
653 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
655 if ((b
= read_body(fp
)) == NULL
)
659 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
662 /* Read the Private-MAC or Private-Hash header line. */
663 if (!read_header(fp
, header
))
665 if (0 == strcmp(header
, "Private-MAC")) {
666 if ((mac
= read_body(fp
)) == NULL
)
669 } else if (0 == strcmp(header
, "Private-Hash") &&
670 alg
== &ssh_rsa
&& old_fmt
) {
671 if ((mac
= read_body(fp
)) == NULL
)
681 * Decrypt the private blob.
684 unsigned char key
[40];
689 if (private_blob_len
% cipherblk
)
693 SHA_Bytes(&s
, "\0\0\0\0", 4);
694 SHA_Bytes(&s
, passphrase
, passlen
);
695 SHA_Final(&s
, key
+ 0);
697 SHA_Bytes(&s
, "\0\0\0\1", 4);
698 SHA_Bytes(&s
, passphrase
, passlen
);
699 SHA_Final(&s
, key
+ 20);
700 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
708 unsigned char binary
[20];
709 unsigned char *macdata
;
714 /* MAC (or hash) only covers the private blob. */
715 macdata
= private_blob
;
716 maclen
= private_blob_len
;
720 int namelen
= strlen(alg
->name
);
721 int enclen
= strlen(encryption
);
722 int commlen
= strlen(comment
);
723 maclen
= (4 + namelen
+
726 4 + public_blob_len
+
727 4 + private_blob_len
);
728 macdata
= smalloc(maclen
);
730 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
731 DO_STR(alg
->name
, namelen
);
732 DO_STR(encryption
, enclen
);
733 DO_STR(comment
, commlen
);
734 DO_STR(public_blob
, public_blob_len
);
735 DO_STR(private_blob
, private_blob_len
);
742 unsigned char mackey
[20];
743 char header
[] = "putty-private-key-file-mac-key";
746 SHA_Bytes(&s
, header
, sizeof(header
)-1);
748 SHA_Bytes(&s
, passphrase
, passlen
);
749 SHA_Final(&s
, mackey
);
751 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
753 memset(mackey
, 0, sizeof(mackey
));
754 memset(&s
, 0, sizeof(s
));
756 SHA_Simple(macdata
, maclen
, binary
);
760 memset(macdata
, 0, maclen
);
764 for (i
= 0; i
< 20; i
++)
765 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
767 if (strcmp(mac
, realmac
)) {
768 /* An incorrect MAC is an unconditional Error if the key is
769 * unencrypted. Otherwise, it means Wrong Passphrase. */
770 ret
= cipher ? SSH2_WRONG_PASSPHRASE
: NULL
;
777 * Create and return the key.
779 ret
= smalloc(sizeof(struct ssh2_userkey
));
781 ret
->comment
= comment
;
782 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
783 private_blob
, private_blob_len
);
813 char *ssh2_userkey_loadpub(char *filename
, char **algorithm
,
818 const struct ssh_signkey
*alg
;
819 unsigned char *public_blob
;
825 fp
= fopen(filename
, "rb");
829 /* Read the first header line which contains the key type. */
830 if (!read_header(fp
, header
)
831 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
832 0 != strcmp(header
, "PuTTY-User-Key-File-1")))
834 if ((b
= read_body(fp
)) == NULL
)
836 /* Select key algorithm structure. Currently only ssh-rsa. */
837 if (!strcmp(b
, "ssh-rsa"))
839 else if (!strcmp(b
, "ssh-dss"))
847 /* Read the Encryption header line. */
848 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
850 if ((b
= read_body(fp
)) == NULL
)
852 sfree(b
); /* we don't care */
854 /* Read the Comment header line. */
855 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
857 if ((b
= read_body(fp
)) == NULL
)
859 sfree(b
); /* we don't care */
861 /* Read the Public-Lines header line and the public blob. */
862 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
864 if ((b
= read_body(fp
)) == NULL
)
868 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
873 *pub_blob_len
= public_blob_len
;
875 *algorithm
= alg
->name
;
889 int ssh2_userkey_encrypted(char *filename
, char **commentptr
)
892 char header
[40], *b
, *comment
;
898 fp
= fopen(filename
, "rb");
901 if (!read_header(fp
, header
)
902 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
903 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
907 if ((b
= read_body(fp
)) == NULL
) {
911 sfree(b
); /* we don't care about key type here */
912 /* Read the Encryption header line. */
913 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
917 if ((b
= read_body(fp
)) == NULL
) {
922 /* Read the Comment header line. */
923 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
928 if ((comment
= read_body(fp
)) == NULL
) {
935 *commentptr
= comment
;
938 if (!strcmp(b
, "aes256-cbc"))
946 int base64_lines(int datalen
)
948 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
949 return (datalen
+ 47) / 48;
952 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
958 while (datalen
> 0) {
959 n
= (datalen
< 3 ? datalen
: 3);
960 base64_encode_atom(data
, n
, out
);
963 for (i
= 0; i
< 4; i
++) {
964 if (linelen
>= cpl
) {
975 int ssh2_save_userkey(char *filename
, struct ssh2_userkey
*key
,
979 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
980 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
985 unsigned char priv_mac
[20];
988 * Fetch the key component blobs.
990 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
991 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
992 if (!pub_blob
|| !priv_blob
) {
999 * Determine encryption details, and encrypt the private blob.
1002 cipherstr
= "aes256-cbc";
1008 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1009 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1010 priv_blob_encrypted
= smalloc(priv_encrypted_len
);
1011 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1012 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1013 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1014 * too easy a known-plaintext attack on the last block. */
1015 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1016 assert(priv_encrypted_len
- priv_blob_len
< 20);
1017 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1018 priv_encrypted_len
- priv_blob_len
);
1020 /* Now create the MAC. */
1022 unsigned char *macdata
;
1025 int namelen
= strlen(key
->alg
->name
);
1026 int enclen
= strlen(cipherstr
);
1027 int commlen
= strlen(key
->comment
);
1029 unsigned char mackey
[20];
1030 char header
[] = "putty-private-key-file-mac-key";
1032 maclen
= (4 + namelen
+
1036 4 + priv_encrypted_len
);
1037 macdata
= smalloc(maclen
);
1039 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1040 DO_STR(key
->alg
->name
, namelen
);
1041 DO_STR(cipherstr
, enclen
);
1042 DO_STR(key
->comment
, commlen
);
1043 DO_STR(pub_blob
, pub_blob_len
);
1044 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1047 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1049 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1050 SHA_Final(&s
, mackey
);
1051 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1052 memset(macdata
, 0, maclen
);
1054 memset(mackey
, 0, sizeof(mackey
));
1055 memset(&s
, 0, sizeof(s
));
1062 passlen
= strlen(passphrase
);
1065 SHA_Bytes(&s
, "\0\0\0\0", 4);
1066 SHA_Bytes(&s
, passphrase
, passlen
);
1067 SHA_Final(&s
, key
+ 0);
1069 SHA_Bytes(&s
, "\0\0\0\1", 4);
1070 SHA_Bytes(&s
, passphrase
, passlen
);
1071 SHA_Final(&s
, key
+ 20);
1072 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1073 priv_encrypted_len
);
1075 memset(key
, 0, sizeof(key
));
1076 memset(&s
, 0, sizeof(s
));
1079 fp
= fopen(filename
, "w");
1082 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1083 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1084 fprintf(fp
, "Comment: %s\n", key
->comment
);
1085 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1086 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1087 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1088 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1089 fprintf(fp
, "Private-MAC: ");
1090 for (i
= 0; i
< 20; i
++)
1091 fprintf(fp
, "%02x", priv_mac
[i
]);
1096 memset(priv_blob
, 0, priv_blob_len
);
1098 sfree(priv_blob_encrypted
);
1102 /* ----------------------------------------------------------------------
1103 * A function to determine the type of a private key file. Returns
1104 * 0 on failure, 1 or 2 on success.
1106 int key_type(char *filename
)
1110 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1111 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1112 const char openssh_sig
[] = "-----BEGIN ";
1115 fp
= fopen(filename
, "r");
1117 return SSH_KEYTYPE_UNOPENABLE
;
1118 i
= fread(buf
, 1, sizeof(buf
), fp
);
1121 return SSH_KEYTYPE_UNOPENABLE
;
1123 return SSH_KEYTYPE_UNKNOWN
;
1124 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1125 return SSH_KEYTYPE_SSH1
;
1126 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1127 return SSH_KEYTYPE_SSH2
;
1128 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1129 return SSH_KEYTYPE_OPENSSH
;
1130 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1131 return SSH_KEYTYPE_SSHCOM
;
1132 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1136 * Convert the type word to a string, for `wrong type' error
1139 char *key_type_to_str(int type
)
1142 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1143 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1144 case SSH_KEYTYPE_SSH1
: return "SSH1 private key"; break;
1145 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH2 private key"; break;
1146 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH2 private key"; break;
1147 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH2 private key"; break;
1148 default: return "INTERNAL ERROR"; break;