Prepare for OpenPGP cards with extended length support.
[gnupg.git] / scd / app-help.c
blob83b34c64ec4bcfc66b1282bbdf32e07d2d250d94
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];
43 p = ksba_cert_get_public_key (cert);
44 if (!p)
45 return gpg_error (GPG_ERR_BUG);
46 n = gcry_sexp_canon_len (p, 0, NULL, NULL);
47 if (!n)
48 return gpg_error (GPG_ERR_INV_SEXP);
49 err = gcry_sexp_sscan (&s_pkey, NULL, (char*)p, n);
50 xfree (p);
51 if (err)
52 return err; /* Can't parse that S-expression. */
53 if (!gcry_pk_get_keygrip (s_pkey, array))
55 gcry_sexp_release (s_pkey);
56 return gpg_error (GPG_ERR_GENERAL); /* Failed to calculate the keygrip.*/
58 gcry_sexp_release (s_pkey);
60 bin2hex (array, 20, hexkeygrip);
62 return 0;
67 /* Given the SLOT and the File ID FID, return the length of the
68 certificate contained in that file. Returns 0 if the file does not
69 exists or does not contain a certificate. If R_CERTOFF is not
70 NULL, the length the header will be stored at this address; thus to
71 parse the X.509 certificate a read should start at that offset.
73 On success the file is still selected.
75 size_t
76 app_help_read_length_of_cert (int slot, int fid, size_t *r_certoff)
78 gpg_error_t err;
79 unsigned char *buffer;
80 const unsigned char *p;
81 size_t buflen, n;
82 int class, tag, constructed, ndef;
83 size_t resultlen, objlen, hdrlen;
85 err = iso7816_select_file (slot, fid, 0, NULL, NULL);
86 if (err)
88 log_info ("error selecting FID 0x%04X: %s\n", fid, gpg_strerror (err));
89 return 0;
92 err = iso7816_read_binary (slot, 0, 32, &buffer, &buflen);
93 if (err)
95 log_info ("error reading certificate from FID 0x%04X: %s\n",
96 fid, gpg_strerror (err));
97 return 0;
100 if (!buflen || *buffer == 0xff)
102 log_info ("no certificate contained in FID 0x%04X\n", fid);
103 xfree (buffer);
104 return 0;
107 p = buffer;
108 n = buflen;
109 err = parse_ber_header (&p, &n, &class, &tag, &constructed,
110 &ndef, &objlen, &hdrlen);
111 if (err)
113 log_info ("error parsing certificate in FID 0x%04X: %s\n",
114 fid, gpg_strerror (err));
115 xfree (buffer);
116 return 0;
119 /* All certificates should commence with a SEQUENCE except for the
120 special ROOT CA which are enclosed in a SET. */
121 if ( !(class == CLASS_UNIVERSAL && constructed
122 && (tag == TAG_SEQUENCE || tag == TAG_SET)))
124 log_info ("data at FID 0x%04X does not look like a certificate\n", fid);
125 return 0;
128 resultlen = objlen + hdrlen;
129 if (r_certoff)
131 /* The callers want the offset to the actual certificate. */
132 *r_certoff = hdrlen;
134 err = parse_ber_header (&p, &n, &class, &tag, &constructed,
135 &ndef, &objlen, &hdrlen);
136 if (err)
137 return 0;
139 if (class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !constructed)
141 /* The certificate seems to be contained in a
142 userCertificate container. Assume the following sequence
143 is the certificate. */
144 *r_certoff += hdrlen + objlen;
145 if (*r_certoff > resultlen)
147 *r_certoff = 0;
148 return 0; /* That should never happen. */
151 else
152 *r_certoff = 0;
155 return resultlen;