projects
/
u
/
mdw
/
catacomb
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Major and incompatible overhaul of key_data representation. Fix leaks.
[u/mdw/catacomb]
/
key-binary.c
diff --git
a/key-binary.c
b/key-binary.c
index
dc8c701
..
087d992
100644
(file)
--- a/
key-binary.c
+++ b/
key-binary.c
@@
-47,17
+47,17
@@
*
* Arguments: @const void *p@ = pointer to buffer to read
* @size_t sz@ = size of the buffer
*
* Arguments: @const void *p@ = pointer to buffer to read
* @size_t sz@ = size of the buffer
- * @key_data *k@ = pointer to key data block to write to
*
*
- * Returns:
Zero if everything worked, nonzero otherwise
.
+ * Returns:
The newly-read key data, or null if it failed
.
*
* Use: Decodes a binary representation of a key.
*/
*
* Use: Decodes a binary representation of a key.
*/
-
int key_decode(const void *p, size_t sz, key_data *k
)
+
key_data *key_decode(const void *p, size_t sz
)
{
const octet *q = p;
size_t psz;
{
const octet *q = p;
size_t psz;
+ key_data *kd;
unsigned e;
/* --- Parse the header information --- *
unsigned e;
/* --- Parse the header information --- *
@@
-70,8
+70,7
@@
int key_decode(const void *p, size_t sz, key_data *k)
e = LOAD16(q);
psz = LOAD16(q + 2);
if (psz + 4 > sz)
e = LOAD16(q);
psz = LOAD16(q + 2);
if (psz + 4 > sz)
- return (-1);
- k->e = e;
+ return (0);
/* --- Now decide what to do --- */
/* --- Now decide what to do --- */
@@
-81,51
+80,51
@@
int key_decode(const void *p, size_t sz, key_data *k)
case KENC_BINARY:
case KENC_ENCRYPT:
case KENC_BINARY:
case KENC_ENCRYPT:
- k->u.k.k = sub_alloc(psz);
- memcpy(k->u.k.k, q + 4, psz);
- k->u.k.sz = psz;
+ kd = key_newbinary(e, q + 4, psz);
break;
/* --- Multiprecision integer data --- */
case KENC_MP:
break;
/* --- Multiprecision integer data --- */
case KENC_MP:
- k->u.m = mp_loadb(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, q + 4, psz);
+ kd = key_newmp(e, mp_loadb(e & KF_BURN ? MP_NEWSEC : MP_NEW,
+ q + 4, psz));
break;
/* --- String data --- */
case KENC_STRING:
break;
/* --- String data --- */
case KENC_STRING:
- k->u.p = xmalloc(sz + 1);
- memcpy(k->u.p, q + 4, sz);
- k->u.p[sz] = 0;
+ kd = key_newraw(e);
+ kd->u.p = xmalloc(sz + 1);
+ memcpy(kd->u.p, q + 4, sz);
+ kd->u.p[sz] = 0;
break;
/* --- Elliptic curve point data --- */
case KENC_EC: {
size_t xsz, ysz;
break;
/* --- Elliptic curve point data --- */
case KENC_EC: {
size_t xsz, ysz;
- EC_CREATE(&k->u.e);
+ kd = key_newraw(e);
+ EC_CREATE(&kd->u.e);
if (!sz) break;
if (!sz) break;
- if (sz < 2) return (
-1
);
+ if (sz < 2) return (
0
);
xsz = LOAD16(q + 4);
xsz = LOAD16(q + 4);
- if (sz < xsz + 4) return (
-1
);
+ if (sz < xsz + 4) return (
0
);
ysz = LOAD16(q + 6 + xsz);
ysz = LOAD16(q + 6 + xsz);
- if (sz < xsz + ysz + 4) return (
-1
);
- k->u.e.x = mp_loadb(MP_NEW, q + 6, xsz);
- k->u.e.y = mp_loadb(MP_NEW, q + 8 + xsz, ysz);
+ if (sz < xsz + ysz + 4) return (
0
);
+ k
d
->u.e.x = mp_loadb(MP_NEW, q + 6, xsz);
+ k
d
->u.e.y = mp_loadb(MP_NEW, q + 8 + xsz, ysz);
} break;
/* --- Structured key data --- */
case KENC_STRUCT: {
dstr d = DSTR_INIT;
} break;
/* --- Structured key data --- */
case KENC_STRUCT: {
dstr d = DSTR_INIT;
- key_struct *ks;
- unsigned f;
+ key_data *nkd;
- if ((
k->
e & ~KF_ENCMASK) || (psz & 3))
- return (
-1
);
+ if ((e & ~KF_ENCMASK) || (psz & 3))
+ return (
0
);
q += 4;
q += 4;
-
sym_create(&k->u.s
);
+
kd = key_newstruct(
);
while (psz) {
while (psz) {
@@
-142,20
+141,15
@@
int key_decode(const void *p, size_t sz, key_data *k)
/* --- Read the encoding and size --- */
/* --- Read the encoding and size --- */
- e = LOAD16(q);
sz = (LOAD16(q + 2) + 7) & ~3;
if (sz > psz)
goto fail;
/* --- Create a table node and fill it in --- */
sz = (LOAD16(q + 2) + 7) & ~3;
if (sz > psz)
goto fail;
/* --- Create a table node and fill it in --- */
- ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f);
- if (f)
- goto fail;
- if (key_decode(q, sz, &ks->k)) {
- sym_remove(&k->u.s, ks);
+ if ((nkd = key_decode(q, sz)) == 0)
goto fail;
goto fail;
- }
+ key_structsteal(kd, d.buf, nkd);
psz -= sz;
q += sz;
}
psz -= sz;
q += sz;
}
@@
-166,19
+160,20
@@
int key_decode(const void *p, size_t sz, key_data *k)
fail:
dstr_destroy(&d);
fail:
dstr_destroy(&d);
- key_d
estroy(k
);
- return (
-1
);
+ key_d
rop(kd
);
+ return (
0
);
} break;
/* --- Everything else --- */
default:
} break;
/* --- Everything else --- */
default:
- return (
-1
);
+ return (
0
);
}
/* --- OK, that was good --- */
}
/* --- OK, that was good --- */
- return (0);
+ kd->e = e;
+ return (kd);
}
/* --- @key_encode@ --- *
}
/* --- @key_encode@ --- *
@@
-297,7
+292,7
@@
int key_encode(key_data *k, dstr *d, const key_filter *kf)
DPUTS(d, SYM_NAME(ks));
while (d->len & 3)
DPUTC(d, 0);
DPUTS(d, SYM_NAME(ks));
while (d->len & 3)
DPUTC(d, 0);
- if (key_encode(
&
ks->k, d, kf))
+ if (key_encode(ks->k, d, kf))
rc = 1;
else
d->len = o;
rc = 1;
else
d->len = o;