po/
[gnupg.git] / scd / card-dinsig.c
blob0cbcd31696511ec1cee3b8c793ee3207e512fd97
1 /* card-dinsig.c - German signature law (DINSIG) functions
2 * Copyright (C) 2002 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 /* The German signature law and its bylaw (SigG and SigV) is currently
21 used with an interface specification described in DIN V 66291-1.
22 The AID to be used is: 'D27600006601'.
24 The file IDs for certificates utilize the generic format:
25 Cxyz
26 C being the hex digit 'C' (12).
27 x being the service indicator:
28 '0' := SigG conform digital signature.
29 '1' := entity authentication.
30 '2' := key encipherment.
31 '3' := data encipherment.
32 '4' := key agreement.
33 other values are reserved for future use.
34 y being the security environment number using '0' for cards
35 not supporting a SE number.
36 z being the certificate type:
37 '0' := C.CH (base certificate of ard holder) or C.ICC.
38 '1' .. '7' := C.CH (business or professional certificate
39 of card holder.
40 '8' .. 'D' := C.CA (certificate of a CA issue by the Root-CA).
41 'E' := C.RCA (self certified certificate of the Root-CA).
42 'F' := reserved.
44 The file IDs used by default are:
45 '1F00' EF.SSD (security service descriptor). [o,o]
46 '2F02' EF.GDO (global data objects) [m,m]
47 'A000' EF.PROT (signature log). Cyclic file with 20 records of 53 byte.
48 Read and update after user authentication. [o,o]
49 'B000' EF.PK.RCA.DS (public keys of Root-CA). Size is 512b or size
50 of keys. [m (unless a 'C00E' is present),m]
51 'B001' EF.PK.CA.DS (public keys of CAs). Size is 512b or size
52 of keys. [o,o]
53 'C00n' EF.C.CH.DS (digital signature certificate of card holder)
54 with n := 0 .. 7. Size is 2k or size of cert. Read and
55 update allowed after user authentication. [m,m]
56 'C00m' EF.C.CA.DS (digital signature certificate of CA)
57 with m := 8 .. E. Size is 1k or size of cert. Read always
58 allowed, update after uder authentication. [o,o]
59 'C100' EF.C.ICC.AUT (AUT certificate of ICC) [o,m]
60 'C108' EF.C.CA.AUT (AUT certificate of CA) [o,m]
61 'D000' EF.DM (display message) [-,m]
63 The letters in brackets indicate optional or mandatory files: The
64 first for card terminals under full control and the second for
65 "business" card terminals.
67 FIXME: Needs a lot more explanation.
72 #include <config.h>
73 #include <errno.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <time.h>
79 #ifdef HAVE_OPENSC
80 #include <opensc/pkcs15.h>
81 #include "scdaemon.h"
82 #include <ksba.h>
84 #include "card-common.h"
86 static int dinsig_read_cert (CARD card, const char *certidstr,
87 unsigned char **cert, size_t *ncert);
91 /* See card.c for interface description. Frankly we don't do any real
92 enumeration but just check whether the well know files are
93 available. */
94 static int
95 dinsig_enum_keypairs (CARD card, int idx,
96 unsigned char *keygrip, char **keyid)
98 int rc;
99 unsigned char *buf;
100 size_t buflen;
101 ksba_cert_t cert;
103 /* fixme: We should locate the application via the EF(DIR) and not
104 assume a Netkey card */
105 if (!idx)
106 rc = dinsig_read_cert (card, "DINSIG-DF01.C000", &buf, &buflen);
107 else if (idx == 1)
108 rc = dinsig_read_cert (card, "DINSIG-DF01.C200", &buf, &buflen);
109 else
110 rc = -1;
111 if (rc)
112 return rc;
114 rc = ksba_cert_new (&cert);
115 if (rc)
117 xfree (buf);
118 return rc;
121 rc = ksba_cert_init_from_mem (cert, buf, buflen);
122 xfree (buf);
123 if (rc)
125 log_error ("failed to parse the certificate at idx %d: %s\n",
126 idx, gpg_strerror (rc));
127 ksba_cert_release (cert);
128 return rc;
130 if (card_help_get_keygrip (cert, keygrip))
132 log_error ("failed to calculate the keygrip at index %d\n", idx);
133 ksba_cert_release (cert);
134 return gpg_error (GPG_ERR_CARD);
136 ksba_cert_release (cert);
138 /* return the iD */
139 if (keyid)
141 *keyid = xtrymalloc (17);
142 if (!*keyid)
143 return gpg_error (gpg_err_code_from_errno (errno));
144 if (!idx)
145 strcpy (*keyid, "DINSIG-DF01.C000");
146 else
147 strcpy (*keyid, "DINSIG-DF01.C200");
150 return 0;
155 /* See card.c for interface description */
156 static int
157 dinsig_read_cert (CARD card, const char *certidstr,
158 unsigned char **cert, size_t *ncert)
160 int rc;
161 struct sc_path path;
162 struct sc_file *file;
163 unsigned char *buf;
164 int buflen;
166 if (!strcmp (certidstr, "DINSIG-DF01.C000"))
167 sc_format_path ("3F00DF01C000", &path);
168 else if (!strcmp (certidstr, "DINSIG-DF01.C200"))
169 sc_format_path ("3F00DF01C200", &path);
170 else
171 return gpg_error (GPG_ERR_INV_ID);
173 rc = sc_select_file (card->scard, &path, &file);
174 if (rc)
176 log_error ("sc_select_file failed: %s\n", sc_strerror (rc));
177 return map_sc_err (rc);
179 if (file->type != SC_FILE_TYPE_WORKING_EF
180 || file->ef_structure != SC_FILE_EF_TRANSPARENT)
182 log_error ("wrong type or structure of certificate EF\n");
183 sc_file_free (file);
184 return gpg_error (GPG_ERR_CARD);
186 if (file->size < 20) /* check against a somewhat arbitrary length */
188 log_error ("certificate EF too short\n");
189 sc_file_free (file);
190 return gpg_error (GPG_ERR_CARD);
192 buf = xtrymalloc (file->size);
193 if (!buf)
195 gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
196 sc_file_free (file);
197 return tmperr;
200 rc = sc_read_binary (card->scard, 0, buf, file->size, 0);
201 if (rc >= 0 && rc != file->size)
203 log_error ("short read on certificate EF\n");
204 sc_file_free (file);
205 xfree (buf);
206 return gpg_error (GPG_ERR_CARD);
208 sc_file_free (file);
209 if (rc < 0)
211 log_error ("error reading certificate EF: %s\n", sc_strerror (rc));
212 xfree (buf);
213 return map_sc_err (rc);
215 buflen = rc;
217 /* The object is not a plain certificate but wrapped into id-at
218 userCertificate - fixme: we should check the specs and decided
219 whether libksba should support it */
220 if (buflen > 9 && buf[0] == 0x30 && buf[4] == 6 && buf[5] == 3
221 && buf[6] == 0x55 && buf[7] == 4 && buf[8] == 0x24)
223 /* We have to strip the padding. Although this is a good idea
224 anyway, we have to do it due to a KSBA problem; KSBA does not
225 work correct when the buffer is larger than the ASN.1
226 structure and the certificates here are padded with FF. So
227 as a workaround we look at the outer structure to get the
228 size of the entire thing and adjust the buflen. We can only
229 do this when there is a 2 byte length field */
230 size_t seqlen;
231 if (buf[1] == 0x82)
233 seqlen = ((buf[2] << 8) | buf[3]) + 4;
234 if (seqlen < buflen)
235 buflen = seqlen;
237 memmove (buf, buf+9, buflen-9);
238 buflen -= 9;
241 *cert = buf;
242 *ncert = buflen;
243 return 0;
249 /* Bind our operations to the card */
250 void
251 card_dinsig_bind (CARD card)
253 card->fnc.enum_keypairs = dinsig_enum_keypairs;
254 card->fnc.read_cert = dinsig_read_cert;
257 #endif /*HAVE_OPENSC*/