| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: key-binary.c,v 1.3 2001/02/03 11:57:00 mdw Exp $ |
| 4 | * |
| 5 | * Key binary encoding |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: key-binary.c,v $ |
| 33 | * Revision 1.3 2001/02/03 11:57:00 mdw |
| 34 | * Track mLib change: symbols no longer need to include a terminating |
| 35 | * null. |
| 36 | * |
| 37 | * Revision 1.2 2000/06/17 11:25:20 mdw |
| 38 | * Use secure memory interface from MP library. |
| 39 | * |
| 40 | * Revision 1.1 2000/02/12 18:21:02 mdw |
| 41 | * Overhaul of key management (again). |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /*----- Header files ------------------------------------------------------*/ |
| 46 | |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
| 49 | |
| 50 | #include <mLib/bits.h> |
| 51 | #include <mLib/dstr.h> |
| 52 | #include <mLib/sub.h> |
| 53 | #include <mLib/sym.h> |
| 54 | |
| 55 | #include "key-data.h" |
| 56 | #include "mp.h" |
| 57 | #include "mptext.h" |
| 58 | |
| 59 | /*----- Main code ---------------------------------------------------------*/ |
| 60 | |
| 61 | /* --- @key_decode@ --- * |
| 62 | * |
| 63 | * Arguments: @const void *p@ = pointer to buffer to read |
| 64 | * @size_t sz@ = size of the buffer |
| 65 | * @key_data *k@ = pointer to key data block to write to |
| 66 | * |
| 67 | * Returns: Zero if everything worked, nonzero otherwise. |
| 68 | * |
| 69 | * Use: Decodes a binary representation of a key. |
| 70 | */ |
| 71 | |
| 72 | int key_decode(const void *p, size_t sz, key_data *k) |
| 73 | { |
| 74 | const octet *q = p; |
| 75 | size_t psz; |
| 76 | unsigned e; |
| 77 | |
| 78 | /* --- Parse the header information --- * |
| 79 | * |
| 80 | * Make sure the size matches external reality. Security holes have been |
| 81 | * known to creep in without this sort of check. (No, this isn't an after- |
| 82 | * the-fact patch-up.) |
| 83 | */ |
| 84 | |
| 85 | e = LOAD16(q); |
| 86 | psz = LOAD16(q + 2); |
| 87 | if (psz + 4 > sz) |
| 88 | return (-1); |
| 89 | k->e = e; |
| 90 | |
| 91 | /* --- Now decide what to do --- */ |
| 92 | |
| 93 | switch (e & KF_ENCMASK) { |
| 94 | |
| 95 | /* --- Plain binary data --- */ |
| 96 | |
| 97 | case KENC_BINARY: |
| 98 | case KENC_ENCRYPT: |
| 99 | k->u.k.k = sub_alloc(psz); |
| 100 | memcpy(k->u.k.k, q + 4, psz); |
| 101 | k->u.k.sz = psz; |
| 102 | break; |
| 103 | |
| 104 | /* --- Multiprecision integer data --- */ |
| 105 | |
| 106 | case KENC_MP: |
| 107 | k->u.m = mp_loadb(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, q + 4, psz); |
| 108 | break; |
| 109 | |
| 110 | /* --- Structured key data --- */ |
| 111 | |
| 112 | case KENC_STRUCT: { |
| 113 | dstr d = DSTR_INIT; |
| 114 | key_struct *ks; |
| 115 | unsigned f; |
| 116 | |
| 117 | if ((k->e & ~KF_ENCMASK) || (psz & 3)) |
| 118 | return (-1); |
| 119 | q += 4; |
| 120 | sym_create(&k->u.s); |
| 121 | |
| 122 | while (psz) { |
| 123 | |
| 124 | /* --- Read the tag string --- */ |
| 125 | |
| 126 | DRESET(&d); |
| 127 | sz = LOAD8(q); |
| 128 | if (sz >= psz) |
| 129 | goto fail; |
| 130 | DPUTM(&d, q + 1, sz); |
| 131 | DPUTZ(&d); |
| 132 | sz = (sz + 4) & ~3; |
| 133 | q += sz; psz -= sz; |
| 134 | |
| 135 | /* --- Read the encoding and size --- */ |
| 136 | |
| 137 | e = LOAD16(q); |
| 138 | sz = (LOAD16(q + 2) + 7) & ~3; |
| 139 | if (sz > psz) |
| 140 | goto fail; |
| 141 | |
| 142 | /* --- Create a table node and fill it in --- */ |
| 143 | |
| 144 | ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f); |
| 145 | if (f) |
| 146 | goto fail; |
| 147 | if (key_decode(q, sz, &ks->k)) { |
| 148 | sym_remove(&k->u.s, ks); |
| 149 | goto fail; |
| 150 | } |
| 151 | psz -= sz; |
| 152 | q += sz; |
| 153 | } |
| 154 | dstr_destroy(&d); |
| 155 | break; |
| 156 | |
| 157 | /* --- Tidy up after a failure --- */ |
| 158 | |
| 159 | fail: |
| 160 | dstr_destroy(&d); |
| 161 | key_destroy(k); |
| 162 | return (-1); |
| 163 | } break; |
| 164 | |
| 165 | /* --- Everything else --- */ |
| 166 | |
| 167 | default: |
| 168 | return (-1); |
| 169 | } |
| 170 | |
| 171 | /* --- OK, that was good --- */ |
| 172 | |
| 173 | return (0); |
| 174 | } |
| 175 | |
| 176 | /* --- @key_encode@ --- * |
| 177 | * |
| 178 | * Arguments: @key_data *k@ = pointer to key data block |
| 179 | * @dstr *d@ = pointer to destination string |
| 180 | * @const key_filter *kf@ = pointer to key selection block |
| 181 | * |
| 182 | * Returns: Nonzero if an item was actually written. |
| 183 | * |
| 184 | * Use: Encodes a key block as binary data. |
| 185 | */ |
| 186 | |
| 187 | int key_encode(key_data *k, dstr *d, const key_filter *kf) |
| 188 | { |
| 189 | int rc = 0; |
| 190 | if (!KEY_MATCH(k, kf)) |
| 191 | return (0); |
| 192 | switch (k->e & KF_ENCMASK) { |
| 193 | case KENC_BINARY: |
| 194 | case KENC_ENCRYPT: { |
| 195 | char *p; |
| 196 | |
| 197 | DENSURE(d, (k->u.k.sz + 7) & ~3); |
| 198 | p = d->buf + d->len; |
| 199 | STORE16(p, k->e); |
| 200 | STORE16(p + 2, k->u.k.sz); |
| 201 | d->len += 4; |
| 202 | DPUTM(d, k->u.k.k, k->u.k.sz); |
| 203 | rc = 1; |
| 204 | } break; |
| 205 | |
| 206 | case KENC_MP: { |
| 207 | char *p; |
| 208 | size_t sz = mp_octets(k->u.m); |
| 209 | |
| 210 | DENSURE(d, (sz + 7) & ~3); |
| 211 | p = d->buf + d->len; |
| 212 | STORE16(p, k->e); |
| 213 | STORE16(p + 2, sz); |
| 214 | mp_storeb(k->u.m, p + 4, sz); |
| 215 | d->len += sz + 4; |
| 216 | rc = 1; |
| 217 | } break; |
| 218 | |
| 219 | case KENC_STRUCT: { |
| 220 | size_t n; |
| 221 | char *p; |
| 222 | key_struct *ks; |
| 223 | sym_iter i; |
| 224 | |
| 225 | n = d->len; |
| 226 | DENSURE(d, 4); |
| 227 | p = d->buf + n; |
| 228 | STORE16(p, k->e & KF_ENCMASK); |
| 229 | d->len += 4; |
| 230 | for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) { |
| 231 | size_t o = d->len; |
| 232 | DENSURE(d, 1); |
| 233 | *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks)); |
| 234 | DPUTS(d, SYM_NAME(ks)); |
| 235 | while (d->len & 3) |
| 236 | DPUTC(d, 0); |
| 237 | if (key_encode(&ks->k, d, kf)) |
| 238 | rc = 1; |
| 239 | else |
| 240 | d->len = o; |
| 241 | } |
| 242 | if (!rc) |
| 243 | d->len = n; |
| 244 | else { |
| 245 | p = d->buf + n + 2; |
| 246 | n = d->len - n - 4; |
| 247 | STORE16(p, n); |
| 248 | } |
| 249 | } break; |
| 250 | } |
| 251 | while (d->len & 3) |
| 252 | DPUTC(d, 0); |
| 253 | return (rc); |
| 254 | } |
| 255 | |
| 256 | /*----- That's all, folks -------------------------------------------------*/ |