2006-04-14 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / sexputil.c
blob8a27ad978ef5710ff509ed9d4056fe939a6f61d7
1 /* sexputil.c - Utility fnctions for S-expressions.
2 * Copyright (C) 2005 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 /* This file implements a few utility functions useful when working
22 with canonical encrypted S-expresions (i.e. not the S-exprssion
23 objects from libgcrypt). */
25 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #ifdef HAVE_LOCALE_H
32 #include <locale.h>
33 #endif
35 #include "util.h"
38 /* Return the so called "keygrip" which is the SHA-1 hash of the
39 public key parameters expressed in a way depended on the algorithm.
41 KEY is expected to be an canonical encoded S-expression with a
42 public or private key. KEYLEN is the length of that buffer.
44 GRIP must be at least 20 bytes long On success 0 is return, on
45 error an aerror code. */
46 gpg_error_t
47 keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
48 unsigned char *grip)
50 gpg_error_t err;
51 gcry_sexp_t sexp;
53 if (!grip)
54 return gpg_error (GPG_ERR_INV_VALUE);
55 err = gcry_sexp_sscan (&sexp, NULL, (const char *)key, keylen);
56 if (err)
57 return err;
58 if (!gcry_pk_get_keygrip (sexp, grip))
59 err = gpg_error (GPG_ERR_INTERNAL);
60 gcry_sexp_release (sexp);
61 return err;
65 /* Compare two simple S-expressions like "(3:foo)". Returns 0 if they
66 are identical or !0 if they are not. Not that this function can't
67 be used for sorting. */
68 int
69 cmp_simple_canon_sexp (const unsigned char *a_orig,
70 const unsigned char *b_orig)
72 const char *a = (const char *)a_orig;
73 const char *b = (const char *)b_orig;
74 unsigned long n1, n2;
75 char *endp;
77 if (!a && !b)
78 return 0; /* Both are NULL, they are identical. */
79 if (!a || !b)
80 return 1; /* One is NULL, they are not identical. */
81 if (*a != '(' || *b != '(')
82 log_bug ("invalid S-exp in cmp_simple_canon_sexp\n");
84 a++;
85 n1 = strtoul (a, &endp, 10);
86 a = endp;
87 b++;
88 n2 = strtoul (b, &endp, 10);
89 b = endp;
91 if (*a != ':' || *b != ':' )
92 log_bug ("invalid S-exp in cmp_simple_canon_sexp\n");
93 if (n1 != n2)
94 return 1; /* Not the same. */
96 for (a++, b++; n1; n1--, a++, b++)
97 if (*a != *b)
98 return 1; /* Not the same. */
99 return 0;
103 /* Create a simple S-expression from the hex string at LIBNE. Returns
104 a newly allocated buffer with that canonical encoded S-expression
105 or NULL in case of an error. On return the number of characters
106 scanned in LINE will be stored at NSCANNED. This fucntions stops
107 converting at the first character not representing a hexdigit. Odd
108 numbers of hex digits are allowed; a leading zero is then
109 assumed. If no characters have been found, NULL is returned.*/
110 unsigned char *
111 make_simple_sexp_from_hexstr (const char *line, size_t *nscanned)
113 size_t n, len;
114 const char *s;
115 unsigned char *buf;
116 unsigned char *p;
117 char numbuf[50];
119 for (n=0, s=line; hexdigitp (s); s++, n++)
121 if (nscanned)
122 *nscanned = n;
123 if (!n)
124 return NULL;
125 len = ((n+1) & ~0x01)/2;
126 sprintf (numbuf, "(%u:", (unsigned int)len);
127 buf = xtrymalloc (strlen (numbuf) + len + 1 + 1);
128 if (!buf)
129 return NULL;
130 p = (unsigned char *)stpcpy ((char *)buf, numbuf);
131 s = line;
132 if ((n&1))
134 *p++ = xtoi_1 (s);
135 s++;
136 n--;
138 for (; n > 1; n -=2, s += 2)
139 *p++ = xtoi_2 (s);
140 *p++ = ')';
141 *p = 0; /* (Not really neaded.) */
143 return buf;