int decode_public_key(const char *filename, unsigned char **blob,
size_t *decoded_size);
+int decode_private_key(const char *key_file, unsigned char **result,
+ size_t *blob_size);
+/** Legacy PEM keys (openssh-7.7 and earlier, paraslash.0.6.2 and earlier) */
+#define PKT_PEM (0)
+/** OPENSSH keys (since openssh-7.8, paraslash.0.6.3) */
+#define PKT_OPENSSH (1)
int check_private_key_file(const char *file);
int find_openssh_bignum_offset(const unsigned char *data, int len);
return p - data;
}
+/** Private PEM keys (legacy format) start with this header. */
+#define PRIVATE_PEM_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
+/** Private OPENSSH keys (RFC4716) start with this header. */
+#define PRIVATE_OPENSSH_KEY_HEADER "-----BEGIN OPENSSH PRIVATE KEY-----"
+/** Private PEM keys (legacy format) end with this footer. */
+#define PRIVATE_PEM_KEY_FOOTER "-----END RSA PRIVATE KEY-----"
+/** Private OPENSSH keys (RFC4716) end with this footer. */
+#define PRIVATE_OPENSSH_KEY_FOOTER "-----END OPENSSH PRIVATE KEY-----"
+
+/**
+ * Decode an openssh-v1 (aka RFC4716) or PEM (aka ASN.1) private key.
+ *
+ * \param key_file The private key file (usually id_rsa).
+ * \param result Pointer to base64-decoded blob is returned here.
+ * \param blob_size The size of the decoded blob.
+ *
+ * This only checks header and footer and base64-decodes the part in between.
+ * No attempt to read the decoded part is made.
+ *
+ * \return Negative on errors, PKT_PEM or PKT_OPENSSH on success, indicating
+ * the type of key.
+ */
+int decode_private_key(const char *key_file, unsigned char **result,
+ size_t *blob_size)
+{
+ int ret, ret2, i, j, key_type;
+ void *map;
+ size_t map_size, key_size;
+ unsigned char *blob = NULL;
+ char *begin, *footer, *key;
+
+ ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
+ if (ret < 0)
+ goto out;
+ ret = -E_KEY_MARKER;
+ if (strncmp(map, PRIVATE_PEM_KEY_HEADER,
+ strlen(PRIVATE_PEM_KEY_HEADER)) == 0) {
+ key_type = PKT_PEM;
+ begin = map + strlen(PRIVATE_PEM_KEY_HEADER);
+ footer = strstr(map, PRIVATE_PEM_KEY_FOOTER);
+ PARA_INFO_LOG("detected legacy PEM key %s\n", key_file);
+ } else if (strncmp(map, PRIVATE_OPENSSH_KEY_HEADER,
+ strlen(PRIVATE_OPENSSH_KEY_HEADER)) == 0) {
+ key_type = PKT_OPENSSH;
+ begin = map + strlen(PRIVATE_OPENSSH_KEY_HEADER);
+ footer = strstr(map, PRIVATE_OPENSSH_KEY_FOOTER);
+ PARA_INFO_LOG("detected openssh key %s\n", key_file);
+ } else
+ goto unmap;
+ if (!footer)
+ goto unmap;
+ /* skip whitespace at the beginning */
+ for (; begin < footer; begin++) {
+ if (para_isspace(*begin))
+ continue;
+ break;
+ }
+ ret = -E_KEY_MARKER;
+ if (begin >= footer)
+ goto unmap;
+
+ key_size = footer - begin;
+ key = para_malloc(key_size + 1);
+ for (i = 0, j = 0; begin + i < footer; i++) {
+ if (para_isspace(begin[i]))
+ continue;
+ key[j++] = begin[i];
+ }
+ key[j] = '\0';
+ ret = base64_decode(key, j, (char **)&blob, blob_size);
+ free(key);
+ if (ret < 0)
+ goto unmap;
+ ret = key_type;
+unmap:
+ ret2 = para_munmap(map, map_size);
+ if (ret >= 0 && ret2 < 0)
+ ret = ret2;
+ if (ret < 0) {
+ free(blob);
+ blob = NULL;
+ }
+out:
+ *result = blob;
+ return ret;
+}
+
#include "crypt_backend.h"
#include "fd.h"
#include "base64.h"
-#include "portable_io.h"
//#define GCRYPT_DEBUG 1
return gcry_strerror(gcry_err_code(gret));
}
-/** Private PEM keys (legacy format) start with this header. */
-#define PRIVATE_PEM_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
-/** Private OPENSSH keys (RFC4716) start with this header. */
-#define PRIVATE_OPENSSH_KEY_HEADER "-----BEGIN OPENSSH PRIVATE KEY-----"
-/** Private PEM keys (legacy format) end with this footer. */
-#define PRIVATE_PEM_KEY_FOOTER "-----END RSA PRIVATE KEY-----"
-/** Private OPENSSH keys (RFC4716) end with this footer. */
-#define PRIVATE_OPENSSH_KEY_FOOTER "-----END OPENSSH PRIVATE KEY-----"
-/** Legacy PEM keys (openssh-7.7 and earlier, paraslash.0.6.2 and earlier) */
-#define PKT_PEM (0)
-/** OPENSSH keys (since openssh-7.8, paraslash.0.6.3) */
-#define PKT_OPENSSH (1)
-
-static int decode_private_key(const char *key_file, unsigned char **result,
- size_t *blob_size)
-{
- int ret, ret2, i, j, key_type;
- void *map;
- size_t map_size, key_size;
- unsigned char *blob = NULL;
- char *begin, *footer, *key;
-
- ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
- if (ret < 0)
- goto out;
- ret = -E_KEY_MARKER;
- if (strncmp(map, PRIVATE_PEM_KEY_HEADER,
- strlen(PRIVATE_PEM_KEY_HEADER)) == 0) {
- key_type = PKT_PEM;
- begin = map + strlen(PRIVATE_PEM_KEY_HEADER);
- footer = strstr(map, PRIVATE_PEM_KEY_FOOTER);
- PARA_INFO_LOG("detected legacy PEM key %s\n", key_file);
- } else if (strncmp(map, PRIVATE_OPENSSH_KEY_HEADER,
- strlen(PRIVATE_OPENSSH_KEY_HEADER)) == 0) {
- key_type = PKT_OPENSSH;
- begin = map + strlen(PRIVATE_OPENSSH_KEY_HEADER);
- footer = strstr(map, PRIVATE_OPENSSH_KEY_FOOTER);
- PARA_INFO_LOG("detected openssh key %s\n", key_file);
- } else
- goto unmap;
- if (!footer)
- goto unmap;
- /* skip whitespace at the beginning */
- for (; begin < footer; begin++) {
- if (para_isspace(*begin))
- continue;
- break;
- }
- ret = -E_KEY_MARKER;
- if (begin >= footer)
- goto unmap;
-
- key_size = footer - begin;
- key = para_malloc(key_size + 1);
- for (i = 0, j = 0; begin + i < footer; i++) {
- if (para_isspace(begin[i]))
- continue;
- key[j++] = begin[i];
- }
- key[j] = '\0';
- ret = base64_decode(key, j, (char **)&blob, blob_size);
- free(key);
- if (ret < 0)
- goto unmap;
- ret = key_type;
-unmap:
- ret2 = para_munmap(map, map_size);
- if (ret >= 0 && ret2 < 0)
- ret = ret2;
- if (ret < 0) {
- free(blob);
- blob = NULL;
- }
-out:
- *result = blob;
- return ret;
-}
-
/** ASN Types and their code. */
enum asn1_types {
/** The next object is an integer. */
return ret;
}
-static int get_private_key(const char *path, RSA **rsa)
+static int read_pem_private_key(const char *path, RSA **rsa)
{
EVP_PKEY *pkey;
BIO *bio = BIO_new(BIO_s_file());
return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
}
+static int read_private_rsa_params(const unsigned char *blob,
+ const unsigned char *end, RSA **result)
+{
+ int ret;
+ RSA *rsa;
+ BN_CTX *ctx;
+ BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
+ BIGNUM *dmp1, *dmq1; /* these will be computed */
+ BIGNUM *tmp;
+ const unsigned char *cp = blob;
+
+ rsa = RSA_new();
+ if (!rsa)
+ return -E_BIGNUM;
+ ret = -E_BIGNUM;
+ tmp = BN_new();
+ if (!tmp)
+ goto free_rsa;
+ ctx = BN_CTX_new();
+ if (!ctx)
+ goto free_tmp;
+ dmp1 = BN_new();
+ if (!dmp1)
+ goto free_ctx;
+ dmq1 = BN_new();
+ if (!dmq1)
+ goto free_dmp1;
+ ret = read_bignum(cp, end - cp, &n);
+ if (ret < 0)
+ goto free_dmq1;
+ cp += ret;
+ ret = read_bignum(cp, end - cp, &e);
+ if (ret < 0)
+ goto free_n;
+ cp += ret;
+ ret = read_bignum(cp, end - cp, &d);
+ if (ret < 0)
+ goto free_e;
+ cp += ret;
+ ret = read_bignum(cp, end - cp, &iqmp);
+ if (ret < 0)
+ goto free_d;
+ cp += ret;
+ ret = read_bignum(cp, end - cp, &p);
+ if (ret < 0)
+ goto free_iqmp;
+ cp += ret;
+ ret = read_bignum(cp, end - cp, &q);
+ if (ret < 0)
+ goto free_p;
+ ret = -E_BIGNUM;
+ if (!BN_sub(tmp, q, BN_value_one()))
+ goto free_q;
+ if (!BN_mod(dmp1, d, tmp, ctx))
+ goto free_q;
+ if (!BN_sub(tmp, q, BN_value_one()))
+ goto free_q;
+ if (!BN_mod(dmq1, d, tmp, ctx))
+ goto free_q;
+#ifdef HAVE_RSA_SET0_KEY
+ RSA_set0_key(rsa, n, e, d);
+ RSA_set0_factors(rsa, p, q);
+ RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
+#else
+ rsa->n = n;
+ rsa->e = e;
+ rsa->d = d;
+ rsa->p = p;
+ rsa->q = q;
+ rsa->dmp1 = dmp1;
+ rsa->dmq1 = dmq1;
+ rsa->iqmp = iqmp;
+#endif
+ *result = rsa;
+ ret = 1;
+ goto free_ctx;
+free_q:
+ BN_clear_free(q);
+free_p:
+ BN_clear_free(p);
+free_iqmp:
+ BN_clear_free(iqmp);
+free_d:
+ BN_clear_free(d);
+free_e:
+ BN_free(e);
+free_n:
+ BN_free(n);
+free_dmq1:
+ BN_clear_free(dmq1);
+free_dmp1:
+ BN_clear_free(dmp1);
+free_ctx:
+ BN_CTX_free(ctx);
+free_tmp:
+ BN_clear_free(tmp);
+free_rsa:
+ if (ret < 0)
+ RSA_free(rsa);
+ return ret;
+}
+
+static int get_private_key(const char *path, RSA **rsa)
+{
+ int ret;
+ unsigned char *blob, *end;
+ size_t blob_size;
+
+ *rsa = NULL;
+ ret = decode_private_key(path, &blob, &blob_size);
+ if (ret < 0)
+ return ret;
+ end = blob + blob_size;
+ if (ret == PKT_OPENSSH) {
+ ret = find_openssh_bignum_offset(blob, blob_size);
+ if (ret < 0)
+ goto free_blob;
+ PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
+ ret = read_private_rsa_params(blob + ret, end, rsa);
+ } else
+ ret = read_pem_private_key(path, rsa);
+free_blob:
+ free(blob);
+ return ret;
+}
+
int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
{
unsigned char *blob;