Assorted further migration to ptrlen.

The local put_mp_*_from_string functions in import.c now take ptrlen
(which simplifies essentially all their call sites); so does the local
function logwrite() in logging.c, and so does ssh2_fingerprint_blob.
This commit is contained in:
Simon Tatham 2019-02-06 20:48:03 +00:00
Родитель 751a989091
Коммит 5b17a2ce20
7 изменённых файлов: 47 добавлений и 52 удалений

Просмотреть файл

@ -1002,7 +1002,7 @@ int main(int argc, char **argv)
} else {
assert(ssh2blob);
fingerprint = ssh2_fingerprint_blob(
ssh2blob->s, ssh2blob->len);
ptrlen_from_strbuf(ssh2blob));
}
}

Просмотреть файл

@ -281,10 +281,10 @@ struct openssh_pem_key {
strbuf *keyblob;
};
void BinarySink_put_mp_ssh2_from_string(
BinarySink *bs, const void *bytesv, int nbytes)
void BinarySink_put_mp_ssh2_from_string(BinarySink *bs, ptrlen str)
{
const unsigned char *bytes = (const unsigned char *)bytesv;
const unsigned char *bytes = (const unsigned char *)str.ptr;
size_t nbytes = str.len;
while (nbytes > 0 && bytes[0] == 0) {
nbytes--;
bytes++;
@ -297,8 +297,8 @@ void BinarySink_put_mp_ssh2_from_string(
}
put_data(bs, bytes, nbytes);
}
#define put_mp_ssh2_from_string(bs, val, len) \
BinarySink_put_mp_ssh2_from_string(BinarySink_UPCAST(bs), val, len)
#define put_mp_ssh2_from_string(bs, str) \
BinarySink_put_mp_ssh2_from_string(BinarySink_UPCAST(bs), str)
static struct openssh_pem_key *load_openssh_pem_key(const Filename *filename,
const char **errmsg_p)
@ -537,8 +537,6 @@ static ssh2_userkey *openssh_pem_read(
const char *errmsg;
strbuf *blob = strbuf_new();
int privptr = 0, publen;
const char *modptr = NULL;
int modlen = 0;
if (!key)
return NULL;
@ -669,7 +667,7 @@ static ssh2_userkey *openssh_pem_read(
put_stringz(blob, curve->name);
put_stringpl(blob, pubkey.data);
publen = blob->len;
put_mp_ssh2_from_string(blob, privkey.data.ptr, privkey.data.len);
put_mp_ssh2_from_string(blob, privkey.data);
retkey->key = ssh_key_new_priv(
alg, make_ptrlen(blob->u, publen),
@ -685,6 +683,8 @@ static ssh2_userkey *openssh_pem_read(
put_stringz(blob, key->keytype == OP_DSA ? "ssh-dss" : "ssh-rsa");
ptrlen rsa_modulus = PTRLEN_LITERAL("");
for (i = 0; i < num_integers; i++) {
ber_item integer = get_ber(src);
@ -712,13 +712,11 @@ static ssh2_userkey *openssh_pem_read(
*/
if (i == 1) {
/* Save the details for after we deal with number 2. */
modptr = integer.data.ptr;
modlen = integer.data.len;
rsa_modulus = integer.data;
} else if (i != 6 && i != 7) {
put_mp_ssh2_from_string(blob, integer.data.ptr,
integer.data.len);
put_mp_ssh2_from_string(blob, integer.data);
if (i == 2) {
put_mp_ssh2_from_string(blob, modptr, modlen);
put_mp_ssh2_from_string(blob, rsa_modulus);
privptr = blob->len;
}
}
@ -727,8 +725,7 @@ static ssh2_userkey *openssh_pem_read(
* Integers 1-4 go into the public blob; integer 5 goes
* into the private blob.
*/
put_mp_ssh2_from_string(blob, integer.data.ptr,
integer.data.len);
put_mp_ssh2_from_string(blob, integer.data);
if (i == 4)
privptr = blob->len;
}
@ -1918,10 +1915,10 @@ static bool sshcom_encrypted(const Filename *filename, char **comment)
return answer;
}
void BinarySink_put_mp_sshcom_from_string(
BinarySink *bs, const void *bytesv, int nbytes)
void BinarySink_put_mp_sshcom_from_string(BinarySink *bs, ptrlen str)
{
const unsigned char *bytes = (const unsigned char *)bytesv;
const unsigned char *bytes = (const unsigned char *)str.ptr;
size_t nbytes = str.len;
int bits = nbytes * 8 - 1;
while (bits > 0) {
@ -1935,8 +1932,8 @@ void BinarySink_put_mp_sshcom_from_string(
put_data(bs, bytes, nbytes);
}
#define put_mp_sshcom_from_string(bs, val, len) \
BinarySink_put_mp_sshcom_from_string(BinarySink_UPCAST(bs), val, len)
#define put_mp_sshcom_from_string(bs, str) \
BinarySink_put_mp_sshcom_from_string(BinarySink_UPCAST(bs), str)
static ptrlen BinarySource_get_mp_sshcom_as_string(BinarySource *src)
{
@ -2107,13 +2104,13 @@ static ssh2_userkey *sshcom_read(
alg = &ssh_rsa;
put_stringz(blob, "ssh-rsa");
put_mp_ssh2_from_string(blob, e.ptr, e.len);
put_mp_ssh2_from_string(blob, n.ptr, n.len);
put_mp_ssh2_from_string(blob, e);
put_mp_ssh2_from_string(blob, n);
publen = blob->len;
put_mp_ssh2_from_string(blob, d.ptr, d.len);
put_mp_ssh2_from_string(blob, q.ptr, q.len);
put_mp_ssh2_from_string(blob, p.ptr, p.len);
put_mp_ssh2_from_string(blob, u.ptr, u.len);
put_mp_ssh2_from_string(blob, d);
put_mp_ssh2_from_string(blob, q);
put_mp_ssh2_from_string(blob, p);
put_mp_ssh2_from_string(blob, u);
} else {
ptrlen p, q, g, x, y;
@ -2135,12 +2132,12 @@ static ssh2_userkey *sshcom_read(
alg = &ssh_dss;
put_stringz(blob, "ssh-dss");
put_mp_ssh2_from_string(blob, p.ptr, p.len);
put_mp_ssh2_from_string(blob, q.ptr, q.len);
put_mp_ssh2_from_string(blob, g.ptr, g.len);
put_mp_ssh2_from_string(blob, y.ptr, y.len);
put_mp_ssh2_from_string(blob, p);
put_mp_ssh2_from_string(blob, q);
put_mp_ssh2_from_string(blob, g);
put_mp_ssh2_from_string(blob, y);
publen = blob->len;
put_mp_ssh2_from_string(blob, x.ptr, x.len);
put_mp_ssh2_from_string(blob, x);
}
retkey = snew(ssh2_userkey);
@ -2271,7 +2268,7 @@ static bool sshcom_write(
if (initial_zero)
put_uint32(outblob, 0);
for (i = 0; i < nnumbers; i++)
put_mp_sshcom_from_string(outblob, numbers[i].ptr, numbers[i].len);
put_mp_sshcom_from_string(outblob, numbers[i]);
/* Now wrap up the encrypted payload. */
PUT_32BIT_MSB_FIRST(outblob->s + lenpos + 4,
outblob->len - (lenpos + 8));

Просмотреть файл

@ -31,7 +31,7 @@ static Filename *xlatlognam(Filename *s, char *hostname, int port,
* isn't open, buffering data if it's in the process of being
* opened asynchronously, etc.
*/
static void logwrite(LogContext *ctx, const void *data, size_t len)
static void logwrite(LogContext *ctx, ptrlen data)
{
/*
* In state L_CLOSED, we call logfopen, which will set the state
@ -42,10 +42,10 @@ static void logwrite(LogContext *ctx, const void *data, size_t len)
logfopen(ctx);
if (ctx->state == L_OPENING) {
bufchain_add(&ctx->queue, data, len);
bufchain_add(&ctx->queue, data.ptr, data.len);
} else if (ctx->state == L_OPEN) {
assert(ctx->lgfp);
if (fwrite(data, 1, len, ctx->lgfp) < len) {
if (fwrite(data.ptr, 1, data.len, ctx->lgfp) < data.len) {
logfclose(ctx);
ctx->state = L_ERROR;
lp_eventlog(ctx->lp, "Disabled writing session log "
@ -67,7 +67,7 @@ static void logprintf(LogContext *ctx, const char *fmt, ...)
data = dupvprintf(fmt, ap);
va_end(ap);
logwrite(ctx, data, strlen(data));
logwrite(ctx, ptrlen_from_asciz(data));
sfree(data);
}
@ -138,7 +138,7 @@ static void logfopen_callback(void *vctx, int mode)
assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
while (bufchain_size(&ctx->queue)) {
ptrlen data = bufchain_prefix(&ctx->queue);
logwrite(ctx, data.ptr, data.len);
logwrite(ctx, data);
bufchain_consume(&ctx->queue, data.len);
}
logflush(ctx);
@ -203,7 +203,7 @@ void logtraffic(LogContext *ctx, unsigned char c, int logmode)
{
if (ctx->logtype > 0) {
if (ctx->logtype == logmode)
logwrite(ctx, &c, 1);
logwrite(ctx, make_ptrlen(&c, 1));
}
}
@ -378,7 +378,7 @@ void log_packet(LogContext *ctx, int direction, int type,
if (((p % 16) == 0) || (p == len) || omitted) {
if (output_pos) {
strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
logwrite(ctx, dumpdata, strlen(dumpdata));
logwrite(ctx, ptrlen_from_asciz(dumpdata));
output_pos = 0;
}
}

Просмотреть файл

@ -320,8 +320,7 @@ void pageant_handle_msg(BinarySink *bs,
have_flags = true;
if (logfn) {
char *fingerprint = ssh2_fingerprint_blob(
keyblob.ptr, keyblob.len);
char *fingerprint = ssh2_fingerprint_blob(keyblob);
plog(logctx, logfn, "requested key: %s", fingerprint);
sfree(fingerprint);
}
@ -553,7 +552,7 @@ void pageant_handle_msg(BinarySink *bs,
}
if (logfn) {
char *fingerprint = ssh2_fingerprint_blob(blob.ptr, blob.len);
char *fingerprint = ssh2_fingerprint_blob(blob);
plog(logctx, logfn, "unwanted key: %s", fingerprint);
sfree(fingerprint);
}
@ -1387,7 +1386,7 @@ int pageant_enum_keys(pageant_key_enum_fn_t callback, void *callback_ctx,
return PAGEANT_ACTION_FAILURE;
}
fingerprint = ssh2_fingerprint_blob(pubblob.ptr, pubblob.len);
fingerprint = ssh2_fingerprint_blob(pubblob);
cbkey.blob = strbuf_new();
put_datapl(cbkey.blob, pubblob);

2
ssh.h
Просмотреть файл

@ -1113,7 +1113,7 @@ char *ssh2_pubkey_openssh_str(ssh2_userkey *key);
void ssh2_write_pubkey(FILE *fp, const char *comment,
const void *v_pub_blob, int pub_len,
int keytype);
char *ssh2_fingerprint_blob(const void *blob, int bloblen);
char *ssh2_fingerprint_blob(ptrlen);
char *ssh2_fingerprint(ssh_key *key);
int key_type(const Filename *filename);
const char *key_type_to_str(int type);

Просмотреть файл

@ -428,8 +428,7 @@ void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted)
if (s->hostkey_alg) {
s->hkey = ssh_key_new_pub(s->hostkey_alg,
s->hostkeydata);
put_string(s->exhash,
s->hostkeydata.ptr, s->hostkeydata.len);
put_stringpl(s->exhash, s->hostkeydata);
}
/*
* Can't loop as we have no token to pass to

Просмотреть файл

@ -1474,7 +1474,7 @@ void ssh2_write_pubkey(FILE *fp, const char *comment,
/* ----------------------------------------------------------------------
* Utility functions to compute SSH-2 fingerprints in a uniform way.
*/
char *ssh2_fingerprint_blob(const void *blob, int bloblen)
char *ssh2_fingerprint_blob(ptrlen blob)
{
unsigned char digest[16];
char fingerprint_str[16*3];
@ -1486,19 +1486,19 @@ char *ssh2_fingerprint_blob(const void *blob, int bloblen)
/*
* The fingerprint hash itself is always just the MD5 of the blob.
*/
hash_simple(&ssh_md5, make_ptrlen(blob, bloblen), digest);
hash_simple(&ssh_md5, blob, digest);
for (i = 0; i < 16; i++)
sprintf(fingerprint_str + i*3, "%02x%s", digest[i], i==15 ? "" : ":");
/*
* Identify the key algorithm, if possible.
*/
BinarySource_BARE_INIT(src, blob, bloblen);
BinarySource_BARE_INIT_PL(src, blob);
algname = get_string(src);
if (!get_err(src)) {
alg = find_pubkey_alg_len(algname);
if (alg) {
int bits = ssh_key_public_bits(alg, make_ptrlen(blob, bloblen));
int bits = ssh_key_public_bits(alg, blob);
return dupprintf("%.*s %d %s", PTRLEN_PRINTF(algname),
bits, fingerprint_str);
} else {
@ -1518,7 +1518,7 @@ char *ssh2_fingerprint(ssh_key *data)
{
strbuf *blob = strbuf_new();
ssh_key_public_blob(data, BinarySink_UPCAST(blob));
char *ret = ssh2_fingerprint_blob(blob->s, blob->len);
char *ret = ssh2_fingerprint_blob(ptrlen_from_strbuf(blob));
strbuf_free(blob);
return ret;
}