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
)
39 unsigned char buf
[16384];
40 unsigned char keybuf
[16];
44 struct MD5Context md5c
;
47 /* Slurp the whole file (minus the header) into a buffer. */
48 len
= fread(buf
, 1, sizeof(buf
), fp
);
50 if (len
< 0 || len
== sizeof(buf
))
51 goto end
; /* file too big or not read */
56 * A zero byte. (The signature includes a terminating NUL.)
58 if (len
- i
< 1 || buf
[i
] != 0)
62 /* One byte giving encryption type, and one reserved uint32. */
66 if (ciphertype
!= 0 && ciphertype
!= SSH_CIPHER_3DES
)
70 goto end
; /* reserved field not present */
71 if (buf
[i
] != 0 || buf
[i
+ 1] != 0 || buf
[i
+ 2] != 0
72 || buf
[i
+ 3] != 0) goto end
; /* reserved field nonzero, panic! */
75 /* Now the serious stuff. An ordinary SSH 1 public key. */
76 i
+= makekey(buf
+ i
, key
, NULL
, 1);
78 goto end
; /* overran */
85 /* Next, the comment field. */
86 j
= GET_32BIT(buf
+ i
);
90 comment
= snewn(j
+ 1, char);
92 memcpy(comment
, buf
+ i
, j
);
97 *commentptr
= comment
;
99 key
->comment
= comment
;
101 return ciphertype
!= 0;
105 * Decrypt remainder of buffer.
109 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
110 MD5Final(keybuf
, &md5c
);
111 des3_decrypt_pubkey(keybuf
, buf
+ i
, (len
- i
+ 7) & ~7);
112 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
116 * We are now in the secret part of the key. The first four
117 * bytes should be of the form a, b, a, b.
121 if (buf
[i
] != buf
[i
+ 2] || buf
[i
+ 1] != buf
[i
+ 3]) {
128 * After that, we have one further bignum which is our
129 * decryption exponent, and then the three auxiliary values
132 i
+= makeprivate(buf
+ i
, key
);
135 i
+= ssh1_read_bignum(buf
+ i
, &key
->iqmp
);
138 i
+= ssh1_read_bignum(buf
+ i
, &key
->q
);
141 i
+= ssh1_read_bignum(buf
+ i
, &key
->p
);
145 if (!rsa_verify(key
)) {
152 memset(buf
, 0, sizeof(buf
)); /* burn the evidence */
156 int loadrsakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
161 fp
= f_open(*filename
, "rb");
163 return 0; /* doesn't even exist */
166 * Read the first line of the file and see if it's a v1 private
169 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
170 return loadrsakey_main(fp
, key
, FALSE
, NULL
, passphrase
);
174 * Otherwise, we have nothing. Return empty-handed.
181 * See whether an RSA key is encrypted. Return its comment field as
184 int rsakey_encrypted(const Filename
*filename
, char **comment
)
189 fp
= f_open(*filename
, "rb");
191 return 0; /* doesn't even exist */
194 * Read the first line of the file and see if it's a v1 private
197 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
198 return loadrsakey_main(fp
, NULL
, FALSE
, comment
, NULL
);
201 return 0; /* wasn't the right kind of file */
205 * Return a malloc'ed chunk of memory containing the public blob of
206 * an RSA key, as given in the agent protocol (modulus bits,
207 * exponent, modulus).
209 int rsakey_pubblob(const Filename
*filename
, void **blob
, int *bloblen
)
216 /* Default return if we fail. */
221 fp
= f_open(*filename
, "rb");
223 return 0; /* doesn't even exist */
226 * Read the first line of the file and see if it's a v1 private
229 if (fgets(buf
, sizeof(buf
), fp
) && !strcmp(buf
, rsa_signature
)) {
230 memset(&key
, 0, sizeof(key
));
231 if (loadrsakey_main(fp
, &key
, TRUE
, NULL
, NULL
)) {
232 *blob
= rsa_public_blob(&key
, bloblen
);
242 * Save an RSA key file. Return nonzero on success.
244 int saversakey(const Filename
*filename
, struct RSAKey
*key
, char *passphrase
)
246 unsigned char buf
[16384];
247 unsigned char keybuf
[16];
248 struct MD5Context md5c
;
249 unsigned char *p
, *estart
;
253 * Write the initial signature.
256 memcpy(p
, rsa_signature
, sizeof(rsa_signature
));
257 p
+= sizeof(rsa_signature
);
260 * One byte giving encryption type, and one reserved (zero)
263 *p
++ = (passphrase ? SSH_CIPHER_3DES
: 0);
268 * An ordinary SSH 1 public key consists of: a uint32
269 * containing the bit count, then two bignums containing the
270 * modulus and exponent respectively.
272 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
274 p
+= ssh1_write_bignum(p
, key
->modulus
);
275 p
+= ssh1_write_bignum(p
, key
->exponent
);
278 * A string containing the comment field.
281 PUT_32BIT(p
, strlen(key
->comment
));
283 memcpy(p
, key
->comment
, strlen(key
->comment
));
284 p
+= strlen(key
->comment
);
291 * The encrypted portion starts here.
296 * Two bytes, then the same two bytes repeated.
298 *p
++ = random_byte();
299 *p
++ = random_byte();
305 * Four more bignums: the decryption exponent, then iqmp, then
308 p
+= ssh1_write_bignum(p
, key
->private_exponent
);
309 p
+= ssh1_write_bignum(p
, key
->iqmp
);
310 p
+= ssh1_write_bignum(p
, key
->q
);
311 p
+= ssh1_write_bignum(p
, key
->p
);
314 * Now write zeros until the encrypted portion is a multiple of
317 while ((p
- estart
) % 8)
321 * Now encrypt the encrypted portion.
325 MD5Update(&md5c
, (unsigned char *)passphrase
, strlen(passphrase
));
326 MD5Final(keybuf
, &md5c
);
327 des3_encrypt_pubkey(keybuf
, estart
, p
- estart
);
328 memset(keybuf
, 0, sizeof(keybuf
)); /* burn the evidence */
332 * Done. Write the result to the file.
334 fp
= f_open(*filename
, "wb");
336 int ret
= (fwrite(buf
, 1, p
- buf
, fp
) == (size_t) (p
- buf
));
337 ret
= ret
&& (fclose(fp
) == 0);
343 /* ----------------------------------------------------------------------
344 * SSH2 private key load/store functions.
348 * PuTTY's own format for SSH2 keys is as follows:
350 * The file is text. Lines are terminated by CRLF, although CR-only
351 * and LF-only are tolerated on input.
353 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
354 * algorithm ("ssh-dss", "ssh-rsa" etc).
356 * The next line says "Encryption: " plus an encryption type.
357 * Currently the only supported encryption types are "aes256-cbc"
360 * The next line says "Comment: " plus the comment string.
362 * Next there is a line saying "Public-Lines: " plus a number N.
363 * The following N lines contain a base64 encoding of the public
364 * part of the key. This is encoded as the standard SSH2 public key
365 * blob (with no initial length): so for RSA, for example, it will
372 * Next, there is a line saying "Private-Lines: " plus a number N,
373 * and then N lines containing the (potentially encrypted) private
374 * part of the key. For the key type "ssh-rsa", this will be
377 * mpint private_exponent
378 * mpint p (the larger of the two primes)
379 * mpint q (the smaller prime)
380 * mpint iqmp (the inverse of q modulo p)
381 * data padding (to reach a multiple of the cipher block size)
383 * And for "ssh-dss", it will be composed of
385 * mpint x (the private key parameter)
386 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
388 * Finally, there is a line saying "Private-MAC: " plus a hex
389 * representation of a HMAC-SHA-1 of:
391 * string name of algorithm ("ssh-dss", "ssh-rsa")
392 * string encryption type
395 * string private-plaintext (the plaintext version of the
396 * private part, including the final
399 * The key to the MAC is itself a SHA-1 hash of:
401 * data "putty-private-key-file-mac-key"
404 * Encrypted keys should have a MAC, whereas unencrypted ones must
407 * If the key is encrypted, the encryption key is derived from the
408 * passphrase by means of a succession of SHA-1 hashes. Each hash
411 * uint32 sequence-number
414 * where the sequence-number increases from zero. As many of these
415 * hashes are used as necessary.
417 * For backwards compatibility with snapshots between 0.51 and
418 * 0.52, we also support the older key file format, which begins
419 * with "PuTTY-User-Key-File-1" (version number differs). In this
420 * format the Private-MAC: field only covers the private-plaintext
421 * field and nothing else (and without the 4-byte string length on
422 * the front too). Moreover, for RSA keys the Private-MAC: field
423 * can be replaced with a Private-Hash: field which is a plain
424 * SHA-1 hash instead of an HMAC. This is not allowable in DSA
425 * keys. (Yes, the old format was a mess. Guess why it changed :-)
428 static int read_header(FILE * fp
, char *header
)
435 if (c
== '\n' || c
== '\r' || c
== EOF
)
436 return 0; /* failure */
442 return 1; /* success! */
445 return 0; /* failure */
449 return 0; /* failure */
452 static char *read_body(FILE * fp
)
460 text
= snewn(size
, char);
466 if (c
== '\r' || c
== '\n') {
468 if (c
!= '\r' && c
!= '\n' && c
!= EOF
)
476 if (len
+ 1 > size
) {
478 text
= sresize(text
, size
, char);
485 int base64_decode_atom(char *atom
, unsigned char *out
)
492 for (i
= 0; i
< 4; i
++) {
494 if (c
>= 'A' && c
<= 'Z')
496 else if (c
>= 'a' && c
<= 'z')
498 else if (c
>= '0' && c
<= '9')
507 return 0; /* invalid atom */
511 if (vals
[0] == -1 || vals
[1] == -1)
513 if (vals
[2] == -1 && vals
[3] != -1)
518 else if (vals
[2] != -1)
523 word
= ((vals
[0] << 18) |
524 (vals
[1] << 12) | ((vals
[2] & 0x3F) << 6) | (vals
[3] & 0x3F));
525 out
[0] = (word
>> 16) & 0xFF;
527 out
[1] = (word
>> 8) & 0xFF;
529 out
[2] = word
& 0xFF;
533 static unsigned char *read_blob(FILE * fp
, int nlines
, int *bloblen
)
540 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
541 blob
= snewn(48 * nlines
, unsigned char);
543 for (i
= 0; i
< nlines
; i
++) {
544 line
= read_body(fp
);
549 linelen
= strlen(line
);
550 if (linelen
% 4 != 0 || linelen
> 64) {
555 for (j
= 0; j
< linelen
; j
+= 4) {
556 k
= base64_decode_atom(line
+ j
, blob
+ len
);
571 * Magic error return value for when the passphrase is wrong.
573 struct ssh2_userkey ssh2_wrong_passphrase
= {
577 struct ssh2_userkey
*ssh2_load_userkey(const Filename
*filename
,
581 char header
[40], *b
, *encryption
, *comment
, *mac
;
582 const struct ssh_signkey
*alg
;
583 struct ssh2_userkey
*ret
;
584 int cipher
, cipherblk
;
585 unsigned char *public_blob
, *private_blob
;
586 int public_blob_len
, private_blob_len
;
587 int i
, is_mac
, old_fmt
;
588 int passlen
= passphrase ?
strlen(passphrase
) : 0;
590 ret
= NULL
; /* return NULL for most errors */
591 encryption
= comment
= mac
= NULL
;
592 public_blob
= private_blob
= NULL
;
594 fp
= f_open(*filename
, "rb");
598 /* Read the first header line which contains the key type. */
599 if (!read_header(fp
, header
))
601 if (0 == strcmp(header
, "PuTTY-User-Key-File-2")) {
603 } else if (0 == strcmp(header
, "PuTTY-User-Key-File-1")) {
604 /* this is an old key file; warn and then continue */
605 old_keyfile_warning();
609 if ((b
= read_body(fp
)) == NULL
)
611 /* Select key algorithm structure. */
612 if (!strcmp(b
, "ssh-rsa"))
614 else if (!strcmp(b
, "ssh-dss"))
622 /* Read the Encryption header line. */
623 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
625 if ((encryption
= read_body(fp
)) == NULL
)
627 if (!strcmp(encryption
, "aes256-cbc")) {
630 } else if (!strcmp(encryption
, "none")) {
638 /* Read the Comment header line. */
639 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
641 if ((comment
= read_body(fp
)) == NULL
)
644 /* Read the Public-Lines header line and the public blob. */
645 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
647 if ((b
= read_body(fp
)) == NULL
)
651 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
654 /* Read the Private-Lines header line and the Private blob. */
655 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Private-Lines"))
657 if ((b
= read_body(fp
)) == NULL
)
661 if ((private_blob
= read_blob(fp
, i
, &private_blob_len
)) == NULL
)
664 /* Read the Private-MAC or Private-Hash header line. */
665 if (!read_header(fp
, header
))
667 if (0 == strcmp(header
, "Private-MAC")) {
668 if ((mac
= read_body(fp
)) == NULL
)
671 } else if (0 == strcmp(header
, "Private-Hash") &&
672 alg
== &ssh_rsa
&& old_fmt
) {
673 if ((mac
= read_body(fp
)) == NULL
)
683 * Decrypt the private blob.
686 unsigned char key
[40];
691 if (private_blob_len
% cipherblk
)
695 SHA_Bytes(&s
, "\0\0\0\0", 4);
696 SHA_Bytes(&s
, passphrase
, passlen
);
697 SHA_Final(&s
, key
+ 0);
699 SHA_Bytes(&s
, "\0\0\0\1", 4);
700 SHA_Bytes(&s
, passphrase
, passlen
);
701 SHA_Final(&s
, key
+ 20);
702 aes256_decrypt_pubkey(key
, private_blob
, private_blob_len
);
710 unsigned char binary
[20];
711 unsigned char *macdata
;
716 /* MAC (or hash) only covers the private blob. */
717 macdata
= private_blob
;
718 maclen
= private_blob_len
;
722 int namelen
= strlen(alg
->name
);
723 int enclen
= strlen(encryption
);
724 int commlen
= strlen(comment
);
725 maclen
= (4 + namelen
+
728 4 + public_blob_len
+
729 4 + private_blob_len
);
730 macdata
= snewn(maclen
, unsigned char);
732 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
733 DO_STR(alg
->name
, namelen
);
734 DO_STR(encryption
, enclen
);
735 DO_STR(comment
, commlen
);
736 DO_STR(public_blob
, public_blob_len
);
737 DO_STR(private_blob
, private_blob_len
);
744 unsigned char mackey
[20];
745 char header
[] = "putty-private-key-file-mac-key";
748 SHA_Bytes(&s
, header
, sizeof(header
)-1);
750 SHA_Bytes(&s
, passphrase
, passlen
);
751 SHA_Final(&s
, mackey
);
753 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, binary
);
755 memset(mackey
, 0, sizeof(mackey
));
756 memset(&s
, 0, sizeof(s
));
758 SHA_Simple(macdata
, maclen
, binary
);
762 memset(macdata
, 0, maclen
);
766 for (i
= 0; i
< 20; i
++)
767 sprintf(realmac
+ 2 * i
, "%02x", binary
[i
]);
769 if (strcmp(mac
, realmac
)) {
770 /* An incorrect MAC is an unconditional Error if the key is
771 * unencrypted. Otherwise, it means Wrong Passphrase. */
772 ret
= cipher ? SSH2_WRONG_PASSPHRASE
: NULL
;
779 * Create and return the key.
781 ret
= snew(struct ssh2_userkey
);
783 ret
->comment
= comment
;
784 ret
->data
= alg
->createkey(public_blob
, public_blob_len
,
785 private_blob
, private_blob_len
);
815 char *ssh2_userkey_loadpub(const Filename
*filename
, char **algorithm
,
820 const struct ssh_signkey
*alg
;
821 unsigned char *public_blob
;
827 fp
= f_open(*filename
, "rb");
831 /* Read the first header line which contains the key type. */
832 if (!read_header(fp
, header
)
833 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
834 0 != strcmp(header
, "PuTTY-User-Key-File-1")))
836 if ((b
= read_body(fp
)) == NULL
)
838 /* Select key algorithm structure. Currently only ssh-rsa. */
839 if (!strcmp(b
, "ssh-rsa"))
841 else if (!strcmp(b
, "ssh-dss"))
849 /* Read the Encryption header line. */
850 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption"))
852 if ((b
= read_body(fp
)) == NULL
)
854 sfree(b
); /* we don't care */
856 /* Read the Comment header line. */
857 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment"))
859 if ((b
= read_body(fp
)) == NULL
)
861 sfree(b
); /* we don't care */
863 /* Read the Public-Lines header line and the public blob. */
864 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Public-Lines"))
866 if ((b
= read_body(fp
)) == NULL
)
870 if ((public_blob
= read_blob(fp
, i
, &public_blob_len
)) == NULL
)
875 *pub_blob_len
= public_blob_len
;
877 *algorithm
= alg
->name
;
878 return (char *)public_blob
;
891 int ssh2_userkey_encrypted(const Filename
*filename
, char **commentptr
)
894 char header
[40], *b
, *comment
;
900 fp
= f_open(*filename
, "rb");
903 if (!read_header(fp
, header
)
904 || (0 != strcmp(header
, "PuTTY-User-Key-File-2") &&
905 0 != strcmp(header
, "PuTTY-User-Key-File-1"))) {
909 if ((b
= read_body(fp
)) == NULL
) {
913 sfree(b
); /* we don't care about key type here */
914 /* Read the Encryption header line. */
915 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Encryption")) {
919 if ((b
= read_body(fp
)) == NULL
) {
924 /* Read the Comment header line. */
925 if (!read_header(fp
, header
) || 0 != strcmp(header
, "Comment")) {
930 if ((comment
= read_body(fp
)) == NULL
) {
937 *commentptr
= comment
;
940 if (!strcmp(b
, "aes256-cbc"))
948 int base64_lines(int datalen
)
950 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
951 return (datalen
+ 47) / 48;
954 void base64_encode(FILE * fp
, unsigned char *data
, int datalen
, int cpl
)
960 while (datalen
> 0) {
961 n
= (datalen
< 3 ? datalen
: 3);
962 base64_encode_atom(data
, n
, out
);
965 for (i
= 0; i
< 4; i
++) {
966 if (linelen
>= cpl
) {
977 int ssh2_save_userkey(const Filename
*filename
, struct ssh2_userkey
*key
,
981 unsigned char *pub_blob
, *priv_blob
, *priv_blob_encrypted
;
982 int pub_blob_len
, priv_blob_len
, priv_encrypted_len
;
987 unsigned char priv_mac
[20];
990 * Fetch the key component blobs.
992 pub_blob
= key
->alg
->public_blob(key
->data
, &pub_blob_len
);
993 priv_blob
= key
->alg
->private_blob(key
->data
, &priv_blob_len
);
994 if (!pub_blob
|| !priv_blob
) {
1001 * Determine encryption details, and encrypt the private blob.
1004 cipherstr
= "aes256-cbc";
1010 priv_encrypted_len
= priv_blob_len
+ cipherblk
- 1;
1011 priv_encrypted_len
-= priv_encrypted_len
% cipherblk
;
1012 priv_blob_encrypted
= snewn(priv_encrypted_len
, unsigned char);
1013 memset(priv_blob_encrypted
, 0, priv_encrypted_len
);
1014 memcpy(priv_blob_encrypted
, priv_blob
, priv_blob_len
);
1015 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1016 * too easy a known-plaintext attack on the last block. */
1017 SHA_Simple(priv_blob
, priv_blob_len
, priv_mac
);
1018 assert(priv_encrypted_len
- priv_blob_len
< 20);
1019 memcpy(priv_blob_encrypted
+ priv_blob_len
, priv_mac
,
1020 priv_encrypted_len
- priv_blob_len
);
1022 /* Now create the MAC. */
1024 unsigned char *macdata
;
1027 int namelen
= strlen(key
->alg
->name
);
1028 int enclen
= strlen(cipherstr
);
1029 int commlen
= strlen(key
->comment
);
1031 unsigned char mackey
[20];
1032 char header
[] = "putty-private-key-file-mac-key";
1034 maclen
= (4 + namelen
+
1038 4 + priv_encrypted_len
);
1039 macdata
= snewn(maclen
, unsigned char);
1041 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1042 DO_STR(key
->alg
->name
, namelen
);
1043 DO_STR(cipherstr
, enclen
);
1044 DO_STR(key
->comment
, commlen
);
1045 DO_STR(pub_blob
, pub_blob_len
);
1046 DO_STR(priv_blob_encrypted
, priv_encrypted_len
);
1049 SHA_Bytes(&s
, header
, sizeof(header
)-1);
1051 SHA_Bytes(&s
, passphrase
, strlen(passphrase
));
1052 SHA_Final(&s
, mackey
);
1053 hmac_sha1_simple(mackey
, 20, macdata
, maclen
, priv_mac
);
1054 memset(macdata
, 0, maclen
);
1056 memset(mackey
, 0, sizeof(mackey
));
1057 memset(&s
, 0, sizeof(s
));
1061 unsigned char key
[40];
1064 passlen
= strlen(passphrase
);
1067 SHA_Bytes(&s
, "\0\0\0\0", 4);
1068 SHA_Bytes(&s
, passphrase
, passlen
);
1069 SHA_Final(&s
, key
+ 0);
1071 SHA_Bytes(&s
, "\0\0\0\1", 4);
1072 SHA_Bytes(&s
, passphrase
, passlen
);
1073 SHA_Final(&s
, key
+ 20);
1074 aes256_encrypt_pubkey(key
, priv_blob_encrypted
,
1075 priv_encrypted_len
);
1077 memset(key
, 0, sizeof(key
));
1078 memset(&s
, 0, sizeof(s
));
1081 fp
= f_open(*filename
, "w");
1084 fprintf(fp
, "PuTTY-User-Key-File-2: %s\n", key
->alg
->name
);
1085 fprintf(fp
, "Encryption: %s\n", cipherstr
);
1086 fprintf(fp
, "Comment: %s\n", key
->comment
);
1087 fprintf(fp
, "Public-Lines: %d\n", base64_lines(pub_blob_len
));
1088 base64_encode(fp
, pub_blob
, pub_blob_len
, 64);
1089 fprintf(fp
, "Private-Lines: %d\n", base64_lines(priv_encrypted_len
));
1090 base64_encode(fp
, priv_blob_encrypted
, priv_encrypted_len
, 64);
1091 fprintf(fp
, "Private-MAC: ");
1092 for (i
= 0; i
< 20; i
++)
1093 fprintf(fp
, "%02x", priv_mac
[i
]);
1098 memset(priv_blob
, 0, priv_blob_len
);
1100 sfree(priv_blob_encrypted
);
1104 /* ----------------------------------------------------------------------
1105 * A function to determine the type of a private key file. Returns
1106 * 0 on failure, 1 or 2 on success.
1108 int key_type(const Filename
*filename
)
1112 const char putty2_sig
[] = "PuTTY-User-Key-File-";
1113 const char sshcom_sig
[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1114 const char openssh_sig
[] = "-----BEGIN ";
1117 fp
= f_open(*filename
, "r");
1119 return SSH_KEYTYPE_UNOPENABLE
;
1120 i
= fread(buf
, 1, sizeof(buf
), fp
);
1123 return SSH_KEYTYPE_UNOPENABLE
;
1125 return SSH_KEYTYPE_UNKNOWN
;
1126 if (!memcmp(buf
, rsa_signature
, sizeof(rsa_signature
)-1))
1127 return SSH_KEYTYPE_SSH1
;
1128 if (!memcmp(buf
, putty2_sig
, sizeof(putty2_sig
)-1))
1129 return SSH_KEYTYPE_SSH2
;
1130 if (!memcmp(buf
, openssh_sig
, sizeof(openssh_sig
)-1))
1131 return SSH_KEYTYPE_OPENSSH
;
1132 if (!memcmp(buf
, sshcom_sig
, sizeof(sshcom_sig
)-1))
1133 return SSH_KEYTYPE_SSHCOM
;
1134 return SSH_KEYTYPE_UNKNOWN
; /* unrecognised or EOF */
1138 * Convert the type word to a string, for `wrong type' error
1141 char *key_type_to_str(int type
)
1144 case SSH_KEYTYPE_UNOPENABLE
: return "unable to open file"; break;
1145 case SSH_KEYTYPE_UNKNOWN
: return "not a private key"; break;
1146 case SSH_KEYTYPE_SSH1
: return "SSH1 private key"; break;
1147 case SSH_KEYTYPE_SSH2
: return "PuTTY SSH2 private key"; break;
1148 case SSH_KEYTYPE_OPENSSH
: return "OpenSSH SSH2 private key"; break;
1149 case SSH_KEYTYPE_SSHCOM
: return "ssh.com SSH2 private key"; break;
1150 default: return "INTERNAL ERROR"; break;