1 /* certlist.c - build list of certificates
2 * Copyright (C) 2001, 2003 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
37 /* Return 0 if the cert is usable for encryption. A MODE of 0 checks
38 for signing a MODE of 1 checks for encryption, a MODE of 2 checks
39 for verification and a MODE of 3 for decryption (just for
42 cert_usage_p (ksba_cert_t cert, int mode)
47 err = ksba_cert_get_key_usage (cert, &use);
48 if (gpg_err_code (err) == GPG_ERR_NO_DATA)
50 if (opt.verbose && mode < 2)
52 _("no key usage specified - accepted for encryption\n"):
53 _("no key usage specified - accepted for signing\n"));
58 log_error (_("error getting key usage information: %s\n"),
65 if ((use & (KSBA_KEYUSAGE_KEY_CERT_SIGN)))
67 log_info ( _("certificate should have not been used certification\n"));
68 return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
72 (KSBA_KEYUSAGE_KEY_ENCIPHERMENT|KSBA_KEYUSAGE_DATA_ENCIPHERMENT):
73 (KSBA_KEYUSAGE_DIGITAL_SIGNATURE|KSBA_KEYUSAGE_NON_REPUDIATION)))
76 log_info (mode==3? _("certificate should have not been used for encryption\n"):
77 mode==2? _("certificate should have not been used for signing\n"):
78 mode==1? _("certificate is not usable for encryption\n"):
79 _("certificate is not usable for signing\n"));
80 return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
84 /* Return 0 if the cert is usable for signing */
86 gpgsm_cert_use_sign_p (ksba_cert_t cert)
88 return cert_usage_p (cert, 0);
92 /* Return 0 if the cert is usable for encryption */
94 gpgsm_cert_use_encrypt_p (ksba_cert_t cert)
96 return cert_usage_p (cert, 1);
100 gpgsm_cert_use_verify_p (ksba_cert_t cert)
102 return cert_usage_p (cert, 2);
106 gpgsm_cert_use_decrypt_p (ksba_cert_t cert)
108 return cert_usage_p (cert, 3);
112 gpgsm_cert_use_cert_p (ksba_cert_t cert)
114 return cert_usage_p (cert, 4);
119 same_subject_issuer (const char *subject, const char *issuer, ksba_cert_t cert)
121 char *subject2 = ksba_cert_get_subject (cert, 0);
122 char *issuer2 = ksba_cert_get_subject (cert, 0);
125 tmp = (subject && subject2
126 && !strcmp (subject, subject2)
128 && !strcmp (issuer, issuer2));
134 /* Return true if CERT is already contained in CERTLIST. */
136 is_cert_in_certlist (ksba_cert_t cert, certlist_t certlist)
138 const unsigned char *img_a, *img_b;
141 img_a = ksba_cert_get_image (cert, &len_a);
144 for ( ; certlist; certlist = certlist->next)
146 img_b = ksba_cert_get_image (certlist->cert, &len_b);
147 if (img_b && len_a == len_b && !memcmp (img_a, img_b, len_a))
148 return 1; /* Already contained. */
155 /* Add CERT to the list of certificates at CERTADDR but avoid
158 gpgsm_add_cert_to_certlist (ctrl_t ctrl, ksba_cert_t cert,
159 certlist_t *listaddr, int is_encrypt_to)
161 if (!is_cert_in_certlist (cert, *listaddr))
163 certlist_t cl = xtrycalloc (1, sizeof *cl);
165 return OUT_OF_CORE (errno);
167 ksba_cert_ref (cert);
168 cl->next = *listaddr;
169 cl->is_encrypt_to = is_encrypt_to;
175 /* Add a certificate to a list of certificate and make sure that it is
176 a valid certificate. With SECRET set to true a secret key must be
177 available for the certificate. IS_ENCRYPT_TO sets the corresponding
178 flag in the new create LISTADDR item. */
180 gpgsm_add_to_certlist (CTRL ctrl, const char *name, int secret,
181 CERTLIST *listaddr, int is_encrypt_to)
184 KEYDB_SEARCH_DESC desc;
185 KEYDB_HANDLE kh = NULL;
186 ksba_cert_t cert = NULL;
188 rc = keydb_classify_name (name, &desc);
193 rc = gpg_error (GPG_ERR_ENOMEM);
197 char *subject = NULL;
201 rc = keydb_search (kh, &desc, 1);
203 rc = keydb_get_cert (kh, &cert);
206 rc = secret? gpgsm_cert_use_sign_p (cert)
207 : gpgsm_cert_use_encrypt_p (cert);
208 if (gpg_err_code (rc) == GPG_ERR_WRONG_KEY_USAGE)
210 /* There might be another certificate with the
211 correct usage, so we try again */
213 { /* save the first match */
215 subject = ksba_cert_get_subject (cert, 0);
216 issuer = ksba_cert_get_subject (cert, 0);
217 ksba_cert_release (cert);
221 else if (same_subject_issuer (subject, issuer, cert))
224 ksba_cert_release (cert);
233 /* We want the error code from the first match in this case. */
234 if (rc && wrong_usage)
240 rc = keydb_search (kh, &desc, 1);
245 ksba_cert_t cert2 = NULL;
247 /* We have to ignore ambigious names as long as
248 there only fault is a bad key usage */
249 if (!keydb_get_cert (kh, &cert2))
251 int tmp = (same_subject_issuer (subject, issuer, cert2)
253 secret? gpgsm_cert_use_sign_p (cert2)
254 : gpgsm_cert_use_encrypt_p (cert2)
256 ) == GPG_ERR_WRONG_KEY_USAGE));
257 ksba_cert_release (cert2);
261 rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
267 if (!rc && !is_cert_in_certlist (cert, *listaddr))
273 rc = gpg_error (GPG_ERR_NO_SECKEY);
274 p = gpgsm_get_keygrip_hexstring (cert);
277 if (!gpgsm_agent_havekey (p))
283 rc = gpgsm_validate_chain (ctrl, cert, NULL);
286 CERTLIST cl = xtrycalloc (1, sizeof *cl);
288 rc = OUT_OF_CORE (errno);
291 cl->cert = cert; cert = NULL;
292 cl->next = *listaddr;
293 cl->is_encrypt_to = is_encrypt_to;
302 ksba_cert_release (cert);
303 return rc == -1? gpg_error (GPG_ERR_NO_PUBKEY): rc;
307 gpgsm_release_certlist (CERTLIST list)
311 CERTLIST cl = list->next;
312 ksba_cert_release (list->cert);
319 /* Like gpgsm_add_to_certlist, but look only for one certificate. No
320 chain validation is done */
322 gpgsm_find_cert (const char *name, ksba_cert_t *r_cert)
325 KEYDB_SEARCH_DESC desc;
326 KEYDB_HANDLE kh = NULL;
329 rc = keydb_classify_name (name, &desc);
334 rc = gpg_error (GPG_ERR_ENOMEM);
337 rc = keydb_search (kh, &desc, 1);
339 rc = keydb_get_cert (kh, r_cert);
342 rc = keydb_search (kh, &desc, 1);
348 rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
349 ksba_cert_release (*r_cert);
357 return rc == -1? gpg_error (GPG_ERR_NO_PUBKEY): rc;