1 /* g13tuple.c - Tuple handling
2 * Copyright (C) 2009 Free Software Foundation, Inc.
3 * Copyright (C) 2009, 2015, 2016 Werner Koch
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
30 #include "keyblob.h" /* Required for dump_tupledesc. */
33 /* Definition of the tuple descriptor object. */
36 unsigned char *data; /* The tuple data. */
37 size_t datalen; /* The length of the data. */
38 size_t pos; /* The current position as used by next_tuple. */
39 int refcount; /* Number of references hold. */
44 /* Append the TAG and the VALUE to the MEMBUF. There is no error
45 checking here; this is instead done while getting the value back
48 append_tuple (membuf_t *membuf, int tag, const void *value, size_t length)
52 assert (tag >= 0 && tag <= 0xffff);
53 assert (length <= 0xffff);
57 put_membuf (membuf, buf, 2);
60 put_membuf (membuf, buf, 2);
62 put_membuf (membuf, value, length);
66 /* Append the unsigned integer VALUE under TAG to MEMBUF. We make
67 * sure that the most significant bit is always cleared to explicitly
68 * flag the value as unsigned. */
70 append_tuple_uint (membuf_t *membuf, int tag, unsigned long long value)
72 unsigned char buf[16];
82 *--p = (value & 0xff);
88 /* Prepend a zero byte if the first byte has its MSB set. */
97 append_tuple (membuf, tag, p, len);
101 /* Create a tuple object by moving the ownership of (DATA,DATALEN) to
102 * a new object. Returns 0 on success and stores the new object at
103 * R_TUPLEHD. The return object must be released using
104 * destroy_tuples(). */
106 create_tupledesc (tupledesc_t *r_desc, void *data, size_t datalen)
108 if (datalen < 5 || memcmp (data, "\x00\x00\x00\x01\x01", 5))
109 return gpg_error (GPG_ERR_NOT_SUPPORTED);
111 *r_desc = xtrymalloc (sizeof **r_desc);
113 return gpg_error_from_syserror ();
114 (*r_desc)->data = data;
115 (*r_desc)->datalen = datalen;
117 (*r_desc)->refcount = 1;
121 /* Unref a tuple descriptor and if the refcount is down to 0 release
122 its allocated storage. */
124 destroy_tupledesc (tupledesc_t tupledesc)
129 if (!--tupledesc->refcount)
131 xfree (tupledesc->data);
138 ref_tupledesc (tupledesc_t tupledesc)
141 tupledesc->refcount++;
146 /* Return a pointer to the memory used to store the tuples. This is
147 * the data originally provided to create_tupledesc. It is higly
148 * recommended that the callers uses ref_tupledesc before calling this
149 * function and unref_tupledesc when the return data will not anymore
152 get_tupledesc_data (tupledesc_t tupledesc, size_t *r_datalen)
154 *r_datalen = tupledesc->datalen;
155 return tupledesc->data;
158 /* Find the first tuple with tag TAG. On success return a pointer to
159 its value and store the length of the value at R_LENGTH. If no
160 tuple was found return NULL. For use by next_tuple, the last
161 position is stored in the descriptor. */
163 find_tuple (tupledesc_t tupledesc, unsigned int tag, size_t *r_length)
165 const unsigned char *s;
166 const unsigned char *s_end; /* Points right behind the data. */
173 s_end = s + tupledesc->datalen;
176 /* We use addresses for the overflow check to avoid undefined
177 behaviour. size_t should work with all flat memory models. */
178 if ((size_t)s+3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s)
185 if ((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s)
189 tupledesc->pos = (s + n) - tupledesc->data;
199 /* Helper for find_tuple_uint and others. */
201 convert_uint (const unsigned char *s, size_t n, unsigned long long *r_value)
203 unsigned long long value = 0;
208 return gpg_error (GPG_ERR_NOT_FOUND);
209 if (!n || (*s & 0x80)) /* No bytes or negative. */
210 return gpg_error (GPG_ERR_ERANGE);
211 if (n && !*s) /* Skip a leading zero. */
216 if (n > sizeof value)
217 return gpg_error (GPG_ERR_ERANGE);
228 /* Similar to find-tuple but expects an unsigned int value and stores
229 * that at R_VALUE. If the tag was not found GPG_ERR_NOT_FOUND is
230 * returned and 0 stored at R_VALUE. If the value cannot be converted
231 * to an unsigned integer GPG_ERR_ERANGE is returned. */
233 find_tuple_uint (tupledesc_t tupledesc, unsigned int tag,
234 unsigned long long *r_value)
236 const unsigned char *s;
239 s = find_tuple (tupledesc, tag, &n);
240 return convert_uint (s, n, r_value);
245 next_tuple (tupledesc_t tupledesc, unsigned int *r_tag, size_t *r_length)
247 const unsigned char *s;
248 const unsigned char *s_end; /* Points right behind the data. */
255 s_end = s + tupledesc->datalen;
258 && !((size_t)s + 3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s))
265 if (!((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s))
267 tupledesc->pos = (s + n) - tupledesc->data;
278 /* Return true if BUF has only printable characters. */
280 all_printable (const void *buf, size_t buflen)
282 const unsigned char *s;
284 for (s=buf ; buflen; s++, buflen--)
285 if (*s < 32 || *s > 126)
291 /* Print information about TUPLES to the log stream. */
293 dump_tupledesc (tupledesc_t tuples)
298 unsigned long long uint;
300 log_info ("keyblob dump:\n");
301 tag = KEYBLOB_TAG_BLOBVERSION;
302 value = find_tuple (tuples, tag, &n);
305 log_info (" tag: %-5u len: %-2u value: ", tag, (unsigned int)n);
307 log_printf ("[none]\n");
312 case KEYBLOB_TAG_ENCKEY:
313 case KEYBLOB_TAG_MACKEY:
314 log_printf ("[confidential]\n");
317 case KEYBLOB_TAG_ALGOSTR:
318 if (n < 100 && all_printable (value, n))
319 log_printf ("%.*s\n", (int)n, (const char*)value);
321 log_printhex ("", value, n);
324 case KEYBLOB_TAG_CONT_NSEC:
325 case KEYBLOB_TAG_ENC_NSEC:
326 case KEYBLOB_TAG_ENC_OFF:
327 if (!convert_uint (value, n, &uint))
328 log_printf ("%llu\n", uint);
330 log_printhex ("", value, n);
334 log_printhex ("", value, n);
338 value = next_tuple (tuples, &tag, &n);