~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to common/sexputil.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* sexputil.c - Utility fnctions for S-expressions.
2
 
 * Copyright (C) 2005 Free Software Foundation, Inc.
 
1
/* sexputil.c - Utility functions for S-expressions.
 
2
 * Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
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
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
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
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
/* This file implements a few utility functions useful when working
33
32
#endif
34
33
 
35
34
#include "util.h"
36
 
 
 
35
#include "sexp-parse.h"
37
36
 
38
37
/* Return the so called "keygrip" which is the SHA-1 hash of the
39
38
   public key parameters expressed in a way depended on the algorithm.
41
40
   KEY is expected to be an canonical encoded S-expression with a
42
41
   public or private key. KEYLEN is the length of that buffer.
43
42
 
44
 
   GRIP must be at least 20 bytes long On success 0 is return, on
45
 
   error an aerror code. */
 
43
   GRIP must be at least 20 bytes long.  On success 0 is returned, on
 
44
   error an error code. */
46
45
gpg_error_t
47
46
keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
48
47
                         unsigned char *grip)
114
113
  const char *s;
115
114
  unsigned char *buf;
116
115
  unsigned char *p;
117
 
  char numbuf[50];
 
116
  char numbuf[50], *numbufp;
 
117
  size_t numbuflen;
118
118
 
119
119
  for (n=0, s=line; hexdigitp (s); s++, n++)
120
120
    ;
123
123
  if (!n)
124
124
    return NULL;
125
125
  len = ((n+1) & ~0x01)/2; 
126
 
  sprintf (numbuf, "(%u:", (unsigned int)len);
127
 
  buf = xtrymalloc (strlen (numbuf) + len + 1 + 1);
 
126
  numbufp = smklen (numbuf, sizeof numbuf, len, &numbuflen);
 
127
  buf = xtrymalloc (1 + numbuflen + len + 1 + 1);
128
128
  if (!buf)
129
129
    return NULL;
130
 
  p = (unsigned char *)stpcpy ((char *)buf, numbuf);
 
130
  buf[0] = '(';
 
131
  p = (unsigned char *)stpcpy ((char *)buf+1, numbufp);
131
132
  s = line;
132
133
  if ((n&1))
133
134
    {
142
143
 
143
144
  return buf;
144
145
}
 
146
 
 
147
 
 
148
/* Return the hash algorithm from a KSBA sig-val. SIGVAL is a
 
149
   canonical encoded S-expression.  Return 0 if the hash algorithm is
 
150
   not encoded in SIG-VAL or it is not supported by libgcrypt.  */
 
151
int
 
152
hash_algo_from_sigval (const unsigned char *sigval)
 
153
{
 
154
  const unsigned char *s = sigval;
 
155
  size_t n;
 
156
  int depth;
 
157
  char buffer[50];
 
158
 
 
159
  if (!s || *s != '(')
 
160
    return 0; /* Invalid S-expression.  */
 
161
  s++;
 
162
  n = snext (&s);
 
163
  if (!n)
 
164
    return 0; /* Invalid S-expression.  */
 
165
  if (!smatch (&s, n, "sig-val"))
 
166
    return 0; /* Not a sig-val.  */
 
167
  if (*s != '(')
 
168
    return 0; /* Invalid S-expression.  */
 
169
  s++;
 
170
  /* Skip over the algo+parameter list.  */
 
171
  depth = 1;
 
172
  if (sskip (&s, &depth) || depth)
 
173
    return 0; /* Invalid S-expression.  */
 
174
  if (*s != '(')
 
175
    return 0; /* No futher list.  */
 
176
  /* Check whether this is (hash ALGO).  */
 
177
  s++;
 
178
  n = snext (&s);
 
179
  if (!n)
 
180
    return 0; /* Invalid S-expression.  */
 
181
  if (!smatch (&s, n, "hash"))
 
182
    return 0; /* Not a "hash" keyword.  */
 
183
  n = snext (&s);
 
184
  if (!n || n+1 >= sizeof (buffer))
 
185
    return 0; /* Algorithm string is missing or too long.  */
 
186
  memcpy (buffer, s, n);
 
187
  buffer[n] = 0;
 
188
  
 
189
  return gcry_md_map_name (buffer);
 
190
}
 
191