#include "sshcrypt.h" #include #include #include #include #include #include #include #include /* * The OpenSSH private key container, as written by ssh-keygen(1): * * "openssh-key-v1\0" * string ciphername "none" for an unencrypted key * string kdfname "none" or "bcrypt" * string kdfoptions * uint32 number of keys * string public key blob(s) * string private section (possibly encrypted) * * The private section holds two equal check integers, the key type, the key * fields, then padding. We only ever look at an unencrypted container; a * passphrase is removed by ssh-keygen first (keys.c). Parsing it here rather * than asking ssh-keygen for PKCS#8 is what makes this version independent: * OpenSSH 10.2 silently ignores -m PKCS8 for ed25519 keys. */ #define OS_MAGIC "openssh-key-v1" #define OS_MAGIC_LEN 15 /* the trailing NUL belongs to the magic */ #define OS_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----" #define OS_END "-----END OPENSSH PRIVATE KEY-----" static const void *find_bytes(const void *hay, size_t n, const char *needle, size_t nl) { const unsigned char *h = hay; size_t i; if (nl == 0 || n < nl) return NULL; for (i = 0; i + nl <= n; i++) if (memcmp(h + i, needle, nl) == 0) return h + i; return NULL; } static int rd_u32(const unsigned char **p, size_t *left, unsigned long *v) { if (*left < 4) return -1; *v = ((unsigned long)(*p)[0] << 24) | ((unsigned long)(*p)[1] << 16) | ((unsigned long)(*p)[2] << 8) | (unsigned long)(*p)[3]; *p += 4; *left -= 4; return 0; } static int rd_string(const unsigned char **p, size_t *left, const unsigned char **s, size_t *len) { unsigned long l; if (rd_u32(p, left, &l) < 0 || l > *left) return -1; *s = *p; *len = l; *p += l; *left -= l; return 0; } static int decode_body(const unsigned char *text, size_t n, struct buf *out) { const char *begin, *end; size_t skip; begin = find_bytes(text, n, OS_BEGIN, strlen(OS_BEGIN)); if (!begin) return -1; begin += strlen(OS_BEGIN); skip = (size_t)(begin - (const char *)text); end = find_bytes(begin, n - skip, OS_END, strlen(OS_END)); if (!end) return -1; return scr_b64_decode(begin, (size_t)(end - begin), out); } static EVP_PKEY *rsa_from_parts(const unsigned char *nb, size_t nl, const unsigned char *eb, size_t el, const unsigned char *db, size_t dl, const unsigned char *iqb, size_t iql, const unsigned char *pb, size_t pl, const unsigned char *qb, size_t ql) { BN_CTX *ctx = NULL; BIGNUM *n = NULL, *e = NULL, *d = NULL, *iq = NULL, *p = NULL, *q = NULL; BIGNUM *pm1 = NULL, *qm1 = NULL, *dp = NULL, *dq = NULL; OSSL_PARAM_BLD *bld = NULL; OSSL_PARAM *params = NULL; EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; ctx = BN_CTX_new(); n = BN_bin2bn(nb, (int)nl, NULL); e = BN_bin2bn(eb, (int)el, NULL); d = BN_bin2bn(db, (int)dl, NULL); iq = BN_bin2bn(iqb, (int)iql, NULL); p = BN_bin2bn(pb, (int)pl, NULL); q = BN_bin2bn(qb, (int)ql, NULL); pm1 = BN_dup(p); qm1 = BN_dup(q); dp = BN_new(); dq = BN_new(); if (!ctx || !n || !e || !d || !iq || !p || !q || !pm1 || !qm1 || !dp || !dq) goto done; if (!BN_sub_word(pm1, 1) || !BN_sub_word(qm1, 1)) goto done; if (BN_is_zero(pm1) || BN_is_zero(qm1)) goto done; if (!BN_mod(dp, d, pm1, ctx) || !BN_mod(dq, d, qm1, ctx)) goto done; bld = OSSL_PARAM_BLD_new(); if (!bld) goto done; if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1, p) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2, q) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_EXPONENT1, dp) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_EXPONENT2, dq) || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_COEFFICIENT1, iq)) goto done; params = OSSL_PARAM_BLD_to_param(bld); pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL); if (!params || !pctx || EVP_PKEY_fromdata_init(pctx) <= 0) goto done; if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) goto done; done: BN_free(n); BN_free(e); BN_free(d); BN_free(iq); BN_free(p); BN_free(q); BN_free(pm1); BN_free(qm1); BN_free(dp); BN_free(dq); OSSL_PARAM_free(params); OSSL_PARAM_BLD_free(bld); EVP_PKEY_CTX_free(pctx); BN_CTX_free(ctx); return pkey; } static int parse_private(const unsigned char *priv, size_t privlen, const unsigned char *outer, size_t outerlen, struct scr_priv *out) { const unsigned char *p = priv, *type, *op = outer; size_t left = privlen, tl, ol = outerlen; unsigned long c1, c2; if (rd_u32(&p, &left, &c1) < 0 || rd_u32(&p, &left, &c2) < 0) return -1; if (c1 != c2) { fprintf(stderr, "sshcrypt: key check integers differ; the " "container is corrupt\n"); return -1; } if (rd_string(&p, &left, &type, &tl) < 0) return -1; if (tl == 11 && memcmp(type, "ssh-ed25519", 11) == 0) { const unsigned char *pb, *sk, *ot, *ok; size_t pbl, skl, otl, okl; unsigned char h[SHA512_DIGEST_LENGTH]; if (rd_string(&p, &left, &pb, &pbl) < 0 || rd_string(&p, &left, &sk, &skl) < 0) return -1; if (pbl != 32 || skl != 64 || memcmp(sk + 32, pb, 32) != 0) { fprintf(stderr, "sshcrypt: malformed ed25519 key\n"); return -1; } if (rd_string(&op, &ol, &ot, &otl) < 0 || rd_string(&op, &ol, &ok, &okl) < 0 || otl != 11 || memcmp(ot, "ssh-ed25519", 11) != 0 || okl != 32 || memcmp(ok, pb, 32) != 0) { fprintf(stderr, "sshcrypt: the public and private halves " "of this key disagree\n"); return -1; } SHA512(sk, 32, h); memcpy(out->ed_scalar, h, 32); out->ed_scalar[0] &= 248; out->ed_scalar[31] &= 127; out->ed_scalar[31] |= 64; memcpy(out->ed_pub, pb, 32); out->suite = SCR_SUITE_X25519; scr_secure_zero(h, sizeof h); return 0; } if (tl == 7 && memcmp(type, "ssh-rsa", 7) == 0) { const unsigned char *nb, *eb, *db, *iqb, *pb, *qb; size_t nl, el, dl, iql, pl, ql; if (rd_string(&p, &left, &nb, &nl) < 0 || rd_string(&p, &left, &eb, &el) < 0 || rd_string(&p, &left, &db, &dl) < 0 || rd_string(&p, &left, &iqb, &iql) < 0 || rd_string(&p, &left, &pb, &pl) < 0 || rd_string(&p, &left, &qb, &ql) < 0) return -1; out->rsa = rsa_from_parts(nb, nl, eb, el, db, dl, iqb, iql, pb, pl, qb, ql); if (!out->rsa) { fprintf(stderr, "sshcrypt: cannot build the RSA key\n"); return -1; } out->suite = SCR_SUITE_RSA; return 0; } fprintf(stderr, "sshcrypt: unsupported key type in the OpenSSH key " "(need ssh-ed25519 or ssh-rsa)\n"); return -1; } /* 0 = loaded, 1 = the container is encrypted, -1 = malformed */ int scr_openssh_load(const unsigned char *text, size_t n, struct scr_priv *out) { struct buf body = { NULL, 0 }; const unsigned char *p, *cipher, *kdf, *kdfopts, *pub, *priv; size_t left, cl, kl, kol, publ, privl; unsigned long nkeys; int rc = -1; memset(out, 0, sizeof *out); if (decode_body(text, n, &body) < 0) { fprintf(stderr, "sshcrypt: cannot decode the OpenSSH key\n"); return -1; } if (body.len < OS_MAGIC_LEN || memcmp(body.data, OS_MAGIC, OS_MAGIC_LEN) != 0) { fprintf(stderr, "sshcrypt: bad openssh-key-v1 magic\n"); goto done; } p = body.data + OS_MAGIC_LEN; left = body.len - OS_MAGIC_LEN; if (rd_string(&p, &left, &cipher, &cl) < 0 || rd_string(&p, &left, &kdf, &kl) < 0 || rd_string(&p, &left, &kdfopts, &kol) < 0 || rd_u32(&p, &left, &nkeys) < 0 || rd_string(&p, &left, &pub, &publ) < 0 || rd_string(&p, &left, &priv, &privl) < 0) { fprintf(stderr, "sshcrypt: truncated OpenSSH key\n"); goto done; } if (cl != 4 || memcmp(cipher, "none", 4) != 0) { rc = 1; /* encrypted; the caller strips the passphrase */ goto done; } if (nkeys != 1) { fprintf(stderr, "sshcrypt: this file holds %lu keys; only one is " "supported\n", nkeys); goto done; } if (publ == 0 || publ > sizeof out->blob) { fprintf(stderr, "sshcrypt: the public key blob is %zu bytes; the " "limit is %zu\n", publ, sizeof out->blob); goto done; } if (parse_private(priv, privl, pub, publ, out) < 0) goto done; memcpy(out->blob, pub, publ); out->bloblen = publ; scr_fingerprint(out->blob, out->bloblen, out->fingerprint); rc = 0; done: scr_secure_zero(body.data, body.len); free(body.data); if (rc != 0) { if (out->rsa) EVP_PKEY_free((EVP_PKEY *)out->rsa); memset(out, 0, sizeof *out); } return rc; }