+++ /dev/null
-/*\r
- * Generic SSH public-key handling operations. In particular,\r
- * reading of SSH public-key files, and also the generic `sign'\r
- * operation for SSH-2 (which checks the type of the key and\r
- * dispatches to the appropriate key-type specific function).\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <assert.h>\r
-\r
-#include "putty.h"\r
-#include "ssh.h"\r
-#include "misc.h"\r
-\r
-#define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"\r
-\r
-#define BASE64_TOINT(x) ( (x)-'A'<26 ? (x)-'A'+0 :\\r
- (x)-'a'<26 ? (x)-'a'+26 :\\r
- (x)-'0'<10 ? (x)-'0'+52 :\\r
- (x)=='+' ? 62 : \\r
- (x)=='/' ? 63 : 0 )\r
-\r
-static int loadrsakey_main(FILE * fp, struct RSAKey *key, int pub_only,\r
- char **commentptr, char *passphrase,\r
- const char **error)\r
-{\r
- unsigned char buf[16384];\r
- unsigned char keybuf[16];\r
- int len;\r
- int i, j, ciphertype;\r
- int ret = 0;\r
- struct MD5Context md5c;\r
- char *comment;\r
-\r
- *error = NULL;\r
-\r
- /* Slurp the whole file (minus the header) into a buffer. */\r
- len = fread(buf, 1, sizeof(buf), fp);\r
- fclose(fp);\r
- if (len < 0 || len == sizeof(buf)) {\r
- *error = "error reading file";\r
- goto end; /* file too big or not read */\r
- }\r
-\r
- i = 0;\r
- *error = "file format error";\r
-\r
- /*\r
- * A zero byte. (The signature includes a terminating NUL.)\r
- */\r
- if (len - i < 1 || buf[i] != 0)\r
- goto end;\r
- i++;\r
-\r
- /* One byte giving encryption type, and one reserved uint32. */\r
- if (len - i < 1)\r
- goto end;\r
- ciphertype = buf[i];\r
- if (ciphertype != 0 && ciphertype != SSH_CIPHER_3DES)\r
- goto end;\r
- i++;\r
- if (len - i < 4)\r
- goto end; /* reserved field not present */\r
- if (buf[i] != 0 || buf[i + 1] != 0 || buf[i + 2] != 0\r
- || buf[i + 3] != 0) goto end; /* reserved field nonzero, panic! */\r
- i += 4;\r
-\r
- /* Now the serious stuff. An ordinary SSH-1 public key. */\r
- i += makekey(buf + i, len, key, NULL, 1);\r
- if (i < 0)\r
- goto end; /* overran */\r
-\r
- /* Next, the comment field. */\r
- j = GET_32BIT(buf + i);\r
- i += 4;\r
- if (len - i < j)\r
- goto end;\r
- comment = snewn(j + 1, char);\r
- if (comment) {\r
- memcpy(comment, buf + i, j);\r
- comment[j] = '\0';\r
- }\r
- i += j;\r
- if (commentptr)\r
- *commentptr = dupstr(comment);\r
- if (key)\r
- key->comment = comment;\r
- else\r
- sfree(comment);\r
-\r
- if (pub_only) {\r
- ret = 1;\r
- goto end;\r
- }\r
-\r
- if (!key) {\r
- ret = ciphertype != 0;\r
- *error = NULL;\r
- goto end;\r
- }\r
-\r
- /*\r
- * Decrypt remainder of buffer.\r
- */\r
- if (ciphertype) {\r
- MD5Init(&md5c);\r
- MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));\r
- MD5Final(keybuf, &md5c);\r
- des3_decrypt_pubkey(keybuf, buf + i, (len - i + 7) & ~7);\r
- memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */\r
- }\r
-\r
- /*\r
- * We are now in the secret part of the key. The first four\r
- * bytes should be of the form a, b, a, b.\r
- */\r
- if (len - i < 4)\r
- goto end;\r
- if (buf[i] != buf[i + 2] || buf[i + 1] != buf[i + 3]) {\r
- *error = "wrong passphrase";\r
- ret = -1;\r
- goto end;\r
- }\r
- i += 4;\r
-\r
- /*\r
- * After that, we have one further bignum which is our\r
- * decryption exponent, and then the three auxiliary values\r
- * (iqmp, q, p).\r
- */\r
- j = makeprivate(buf + i, len - i, key);\r
- if (j < 0) goto end;\r
- i += j;\r
- j = ssh1_read_bignum(buf + i, len - i, &key->iqmp);\r
- if (j < 0) goto end;\r
- i += j;\r
- j = ssh1_read_bignum(buf + i, len - i, &key->q);\r
- if (j < 0) goto end;\r
- i += j;\r
- j = ssh1_read_bignum(buf + i, len - i, &key->p);\r
- if (j < 0) goto end;\r
- i += j;\r
-\r
- if (!rsa_verify(key)) {\r
- *error = "rsa_verify failed";\r
- freersakey(key);\r
- ret = 0;\r
- } else\r
- ret = 1;\r
-\r
- end:\r
- memset(buf, 0, sizeof(buf)); /* burn the evidence */\r
- return ret;\r
-}\r
-\r
-int loadrsakey(const Filename *filename, struct RSAKey *key, char *passphrase,\r
- const char **errorstr)\r
-{\r
- FILE *fp;\r
- char buf[64];\r
- int ret = 0;\r
- const char *error = NULL;\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp) {\r
- error = "can't open file";\r
- goto end;\r
- }\r
-\r
- /*\r
- * Read the first line of the file and see if it's a v1 private\r
- * key file.\r
- */\r
- if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {\r
- /*\r
- * This routine will take care of calling fclose() for us.\r
- */\r
- ret = loadrsakey_main(fp, key, FALSE, NULL, passphrase, &error);\r
- fp = NULL;\r
- goto end;\r
- }\r
-\r
- /*\r
- * Otherwise, we have nothing. Return empty-handed.\r
- */\r
- error = "not an SSH-1 RSA file";\r
-\r
- end:\r
- if (fp)\r
- fclose(fp);\r
- if ((ret != 1) && errorstr)\r
- *errorstr = error;\r
- return ret;\r
-}\r
-\r
-/*\r
- * See whether an RSA key is encrypted. Return its comment field as\r
- * well.\r
- */\r
-int rsakey_encrypted(const Filename *filename, char **comment)\r
-{\r
- FILE *fp;\r
- char buf[64];\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp)\r
- return 0; /* doesn't even exist */\r
-\r
- /*\r
- * Read the first line of the file and see if it's a v1 private\r
- * key file.\r
- */\r
- if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {\r
- const char *dummy;\r
- /*\r
- * This routine will take care of calling fclose() for us.\r
- */\r
- return loadrsakey_main(fp, NULL, FALSE, comment, NULL, &dummy);\r
- }\r
- fclose(fp);\r
- return 0; /* wasn't the right kind of file */\r
-}\r
-\r
-/*\r
- * Return a malloc'ed chunk of memory containing the public blob of\r
- * an RSA key, as given in the agent protocol (modulus bits,\r
- * exponent, modulus).\r
- */\r
-int rsakey_pubblob(const Filename *filename, void **blob, int *bloblen,\r
- char **commentptr, const char **errorstr)\r
-{\r
- FILE *fp;\r
- char buf[64];\r
- struct RSAKey key;\r
- int ret;\r
- const char *error = NULL;\r
-\r
- /* Default return if we fail. */\r
- *blob = NULL;\r
- *bloblen = 0;\r
- ret = 0;\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp) {\r
- error = "can't open file";\r
- goto end;\r
- }\r
-\r
- /*\r
- * Read the first line of the file and see if it's a v1 private\r
- * key file.\r
- */\r
- if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {\r
- memset(&key, 0, sizeof(key));\r
- if (loadrsakey_main(fp, &key, TRUE, commentptr, NULL, &error)) {\r
- *blob = rsa_public_blob(&key, bloblen);\r
- freersakey(&key);\r
- ret = 1;\r
- fp = NULL;\r
- }\r
- } else {\r
- error = "not an SSH-1 RSA file";\r
- }\r
-\r
- end:\r
- if (fp)\r
- fclose(fp);\r
- if ((ret != 1) && errorstr)\r
- *errorstr = error;\r
- return ret;\r
-}\r
-\r
-/*\r
- * Save an RSA key file. Return nonzero on success.\r
- */\r
-int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase)\r
-{\r
- unsigned char buf[16384];\r
- unsigned char keybuf[16];\r
- struct MD5Context md5c;\r
- unsigned char *p, *estart;\r
- FILE *fp;\r
-\r
- /*\r
- * Write the initial signature.\r
- */\r
- p = buf;\r
- memcpy(p, rsa_signature, sizeof(rsa_signature));\r
- p += sizeof(rsa_signature);\r
-\r
- /*\r
- * One byte giving encryption type, and one reserved (zero)\r
- * uint32.\r
- */\r
- *p++ = (passphrase ? SSH_CIPHER_3DES : 0);\r
- PUT_32BIT(p, 0);\r
- p += 4;\r
-\r
- /*\r
- * An ordinary SSH-1 public key consists of: a uint32\r
- * containing the bit count, then two bignums containing the\r
- * modulus and exponent respectively.\r
- */\r
- PUT_32BIT(p, bignum_bitcount(key->modulus));\r
- p += 4;\r
- p += ssh1_write_bignum(p, key->modulus);\r
- p += ssh1_write_bignum(p, key->exponent);\r
-\r
- /*\r
- * A string containing the comment field.\r
- */\r
- if (key->comment) {\r
- PUT_32BIT(p, strlen(key->comment));\r
- p += 4;\r
- memcpy(p, key->comment, strlen(key->comment));\r
- p += strlen(key->comment);\r
- } else {\r
- PUT_32BIT(p, 0);\r
- p += 4;\r
- }\r
-\r
- /*\r
- * The encrypted portion starts here.\r
- */\r
- estart = p;\r
-\r
- /*\r
- * Two bytes, then the same two bytes repeated.\r
- */\r
- *p++ = random_byte();\r
- *p++ = random_byte();\r
- p[0] = p[-2];\r
- p[1] = p[-1];\r
- p += 2;\r
-\r
- /*\r
- * Four more bignums: the decryption exponent, then iqmp, then\r
- * q, then p.\r
- */\r
- p += ssh1_write_bignum(p, key->private_exponent);\r
- p += ssh1_write_bignum(p, key->iqmp);\r
- p += ssh1_write_bignum(p, key->q);\r
- p += ssh1_write_bignum(p, key->p);\r
-\r
- /*\r
- * Now write zeros until the encrypted portion is a multiple of\r
- * 8 bytes.\r
- */\r
- while ((p - estart) % 8)\r
- *p++ = '\0';\r
-\r
- /*\r
- * Now encrypt the encrypted portion.\r
- */\r
- if (passphrase) {\r
- MD5Init(&md5c);\r
- MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));\r
- MD5Final(keybuf, &md5c);\r
- des3_encrypt_pubkey(keybuf, estart, p - estart);\r
- memset(keybuf, 0, sizeof(keybuf)); /* burn the evidence */\r
- }\r
-\r
- /*\r
- * Done. Write the result to the file.\r
- */\r
- fp = f_open(*filename, "wb", TRUE);\r
- if (fp) {\r
- int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf));\r
- if (fclose(fp))\r
- ret = 0;\r
- return ret;\r
- } else\r
- return 0;\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * SSH-2 private key load/store functions.\r
- */\r
-\r
-/*\r
- * PuTTY's own format for SSH-2 keys is as follows:\r
- *\r
- * The file is text. Lines are terminated by CRLF, although CR-only\r
- * and LF-only are tolerated on input.\r
- *\r
- * The first line says "PuTTY-User-Key-File-2: " plus the name of the\r
- * algorithm ("ssh-dss", "ssh-rsa" etc).\r
- *\r
- * The next line says "Encryption: " plus an encryption type.\r
- * Currently the only supported encryption types are "aes256-cbc"\r
- * and "none".\r
- *\r
- * The next line says "Comment: " plus the comment string.\r
- *\r
- * Next there is a line saying "Public-Lines: " plus a number N.\r
- * The following N lines contain a base64 encoding of the public\r
- * part of the key. This is encoded as the standard SSH-2 public key\r
- * blob (with no initial length): so for RSA, for example, it will\r
- * read\r
- *\r
- * string "ssh-rsa"\r
- * mpint exponent\r
- * mpint modulus\r
- *\r
- * Next, there is a line saying "Private-Lines: " plus a number N,\r
- * and then N lines containing the (potentially encrypted) private\r
- * part of the key. For the key type "ssh-rsa", this will be\r
- * composed of\r
- *\r
- * mpint private_exponent\r
- * mpint p (the larger of the two primes)\r
- * mpint q (the smaller prime)\r
- * mpint iqmp (the inverse of q modulo p)\r
- * data padding (to reach a multiple of the cipher block size)\r
- *\r
- * And for "ssh-dss", it will be composed of\r
- *\r
- * mpint x (the private key parameter)\r
- * [ string hash 20-byte hash of mpints p || q || g only in old format ]\r
- * \r
- * Finally, there is a line saying "Private-MAC: " plus a hex\r
- * representation of a HMAC-SHA-1 of:\r
- *\r
- * string name of algorithm ("ssh-dss", "ssh-rsa")\r
- * string encryption type\r
- * string comment\r
- * string public-blob\r
- * string private-plaintext (the plaintext version of the\r
- * private part, including the final\r
- * padding)\r
- * \r
- * The key to the MAC is itself a SHA-1 hash of:\r
- * \r
- * data "putty-private-key-file-mac-key"\r
- * data passphrase\r
- *\r
- * (An empty passphrase is used for unencrypted keys.)\r
- *\r
- * If the key is encrypted, the encryption key is derived from the\r
- * passphrase by means of a succession of SHA-1 hashes. Each hash\r
- * is the hash of:\r
- *\r
- * uint32 sequence-number\r
- * data passphrase\r
- *\r
- * where the sequence-number increases from zero. As many of these\r
- * hashes are used as necessary.\r
- *\r
- * For backwards compatibility with snapshots between 0.51 and\r
- * 0.52, we also support the older key file format, which begins\r
- * with "PuTTY-User-Key-File-1" (version number differs). In this\r
- * format the Private-MAC: field only covers the private-plaintext\r
- * field and nothing else (and without the 4-byte string length on\r
- * the front too). Moreover, the Private-MAC: field can be replaced\r
- * with a Private-Hash: field which is a plain SHA-1 hash instead of\r
- * an HMAC (this was generated for unencrypted keys).\r
- */\r
-\r
-static int read_header(FILE * fp, char *header)\r
-{\r
- int len = 39;\r
- int c;\r
-\r
- while (len > 0) {\r
- c = fgetc(fp);\r
- if (c == '\n' || c == '\r' || c == EOF)\r
- return 0; /* failure */\r
- if (c == ':') {\r
- c = fgetc(fp);\r
- if (c != ' ')\r
- return 0;\r
- *header = '\0';\r
- return 1; /* success! */\r
- }\r
- if (len == 0)\r
- return 0; /* failure */\r
- *header++ = c;\r
- len--;\r
- }\r
- return 0; /* failure */\r
-}\r
-\r
-static char *read_body(FILE * fp)\r
-{\r
- char *text;\r
- int len;\r
- int size;\r
- int c;\r
-\r
- size = 128;\r
- text = snewn(size, char);\r
- len = 0;\r
- text[len] = '\0';\r
-\r
- while (1) {\r
- c = fgetc(fp);\r
- if (c == '\r' || c == '\n' || c == EOF) {\r
- if (c != EOF) {\r
- c = fgetc(fp);\r
- if (c != '\r' && c != '\n')\r
- ungetc(c, fp);\r
- }\r
- return text;\r
- }\r
- if (len + 1 >= size) {\r
- size += 128;\r
- text = sresize(text, size, char);\r
- }\r
- text[len++] = c;\r
- text[len] = '\0';\r
- }\r
-}\r
-\r
-int base64_decode_atom(char *atom, unsigned char *out)\r
-{\r
- int vals[4];\r
- int i, v, len;\r
- unsigned word;\r
- char c;\r
-\r
- for (i = 0; i < 4; i++) {\r
- c = atom[i];\r
- if (c >= 'A' && c <= 'Z')\r
- v = c - 'A';\r
- else if (c >= 'a' && c <= 'z')\r
- v = c - 'a' + 26;\r
- else if (c >= '0' && c <= '9')\r
- v = c - '0' + 52;\r
- else if (c == '+')\r
- v = 62;\r
- else if (c == '/')\r
- v = 63;\r
- else if (c == '=')\r
- v = -1;\r
- else\r
- return 0; /* invalid atom */\r
- vals[i] = v;\r
- }\r
-\r
- if (vals[0] == -1 || vals[1] == -1)\r
- return 0;\r
- if (vals[2] == -1 && vals[3] != -1)\r
- return 0;\r
-\r
- if (vals[3] != -1)\r
- len = 3;\r
- else if (vals[2] != -1)\r
- len = 2;\r
- else\r
- len = 1;\r
-\r
- word = ((vals[0] << 18) |\r
- (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));\r
- out[0] = (word >> 16) & 0xFF;\r
- if (len > 1)\r
- out[1] = (word >> 8) & 0xFF;\r
- if (len > 2)\r
- out[2] = word & 0xFF;\r
- return len;\r
-}\r
-\r
-static unsigned char *read_blob(FILE * fp, int nlines, int *bloblen)\r
-{\r
- unsigned char *blob;\r
- char *line;\r
- int linelen, len;\r
- int i, j, k;\r
-\r
- /* We expect at most 64 base64 characters, ie 48 real bytes, per line. */\r
- blob = snewn(48 * nlines, unsigned char);\r
- len = 0;\r
- for (i = 0; i < nlines; i++) {\r
- line = read_body(fp);\r
- if (!line) {\r
- sfree(blob);\r
- return NULL;\r
- }\r
- linelen = strlen(line);\r
- if (linelen % 4 != 0 || linelen > 64) {\r
- sfree(blob);\r
- sfree(line);\r
- return NULL;\r
- }\r
- for (j = 0; j < linelen; j += 4) {\r
- k = base64_decode_atom(line + j, blob + len);\r
- if (!k) {\r
- sfree(line);\r
- sfree(blob);\r
- return NULL;\r
- }\r
- len += k;\r
- }\r
- sfree(line);\r
- }\r
- *bloblen = len;\r
- return blob;\r
-}\r
-\r
-/*\r
- * Magic error return value for when the passphrase is wrong.\r
- */\r
-struct ssh2_userkey ssh2_wrong_passphrase = {\r
- NULL, NULL, NULL\r
-};\r
-\r
-const struct ssh_signkey *find_pubkey_alg(const char *name)\r
-{\r
- if (!strcmp(name, "ssh-rsa"))\r
- return &ssh_rsa;\r
- else if (!strcmp(name, "ssh-dss"))\r
- return &ssh_dss;\r
- else\r
- return NULL;\r
-}\r
-\r
-struct ssh2_userkey *ssh2_load_userkey(const Filename *filename,\r
- char *passphrase, const char **errorstr)\r
-{\r
- FILE *fp;\r
- char header[40], *b, *encryption, *comment, *mac;\r
- const struct ssh_signkey *alg;\r
- struct ssh2_userkey *ret;\r
- int cipher, cipherblk;\r
- unsigned char *public_blob, *private_blob;\r
- int public_blob_len, private_blob_len;\r
- int i, is_mac, old_fmt;\r
- int passlen = passphrase ? strlen(passphrase) : 0;\r
- const char *error = NULL;\r
-\r
- ret = NULL; /* return NULL for most errors */\r
- encryption = comment = mac = NULL;\r
- public_blob = private_blob = NULL;\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp) {\r
- error = "can't open file";\r
- goto error;\r
- }\r
-\r
- /* Read the first header line which contains the key type. */\r
- if (!read_header(fp, header))\r
- goto error;\r
- if (0 == strcmp(header, "PuTTY-User-Key-File-2")) {\r
- old_fmt = 0;\r
- } else if (0 == strcmp(header, "PuTTY-User-Key-File-1")) {\r
- /* this is an old key file; warn and then continue */\r
- old_keyfile_warning();\r
- old_fmt = 1;\r
- } else {\r
- error = "not a PuTTY SSH-2 private key";\r
- goto error;\r
- }\r
- error = "file format error";\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- /* Select key algorithm structure. */\r
- alg = find_pubkey_alg(b);\r
- if (!alg) {\r
- sfree(b);\r
- goto error;\r
- }\r
- sfree(b);\r
-\r
- /* Read the Encryption header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))\r
- goto error;\r
- if ((encryption = read_body(fp)) == NULL)\r
- goto error;\r
- if (!strcmp(encryption, "aes256-cbc")) {\r
- cipher = 1;\r
- cipherblk = 16;\r
- } else if (!strcmp(encryption, "none")) {\r
- cipher = 0;\r
- cipherblk = 1;\r
- } else {\r
- sfree(encryption);\r
- goto error;\r
- }\r
-\r
- /* Read the Comment header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))\r
- goto error;\r
- if ((comment = read_body(fp)) == NULL)\r
- goto error;\r
-\r
- /* Read the Public-Lines header line and the public blob. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))\r
- goto error;\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- i = atoi(b);\r
- sfree(b);\r
- if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)\r
- goto error;\r
-\r
- /* Read the Private-Lines header line and the Private blob. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Private-Lines"))\r
- goto error;\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- i = atoi(b);\r
- sfree(b);\r
- if ((private_blob = read_blob(fp, i, &private_blob_len)) == NULL)\r
- goto error;\r
-\r
- /* Read the Private-MAC or Private-Hash header line. */\r
- if (!read_header(fp, header))\r
- goto error;\r
- if (0 == strcmp(header, "Private-MAC")) {\r
- if ((mac = read_body(fp)) == NULL)\r
- goto error;\r
- is_mac = 1;\r
- } else if (0 == strcmp(header, "Private-Hash") && old_fmt) {\r
- if ((mac = read_body(fp)) == NULL)\r
- goto error;\r
- is_mac = 0;\r
- } else\r
- goto error;\r
-\r
- fclose(fp);\r
- fp = NULL;\r
-\r
- /*\r
- * Decrypt the private blob.\r
- */\r
- if (cipher) {\r
- unsigned char key[40];\r
- SHA_State s;\r
-\r
- if (!passphrase)\r
- goto error;\r
- if (private_blob_len % cipherblk)\r
- goto error;\r
-\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, "\0\0\0\0", 4);\r
- SHA_Bytes(&s, passphrase, passlen);\r
- SHA_Final(&s, key + 0);\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, "\0\0\0\1", 4);\r
- SHA_Bytes(&s, passphrase, passlen);\r
- SHA_Final(&s, key + 20);\r
- aes256_decrypt_pubkey(key, private_blob, private_blob_len);\r
- }\r
-\r
- /*\r
- * Verify the MAC.\r
- */\r
- {\r
- char realmac[41];\r
- unsigned char binary[20];\r
- unsigned char *macdata;\r
- int maclen;\r
- int free_macdata;\r
-\r
- if (old_fmt) {\r
- /* MAC (or hash) only covers the private blob. */\r
- macdata = private_blob;\r
- maclen = private_blob_len;\r
- free_macdata = 0;\r
- } else {\r
- unsigned char *p;\r
- int namelen = strlen(alg->name);\r
- int enclen = strlen(encryption);\r
- int commlen = strlen(comment);\r
- maclen = (4 + namelen +\r
- 4 + enclen +\r
- 4 + commlen +\r
- 4 + public_blob_len +\r
- 4 + private_blob_len);\r
- macdata = snewn(maclen, unsigned char);\r
- p = macdata;\r
-#define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)\r
- DO_STR(alg->name, namelen);\r
- DO_STR(encryption, enclen);\r
- DO_STR(comment, commlen);\r
- DO_STR(public_blob, public_blob_len);\r
- DO_STR(private_blob, private_blob_len);\r
-\r
- free_macdata = 1;\r
- }\r
-\r
- if (is_mac) {\r
- SHA_State s;\r
- unsigned char mackey[20];\r
- char header[] = "putty-private-key-file-mac-key";\r
-\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, header, sizeof(header)-1);\r
- if (cipher && passphrase)\r
- SHA_Bytes(&s, passphrase, passlen);\r
- SHA_Final(&s, mackey);\r
-\r
- hmac_sha1_simple(mackey, 20, macdata, maclen, binary);\r
-\r
- memset(mackey, 0, sizeof(mackey));\r
- memset(&s, 0, sizeof(s));\r
- } else {\r
- SHA_Simple(macdata, maclen, binary);\r
- }\r
-\r
- if (free_macdata) {\r
- memset(macdata, 0, maclen);\r
- sfree(macdata);\r
- }\r
-\r
- for (i = 0; i < 20; i++)\r
- sprintf(realmac + 2 * i, "%02x", binary[i]);\r
-\r
- if (strcmp(mac, realmac)) {\r
- /* An incorrect MAC is an unconditional Error if the key is\r
- * unencrypted. Otherwise, it means Wrong Passphrase. */\r
- if (cipher) {\r
- error = "wrong passphrase";\r
- ret = SSH2_WRONG_PASSPHRASE;\r
- } else {\r
- error = "MAC failed";\r
- ret = NULL;\r
- }\r
- goto error;\r
- }\r
- }\r
- sfree(mac);\r
-\r
- /*\r
- * Create and return the key.\r
- */\r
- ret = snew(struct ssh2_userkey);\r
- ret->alg = alg;\r
- ret->comment = comment;\r
- ret->data = alg->createkey(public_blob, public_blob_len,\r
- private_blob, private_blob_len);\r
- if (!ret->data) {\r
- sfree(ret->comment);\r
- sfree(ret);\r
- ret = NULL;\r
- error = "createkey failed";\r
- goto error;\r
- }\r
- sfree(public_blob);\r
- sfree(private_blob);\r
- sfree(encryption);\r
- if (errorstr)\r
- *errorstr = NULL;\r
- return ret;\r
-\r
- /*\r
- * Error processing.\r
- */\r
- error:\r
- if (fp)\r
- fclose(fp);\r
- if (comment)\r
- sfree(comment);\r
- if (encryption)\r
- sfree(encryption);\r
- if (mac)\r
- sfree(mac);\r
- if (public_blob)\r
- sfree(public_blob);\r
- if (private_blob)\r
- sfree(private_blob);\r
- if (errorstr)\r
- *errorstr = error;\r
- return ret;\r
-}\r
-\r
-unsigned char *ssh2_userkey_loadpub(const Filename *filename, char **algorithm,\r
- int *pub_blob_len, char **commentptr,\r
- const char **errorstr)\r
-{\r
- FILE *fp;\r
- char header[40], *b;\r
- const struct ssh_signkey *alg;\r
- unsigned char *public_blob;\r
- int public_blob_len;\r
- int i;\r
- const char *error = NULL;\r
- char *comment;\r
-\r
- public_blob = NULL;\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp) {\r
- error = "can't open file";\r
- goto error;\r
- }\r
-\r
- /* Read the first header line which contains the key type. */\r
- if (!read_header(fp, header)\r
- || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&\r
- 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {\r
- error = "not a PuTTY SSH-2 private key";\r
- goto error;\r
- }\r
- error = "file format error";\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- /* Select key algorithm structure. */\r
- alg = find_pubkey_alg(b);\r
- if (!alg) {\r
- sfree(b);\r
- goto error;\r
- }\r
- sfree(b);\r
-\r
- /* Read the Encryption header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Encryption"))\r
- goto error;\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- sfree(b); /* we don't care */\r
-\r
- /* Read the Comment header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Comment"))\r
- goto error;\r
- if ((comment = read_body(fp)) == NULL)\r
- goto error;\r
-\r
- if (commentptr)\r
- *commentptr = comment;\r
- else\r
- sfree(comment);\r
-\r
- /* Read the Public-Lines header line and the public blob. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Public-Lines"))\r
- goto error;\r
- if ((b = read_body(fp)) == NULL)\r
- goto error;\r
- i = atoi(b);\r
- sfree(b);\r
- if ((public_blob = read_blob(fp, i, &public_blob_len)) == NULL)\r
- goto error;\r
-\r
- fclose(fp);\r
- if (pub_blob_len)\r
- *pub_blob_len = public_blob_len;\r
- if (algorithm)\r
- *algorithm = alg->name;\r
- return public_blob;\r
-\r
- /*\r
- * Error processing.\r
- */\r
- error:\r
- if (fp)\r
- fclose(fp);\r
- if (public_blob)\r
- sfree(public_blob);\r
- if (errorstr)\r
- *errorstr = error;\r
- return NULL;\r
-}\r
-\r
-int ssh2_userkey_encrypted(const Filename *filename, char **commentptr)\r
-{\r
- FILE *fp;\r
- char header[40], *b, *comment;\r
- int ret;\r
-\r
- if (commentptr)\r
- *commentptr = NULL;\r
-\r
- fp = f_open(*filename, "rb", FALSE);\r
- if (!fp)\r
- return 0;\r
- if (!read_header(fp, header)\r
- || (0 != strcmp(header, "PuTTY-User-Key-File-2") &&\r
- 0 != strcmp(header, "PuTTY-User-Key-File-1"))) {\r
- fclose(fp);\r
- return 0;\r
- }\r
- if ((b = read_body(fp)) == NULL) {\r
- fclose(fp);\r
- return 0;\r
- }\r
- sfree(b); /* we don't care about key type here */\r
- /* Read the Encryption header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {\r
- fclose(fp);\r
- return 0;\r
- }\r
- if ((b = read_body(fp)) == NULL) {\r
- fclose(fp);\r
- return 0;\r
- }\r
-\r
- /* Read the Comment header line. */\r
- if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {\r
- fclose(fp);\r
- sfree(b);\r
- return 1;\r
- }\r
- if ((comment = read_body(fp)) == NULL) {\r
- fclose(fp);\r
- sfree(b);\r
- return 1;\r
- }\r
-\r
- if (commentptr)\r
- *commentptr = comment;\r
-\r
- fclose(fp);\r
- if (!strcmp(b, "aes256-cbc"))\r
- ret = 1;\r
- else\r
- ret = 0;\r
- sfree(b);\r
- return ret;\r
-}\r
-\r
-int base64_lines(int datalen)\r
-{\r
- /* When encoding, we use 64 chars/line, which equals 48 real chars. */\r
- return (datalen + 47) / 48;\r
-}\r
-\r
-void base64_encode(FILE * fp, unsigned char *data, int datalen, int cpl)\r
-{\r
- int linelen = 0;\r
- char out[4];\r
- int n, i;\r
-\r
- while (datalen > 0) {\r
- n = (datalen < 3 ? datalen : 3);\r
- base64_encode_atom(data, n, out);\r
- data += n;\r
- datalen -= n;\r
- for (i = 0; i < 4; i++) {\r
- if (linelen >= cpl) {\r
- linelen = 0;\r
- fputc('\n', fp);\r
- }\r
- fputc(out[i], fp);\r
- linelen++;\r
- }\r
- }\r
- fputc('\n', fp);\r
-}\r
-\r
-int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,\r
- char *passphrase)\r
-{\r
- FILE *fp;\r
- unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;\r
- int pub_blob_len, priv_blob_len, priv_encrypted_len;\r
- int passlen;\r
- int cipherblk;\r
- int i;\r
- char *cipherstr;\r
- unsigned char priv_mac[20];\r
-\r
- /*\r
- * Fetch the key component blobs.\r
- */\r
- pub_blob = key->alg->public_blob(key->data, &pub_blob_len);\r
- priv_blob = key->alg->private_blob(key->data, &priv_blob_len);\r
- if (!pub_blob || !priv_blob) {\r
- sfree(pub_blob);\r
- sfree(priv_blob);\r
- return 0;\r
- }\r
-\r
- /*\r
- * Determine encryption details, and encrypt the private blob.\r
- */\r
- if (passphrase) {\r
- cipherstr = "aes256-cbc";\r
- cipherblk = 16;\r
- } else {\r
- cipherstr = "none";\r
- cipherblk = 1;\r
- }\r
- priv_encrypted_len = priv_blob_len + cipherblk - 1;\r
- priv_encrypted_len -= priv_encrypted_len % cipherblk;\r
- priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);\r
- memset(priv_blob_encrypted, 0, priv_encrypted_len);\r
- memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);\r
- /* Create padding based on the SHA hash of the unpadded blob. This prevents\r
- * too easy a known-plaintext attack on the last block. */\r
- SHA_Simple(priv_blob, priv_blob_len, priv_mac);\r
- assert(priv_encrypted_len - priv_blob_len < 20);\r
- memcpy(priv_blob_encrypted + priv_blob_len, priv_mac,\r
- priv_encrypted_len - priv_blob_len);\r
-\r
- /* Now create the MAC. */\r
- {\r
- unsigned char *macdata;\r
- int maclen;\r
- unsigned char *p;\r
- int namelen = strlen(key->alg->name);\r
- int enclen = strlen(cipherstr);\r
- int commlen = strlen(key->comment);\r
- SHA_State s;\r
- unsigned char mackey[20];\r
- char header[] = "putty-private-key-file-mac-key";\r
-\r
- maclen = (4 + namelen +\r
- 4 + enclen +\r
- 4 + commlen +\r
- 4 + pub_blob_len +\r
- 4 + priv_encrypted_len);\r
- macdata = snewn(maclen, unsigned char);\r
- p = macdata;\r
-#define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)\r
- DO_STR(key->alg->name, namelen);\r
- DO_STR(cipherstr, enclen);\r
- DO_STR(key->comment, commlen);\r
- DO_STR(pub_blob, pub_blob_len);\r
- DO_STR(priv_blob_encrypted, priv_encrypted_len);\r
-\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, header, sizeof(header)-1);\r
- if (passphrase)\r
- SHA_Bytes(&s, passphrase, strlen(passphrase));\r
- SHA_Final(&s, mackey);\r
- hmac_sha1_simple(mackey, 20, macdata, maclen, priv_mac);\r
- memset(macdata, 0, maclen);\r
- sfree(macdata);\r
- memset(mackey, 0, sizeof(mackey));\r
- memset(&s, 0, sizeof(s));\r
- }\r
-\r
- if (passphrase) {\r
- unsigned char key[40];\r
- SHA_State s;\r
-\r
- passlen = strlen(passphrase);\r
-\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, "\0\0\0\0", 4);\r
- SHA_Bytes(&s, passphrase, passlen);\r
- SHA_Final(&s, key + 0);\r
- SHA_Init(&s);\r
- SHA_Bytes(&s, "\0\0\0\1", 4);\r
- SHA_Bytes(&s, passphrase, passlen);\r
- SHA_Final(&s, key + 20);\r
- aes256_encrypt_pubkey(key, priv_blob_encrypted,\r
- priv_encrypted_len);\r
-\r
- memset(key, 0, sizeof(key));\r
- memset(&s, 0, sizeof(s));\r
- }\r
-\r
- fp = f_open(*filename, "w", TRUE);\r
- if (!fp)\r
- return 0;\r
- fprintf(fp, "PuTTY-User-Key-File-2: %s\n", key->alg->name);\r
- fprintf(fp, "Encryption: %s\n", cipherstr);\r
- fprintf(fp, "Comment: %s\n", key->comment);\r
- fprintf(fp, "Public-Lines: %d\n", base64_lines(pub_blob_len));\r
- base64_encode(fp, pub_blob, pub_blob_len, 64);\r
- fprintf(fp, "Private-Lines: %d\n", base64_lines(priv_encrypted_len));\r
- base64_encode(fp, priv_blob_encrypted, priv_encrypted_len, 64);\r
- fprintf(fp, "Private-MAC: ");\r
- for (i = 0; i < 20; i++)\r
- fprintf(fp, "%02x", priv_mac[i]);\r
- fprintf(fp, "\n");\r
- fclose(fp);\r
-\r
- sfree(pub_blob);\r
- memset(priv_blob, 0, priv_blob_len);\r
- sfree(priv_blob);\r
- sfree(priv_blob_encrypted);\r
- return 1;\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * A function to determine the type of a private key file. Returns\r
- * 0 on failure, 1 or 2 on success.\r
- */\r
-int key_type(const Filename *filename)\r
-{\r
- FILE *fp;\r
- char buf[32];\r
- const char putty2_sig[] = "PuTTY-User-Key-File-";\r
- const char sshcom_sig[] = "---- BEGIN SSH2 ENCRYPTED PRIVAT";\r
- const char openssh_sig[] = "-----BEGIN ";\r
- int i;\r
-\r
- fp = f_open(*filename, "r", FALSE);\r
- if (!fp)\r
- return SSH_KEYTYPE_UNOPENABLE;\r
- i = fread(buf, 1, sizeof(buf), fp);\r
- fclose(fp);\r
- if (i < 0)\r
- return SSH_KEYTYPE_UNOPENABLE;\r
- if (i < 32)\r
- return SSH_KEYTYPE_UNKNOWN;\r
- if (!memcmp(buf, rsa_signature, sizeof(rsa_signature)-1))\r
- return SSH_KEYTYPE_SSH1;\r
- if (!memcmp(buf, putty2_sig, sizeof(putty2_sig)-1))\r
- return SSH_KEYTYPE_SSH2;\r
- if (!memcmp(buf, openssh_sig, sizeof(openssh_sig)-1))\r
- return SSH_KEYTYPE_OPENSSH;\r
- if (!memcmp(buf, sshcom_sig, sizeof(sshcom_sig)-1))\r
- return SSH_KEYTYPE_SSHCOM;\r
- return SSH_KEYTYPE_UNKNOWN; /* unrecognised or EOF */\r
-}\r
-\r
-/*\r
- * Convert the type word to a string, for `wrong type' error\r
- * messages.\r
- */\r
-char *key_type_to_str(int type)\r
-{\r
- switch (type) {\r
- case SSH_KEYTYPE_UNOPENABLE: return "unable to open file"; break;\r
- case SSH_KEYTYPE_UNKNOWN: return "not a private key"; break;\r
- case SSH_KEYTYPE_SSH1: return "SSH-1 private key"; break;\r
- case SSH_KEYTYPE_SSH2: return "PuTTY SSH-2 private key"; break;\r
- case SSH_KEYTYPE_OPENSSH: return "OpenSSH SSH-2 private key"; break;\r
- case SSH_KEYTYPE_SSHCOM: return "ssh.com SSH-2 private key"; break;\r
- default: return "INTERNAL ERROR"; break;\r
- }\r
-}\r