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). */
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. */
47 keygrip_from_canon_sexp (const unsigned char *key
, size_t keylen
,
54 return gpg_error (GPG_ERR_INV_VALUE
);
55 err
= gcry_sexp_sscan (&sexp
, NULL
, (const char *)key
, keylen
);
58 if (!gcry_pk_get_keygrip (sexp
, grip
))
59 err
= gpg_error (GPG_ERR_INTERNAL
);
60 gcry_sexp_release (sexp
);
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. */
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
;
78 return 0; /* Both are NULL, they are identical. */
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");
85 n1
= strtoul (a
, &endp
, 10);
88 n2
= strtoul (b
, &endp
, 10);
91 if (*a
!= ':' || *b
!= ':' )
92 log_bug ("invalid S-exp in cmp_simple_canon_sexp\n");
94 return 1; /* Not the same. */
96 for (a
++, b
++; n1
; n1
--, a
++, b
++)
98 return 1; /* Not the same. */
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.*/
111 make_simple_sexp_from_hexstr (const char *line
, size_t *nscanned
)
119 for (n
=0, s
=line
; hexdigitp (s
); s
++, n
++)
125 len
= ((n
+1) & ~0x01)/2;
126 sprintf (numbuf
, "(%u:", (unsigned int)len
);
127 buf
= xtrymalloc (strlen (numbuf
) + len
+ 1 + 1);
130 p
= (unsigned char *)stpcpy ((char *)buf
, numbuf
);
138 for (; n
> 1; n
-=2, s
+= 2)
141 *p
= 0; /* (Not really neaded.) */