g10/
[gnupg.git] / scd / app-help.c
blob45c7586f7846a41bf3e57ea47a4871127a553d7d
1 /* app-help.c - Application helper functions
2 * Copyright (C) 2004 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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "scdaemon.h"
27 #include "app-common.h"
28 #include "iso7816.h"
29 #include "tlv.h"
31 /* Return the KEYGRIP for the certificate CERT as an hex encoded
32 string in the user provided buffer HEXKEYGRIP which must be of at
33 least 41 bytes. */
34 gpg_error_t
35 app_help_get_keygrip_string (ksba_cert_t cert, char *hexkeygrip)
37 gpg_error_t err;
38 gcry_sexp_t s_pkey;
39 ksba_sexp_t p;
40 size_t n;
41 unsigned char array[20];
42 int i;
44 p = ksba_cert_get_public_key (cert);
45 if (!p)
46 return gpg_error (GPG_ERR_BUG);
47 n = gcry_sexp_canon_len (p, 0, NULL, NULL);
48 if (!n)
49 return gpg_error (GPG_ERR_INV_SEXP);
50 err = gcry_sexp_sscan (&s_pkey, NULL, (char*)p, n);
51 xfree (p);
52 if (err)
53 return err; /* Can't parse that S-expression. */
54 if (!gcry_pk_get_keygrip (s_pkey, array))
56 gcry_sexp_release (s_pkey);
57 return gpg_error (GPG_ERR_GENERAL); /* Failed to calculate the keygrip.*/
59 gcry_sexp_release (s_pkey);
61 for (i=0; i < 20; i++)
62 sprintf (hexkeygrip+i*2, "%02X", array[i]);
64 return 0;
69 /* Given the SLOT and the File ID FID, return the length of the
70 certificate contained in that file. Returns 0 if the file does not
71 exists or does not contain a certificate. If R_CERTOFF is not
72 NULL, the length the header will be stored at this address; thus to
73 parse the X.509 certificate a read should start at that offset.
75 On success the file is still selected.
77 size_t
78 app_help_read_length_of_cert (int slot, int fid, size_t *r_certoff)
80 gpg_error_t err;
81 unsigned char *buffer;
82 const unsigned char *p;
83 size_t buflen, n;
84 int class, tag, constructed, ndef;
85 size_t resultlen, objlen, hdrlen;
87 err = iso7816_select_file (slot, fid, 0, NULL, NULL);
88 if (err)
90 log_info ("error selecting FID 0x%04X: %s\n", fid, gpg_strerror (err));
91 return 0;
94 err = iso7816_read_binary (slot, 0, 32, &buffer, &buflen);
95 if (err)
97 log_info ("error reading certificate from FID 0x%04X: %s\n",
98 fid, gpg_strerror (err));
99 return 0;
102 if (!buflen || *buffer == 0xff)
104 log_info ("no certificate contained in FID 0x%04X\n", fid);
105 xfree (buffer);
106 return 0;
109 p = buffer;
110 n = buflen;
111 err = parse_ber_header (&p, &n, &class, &tag, &constructed,
112 &ndef, &objlen, &hdrlen);
113 if (err)
115 log_info ("error parsing certificate in FID 0x%04X: %s\n",
116 fid, gpg_strerror (err));
117 xfree (buffer);
118 return 0;
121 /* All certificates should commence with a SEQUENCE except for the
122 special ROOT CA which are enclosed in a SET. */
123 if ( !(class == CLASS_UNIVERSAL && constructed
124 && (tag == TAG_SEQUENCE || tag == TAG_SET)))
126 log_info ("contents of FID 0x%04X does not look like a certificate\n",
127 fid);
128 return 0;
131 resultlen = objlen + hdrlen;
132 if (r_certoff)
134 /* The callers want the offset to the actual certificate. */
135 *r_certoff = hdrlen;
137 err = parse_ber_header (&p, &n, &class, &tag, &constructed,
138 &ndef, &objlen, &hdrlen);
139 if (err)
140 return 0;
142 if (class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !constructed)
144 /* The certificate seems to be contained in a
145 userCertificate container. Assume the following sequence
146 is the certificate. */
147 *r_certoff += hdrlen + objlen;
148 if (*r_certoff > resultlen)
150 *r_certoff = 0;
151 return 0; /* That should never happen. */
154 else
155 *r_certoff = 0;
158 return resultlen;