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 i += makekey(buf + i, len, key, NULL, 1);
72 goto end; /* overran */
79 /* Next, the comment field. */
80 j = GET_32BIT(buf + i);
84 comment = snewn(j + 1, char);
86 memcpy(comment, buf + i, j);
91 *commentptr = comment;
93 key->comment = comment;
95 ret = ciphertype != 0;
101 * Decrypt remainder of buffer.
105 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
106 MD5Final(keybuf, &md5c);
107 des3_decrypt_pubkey(keybuf, buf + i, (len - i + 7) & ~7);
108 memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */
112 * We are now in the secret part of the key. The first four
113 * bytes should be of the form a, b, a, b.
117 if (buf[i] != buf[i + 2] || buf[i + 1] != buf[i + 3]) {
118 *error = "wrong passphrase";
125 * After that, we have one further bignum which is our
126 * decryption exponent, and then the three auxiliary values
129 j = makeprivate(buf + i, len - i, key);
132 j = ssh1_read_bignum(buf + i, len - i, &key->iqmp);
135 j = ssh1_read_bignum(buf + i, len - i, &key->q);
138 j = ssh1_read_bignum(buf + i, len - i, &key->p);
142 if (!rsa_verify(key)) {
143 *error = "rsa_verify failed";
150 memset(buf, 0, sizeof(buf)); /* burn the evidence */
154 int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,
155 const char **errorstr)
160 const char *error = NULL;
162 fp = f_open(*filename, "rb");
164 error = "can't open file";
169 * Read the first line of the file and see if it's a v1 private
172 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
174 * This routine will take care of calling fclose() for us.
176 ret = loadrsakey_main(fp, key, FALSE, NULL, passphrase, &error);
182 * Otherwise, we have nothing. Return empty-handed.
184 error = "not an SSH-1 RSA file";
189 if ((ret != 1) && errorstr)
195 * See whether an RSA key is encrypted. Return its comment field as
198 int rsakey_encrypted(const Filename *filename, char **comment)
203 fp = f_open(*filename, "rb");
205 return 0; /* doesn't even exist */
208 * Read the first line of the file and see if it's a v1 private
211 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
214 * This routine will take care of calling fclose() for us.
216 return loadrsakey_main(fp, NULL, FALSE, comment, NULL, &dummy);
219 return 0; /* wasn't the right kind of file */
223 * Return a malloc'ed chunk of memory containing the public blob of
224 * an RSA key, as given in the agent protocol (modulus bits,
225 * exponent, modulus).
227 int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,
228 const char **errorstr)
234 const char *error = NULL;
236 /* Default return if we fail. */
241 fp = f_open(*filename, "rb");
243 error = "can't open file";
248 * Read the first line of the file and see if it's a v1 private
251 if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
252 memset(&key, 0, sizeof(key));
253 if (loadrsakey_main(fp, &key, TRUE, NULL, NULL, &error)) {
254 *blob = rsa_public_blob(&key, bloblen);
260 error = "not an SSH-1 RSA file";
266 if ((ret != 1) && errorstr)
272 * Save an RSA key file. Return nonzero on success.
274 int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)
276 unsigned char buf[16384];
277 unsigned char keybuf[16];
278 struct MD5Context md5c;
279 unsigned char *p, *estart;
283 * Write the initial signature.
286 memcpy(p, rsa_signature, sizeof(rsa_signature));
287 p += sizeof(rsa_signature);
290 * One byte giving encryption type, and one reserved (zero)
293 *p++ = (passphrase ? SSH_CIPHER_3DES : 0);
298 * An ordinary SSH-1 public key consists of: a uint32
299 * containing the bit count, then two bignums containing the
300 * modulus and exponent respectively.
302 PUT_32BIT(p, bignum_bitcount(key->modulus));
304 p += ssh1_write_bignum(p, key->modulus);
305 p += ssh1_write_bignum(p, key->exponent);
308 * A string containing the comment field.
311 PUT_32BIT(p, strlen(key->comment));
313 memcpy(p, key->comment, strlen(key->comment));
314 p += strlen(key->comment);
321 * The encrypted portion starts here.
326 * Two bytes, then the same two bytes repeated.
328 *p++ = random_byte();
329 *p++ = random_byte();
335 * Four more bignums: the decryption exponent, then iqmp, then
338 p += ssh1_write_bignum(p, key->private_exponent);
339 p += ssh1_write_bignum(p, key->iqmp);
340 p += ssh1_write_bignum(p, key->q);
341 p += ssh1_write_bignum(p, key->p);
344 * Now write zeros until the encrypted portion is a multiple of
347 while ((p - estart) % 8)
351 * Now encrypt the encrypted portion.
355 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
356 MD5Final(keybuf, &md5c);
357 des3_encrypt_pubkey(keybuf, estart, p - estart);
358 memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */
362 * Done. Write the result to the file.
364 fp = f_open(*filename, "wb");
366 int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));
374 /* ----------------------------------------------------------------------
375 * SSH-2 private key load/store functions.
379 * PuTTY's own format for SSH-2 keys is as follows:
381 * The file is text. Lines are terminated by CRLF, although CR-only
382 * and LF-only are tolerated on input.
384 * The first line says "PuTTY-User-Key-File-2: " plus the name of the
385 * algorithm ("ssh-dss", "ssh-rsa" etc).
387 * The next line says "Encryption: " plus an encryption type.
388 * Currently the only supported encryption types are "aes256-cbc"
391 * The next line says "Comment: " plus the comment string.
393 * Next there is a line saying "Public-Lines: " plus a number N.
394 * The following N lines contain a base64 encoding of the public
395 * part of the key. This is encoded as the standard SSH-2 public key
396 * blob (with no initial length): so for RSA, for example, it will
403 * Next, there is a line saying "Private-Lines: " plus a number N,
404 * and then N lines containing the (potentially encrypted) private
405 * part of the key. For the key type "ssh-rsa", this will be
408 * mpint private_exponent
409 * mpint p (the larger of the two primes)
410 * mpint q (the smaller prime)
411 * mpint iqmp (the inverse of q modulo p)
412 * data padding (to reach a multiple of the cipher block size)
414 * And for "ssh-dss", it will be composed of
416 * mpint x (the private key parameter)
417 * [ string hash 20-byte hash of mpints p || q || g only in old format ]
419 * Finally, there is a line saying "Private-MAC: " plus a hex
420 * representation of a HMAC-SHA-1 of:
422 * string name of algorithm ("ssh-dss", "ssh-rsa")
423 * string encryption type
426 * string private-plaintext (the plaintext version of the
427 * private part, including the final
430 * The key to the MAC is itself a SHA-1 hash of:
432 * data "putty-private-key-file-mac-key"
435 * (An empty passphrase is used for unencrypted keys.)
437 * If the key is encrypted, the encryption key is derived from the
438 * passphrase by means of a succession of SHA-1 hashes. Each hash
441 * uint32 sequence-number
444 * where the sequence-number increases from zero. As many of these
445 * hashes are used as necessary.
447 * For backwards compatibility with snapshots between 0.51 and
448 * 0.52, we also support the older key file format, which begins
449 * with "PuTTY-User-Key-File-1" (version number differs). In this
450 * format the Private-MAC: field only covers the private-plaintext
451 * field and nothing else (and without the 4-byte string length on
452 * the front too). Moreover, the Private-MAC: field can be replaced
453 * with a Private-Hash: field which is a plain SHA-1 hash instead of
454 * an HMAC (this was generated for unencrypted keys).
457 static int read_header(FILE * fp, char *header)
464 if (c == '\n' || c == '\r' || c == EOF)
465 return 0; /* failure */
471 return 1; /* success! */
474 return 0; /* failure */
478 return 0; /* failure */
481 static char *read_body(FILE * fp)
489 text = snewn(size, char);
495 if (c == '\r' || c == '\n') {
497 if (c != '\r' && c != '\n' && c != EOF)
505 if (len + 1 >= size) {
507 text = sresize(text, size, char);
514 int base64_decode_atom(char *atom, unsigned char *out)
521 for (i = 0; i < 4; i++) {
523 if (c >= 'A' && c <= 'Z')
525 else if (c >= 'a' && c <= 'z')
527 else if (c >= '0' && c <= '9')
536 return 0; /* invalid atom */
540 if (vals[0] == -1 || vals[1] == -1)
542 if (vals[2] == -1 && vals[3] != -1)
547 else if (vals[2] != -1)
552 word = ((vals[0] << 18) |
553 (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
554 out[0] = (word >> 16) & 0xFF;
556 out[1] = (word >> 8) & 0xFF;
558 out[2] = word & 0xFF;
562 static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)
569 /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
570 blob = snewn(48 * nlines, unsigned char);
572 for (i = 0; i < nlines; i++) {
573 line = read_body(fp);
578 linelen = strlen(line);
579 if (linelen % 4 != 0 || linelen > 64) {
584 for (j = 0; j < linelen; j += 4) {
585 k = base64_decode_atom(line + j, blob + len);
600 * Magic error return value for when the passphrase is wrong.
602 struct ssh2_userkey ssh2_wrong_passphrase = {
606 const struct ssh_signkey *find_pubkey_alg(const char *name)
608 if (!strcmp(name, "ssh-rsa"))
610 else if (!strcmp(name, "ssh-dss"))
616 struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,
617 char *passphrase, const char **errorstr)
620 char header[40], *b, *encryption, *comment, *mac;
621 const struct ssh_signkey *alg;
622 struct ssh2_userkey *ret;
623 int cipher, cipherblk;
624 unsigned char *public_blob, *private_blob;
625 int public_blob_len, private_blob_len;
626 int i, is_mac, old_fmt;
627 int passlen = passphrase ? strlen(passphrase) : 0;
628 const char *error = NULL;
630 ret = NULL; /* return NULL for most errors */
631 encryption = comment = mac = NULL;
632 public_blob = private_blob = NULL;
634 fp = f_open(*filename, "rb");
636 error = "can't open file";
640 /* Read the first header line which contains the key type. */
641 if (!read_header(fp, header))
643 if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {
645 } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {
646 /* this is an old key file; warn and then continue */
647 old_keyfile_warning();
650 error = "not a PuTTY SSH-2 private key";
653 error = "file format error";
654 if ((b = read_body(fp)) == NULL)
656 /* Select key algorithm structure. */
657 alg = find_pubkey_alg(b);
664 /* Read the Encryption header line. */
665 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
667 if ((encryption = read_body(fp)) == NULL)
669 if (!strcmp(encryption, "aes256-cbc")) {
672 } else if (!strcmp(encryption, "none")) {
680 /* Read the Comment header line. */
681 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
683 if ((comment = read_body(fp)) == NULL)
686 /* Read the Public-Lines header line and the public blob. */
687 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
689 if ((b = read_body(fp)) == NULL)
693 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
696 /* Read the Private-Lines header line and the Private blob. */
697 if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))
699 if ((b = read_body(fp)) == NULL)
703 if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)
706 /* Read the Private-MAC or Private-Hash header line. */
707 if (!read_header(fp, header))
709 if (0 == strcmp(header, "Private-MAC")) {
710 if ((mac = read_body(fp)) == NULL)
713 } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {
714 if ((mac = read_body(fp)) == NULL)
724 * Decrypt the private blob.
727 unsigned char key[40];
732 if (private_blob_len % cipherblk)
736 SHA_Bytes(&s, "\0\0\0\0", 4);
737 SHA_Bytes(&s, passphrase, passlen);
738 SHA_Final(&s, key + 0);
740 SHA_Bytes(&s, "\0\0\0\1", 4);
741 SHA_Bytes(&s, passphrase, passlen);
742 SHA_Final(&s, key + 20);
743 aes256_decrypt_pubkey(key, private_blob, private_blob_len);
751 unsigned char binary[20];
752 unsigned char *macdata;
757 /* MAC (or hash) only covers the private blob. */
758 macdata = private_blob;
759 maclen = private_blob_len;
763 int namelen = strlen(alg->name);
764 int enclen = strlen(encryption);
765 int commlen = strlen(comment);
766 maclen = (4 + namelen +
769 4 + public_blob_len +
770 4 + private_blob_len);
771 macdata = snewn(maclen, unsigned char);
773 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
774 DO_STR(alg->name, namelen);
775 DO_STR(encryption, enclen);
776 DO_STR(comment, commlen);
777 DO_STR(public_blob, public_blob_len);
778 DO_STR(private_blob, private_blob_len);
785 unsigned char mackey[20];
786 char header[] = "putty-private-key-file-mac-key";
789 SHA_Bytes(&s, header, sizeof(header)-1);
790 if (cipher && passphrase)
791 SHA_Bytes(&s, passphrase, passlen);
792 SHA_Final(&s, mackey);
794 hmac_sha1_simple(mackey, 20, macdata, maclen, binary);
796 memset(mackey, 0, sizeof(mackey));
797 memset(&s, 0, sizeof(s));
799 SHA_Simple(macdata, maclen, binary);
803 memset(macdata, 0, maclen);
807 for (i = 0; i < 20; i++)
808 sprintf(realmac + 2 * i, "%02x", binary[i]);
810 if (strcmp(mac, realmac)) {
811 /* An incorrect MAC is an unconditional Error if the key is
812 * unencrypted. Otherwise, it means Wrong Passphrase. */
814 error = "wrong passphrase";
815 ret = SSH2_WRONG_PASSPHRASE;
817 error = "MAC failed";
826 * Create and return the key.
828 ret = snew(struct ssh2_userkey);
830 ret->comment = comment;
831 ret->data = alg->createkey(public_blob, public_blob_len,
832 private_blob, private_blob_len);
837 error = "createkey failed";
868 char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,
869 int *pub_blob_len, const char **errorstr)
873 const struct ssh_signkey *alg;
874 unsigned char *public_blob;
877 const char *error = NULL;
881 fp = f_open(*filename, "rb");
883 error = "can't open file";
887 /* Read the first header line which contains the key type. */
888 if (!read_header(fp, header)
889 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
890 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
891 error = "not a PuTTY SSH-2 private key";
894 error = "file format error";
895 if ((b = read_body(fp)) == NULL)
897 /* Select key algorithm structure. */
898 alg = find_pubkey_alg(b);
905 /* Read the Encryption header line. */
906 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))
908 if ((b = read_body(fp)) == NULL)
910 sfree(b); /* we don't care */
912 /* Read the Comment header line. */
913 if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))
915 if ((b = read_body(fp)) == NULL)
917 sfree(b); /* we don't care */
919 /* Read the Public-Lines header line and the public blob. */
920 if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))
922 if ((b = read_body(fp)) == NULL)
926 if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)
931 *pub_blob_len = public_blob_len;
933 *algorithm = alg->name;
934 return (char *)public_blob;
949 int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)
952 char header[40], *b, *comment;
958 fp = f_open(*filename, "rb");
961 if (!read_header(fp, header)
962 || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&
963 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {
967 if ((b = read_body(fp)) == NULL) {
971 sfree(b); /* we don't care about key type here */
972 /* Read the Encryption header line. */
973 if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
977 if ((b = read_body(fp)) == NULL) {
982 /* Read the Comment header line. */
983 if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
988 if ((comment = read_body(fp)) == NULL) {
995 *commentptr = comment;
998 if (!strcmp(b, "aes256-cbc"))
1006 int base64_lines(int datalen)
1008 /* When encoding, we use 64 chars/line, which equals 48 real chars. */
1009 return (datalen + 47) / 48;
1012 void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)
1018 while (datalen > 0) {
1019 n = (datalen < 3 ? datalen : 3);
1020 base64_encode_atom(data, n, out);
1023 for (i = 0; i < 4; i++) {
1024 if (linelen >= cpl) {
1035 int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
1039 unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
1040 int pub_blob_len, priv_blob_len, priv_encrypted_len;
1045 unsigned char priv_mac[20];
1048 * Fetch the key component blobs.
1050 pub_blob = key->alg->public_blob(key->data, &pub_blob_len);
1051 priv_blob = key->alg->private_blob(key->data, &priv_blob_len);
1052 if (!pub_blob || !priv_blob) {
1059 * Determine encryption details, and encrypt the private blob.
1062 cipherstr = "aes256-cbc";
1068 priv_encrypted_len = priv_blob_len + cipherblk - 1;
1069 priv_encrypted_len -= priv_encrypted_len % cipherblk;
1070 priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
1071 memset(priv_blob_encrypted, 0, priv_encrypted_len);
1072 memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
1073 /* Create padding based on the SHA hash of the unpadded blob. This prevents
1074 * too easy a known-plaintext attack on the last block. */
1075 SHA_Simple(priv_blob, priv_blob_len, priv_mac);
1076 assert(priv_encrypted_len - priv_blob_len < 20);
1077 memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,
1078 priv_encrypted_len - priv_blob_len);
1080 /* Now create the MAC. */
1082 unsigned char *macdata;
1085 int namelen = strlen(key->alg->name);
1086 int enclen = strlen(cipherstr);
1087 int commlen = strlen(key->comment);
1089 unsigned char mackey[20];
1090 char header[] = "putty-private-key-file-mac-key";
1092 maclen = (4 + namelen +
1096 4 + priv_encrypted_len);
1097 macdata = snewn(maclen, unsigned char);
1099 #define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
1100 DO_STR(key->alg->name, namelen);
1101 DO_STR(cipherstr, enclen);
1102 DO_STR(key->comment, commlen);
1103 DO_STR(pub_blob, pub_blob_len);
1104 DO_STR(priv_blob_encrypted, priv_encrypted_len);
1107 SHA_Bytes(&s, header, sizeof(header)-1);
1109 SHA_Bytes(&s, passphrase, strlen(passphrase));
1110 SHA_Final(&s, mackey);
1111 hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);
1112 memset(macdata, 0, maclen);
1114 memset(mackey, 0, sizeof(mackey));
1115 memset(&s, 0, sizeof(s));
1119 unsigned char key[40];
1122 passlen = strlen(passphrase);
1125 SHA_Bytes(&s, "\0\0\0\0", 4);
1126 SHA_Bytes(&s, passphrase, passlen);
1127 SHA_Final(&s, key + 0);
1129 SHA_Bytes(&s, "\0\0\0\1", 4);
1130 SHA_Bytes(&s, passphrase, passlen);
1131 SHA_Final(&s, key + 20);
1132 aes256_encrypt_pubkey(key, priv_blob_encrypted,
1133 priv_encrypted_len);
1135 memset(key, 0, sizeof(key));
1136 memset(&s, 0, sizeof(s));
1139 fp = f_open(*filename, "w");
1142 fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);
1143 fprintf(fp, "Encryption: %s\n", cipherstr);
1144 fprintf(fp, "Comment: %s\n", key->comment);
1145 fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));
1146 base64_encode(fp, pub_blob, pub_blob_len, 64);
1147 fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));
1148 base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);
1149 fprintf(fp, "Private-MAC: ");
1150 for (i = 0; i < 20; i++)
1151 fprintf(fp, "%02x", priv_mac[i]);
1156 memset(priv_blob, 0, priv_blob_len);
1158 sfree(priv_blob_encrypted);
1162 /* ----------------------------------------------------------------------
1163 * A function to determine the type of a private key file. Returns
1164 * 0 on failure, 1 or 2 on success.
1166 int key_type(const Filename *filename)
1170 const char putty2_sig[] = "PuTTY-User-Key-File-";
1171 const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";
1172 const char openssh_sig[] = "-----BEGIN ";
1175 fp = f_open(*filename, "r");
1177 return SSH_KEYTYPE_UNOPENABLE;
1178 i = fread(buf, 1, sizeof(buf), fp);
1181 return SSH_KEYTYPE_UNOPENABLE;
1183 return SSH_KEYTYPE_UNKNOWN;
1184 if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))
1185 return SSH_KEYTYPE_SSH1;
1186 if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))
1187 return SSH_KEYTYPE_SSH2;
1188 if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))
1189 return SSH_KEYTYPE_OPENSSH;
1190 if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))
1191 return SSH_KEYTYPE_SSHCOM;
1192 return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */
1196 * Convert the type word to a string, for `wrong type' error
1199 char *key_type_to_str(int type)
1202 case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;
1203 case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;
1204 case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;
1205 case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;
1206 case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;
1207 case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;
1208 default: return "INTERNAL ERROR"; break;