read the OpenSSH key container ourselves
ssh-keygen -p -m PKCS8 is not a conversion path: on OpenSSH 10.2 (Ubuntu 26.04 LTS) it ignores -m for ed25519 keys, exits 0, and leaves the container, so every ed25519 decryption failed there. RSA still converted, which hid it. src/openssh.c now parses the unencrypted openssh-key-v1 container directly: ed25519 seed plus public key, or RSA n/e/d/iqmp/p/q, with the public and private halves checked against each other. ssh-keygen is left with one job, stripping a passphrase (-p -N ''), which works on every version. Unencrypted keys no longer need the program at all, which the suite now asserts by running one decryption with PATH=/nonexistent. Verified on Debian bookworm (OpenSSH 9.2) and Ubuntu 26.04 (10.2).
This commit is contained in:
1 parent
05b8251333
commit
ee477779d0
8 files changed
+371
-50
No files matched your search
@@ -11,7 +11,7 @@ those is a design decision, not a commit.
|
||||
make check # tests/run.sh, generates keys in a tmpdir, no network
|
||||
make install PREFIX=$HOME/.local # installs bin/sshcrypt
|
||||
|
||||
`make check` is the gate: 23 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
||||
`make check` is the gate: 25 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
||||
`python3` (tamper test), `script(1)` (pty cases) and `pgrep` (SIGINT case);
|
||||
those last ones skip themselves if the tool is missing. No network. After
|
||||
touching parsing or the envelope, also run
|
||||
@@ -25,7 +25,9 @@ you pass; do not remove that `override`.
|
||||
|
||||
* `src/main.c` — CLI and mode dispatch, no crypto.
|
||||
* `src/keys.c` — SSH wire parsing, public/private key loading, the ssh-keygen
|
||||
conversion, Ed25519→X25519.
|
||||
passphrase strip, Ed25519→X25519.
|
||||
* `src/openssh.c` — reads the unencrypted `openssh-key-v1` container, so
|
||||
Ed25519 keys work on ssh-keygen versions that will not export PKCS#8.
|
||||
* `src/envelope.c` — KEM, AEAD, the wire format.
|
||||
* `src/armor.c` — base64 armor.
|
||||
* `src/util.c` — buffers, base64, zeroing.
|
||||
@@ -40,10 +42,11 @@ you pass; do not remove that `override`.
|
||||
* `EVP_PKEY_decrypt` needs an output buffer at least as large as the RSA
|
||||
modulus, even when the plaintext is 32 bytes. A 32-byte buffer fails with
|
||||
`bad length` (providers/implementations/asymciphers/rsa_enc.c).
|
||||
* `ssh-keygen -p -m PKCS8` only writes PKCS#8 when the **new** passphrase is
|
||||
empty (`-N ''`). With a non-empty `-N` it silently keeps the OpenSSH
|
||||
container. That is why the conversion always writes an unencrypted key into
|
||||
the 0600 tmpfs file and unlinks it at once.
|
||||
* `ssh-keygen -p -m PKCS8` is not a conversion path: on OpenSSH 10.2 (Ubuntu
|
||||
26.04 LTS) it silently ignores `-m` for ed25519 keys, exits 0, and leaves the
|
||||
OpenSSH container. RSA still converts, which is what made this look fine.
|
||||
`src/openssh.c` therefore parses the container itself. ssh-keygen is only
|
||||
used to strip a passphrase (`-p -N ''`), which every version does.
|
||||
* libcrypto cannot read `-----BEGIN OPENSSH PRIVATE KEY-----` at all: no
|
||||
openssh-key-v1, no bcrypt_pbkdf. Do not feed those bytes to
|
||||
`PEM_read_PrivateKey`.
|
||||
|
||||
@@ -9,7 +9,7 @@ override CFLAGS += -std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Wpedantic
|
||||
CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libcrypto 2>/dev/null)
|
||||
LDLIBS += $(shell $(PKG_CONFIG) --libs libcrypto 2>/dev/null || echo -lcrypto)
|
||||
|
||||
SRC = src/main.c src/util.c src/armor.c src/keys.c src/envelope.c
|
||||
SRC = src/main.c src/util.c src/armor.c src/keys.c src/openssh.c src/envelope.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: sshcrypt
|
||||
|
||||
@@ -45,11 +45,12 @@ terminal.
|
||||
* A file with more than one supported key is rejected: put the one recipient's
|
||||
key in its own file.
|
||||
* Encryption never runs another program.
|
||||
* Decryption reads PEM/PKCS#8 directly. An OpenSSH-format private key
|
||||
(`-----BEGIN OPENSSH PRIVATE KEY-----`) is converted through `ssh-keygen`,
|
||||
which therefore has to be installed. The conversion happens in a `0600`
|
||||
file under `/dev/shm` (or `$TMPDIR`), which is unlinked as soon as it has
|
||||
been read.
|
||||
* Decryption reads PEM/PKCS#8 with libcrypto, and the OpenSSH container
|
||||
(`-----BEGIN OPENSSH PRIVATE KEY-----`) with its own parser. A
|
||||
passphrase-protected OpenSSH key is first rewritten without its passphrase by
|
||||
`ssh-keygen`, so that program is needed only for those keys. The rewrite
|
||||
happens in a `0600` file under `/dev/shm` (or `$TMPDIR`) that is unlinked as
|
||||
soon as it has been read.
|
||||
* A passphrase is asked for on `/dev/tty` with echo off, never on stdin (that
|
||||
is the payload). `SSHCRYPT_PASSPHRASE` supplies it non-interactively.
|
||||
|
||||
@@ -107,7 +108,7 @@ wrong key reports both fingerprints instead of a padding error:
|
||||
* C99 + POSIX.1-2008, `cc`, `make`.
|
||||
* OpenSSL 3.x `libcrypto` (development headers). `pkg-config` is used when
|
||||
present, `-lcrypto` otherwise.
|
||||
* `ssh-keygen` at runtime, only for OpenSSH-format private keys.
|
||||
* `ssh-keygen` at runtime, only for passphrase-protected OpenSSH keys.
|
||||
|
||||
Tested on Debian with OpenSSL 3.6.3 and OpenSSH 10.5p1.
|
||||
|
||||
|
||||
+5
-4
@@ -71,9 +71,10 @@ Your private key, for
|
||||
PEM and PKCS#8 keys are read directly.
|
||||
An OpenSSH-format key
|
||||
.RB ( "BEGIN OPENSSH PRIVATE KEY" )
|
||||
is converted through
|
||||
.BR ssh-keygen (1),
|
||||
which must then be installed.
|
||||
is parsed directly.
|
||||
If it has a passphrase,
|
||||
.BR ssh-keygen (1)
|
||||
rewrites it without one first, so that program is needed only for such keys.
|
||||
.TP
|
||||
.BR \-i ", " \-\-input " " \fIFILE\fP
|
||||
Read from
|
||||
@@ -127,7 +128,7 @@ is not writable.
|
||||
The passphrase prompt.
|
||||
.TP
|
||||
.I /dev/shm/sshcrypt\-XXXXXX
|
||||
A conversion file, mode 0600, created only for an OpenSSH private key.
|
||||
A rewrite file, mode 0600, created only for a passphrase-protected OpenSSH key.
|
||||
It is unlinked as soon as it has been read, and on SIGINT, SIGTERM or SIGHUP.
|
||||
.SH "EXIT STATUS"
|
||||
.TP
|
||||
|
||||
+41
-32
@@ -524,10 +524,12 @@ static int run_ssh_keygen(const char *path, const char *pass)
|
||||
return -1;
|
||||
if (pid == 0) {
|
||||
int fd;
|
||||
/* -p with an empty new passphrase rewrites the key unencrypted,
|
||||
* in the same OpenSSH container. Do not ask ssh-keygen for
|
||||
* PKCS#8: OpenSSH 10.2 silently ignores -m for ed25519 keys. */
|
||||
char *const argv[] = {
|
||||
(char *)"ssh-keygen", (char *)"-p", (char *)"-m",
|
||||
(char *)"PKCS8", (char *)"-N", (char *)"", (char *)"-f",
|
||||
(char *)path, NULL
|
||||
(char *)"ssh-keygen", (char *)"-p", (char *)"-N", (char *)"",
|
||||
(char *)"-f", (char *)path, NULL
|
||||
};
|
||||
|
||||
setsid();
|
||||
@@ -551,9 +553,9 @@ static int run_ssh_keygen(const char *path, const char *pass)
|
||||
return WIFEXITED(status) && WEXITSTATUS(status) == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
/* Convert an OpenSSH-container key to unencrypted PKCS#8 in a 0600 temp file.
|
||||
* Returns the path; the caller must unlink it. */
|
||||
static char *convert_openssh_key(const char *path, const struct buf *raw)
|
||||
/* Rewrite an encrypted OpenSSH-container key without its passphrase, in a 0600
|
||||
* temp file. Returns the path; the caller must unlink it. */
|
||||
static char *strip_passphrase(const char *path, const struct buf *raw)
|
||||
{
|
||||
const char *t = getenv("TMPDIR");
|
||||
const char *dir = access("/dev/shm", W_OK) == 0 ? "/dev/shm" :
|
||||
@@ -561,6 +563,7 @@ static char *convert_openssh_key(const char *path, const struct buf *raw)
|
||||
size_t plen = strlen(dir) + 32;
|
||||
char *tmp = scr_xmalloc(plen);
|
||||
const char *env = passphrase_env();
|
||||
char *pw;
|
||||
int fd;
|
||||
|
||||
snprintf(tmp, plen, "%s/sshcrypt-XXXXXX", dir);
|
||||
@@ -575,33 +578,26 @@ static char *convert_openssh_key(const char *path, const struct buf *raw)
|
||||
g_tmp_active = 1;
|
||||
tmp_signals_install();
|
||||
|
||||
if (!env && run_ssh_keygen(tmp, "") == 0)
|
||||
return tmp;
|
||||
{
|
||||
char *pw = env ? scr_xstrdup(env) : get_passphrase(path);
|
||||
|
||||
if (run_ssh_keygen(tmp, pw) != 0) {
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
unlink(tmp);
|
||||
free(tmp);
|
||||
scr_die(SCR_EUSAGE,
|
||||
"%s: ssh-keygen could not convert this key "
|
||||
"(wrong passphrase?)", path);
|
||||
}
|
||||
pw = env ? scr_xstrdup(env) : get_passphrase(path);
|
||||
if (run_ssh_keygen(tmp, pw) != 0) {
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
unlink(tmp);
|
||||
free(tmp);
|
||||
scr_die(SCR_EUSAGE,
|
||||
"%s: wrong passphrase, or ssh-keygen could not read it",
|
||||
path);
|
||||
}
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int scr_priv_load(const char *path, struct scr_priv *out)
|
||||
{
|
||||
struct buf b = scr_read_file(path, 4 * 1024 * 1024);
|
||||
char *tmp = NULL;
|
||||
EVP_PKEY *pkey;
|
||||
FILE *f;
|
||||
const char *readpath;
|
||||
|
||||
memset(out, 0, sizeof *out);
|
||||
if (!is_private_blob(&b, 8192))
|
||||
@@ -609,15 +605,33 @@ int scr_priv_load(const char *path, struct scr_priv *out)
|
||||
"%s does not look like a PEM or OpenSSH private key", path);
|
||||
|
||||
if (find_bytes(b.data, b.len < 8192 ? b.len : 8192,
|
||||
"BEGIN OPENSSH PRIVATE KEY", 25))
|
||||
tmp = convert_openssh_key(path, &b);
|
||||
"BEGIN OPENSSH PRIVATE KEY", 25)) {
|
||||
int rc = scr_openssh_load(b.data, b.len, out);
|
||||
|
||||
if (rc == 1) {
|
||||
char *tmp = strip_passphrase(path, &b);
|
||||
struct buf c = scr_read_file(tmp, 4 * 1024 * 1024);
|
||||
|
||||
unlink(tmp);
|
||||
g_tmp_active = 0;
|
||||
free(tmp);
|
||||
rc = scr_openssh_load(c.data, c.len, out);
|
||||
scr_secure_zero(c.data, c.len);
|
||||
free(c.data);
|
||||
}
|
||||
if (rc != 0)
|
||||
scr_die(SCR_EUSAGE,
|
||||
"%s: cannot read the OpenSSH private key", path);
|
||||
scr_secure_zero(b.data, b.len);
|
||||
free(b.data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
readpath = tmp ? tmp : path;
|
||||
g_pass = NULL;
|
||||
g_pass_path = path;
|
||||
f = fopen(readpath, "r");
|
||||
f = fopen(path, "r");
|
||||
if (!f)
|
||||
scr_die(SCR_EIO, "%s: %s", readpath, strerror(errno));
|
||||
scr_die(SCR_EIO, "%s: %s", path, strerror(errno));
|
||||
pkey = PEM_read_PrivateKey(f, NULL, pass_cb, NULL);
|
||||
fclose(f);
|
||||
if (g_pass) {
|
||||
@@ -625,11 +639,6 @@ int scr_priv_load(const char *path, struct scr_priv *out)
|
||||
free(g_pass);
|
||||
g_pass = NULL;
|
||||
}
|
||||
if (tmp) {
|
||||
unlink(tmp);
|
||||
g_tmp_active = 0;
|
||||
free(tmp);
|
||||
}
|
||||
if (!pkey)
|
||||
scr_die(SCR_EUSAGE, "%s: cannot read the private key "
|
||||
"(wrong passphrase?)", path);
|
||||
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
#include "sshcrypt.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/param_build.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
/*
|
||||
* 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_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 (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;
|
||||
}
|
||||
@@ -66,6 +66,9 @@ int scr_pub_load_any(const char *path, struct scr_pub *out);
|
||||
void scr_pub_free(struct scr_pub *k);
|
||||
int scr_priv_load(const char *path, struct scr_priv *out);
|
||||
void scr_priv_free(struct scr_priv *k);
|
||||
|
||||
/* 0 = loaded, 1 = the container is encrypted, -1 = malformed */
|
||||
int scr_openssh_load(const unsigned char *text, size_t n, struct scr_priv *out);
|
||||
void scr_fingerprint(const unsigned char *blob, size_t n, char out[64]);
|
||||
int scr_ed25519_pk_to_x25519(const unsigned char ed[32], unsigned char out[32]);
|
||||
|
||||
|
||||
+13
-1
@@ -114,7 +114,8 @@ if command -v script >/dev/null 2>&1; then
|
||||
"\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o /dev/null; stty -a" \
|
||||
/dev/null)
|
||||
check "echo is restored after the prompt" \
|
||||
'printf "%s\n" "$out" | tr " " "\n" | grep -qx echo'
|
||||
'printf "%s\n" "$out" | tr -d "\r" | tr " " "\n" |
|
||||
grep -qx echo'
|
||||
|
||||
# Interrupting the conversion must not leave key material behind.
|
||||
if command -v pgrep >/dev/null 2>&1; then
|
||||
@@ -158,6 +159,17 @@ fi
|
||||
"$BIN" -d -k "$T/ed" -i "$T/viapriv.asc" -o "$T/viapriv.out"
|
||||
check "encrypting to a private-key path works" 'cmp -s "$T/msg" "$T/viapriv.out"'
|
||||
|
||||
# An unencrypted OpenSSH key is parsed here, not by ssh-keygen.
|
||||
env PATH=/nonexistent "$BIN" -d -k "$T/ed" -i "$T/msg.asc" \
|
||||
-o "$T/nossh.out" >/dev/null 2>&1
|
||||
check "unencrypted keys need no ssh-keygen" 'cmp -s "$T/msg" "$T/nossh.out"'
|
||||
|
||||
ssh-keygen -q -t rsa -b 3072 -N 'hunter2' -C pwr -f "$T/pwr" >/dev/null
|
||||
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pwr.pub" -i "$T/msg" -o "$T/pwr.asc"
|
||||
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pwr" -i "$T/pwr.asc" \
|
||||
-o "$T/pwr.out"
|
||||
check "passphrase-protected RSA key" 'cmp -s "$T/msg" "$T/pwr.out"'
|
||||
|
||||
# --- 1 MiB -------------------------------------------------------------------
|
||||
head -c 1048576 /dev/urandom >"$T/big"
|
||||
"$BIN" -r "$T/ed.pub" -i "$T/big" -o "$T/big.asc"
|
||||
|
||||
Reference in new issue
Block a user