X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/21aac40c87e498ae0a8c4c3d0960ca298a89b54c..95d9246390251adba7e6e9f0cc70bf0ebe0b2e60:/key-binary.c diff --git a/key-binary.c b/key-binary.c index dc8c701..c1ef53b 100644 --- a/key-binary.c +++ b/key-binary.c @@ -7,7 +7,7 @@ * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,12 +15,12 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, @@ -47,17 +47,17 @@ * * 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. */ -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; + key_data *kd; 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) - return (-1); - k->e = e; + return (0); /* --- 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: - 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: - 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: - 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; - EC_CREATE(&k->u.e); + kd = key_newraw(e); + EC_CREATE(&kd->u.e); if (!sz) break; - if (sz < 2) return (-1); + if (sz < 2) return (0); xsz = LOAD16(q + 4); - if (sz < xsz + 4) return (-1); + if (sz < xsz + 4) return (0); 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); + kd->u.e.x = mp_loadb(MP_NEW, q + 6, xsz); + kd->u.e.y = mp_loadb(MP_NEW, q + 8 + xsz, ysz); } 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; - sym_create(&k->u.s); + kd = key_newstruct(); while (psz) { @@ -142,20 +141,15 @@ int key_decode(const void *p, size_t sz, key_data *k) /* --- 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 --- */ - ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f); - if (f) + if ((nkd = key_decode(q, sz)) == 0) goto fail; - if (key_decode(q, sz, &ks->k)) { - sym_remove(&k->u.s, ks); - goto fail; - } + key_structsteal(kd, d.buf, nkd); psz -= sz; q += sz; } @@ -166,19 +160,20 @@ int key_decode(const void *p, size_t sz, key_data *k) fail: dstr_destroy(&d); - key_destroy(k); - return (-1); + key_drop(kd); + return (0); } break; /* --- Everything else --- */ default: - return (-1); + return (0); } /* --- OK, that was good --- */ - return (0); + kd->e = e; + return (kd); } /* --- @key_encode@ --- * @@ -280,7 +275,7 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) p = d->buf + n; STORE16(p, k->e & KF_ENCMASK); d->len += 4; - + for (nks = 0, sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; nks++); @@ -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); - if (key_encode(&ks->k, d, kf)) + if (key_encode(ks->k, d, kf)) rc = 1; else d->len = o; @@ -305,7 +300,7 @@ int key_encode(key_data *k, dstr *d, const key_filter *kf) xfree(ksv); } if (!rc) - d->len = n; + d->len = n; else { p = d->buf + n + 2; n = d->len - n - 4;