1 /* cardglue.h - Divert operations to the agent
2  * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #ifndef GNUPG_G10_CARDGLUE_H
20 #define GNUPG_G10_CARDGLUE_H
21 
22 #ifdef ENABLE_CARD_SUPPORT
23 /*
24    Note, that most card related code has been taken from 2.x branch
25    and is maintained over there if at all possible.  Thus, if you make
26    changes here, please check that a similar change has been commited
27    to the 2.x branch.
28 */
29 
30 /* We don't use libgcrypt but the shared codes uses a function type
31    from libgcrypt.  Thus we have to provide this type here.  */
32 typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int);
33 
34 
35 /* Object to hold all info about the card.  */
36 struct agent_card_info_s {
37   int error;         /* private. */
38   char *apptype;     /* Malloced application type string.  */
39   char *serialno;    /* malloced hex string. */
40   char *disp_name;   /* malloced. */
41   char *disp_lang;   /* malloced. */
42   int  disp_sex;     /* 0 = unspecified, 1 = male, 2 = female */
43   char *pubkey_url;  /* malloced. */
44   char *login_data;  /* malloced. */
45   char *private_do[4]; /* malloced. */
46   char cafpr1valid;
47   char cafpr2valid;
48   char cafpr3valid;
49   char cafpr1[20];
50   char cafpr2[20];
51   char cafpr3[20];
52   char fpr1valid;
53   char fpr2valid;
54   char fpr3valid;
55   char fpr1[20];
56   char fpr2[20];
57   char fpr3[20];
58   u32  fpr1time;
59   u32  fpr2time;
60   u32  fpr3time;
61   unsigned long sig_counter;
62   int chv1_cached;   /* True if a PIN is not required for each
63                         signing.  Note that the gpg-agent might cache
64                         it anyway. */
65   int is_v2;         /* True if this is a v2 card.  */
66   int chvmaxlen[3];  /* Maximum allowed length of a CHV. */
67   int chvretry[3];   /* Allowed retries for the CHV; 0 = blocked. */
68   struct {           /* Array with key attributes.  */
69     int algo;              /* Algorithm identifier.  */
70     unsigned int nbits;    /* Supported keysize.  */
71   } key_attr[3];
72   struct {
73     unsigned int ki:1;     /* Key import available.  */
74     unsigned int aac:1;    /* Algorithm attributes are changeable.  */
75   } extcap;
76 };
77 
78 struct agent_card_genkey_s {
79   char fprvalid;
80   char fpr[20];
81   u32  created_at;
82   MPI  n;
83   MPI  e;
84 };
85 
86 
87 struct app_ctx_s;
88 struct ctrl_ctx_s;
89 
90 typedef struct app_ctx_s *APP; /* deprecated. */
91 typedef struct app_ctx_s *app_t;
92 typedef struct ctrl_ctx_s *CTRL; /* deprecated. */
93 typedef struct ctrl_ctx_s *ctrl_t;
94 
95 
96 #define GPG_ERR_GENERAL           G10ERR_GENERAL
97 #define GPG_ERR_BAD_PIN           G10ERR_BAD_PASS
98 #define GPG_ERR_BAD_KEY           G10ERR_BAD_KEY
99 #define GPG_ERR_CARD              G10ERR_GENERAL
100 #define GPG_ERR_EEXIST            G10ERR_FILE_EXISTS
101 #define GPG_ERR_ENOMEM            G10ERR_RESOURCE_LIMIT
102 #define GPG_ERR_GENERAL           G10ERR_GENERAL
103 #define GPG_ERR_HARDWARE          G10ERR_GENERAL
104 #define GPG_ERR_INV_CARD          G10ERR_GENERAL
105 #define GPG_ERR_INV_ID            G10ERR_GENERAL
106 #define GPG_ERR_INV_NAME          G10ERR_GENERAL
107 #define GPG_ERR_INV_VALUE         G10ERR_INV_ARG
108 #define GPG_ERR_INV_SEXP          G10ERR_INV_ARG
109 #define GPG_ERR_NOT_SUPPORTED     G10ERR_UNSUPPORTED
110 #define GPG_ERR_NO_OBJ            G10ERR_GENERAL
111 #define GPG_ERR_PIN_BLOCKED       G10ERR_PASSPHRASE
112 #define GPG_ERR_UNSUPPORTED_ALGORITHM G10ERR_PUBKEY_ALGO
113 #define GPG_ERR_USE_CONDITIONS    G10ERR_GENERAL
114 #define GPG_ERR_WRONG_CARD        G10ERR_GENERAL
115 #define GPG_ERR_WRONG_SECKEY      G10ERR_WRONG_SECKEY
116 #define GPG_ERR_PIN_NOT_SYNCED    G10ERR_GENERAL
117 #define GPG_ERR_NOT_FOUND         G10ERR_GENERAL
118 #define GPG_ERR_BUG               G10ERR_GENERAL
119 #define GPG_ERR_NOT_IMPLEMENTED   G10ERR_GENERAL
120 #define GPG_ERR_BAD_BER           G10ERR_GENERAL
121 #define GPG_ERR_EOF               (-1)
122 #define GPG_ERR_CARD_NOT_PRESENT  G10ERR_NO_CARD
123 #define GPG_ERR_CARD_RESET        G10ERR_GENERAL
124 #define GPG_ERR_WRONG_PUBKEY_ALGO G10ERR_PUBKEY_ALGO
125 #define GPG_ERR_UNKNOWN_SEXP      G10ERR_INV_ARG
126 #define GPG_ERR_DUP_VALUE         G10ERR_INV_ARG
127 #define GPG_ERR_BAD_SECKEY        G10ERR_BAD_SECKEY
128 #define GPG_ERR_TOO_LARGE         G10ERR_GENERAL
129 
130 #define GPG_ERR_EBUSY             G10ERR_GENERAL
131 #define GPG_ERR_ENOENT            G10ERR_OPEN_FILE
132 #define GPG_ERR_EACCES            G10ERR_UNSUPPORTED
133 #define GPG_ERR_EIO               G10ERR_GENERAL
134 #define GPG_ERR_ENODEV            G10ERR_GENERAL
135 #define GPG_ERR_CANCELED          G10ERR_CANCELED
136 
137 #define GPG_ERR_INV_DATA          G10ERR_GENERAL
138 #define GPG_ERR_PUBKEY_ALGO       G10ERR_PUBKEY_ALGO
139 #define GPG_ERR_TOO_SHORT         G10ERR_INV_ARG
140 
141 
142 typedef int gpg_error_t;
143 typedef int gpg_err_code_t;
144 
145 #define gpg_error(n) (n)
146 #define gpg_err_code(n) (n)
147 #define gpg_strerror(n) g10_errstr ((n))
148 #define gpg_error_from_errno(n) (G10ERR_GENERAL) /*FIXME*/
149 #define gpg_err_code_from_errno(n) (G10ERR_GENERAL)
150 #define gpg_error_from_syserror()  (G10ERR_GENERAL) /*FIXME*/
151 
152 /* We are not using it in a library, so we even let xtrymalloc
153    abort. Because we won't never return from these malloc functions,
154    we also don't need the out_of_core function, we simply define it to
155    return -1 */
156 #define xtrymalloc(n)    xmalloc((n))
157 #define xtrycalloc(n,m)  xcalloc((n),(m))
158 #define xtryrealloc(n,m) xrealloc((n),(m))
159 #define xtrymalloc_secure(n)  xmalloc_secure((n))
160 #define out_of_core()    (-1)
161 
162 #define gnupg_get_time() make_timestamp ()
163 
164 
165 void card_set_reader_port (const char *portstr);
166 
167 char *serialno_and_fpr_from_sk (const unsigned char *sn, size_t snlen,
168                                 PKT_secret_key *sk);
169 void send_status_info (ctrl_t ctrl, const char *keyword, ...);
170 void send_status_direct (ctrl_t ctrl, const char *keyword, const char *args);
171 void gcry_md_hash_buffer (int algo, void *digest,
172 			  const void *buffer, size_t length);
173 const char *gcry_md_algo_name (int algorithm);
174 void log_printf (const char *fmt, ...);
175 void log_printhex (const char *text, const void *buffer, size_t length);
176 
177 
178 #define GCRY_MD_SHA1 DIGEST_ALGO_SHA1
179 #define GCRY_MD_RMD160 DIGEST_ALGO_RMD160
180 #define GCRY_MD_SHA256 DIGEST_ALGO_SHA256
181 #define GCRY_MD_SHA384 DIGEST_ALGO_SHA384
182 #define GCRY_MD_SHA512 DIGEST_ALGO_SHA512
183 #define GCRY_MD_SHA224 DIGEST_ALGO_SHA224
184 #define GCRY_MD_MD5    DIGEST_ALGO_MD5
185 
186 void gcry_mpi_release (MPI a);
187 MPI  gcry_mpi_set_opaque (MPI a, void *p, unsigned int len);
188 
189 
190 void card_close (void);
191 
192 
193 /* Release the card info structure. */
194 void agent_release_card_info (struct agent_card_info_s *info);
195 
196 /* Return card info. */
197 int agent_learn (struct agent_card_info_s *info);
198 
199 /* Check whether the secret key for the key identified by HEXKEYGRIP
200    is available.  Return 0 for yes or an error code. */
201 int agent_havekey (const char *hexkeygrip);
202 
203 /* Return card info. */
204 int agent_scd_getattr (const char *name, struct agent_card_info_s *info);
205 
206 /* Send a SETATTR command to the SCdaemon. */
207 int agent_scd_setattr (const char *name,
208                        const unsigned char *value, size_t valuelen,
209                        const char *serialno);
210 
211 /* Send a WRITEKEY command to the SCdaemon. */
212 int agent_scd_writekey (int keyno, const char *serialno,
213                         const unsigned char *keydata, size_t keydatalen);
214 
215 /* Send a GENKEY command to the SCdaemon. */
216 int agent_scd_genkey (struct agent_card_genkey_s *info, int keyno, int force,
217                       const char *serialno, u32 *createtime);
218 
219 /* Send a PKSIGN command to the SCdaemon. */
220 int agent_scd_pksign (const char *keyid, int hashalgo,
221                       const unsigned char *indata, size_t indatalen,
222                       unsigned char **r_buf, size_t *r_buflen);
223 
224 /* Send a PKDECRYPT command to the SCdaemon. */
225 int agent_scd_pkdecrypt (const char *serialno,
226                          const unsigned char *indata, size_t indatalen,
227                          unsigned char **r_buf, size_t *r_buflen);
228 
229 /* Change the PIN of an OpenPGP card or reset the retry counter. */
230 int agent_scd_change_pin (int chvno, const char *serialno);
231 
232 /* Send a CHECKPIN command. */
233 int agent_scd_checkpin (const char *serialnobuf);
234 
235 /* Clear a cached PIN. */
236 void agent_clear_pin_cache (const char *sn);
237 
238 
239 /* Dummy functions.  */
240 int agent_scd_writecert (const char *certidstr,
241                          const unsigned char *certdata, size_t certdatalen);
242 int agent_scd_readcert (const char *certidstr,
243                         void **r_buf, size_t *r_buflen);
244 
245 
246 #endif /*ENABLE_CARD_SUPPORT*/
247 #endif /*GNUPG_G10_CARDGLUE_H*/
248 
249