2 * Generic SSH public-key handling operations. In particular,
3 * reading of SSH public-key files, and also the generic `sign'
4 * operation for SSH-2 (which checks the type of the key and
5 * dispatches to the appropriate key-type specific function).
16 #define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
18 #define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\
19 (x)-'a'<26 ? (x)-'a'+26 :\
20 (x)-'0'<10 ? (x)-'0'+52 :\
24 static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,
25 char **commentptr, char *passphrase,
28 unsigned char buf[16384];
29 unsigned char keybuf[16];
33 struct MD5Context md5c;
38 /* Slurp the whole file (minus the header) into a buffer. */
39 len = fread(buf, 1, sizeof(buf), fp);
41 if (len < 0 || len == sizeof(buf)) {
42 *error = "error reading file";
43 goto end; /* file too big or not read */
47 *error = "file format error";
50 * A zero byte. (The signature includes a terminating NUL.)
52 if (len - i < 1 || buf[i] != 0)
56 /* One byte giving encryption type, and one reserved uint32. */
60 if (ciphertype != 0 && ciphertype != SSH_CIPHER_3DES)
64 goto end; /* reserved field not present */
65 if (buf[i] != 0 || buf[i + 1] != 0 || buf[i + 2] != 0
66 || buf[i + 3] != 0) goto end; /* reserved field nonzero, panic! */
69 /* Now the serious stuff. An ordinary SSH-1 public key. */
70 j = makekey(buf + i, len - i, key, NULL, 1);
72 goto end; /* overran */
75 /* Next, the comment field. */
76 j = toint(GET_32BIT(buf + i));
78 if (j < 0 || len - i < j)
80 comment = snewn(j + 1, char);
82 memcpy(comment, buf + i, j);
87 *commentptr = dupstr(comment);
89 key->comment = comment;
99 ret = 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 smemclr(keybuf, 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]) {
122 *error = "wrong passphrase";
129 * After that, we have one further bignum which is our
130 * decryption exponent, and then the three auxiliary values
133 j = makeprivate(buf + i, len - i, key);
136 j = ssh1_read_bignum(buf + i, len - i, &key->iqmp);
139 j = ssh1_read_bignum(buf + i, len - i, &key->q);
142 j = ssh1_read_bignum(buf + i, len - i, &key->p);
146 if (!rsa_verify(key)) {
147 *error = "rsa_verify failed";
154 smemclr(buf, sizeof(buf)); /* burn the evidence */
158 int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,
159 const char **errorstr)
164 const char *error = NULL;
166 fp = f_open(filename, "rb", FALSE);
168 error = "can't open file";
173 * Read the first line of the file and see if it's a v1 private
176 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
178 * This routine will take care of calling fclose() for us.
180 ret = loadrsakey_main(fp, key, FALSE, NULL, passphrase, &error);
186 * Otherwise, we have nothing. Return empty-handed.
188 error = "not an SSH-1 RSA file";
193 if ((ret != 1) && errorstr)
199 * See whether an RSA key is encrypted. Return its comment field as
202 int rsakey_encrypted(const Filename *filename, char **comment)
207 fp = f_open(filename, "rb", FALSE);
209 return 0; /* doesn't even exist */
212 * Read the first line of the file and see if it's a v1 private
215 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
218 * This routine will take care of calling fclose() for us.
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 char **commentptr, const char **errorstr)
238 const char *error = NULL;
240 /* Default return if we fail. */
245 fp = f_open(filename, "rb", FALSE);
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, commentptr, NULL, &error)) {
258 *blob = rsa_public_blob(&key, bloblen);
262 fp = NULL; /* loadrsakey_main unconditionally closes fp */
264 error = "not an SSH-1 RSA file";
270 if ((ret != 1) && errorstr)
276 * Save an RSA key file. Return nonzero on success.
278 int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)
280 unsigned char buf[16384];
281 unsigned char keybuf[16];
282 struct MD5Context md5c;
283 unsigned char *p, *estart;
287 * Write the initial signature.
290 memcpy(p, rsa_signature, sizeof(rsa_signature));
291 p += sizeof(rsa_signature);
294 * One byte giving encryption type, and one reserved (zero)
297 *p++ = (passphrase ? SSH_CIPHER_3DES : 0);
302 * An ordinary SSH-1 public key consists of: a uint32
303 * containing the bit count, then two bignums containing the
304 * modulus and exponent respectively.
306 PUT_32BIT(p, bignum_bitcount(key->modulus));
308 p += ssh1_write_bignum(p, key->modulus);
309 p += ssh1_write_bignum(p, key->exponent);
312 * A string containing the comment field.
315 PUT_32BIT(p, strlen(key->comment));
317 memcpy(p, key->comment, strlen(key->comment));
318 p += strlen(key->comment);
325 * The encrypted portion starts here.
330 * Two bytes, then the same two bytes repeated.
332 *p++ = random_byte();
333 *p++ = random_byte();
339 * Four more bignums: the decryption exponent, then iqmp, then
342 p += ssh1_write_bignum(p, key->private_exponent);
343 p += ssh1_write_bignum(p, key->iqmp);
344 p += ssh1_write_bignum(p, key->q);
345 p += ssh1_write_bignum(p, key->p);
348 * Now write zeros until the encrypted portion is a multiple of
351 while ((p - estart) % 8)
355 * Now encrypt the encrypted portion.
359 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
360 MD5Final(keybuf, &md5c);
361 des3_encrypt_pubkey(keybuf, estart, p - estart);
362 smemclr(keybuf, sizeof(keybuf)); /* burn the evidence */
366 * Done. Write the result to the file.
368 fp = f_open(filename, "wb", TRUE);
370 int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));
378 /* ----------------------------------------------------------------------
379 * SSH-2 private key load/store functions.
383 * PuTTY's own format for SSH-2 keys is as follows:
385 * The file is text. Lines are terminated by CRLF, although CR-only
386 * and LF-only are tolerated on input.
388 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
389 * algorithm ("ssh-dss", "ssh-rsa" etc).
391 * The next line says "Encryption: " plus an encryption type.
392 * Currently the only supported encryption types are "aes256-cbc"
395 * The next line says "Comment: " plus the comment string.
397 * Next there is a line saying "Public-Lines: " plus a number N.
398 * The following N lines contain a base64 encoding of the public
399 * part of the key. This is encoded as the standard SSH-2 public key
400 * blob (with no initial length): so for RSA, for example, it will
407 * Next, there is a line saying "Private-Lines: " plus a number N,
408 * and then N lines containing the (potentially encrypted) private
409 * part of the key. For the key type "ssh-rsa", this will be
412 * mpint private_exponent
413 * mpint p (the larger of the two primes)
414 * mpint q (the smaller prime)
415 * mpint iqmp (the inverse of q modulo p)
416 * data padding (to reach a multiple of the cipher block size)
418 * And for "ssh-dss", it will be composed of
420 * mpint x (the private key parameter)
421 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
423 * Finally, there is a line saying "Private-MAC: " plus a hex
424 * representation of a HMAC-SHA-1 of:
426 * string name of algorithm ("ssh-dss", "ssh-rsa")
427 * string encryption type
430 * string private-plaintext (the plaintext version of the
431 * private part, including the final
434 * The key to the MAC is itself a SHA-1 hash of:
436 * data "putty-private-key-file-mac-key"
439 * (An empty passphrase is used for unencrypted keys.)
441 * If the key is encrypted, the encryption key is derived from the
442 * passphrase by means of a succession of SHA-1 hashes. Each hash
445 * uint32 sequence-number
448 * where the sequence-number increases from zero. As many of these
449 * hashes are used as necessary.
451 * For backwards compatibility with snapshots between 0.51 and
452 * 0.52, we also support the older key file format, which begins
453 * with "PuTTY-User-Key-File-1" (version number differs). In this
454 * format the Private-MAC: field only covers the private-plaintext
455 * field and nothing else (and without the 4-byte string length on
456 * the front too). Moreover, the Private-MAC: field can be replaced
457 * with a Private-Hash: field which is a plain SHA-1 hash instead of
458 * an HMAC (this was generated for unencrypted keys).
461 static int read_header(FILE * fp, char *header)
468 if (c == '\n' || c == '\r' || c == EOF)
469 return 0; /* failure */
475 return 1; /* success! */
478 return 0; /* failure */
482 return 0; /* failure */
485 static char *read_body(FILE * fp)
493 text = snewn(size, char);
499 if (c == '\r' || c == '\n' || c == EOF) {
502 if (c != '\r' && c != '\n')
507 if (len + 1 >= size) {
509 text = sresize(text, size, char);
516 static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
523 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
524 blob = snewn(48 * nlines, unsigned char);
526 for (i = 0; i < nlines; i++) {
527 line = read_body(fp);
532 linelen = strlen(line);
533 if (linelen % 4 != 0 || linelen > 64) {
538 for (j = 0; j < linelen; j += 4) {
539 k = base64_decode_atom(line + j, blob + len);
554 * Magic error return value for when the passphrase is wrong.
556 struct ssh2_userkey ssh2_wrong_passphrase = {
560 const struct ssh_signkey *find_pubkey_alg(const char *name)
562 if (!strcmp(name, "ssh-rsa"))
564 else if (!strcmp(name, "ssh-dss"))
566 else if (!strcmp(name, "ecdsa-sha2-nistp256"))
567 return &ssh_ecdsa_nistp256;
568 else if (!strcmp(name, "ecdsa-sha2-nistp384"))
569 return &ssh_ecdsa_nistp384;
570 else if (!strcmp(name, "ecdsa-sha2-nistp521"))
571 return &ssh_ecdsa_nistp521;
576 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
577 char *passphrase, const char **errorstr)
580 char header[40], *b, *encryption, *comment, *mac;
581 const struct ssh_signkey *alg;
582 struct ssh2_userkey *ret;
583 int cipher, cipherblk;
584 unsigned char *public_blob, *private_blob;
585 int public_blob_len, private_blob_len;
586 int i, is_mac, old_fmt;
587 int passlen = passphrase ? strlen(passphrase) : 0;
588 const char *error = NULL;
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", FALSE);
596 error = "can't open file";
600 /* Read the first header line which contains the key type. */
601 if (!read_header(fp, header))
603 if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {
605 } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {
606 /* this is an old key file; warn and then continue */
607 old_keyfile_warning();
609 } else if (0 == strncmp(header, "PuTTY-User-Key-File-", 20)) {
610 /* this is a key file FROM THE FUTURE; refuse it, but with a
611 * more specific error message than the generic one below */
612 error = "PuTTY key format too new";
615 error = "not a PuTTY SSH-2 private key";
618 error = "file format error";
619 if ((b = read_body(fp)) == NULL)
621 /* Select key algorithm structure. */
622 alg = find_pubkey_alg(b);
629 /* Read the Encryption header line. */
630 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
632 if ((encryption = read_body(fp)) == NULL)
634 if (!strcmp(encryption, "aes256-cbc")) {
637 } else if (!strcmp(encryption, "none")) {
644 /* Read the Comment header line. */
645 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
647 if ((comment = read_body(fp)) == NULL)
650 /* Read the Public-Lines header line and the public blob. */
651 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
653 if ((b = read_body(fp)) == NULL)
657 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
660 /* Read the Private-Lines header line and the Private blob. */
661 if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))
663 if ((b = read_body(fp)) == NULL)
667 if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)
670 /* Read the Private-MAC or Private-Hash header line. */
671 if (!read_header(fp, header))
673 if (0 == strcmp(header, "Private-MAC")) {
674 if ((mac = read_body(fp)) == NULL)
677 } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {
678 if ((mac = read_body(fp)) == NULL)
688 * Decrypt the private blob.
691 unsigned char key[40];
696 if (private_blob_len % cipherblk)
700 SHA_Bytes(&s, "\0\0\0\0", 4);
701 SHA_Bytes(&s, passphrase, passlen);
702 SHA_Final(&s, key + 0);
704 SHA_Bytes(&s, "\0\0\0\1", 4);
705 SHA_Bytes(&s, passphrase, passlen);
706 SHA_Final(&s, key + 20);
707 aes256_decrypt_pubkey(key, private_blob, private_blob_len);
715 unsigned char binary[20];
716 unsigned char *macdata;
721 /* MAC (or hash) only covers the private blob. */
722 macdata = private_blob;
723 maclen = private_blob_len;
727 int namelen = strlen(alg->name);
728 int enclen = strlen(encryption);
729 int commlen = strlen(comment);
730 maclen = (4 + namelen +
733 4 + public_blob_len +
734 4 + private_blob_len);
735 macdata = snewn(maclen, unsigned char);
737 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
738 DO_STR(alg->name, namelen);
739 DO_STR(encryption, enclen);
740 DO_STR(comment, commlen);
741 DO_STR(public_blob, public_blob_len);
742 DO_STR(private_blob, private_blob_len);
749 unsigned char mackey[20];
750 char header[] = "putty-private-key-file-mac-key";
753 SHA_Bytes(&s, header, sizeof(header)-1);
754 if (cipher && passphrase)
755 SHA_Bytes(&s, passphrase, passlen);
756 SHA_Final(&s, mackey);
758 hmac_sha1_simple(mackey, 20, macdata, maclen, binary);
760 smemclr(mackey, sizeof(mackey));
761 smemclr(&s, sizeof(s));
763 SHA_Simple(macdata, maclen, binary);
767 smemclr(macdata, maclen);
771 for (i = 0; i < 20; i++)
772 sprintf(realmac + 2 * i, "%02x", binary[i]);
774 if (strcmp(mac, realmac)) {
775 /* An incorrect MAC is an unconditional Error if the key is
776 * unencrypted. Otherwise, it means Wrong Passphrase. */
778 error = "wrong passphrase";
779 ret = SSH2_WRONG_PASSPHRASE;
781 error = "MAC failed";
791 * Create and return the key.
793 ret = snew(struct ssh2_userkey);
795 ret->comment = comment;
796 ret->data = alg->createkey(public_blob, public_blob_len,
797 private_blob, private_blob_len);
801 error = "createkey failed";
805 smemclr(private_blob, private_blob_len);
827 smemclr(private_blob, private_blob_len);
835 unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
836 int *pub_blob_len, char **commentptr,
837 const char **errorstr)
841 const struct ssh_signkey *alg;
842 unsigned char *public_blob;
845 const char *error = NULL;
850 fp = f_open(filename, "rb", FALSE);
852 error = "can't open file";
856 /* Read the first header line which contains the key type. */
857 if (!read_header(fp, header)
858 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
859 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
860 if (0 == strncmp(header, "PuTTY-User-Key-File-", 20))
861 error = "PuTTY key format too new";
863 error = "not a PuTTY SSH-2 private key";
866 error = "file format error";
867 if ((b = read_body(fp)) == NULL)
869 /* Select key algorithm structure. */
870 alg = find_pubkey_alg(b);
877 /* Read the Encryption header line. */
878 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
880 if ((b = read_body(fp)) == NULL)
882 sfree(b); /* we don't care */
884 /* Read the Comment header line. */
885 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
887 if ((comment = read_body(fp)) == NULL)
891 *commentptr = comment;
895 /* Read the Public-Lines header line and the public blob. */
896 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
898 if ((b = read_body(fp)) == NULL)
902 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
907 *pub_blob_len = public_blob_len;
909 *algorithm = alg->name;
925 int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
928 char header[40], *b, *comment;
934 fp = f_open(filename, "rb", FALSE);
937 if (!read_header(fp, header)
938 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
939 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
943 if ((b = read_body(fp)) == NULL) {
947 sfree(b); /* we don't care about key type here */
948 /* Read the Encryption header line. */
949 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
953 if ((b = read_body(fp)) == NULL) {
958 /* Read the Comment header line. */
959 if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
964 if ((comment = read_body(fp)) == NULL) {
971 *commentptr = comment;
976 if (!strcmp(b, "aes256-cbc"))
984 int base64_lines(int datalen)
986 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
987 return (datalen + 47) / 48;
990 void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)
996 while (datalen > 0) {
997 n = (datalen < 3 ? datalen : 3);
998 base64_encode_atom(data, n, out);
1001 for (i = 0; i < 4; i++) {
1002 if (linelen >= cpl) {
1013 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
1017 unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
1018 int pub_blob_len, priv_blob_len, priv_encrypted_len;
1023 unsigned char priv_mac[20];
1026 * Fetch the key component blobs.
1028 pub_blob = key->alg->public_blob(key->data, &pub_blob_len);
1029 priv_blob = key->alg->private_blob(key->data, &priv_blob_len);
1030 if (!pub_blob || !priv_blob) {
1037 * Determine encryption details, and encrypt the private blob.
1040 cipherstr = "aes256-cbc";
1046 priv_encrypted_len = priv_blob_len + cipherblk - 1;
1047 priv_encrypted_len -= priv_encrypted_len % cipherblk;
1048 priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
1049 memset(priv_blob_encrypted, 0, priv_encrypted_len);
1050 memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
1051 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1052 * too easy a known-plaintext attack on the last block. */
1053 SHA_Simple(priv_blob, priv_blob_len, priv_mac);
1054 assert(priv_encrypted_len - priv_blob_len < 20);
1055 memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,
1056 priv_encrypted_len - priv_blob_len);
1058 /* Now create the MAC. */
1060 unsigned char *macdata;
1063 int namelen = strlen(key->alg->name);
1064 int enclen = strlen(cipherstr);
1065 int commlen = strlen(key->comment);
1067 unsigned char mackey[20];
1068 char header[] = "putty-private-key-file-mac-key";
1070 maclen = (4 + namelen +
1074 4 + priv_encrypted_len);
1075 macdata = snewn(maclen, unsigned char);
1077 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1078 DO_STR(key->alg->name, namelen);
1079 DO_STR(cipherstr, enclen);
1080 DO_STR(key->comment, commlen);
1081 DO_STR(pub_blob, pub_blob_len);
1082 DO_STR(priv_blob_encrypted, priv_encrypted_len);
1085 SHA_Bytes(&s, header, sizeof(header)-1);
1087 SHA_Bytes(&s, passphrase, strlen(passphrase));
1088 SHA_Final(&s, mackey);
1089 hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);
1090 smemclr(macdata, maclen);
1092 smemclr(mackey, sizeof(mackey));
1093 smemclr(&s, sizeof(s));
1097 unsigned char key[40];
1100 passlen = strlen(passphrase);
1103 SHA_Bytes(&s, "\0\0\0\0", 4);
1104 SHA_Bytes(&s, passphrase, passlen);
1105 SHA_Final(&s, key + 0);
1107 SHA_Bytes(&s, "\0\0\0\1", 4);
1108 SHA_Bytes(&s, passphrase, passlen);
1109 SHA_Final(&s, key + 20);
1110 aes256_encrypt_pubkey(key, priv_blob_encrypted,
1111 priv_encrypted_len);
1113 smemclr(key, sizeof(key));
1114 smemclr(&s, sizeof(s));
1117 fp = f_open(filename, "w", TRUE);
1120 smemclr(priv_blob, priv_blob_len);
1122 smemclr(priv_blob_encrypted, priv_blob_len);
1123 sfree(priv_blob_encrypted);
1126 fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);
1127 fprintf(fp, "Encryption: %s\n", cipherstr);
1128 fprintf(fp, "Comment: %s\n", key->comment);
1129 fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));
1130 base64_encode(fp, pub_blob, pub_blob_len, 64);
1131 fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
1132 base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);
1133 fprintf(fp, "Private-MAC: ");
1134 for (i = 0; i < 20; i++)
1135 fprintf(fp, "%02x", priv_mac[i]);
1140 smemclr(priv_blob, priv_blob_len);
1142 smemclr(priv_blob_encrypted, priv_blob_len);
1143 sfree(priv_blob_encrypted);
1147 /* ----------------------------------------------------------------------
1148 * A function to determine the type of a private key file. Returns
1149 * 0 on failure, 1 or 2 on success.
1151 int key_type(const Filename *filename)
1155 const char putty2_sig[] = "PuTTY-User-Key-File-";
1156 const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1157 const char openssh_sig[] = "-----BEGIN ";
1160 fp = f_open(filename, "r", FALSE);
1162 return SSH_KEYTYPE_UNOPENABLE;
1163 i = fread(buf, 1, sizeof(buf), fp);
1166 return SSH_KEYTYPE_UNOPENABLE;
1168 return SSH_KEYTYPE_UNKNOWN;
1169 if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))
1170 return SSH_KEYTYPE_SSH1;
1171 if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))
1172 return SSH_KEYTYPE_SSH2;
1173 if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))
1174 return SSH_KEYTYPE_OPENSSH;
1175 if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))
1176 return SSH_KEYTYPE_SSHCOM;
1177 return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */
1181 * Convert the type word to a string, for `wrong type' error
1184 char *key_type_to_str(int type)
1187 case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;
1188 case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;
1189 case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;
1190 case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;
1191 case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;
1192 case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;
1193 default: return "INTERNAL ERROR"; break;