| 1 |
/* cert.c - main function for the certificate handling |
| 2 |
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2012 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <assert.h> |
| 36 |
#include <errno.h> |
| 37 |
|
| 38 |
#include "util.h" |
| 39 |
#include "ber-decoder.h" |
| 40 |
#include "ber-help.h" |
| 41 |
#include "convert.h" |
| 42 |
#include "keyinfo.h" |
| 43 |
#include "sexp-parse.h" |
| 44 |
#include "cert.h" |
| 45 |
|
| 46 |
|
| 47 |
static const char oidstr_subjectKeyIdentifier[] = "2.5.29.14"; |
| 48 |
static const char oidstr_keyUsage[] = "2.5.29.15"; |
| 49 |
static const char oidstr_subjectAltName[] = "2.5.29.17"; |
| 50 |
static const char oidstr_issuerAltName[] = "2.5.29.18"; |
| 51 |
static const char oidstr_basicConstraints[] = "2.5.29.19"; |
| 52 |
static const char oidstr_crlDistributionPoints[] = "2.5.29.31"; |
| 53 |
static const char oidstr_certificatePolicies[] = "2.5.29.32"; |
| 54 |
static const char oidstr_authorityKeyIdentifier[] = "2.5.29.35"; |
| 55 |
static const char oidstr_extKeyUsage[] = "2.5.29.37"; |
| 56 |
static const char oidstr_authorityInfoAccess[] = "1.3.6.1.5.5.7.1.1"; |
| 57 |
static const char oidstr_subjectInfoAccess[] = "1.3.6.1.5.5.7.1.11"; |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* ksba_cert_new: |
| 62 |
* |
| 63 |
* Create a new and empty certificate object |
| 64 |
* |
| 65 |
* Return value: 0 on success or error code. For a successful |
| 66 |
* operation, ACERT is set to the new certifixate obbject, otherwise |
| 67 |
* it is set to NULL. |
| 68 |
**/ |
| 69 |
gpg_error_t |
| 70 |
ksba_cert_new (ksba_cert_t *acert) |
| 71 |
{ |
| 72 |
*acert = xtrycalloc (1, sizeof **acert); |
| 73 |
if (!*acert) |
| 74 |
return gpg_error_from_errno (errno); |
| 75 |
(*acert)->ref_count++; |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
ksba_cert_ref (ksba_cert_t cert) |
| 82 |
{ |
| 83 |
if (!cert) |
| 84 |
fprintf (stderr, "BUG: ksba_cert_ref for NULL\n"); |
| 85 |
else |
| 86 |
++cert->ref_count; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* ksba_cert_release: |
| 91 |
* @cert: A certificate object |
| 92 |
* |
| 93 |
* Release a certificate object. |
| 94 |
**/ |
| 95 |
void |
| 96 |
ksba_cert_release (ksba_cert_t cert) |
| 97 |
{ |
| 98 |
int i; |
| 99 |
|
| 100 |
if (!cert) |
| 101 |
return; |
| 102 |
if (cert->ref_count < 1) |
| 103 |
{ |
| 104 |
fprintf (stderr, "BUG: trying to release an already released cert\n"); |
| 105 |
return; |
| 106 |
} |
| 107 |
if (--cert->ref_count) |
| 108 |
return; |
| 109 |
|
| 110 |
if (cert->udata) |
| 111 |
{ |
| 112 |
struct cert_user_data *ud = cert->udata; |
| 113 |
cert->udata = NULL; |
| 114 |
do |
| 115 |
{ |
| 116 |
struct cert_user_data *ud2 = ud->next; |
| 117 |
if (ud->data && ud->data != ud->databuf) |
| 118 |
xfree (ud->data); |
| 119 |
xfree (ud); |
| 120 |
ud = ud2; |
| 121 |
} |
| 122 |
while (ud); |
| 123 |
} |
| 124 |
|
| 125 |
xfree (cert->cache.digest_algo); |
| 126 |
if (cert->cache.extns_valid) |
| 127 |
{ |
| 128 |
for (i=0; i < cert->cache.n_extns; i++) |
| 129 |
xfree (cert->cache.extns[i].oid); |
| 130 |
xfree (cert->cache.extns); |
| 131 |
} |
| 132 |
|
| 133 |
_ksba_asn_release_nodes (cert->root); |
| 134 |
ksba_asn_tree_release (cert->asn_tree); |
| 135 |
|
| 136 |
xfree (cert->image); |
| 137 |
|
| 138 |
xfree (cert); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/* Store arbitrary data along with a certificate. The DATA of length |
| 143 |
DATALEN will be stored under the string KEY. If some data is |
| 144 |
already stored under this key it will be replaced by the new data. |
| 145 |
Using NULL for DATA will effectivly delete the data. |
| 146 |
|
| 147 |
On error (i.e. out or memory) an already existing data object |
| 148 |
stored under KEY may get deleted. |
| 149 |
|
| 150 |
This function is not thread safe because we don't employ any |
| 151 |
locking. */ |
| 152 |
gpg_error_t |
| 153 |
ksba_cert_set_user_data (ksba_cert_t cert, |
| 154 |
const char *key, const void *data, size_t datalen) |
| 155 |
{ |
| 156 |
struct cert_user_data *ud; |
| 157 |
|
| 158 |
if (!cert || !key || !*key) |
| 159 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 160 |
|
| 161 |
for (ud=cert->udata; ud; ud = ud->next) |
| 162 |
if (!strcmp (ud->key, key)) |
| 163 |
break; |
| 164 |
if (ud) /* Update the data stored under this key or reuse this item. */ |
| 165 |
{ |
| 166 |
if (ud->data && ud->data != ud->databuf) |
| 167 |
xfree (ud->data); |
| 168 |
ud->data = NULL; |
| 169 |
if (data && datalen <= sizeof ud->databuf) |
| 170 |
{ |
| 171 |
memcpy (ud->databuf, data, datalen); |
| 172 |
ud->data = ud->databuf; |
| 173 |
ud->datalen = datalen; |
| 174 |
} |
| 175 |
else if (data) |
| 176 |
{ |
| 177 |
ud->data = xtrymalloc (datalen); |
| 178 |
if (!ud->data) |
| 179 |
return gpg_error_from_errno (errno); |
| 180 |
memcpy (ud->data, data, datalen); |
| 181 |
ud->datalen = datalen; |
| 182 |
} |
| 183 |
} |
| 184 |
else if (data) /* Insert as a new item. */ |
| 185 |
{ |
| 186 |
ud = xtrycalloc (1, sizeof *ud + strlen (key)); |
| 187 |
if (!ud) |
| 188 |
return gpg_error_from_errno (errno); |
| 189 |
strcpy (ud->key, key); |
| 190 |
if (datalen <= sizeof ud->databuf) |
| 191 |
{ |
| 192 |
memcpy (ud->databuf, data, datalen); |
| 193 |
ud->data = ud->databuf; |
| 194 |
ud->datalen = datalen; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
ud->data = xtrymalloc (datalen); |
| 199 |
if (!ud->data) |
| 200 |
{ |
| 201 |
xfree (ud); |
| 202 |
return gpg_error_from_errno (errno); |
| 203 |
} |
| 204 |
memcpy (ud->data, data, datalen); |
| 205 |
ud->datalen = datalen; |
| 206 |
} |
| 207 |
ud->next = cert->udata; |
| 208 |
cert->udata = ud; |
| 209 |
} |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
/* Return user data for certificate CERT stored under the string |
| 217 |
KEY. The caller needs to provide a suitable large BUFFER and pass |
| 218 |
the usable length of the buffer in BUFFERLEN. If DATALEN is not |
| 219 |
NULL, the length of the data stored at BUFFER will be stored there. |
| 220 |
|
| 221 |
If BUFFER is NULL, BUFFERLEN will be ignored and the required |
| 222 |
length of the buffer will be returned at DATALEN. |
| 223 |
|
| 224 |
On success 0 is returned. If no data is stored under KEY |
| 225 |
GPG_ERR_NOT_FOUND is returned. If the provided buffer is too |
| 226 |
short, GPG_ERR_BUFFER_TOO_SHORT will be returned (note, that this |
| 227 |
is not the case if BUFFER is NULL). |
| 228 |
*/ |
| 229 |
gpg_error_t |
| 230 |
ksba_cert_get_user_data (ksba_cert_t cert, const char *key, |
| 231 |
void *buffer, size_t bufferlen, size_t *datalen) |
| 232 |
{ |
| 233 |
struct cert_user_data *ud; |
| 234 |
|
| 235 |
if (!cert || !key || !*key) |
| 236 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 237 |
|
| 238 |
for (ud=cert->udata; ud; ud = ud->next) |
| 239 |
if (!strcmp (ud->key, key)) |
| 240 |
break; |
| 241 |
if (!ud || !ud->data) |
| 242 |
return gpg_error (GPG_ERR_NOT_FOUND); |
| 243 |
if (datalen) |
| 244 |
*datalen = ud->datalen; |
| 245 |
if (buffer) |
| 246 |
{ |
| 247 |
if (ud->datalen > bufferlen) |
| 248 |
return gpg_error (GPG_ERR_BUFFER_TOO_SHORT); |
| 249 |
memcpy (buffer, ud->data, ud->datalen); |
| 250 |
} |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* ksba_cert_read_der: |
| 257 |
* @cert: An unitialized certificate object |
| 258 |
* @reader: A KSBA Reader object |
| 259 |
* |
| 260 |
* Read the next certificate from the reader and store it in the |
| 261 |
* certificate object for future access. The certificate is parsed |
| 262 |
* and rejected if it has any syntactical or semantical error |
| 263 |
* (i.e. does not match the ASN.1 description). |
| 264 |
* |
| 265 |
* Return value: 0 on success or an error value |
| 266 |
**/ |
| 267 |
gpg_error_t |
| 268 |
ksba_cert_read_der (ksba_cert_t cert, ksba_reader_t reader) |
| 269 |
{ |
| 270 |
gpg_error_t err = 0; |
| 271 |
BerDecoder decoder = NULL; |
| 272 |
|
| 273 |
if (!cert || !reader) |
| 274 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 275 |
if (cert->initialized) |
| 276 |
return gpg_error (GPG_ERR_CONFLICT); /* Fixme: should remove the old one */ |
| 277 |
|
| 278 |
_ksba_asn_release_nodes (cert->root); |
| 279 |
ksba_asn_tree_release (cert->asn_tree); |
| 280 |
cert->root = NULL; |
| 281 |
cert->asn_tree = NULL; |
| 282 |
|
| 283 |
err = ksba_asn_create_tree ("tmttv2", &cert->asn_tree); |
| 284 |
if (err) |
| 285 |
goto leave; |
| 286 |
|
| 287 |
decoder = _ksba_ber_decoder_new (); |
| 288 |
if (!decoder) |
| 289 |
{ |
| 290 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 291 |
goto leave; |
| 292 |
} |
| 293 |
|
| 294 |
err = _ksba_ber_decoder_set_reader (decoder, reader); |
| 295 |
if (err) |
| 296 |
goto leave; |
| 297 |
|
| 298 |
err = _ksba_ber_decoder_set_module (decoder, cert->asn_tree); |
| 299 |
if (err) |
| 300 |
goto leave; |
| 301 |
|
| 302 |
err = _ksba_ber_decoder_decode (decoder, "TMTTv2.Certificate", 0, |
| 303 |
&cert->root, &cert->image, &cert->imagelen); |
| 304 |
if (!err) |
| 305 |
cert->initialized = 1; |
| 306 |
|
| 307 |
leave: |
| 308 |
_ksba_ber_decoder_release (decoder); |
| 309 |
|
| 310 |
return err; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
gpg_error_t |
| 315 |
ksba_cert_init_from_mem (ksba_cert_t cert, const void *buffer, size_t length) |
| 316 |
{ |
| 317 |
gpg_error_t err; |
| 318 |
ksba_reader_t reader; |
| 319 |
|
| 320 |
err = ksba_reader_new (&reader); |
| 321 |
if (err) |
| 322 |
return err; |
| 323 |
err = ksba_reader_set_mem (reader, buffer, length); |
| 324 |
if (err) |
| 325 |
{ |
| 326 |
ksba_reader_release (reader); |
| 327 |
return err; |
| 328 |
} |
| 329 |
err = ksba_cert_read_der (cert, reader); |
| 330 |
ksba_reader_release (reader); |
| 331 |
return err; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
const unsigned char * |
| 337 |
ksba_cert_get_image (ksba_cert_t cert, size_t *r_length ) |
| 338 |
{ |
| 339 |
AsnNode n; |
| 340 |
|
| 341 |
if (!cert) |
| 342 |
return NULL; |
| 343 |
if (!cert->initialized) |
| 344 |
return NULL; |
| 345 |
|
| 346 |
n = _ksba_asn_find_node (cert->root, "Certificate"); |
| 347 |
if (!n) |
| 348 |
return NULL; |
| 349 |
|
| 350 |
if (n->off == -1) |
| 351 |
{ |
| 352 |
/* fputs ("ksba_cert_get_image problem at node:\n", stderr); */ |
| 353 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 354 |
return NULL; |
| 355 |
} |
| 356 |
|
| 357 |
/* Due to minor problems in our parser we might hit the assertion |
| 358 |
below. Thus we better return a error, proper. */ |
| 359 |
if ( !(n->nhdr + n->len + n->off <= cert->imagelen) ) |
| 360 |
{ |
| 361 |
fprintf (stderr,"\nOops, ksba_cert_get_image failed: " |
| 362 |
"imagelen=%lu hdr=%d len=%d off=%d\n", |
| 363 |
(unsigned long)cert->imagelen, n->nhdr, (int)n->len, n->off); |
| 364 |
return NULL; |
| 365 |
} |
| 366 |
/*assert (n->nhdr + n->len + n->off <= cert->imagelen);*/ |
| 367 |
|
| 368 |
if (r_length) |
| 369 |
*r_length = n->nhdr + n->len; |
| 370 |
return cert->image + n->off; |
| 371 |
} |
| 372 |
|
| 373 |
/* Check whether certificates A and B are identical and return o in |
| 374 |
this case. */ |
| 375 |
int |
| 376 |
_ksba_cert_cmp (ksba_cert_t a, ksba_cert_t b) |
| 377 |
{ |
| 378 |
const unsigned char *img_a, *img_b; |
| 379 |
size_t len_a, len_b; |
| 380 |
|
| 381 |
img_a = ksba_cert_get_image (a, &len_a); |
| 382 |
if (!img_a) |
| 383 |
return 1; |
| 384 |
img_b = ksba_cert_get_image (b, &len_b); |
| 385 |
if (!img_b) |
| 386 |
return 1; |
| 387 |
return !(len_a == len_b && !memcmp (img_a, img_b, len_a)); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
gpg_error_t |
| 394 |
ksba_cert_hash (ksba_cert_t cert, int what, |
| 395 |
void (*hasher)(void *, const void *, size_t length), |
| 396 |
void *hasher_arg) |
| 397 |
{ |
| 398 |
AsnNode n; |
| 399 |
|
| 400 |
if (!cert /*|| !hasher*/) |
| 401 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 402 |
if (!cert->initialized) |
| 403 |
return gpg_error (GPG_ERR_NO_DATA); |
| 404 |
|
| 405 |
n = _ksba_asn_find_node (cert->root, |
| 406 |
what == 1? "Certificate.tbsCertificate" |
| 407 |
: "Certificate"); |
| 408 |
if (!n) |
| 409 |
return gpg_error (GPG_ERR_NO_VALUE); /* oops - should be there */ |
| 410 |
if (n->off == -1) |
| 411 |
{ |
| 412 |
/* fputs ("ksba_cert_hash problem at node:\n", stderr); */ |
| 413 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 414 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 415 |
} |
| 416 |
|
| 417 |
hasher (hasher_arg, cert->image + n->off, n->nhdr + n->len); |
| 418 |
|
| 419 |
|
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
/** |
| 426 |
* ksba_cert_get_digest_algo: |
| 427 |
* @cert: Initialized certificate object |
| 428 |
* |
| 429 |
* Figure out the digest algorithm used for the signature and return |
| 430 |
* its OID. Note that in the case of rsaPSS the returned value is the |
| 431 |
* OID of rsaPSS (1.2.840.113549.1.1.10) and not the hash algorithm to |
| 432 |
* use. The hash algorithm needs to be extracted from the S-expression |
| 433 |
* returned by ksba_cert_get_sig_val. |
| 434 |
* |
| 435 |
* This function is intended as a helper for the ksba_cert_hash(). |
| 436 |
* |
| 437 |
* Return value: NULL for error otherwise a constant string with the OID. |
| 438 |
* This string is valid as long the certificate object is valid. |
| 439 |
**/ |
| 440 |
const char * |
| 441 |
ksba_cert_get_digest_algo (ksba_cert_t cert) |
| 442 |
{ |
| 443 |
gpg_error_t err; |
| 444 |
AsnNode n; |
| 445 |
char *algo; |
| 446 |
size_t nread; |
| 447 |
|
| 448 |
if (!cert) |
| 449 |
return NULL; /* Ooops (can't set cert->last_error :-(). */ |
| 450 |
|
| 451 |
if (!cert->initialized) |
| 452 |
{ |
| 453 |
cert->last_error = gpg_error (GPG_ERR_NO_DATA); |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
|
| 457 |
if (cert->cache.digest_algo) |
| 458 |
return cert->cache.digest_algo; |
| 459 |
|
| 460 |
/* n = _ksba_asn_find_node (cert->root, */ |
| 461 |
/* "Certificate.signatureAlgorithm.algorithm"); */ |
| 462 |
/* algo = _ksba_oid_node_to_str (cert->image, n); */ |
| 463 |
/* if (!algo) */ |
| 464 |
/* cert->last_error = gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); */ |
| 465 |
/* else */ |
| 466 |
/* cert->cache.digest_algo = algo; */ |
| 467 |
|
| 468 |
n = _ksba_asn_find_node (cert->root, "Certificate.signatureAlgorithm"); |
| 469 |
if (!n || n->off == -1) |
| 470 |
{ |
| 471 |
algo = NULL; |
| 472 |
err = gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); |
| 473 |
} |
| 474 |
else |
| 475 |
err = _ksba_parse_algorithm_identifier (cert->image + n->off, |
| 476 |
n->nhdr + n->len, &nread, &algo); |
| 477 |
if (err) |
| 478 |
cert->last_error = err; |
| 479 |
else |
| 480 |
cert->cache.digest_algo = algo; |
| 481 |
|
| 482 |
return algo; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
/** |
| 489 |
* ksba_cert_get_serial: |
| 490 |
* @cert: certificate object |
| 491 |
* |
| 492 |
* This function returnes the serial number of the certificate. The |
| 493 |
* serial number is an integer returned as an canonical encoded |
| 494 |
* S-expression with just one element. |
| 495 |
* |
| 496 |
* Return value: An allocated S-Exp or NULL for no value. |
| 497 |
**/ |
| 498 |
ksba_sexp_t |
| 499 |
ksba_cert_get_serial (ksba_cert_t cert) |
| 500 |
{ |
| 501 |
AsnNode n; |
| 502 |
char *p; |
| 503 |
char numbuf[22]; |
| 504 |
int numbuflen; |
| 505 |
|
| 506 |
if (!cert || !cert->initialized) |
| 507 |
return NULL; |
| 508 |
|
| 509 |
n = _ksba_asn_find_node (cert->root, |
| 510 |
"Certificate.tbsCertificate.serialNumber"); |
| 511 |
if (!n) |
| 512 |
return NULL; /* oops - should be there */ |
| 513 |
|
| 514 |
if (n->off == -1) |
| 515 |
{ |
| 516 |
/* fputs ("get_serial problem at node:\n", stderr); */ |
| 517 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 518 |
return NULL; |
| 519 |
} |
| 520 |
|
| 521 |
if (n->len > MAX_SERIALNO_LENGTH) |
| 522 |
return NULL; |
| 523 |
|
| 524 |
sprintf (numbuf,"(%u:", (unsigned int)n->len); |
| 525 |
numbuflen = strlen (numbuf); |
| 526 |
p = xtrymalloc (numbuflen + n->len + 2); |
| 527 |
if (!p) |
| 528 |
return NULL; |
| 529 |
strcpy (p, numbuf); |
| 530 |
memcpy (p+numbuflen, cert->image + n->off + n->nhdr, n->len); |
| 531 |
p[numbuflen + n->len] = ')'; |
| 532 |
p[numbuflen + n->len + 1] = 0; |
| 533 |
return p; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/* Return a pointer to the DER encoding of the serial number in CERT in |
| 538 |
PTR and the length of that field in LENGTH. */ |
| 539 |
gpg_error_t |
| 540 |
_ksba_cert_get_serial_ptr (ksba_cert_t cert, |
| 541 |
unsigned char const **ptr, size_t *length) |
| 542 |
{ |
| 543 |
asn_node_t n; |
| 544 |
|
| 545 |
if (!cert || !cert->initialized || !ptr || !length) |
| 546 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 547 |
n = _ksba_asn_find_node (cert->root, |
| 548 |
"Certificate.tbsCertificate.serialNumber"); |
| 549 |
if (!n || n->off == -1) |
| 550 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 551 |
|
| 552 |
*ptr = cert->image + n->off; |
| 553 |
*length = n->nhdr + n->len; |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
/* Return a pointer to the DER encoding of the issuer's DN in CERT in |
| 560 |
PTR and the length of that object in LENGTH. */ |
| 561 |
gpg_error_t |
| 562 |
_ksba_cert_get_issuer_dn_ptr (ksba_cert_t cert, |
| 563 |
unsigned char const **ptr, size_t *length) |
| 564 |
{ |
| 565 |
asn_node_t n; |
| 566 |
|
| 567 |
if (!cert || !cert->initialized || !ptr || !length) |
| 568 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 569 |
|
| 570 |
n = _ksba_asn_find_node (cert->root, "Certificate.tbsCertificate.issuer"); |
| 571 |
if (!n || !n->down) |
| 572 |
return gpg_error (GPG_ERR_NO_VALUE); /* oops - should be there */ |
| 573 |
n = n->down; /* dereference the choice node */ |
| 574 |
if (n->off == -1) |
| 575 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 576 |
*ptr = cert->image + n->off; |
| 577 |
*length = n->nhdr + n->len; |
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
/* Return a pointer to the DER encoding of the subject's DN in CERT in |
| 584 |
PTR and the length of that object in LENGTH. */ |
| 585 |
gpg_error_t |
| 586 |
_ksba_cert_get_subject_dn_ptr (ksba_cert_t cert, |
| 587 |
unsigned char const **ptr, size_t *length) |
| 588 |
{ |
| 589 |
asn_node_t n; |
| 590 |
|
| 591 |
if (!cert || !cert->initialized || !ptr || !length) |
| 592 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 593 |
|
| 594 |
n = _ksba_asn_find_node (cert->root, "Certificate.tbsCertificate.subject"); |
| 595 |
if (!n || !n->down) |
| 596 |
return gpg_error (GPG_ERR_NO_VALUE); /* oops - should be there */ |
| 597 |
n = n->down; /* dereference the choice node */ |
| 598 |
if (n->off == -1) |
| 599 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 600 |
*ptr = cert->image + n->off; |
| 601 |
*length = n->nhdr + n->len; |
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
/* Worker function for get_isssuer and get_subject. */ |
| 608 |
static gpg_error_t |
| 609 |
get_name (ksba_cert_t cert, int idx, int use_subject, char **result) |
| 610 |
{ |
| 611 |
gpg_error_t err; |
| 612 |
char *p; |
| 613 |
int i; |
| 614 |
const char *oid; |
| 615 |
struct tag_info ti; |
| 616 |
const unsigned char *der; |
| 617 |
size_t off, derlen, seqlen; |
| 618 |
|
| 619 |
if (!cert || !cert->initialized || !result) |
| 620 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 621 |
if (idx < 0) |
| 622 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 623 |
|
| 624 |
*result = NULL; |
| 625 |
if (!idx) |
| 626 |
{ /* Get the required DN */ |
| 627 |
AsnNode n; |
| 628 |
|
| 629 |
n = _ksba_asn_find_node (cert->root, |
| 630 |
(use_subject? |
| 631 |
"Certificate.tbsCertificate.subject": |
| 632 |
"Certificate.tbsCertificate.issuer") ); |
| 633 |
if (!n || !n->down) |
| 634 |
return gpg_error (GPG_ERR_NO_VALUE); /* oops - should be there */ |
| 635 |
n = n->down; /* dereference the choice node */ |
| 636 |
if (n->off == -1) |
| 637 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 638 |
|
| 639 |
err = _ksba_dn_to_str (cert->image, n, &p); |
| 640 |
if (err) |
| 641 |
return err; |
| 642 |
*result = p; |
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
/* get {issuer,subject}AltName */ |
| 647 |
for (i=0; !(err=ksba_cert_get_extension (cert, i, &oid, NULL, |
| 648 |
&off, &derlen)); i++) |
| 649 |
{ |
| 650 |
if (!strcmp (oid, (use_subject? |
| 651 |
oidstr_subjectAltName:oidstr_issuerAltName))) |
| 652 |
break; |
| 653 |
} |
| 654 |
if (err) |
| 655 |
return err; /* no alt name or error*/ |
| 656 |
|
| 657 |
der = cert->image + off; |
| 658 |
|
| 659 |
|
| 660 |
/* FIXME: We should use _ksba_name_new_from_der and ksba_name_enum here */ |
| 661 |
|
| 662 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 663 |
if (err) |
| 664 |
return err; |
| 665 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 666 |
&& ti.is_constructed) ) |
| 667 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 668 |
if (ti.ndef) |
| 669 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 670 |
seqlen = ti.length; |
| 671 |
if (seqlen > derlen) |
| 672 |
return gpg_error (GPG_ERR_BAD_BER); |
| 673 |
if (!seqlen) |
| 674 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* empty sequence is not allowed */ |
| 675 |
|
| 676 |
while (seqlen) |
| 677 |
{ |
| 678 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 679 |
if (err) |
| 680 |
return err; |
| 681 |
if (ti.class != CLASS_CONTEXT) |
| 682 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 683 |
if (ti.ndef) |
| 684 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 685 |
if (seqlen < ti.nhdr) |
| 686 |
return gpg_error (GPG_ERR_BAD_BER); |
| 687 |
seqlen -= ti.nhdr; |
| 688 |
if (seqlen < ti.length) |
| 689 |
return gpg_error (GPG_ERR_BAD_BER); |
| 690 |
seqlen -= ti.length; |
| 691 |
if (derlen < ti.length) |
| 692 |
return gpg_error (GPG_ERR_BAD_BER); |
| 693 |
if (!(ti.tag == 1 || ti.tag == 2 || ti.tag == 6)) |
| 694 |
; /* Not a supported tag: Do not change IDX. */ |
| 695 |
else if (--idx) |
| 696 |
; /* not yet at the desired index */ |
| 697 |
else if (ti.tag == 1) |
| 698 |
{ /* rfc822Name - this is an implicit IA5_STRING */ |
| 699 |
p = xtrymalloc (ti.length+3); |
| 700 |
if (!p) |
| 701 |
return gpg_error (GPG_ERR_ENOMEM); |
| 702 |
*p = '<'; |
| 703 |
memcpy (p+1, der, ti.length); |
| 704 |
p[ti.length+1] = '>'; |
| 705 |
p[ti.length+2] = 0; |
| 706 |
*result = p; |
| 707 |
return 0; |
| 708 |
} |
| 709 |
else if (ti.tag == 2 || ti.tag == 6) |
| 710 |
{ /* dNSName or URI - this are implicit IA5_STRINGs */ |
| 711 |
char numbuf[20], *numbufp; |
| 712 |
size_t numbuflen; |
| 713 |
|
| 714 |
numbufp = smklen (numbuf, DIM(numbuf), ti.length, &numbuflen); |
| 715 |
p = xtrymalloc (11 + numbuflen + ti.length + 3); |
| 716 |
if (!p) |
| 717 |
return gpg_error (GPG_ERR_ENOMEM); |
| 718 |
*result = p; |
| 719 |
p = stpcpy (p, ti.tag == 2? "(8:dns-name" : "(3:uri"); |
| 720 |
p = stpcpy (p, numbufp); |
| 721 |
memcpy (p, der, ti.length); |
| 722 |
p += ti.length; |
| 723 |
*p++ = ')'; |
| 724 |
*p = 0; |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
/* advance pointer */ |
| 729 |
der += ti.length; |
| 730 |
derlen -= ti.length; |
| 731 |
} |
| 732 |
|
| 733 |
return gpg_error (GPG_ERR_EOF); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
/** |
| 739 |
* ksba_cert_get_issuer: |
| 740 |
* @cert: certificate object |
| 741 |
* |
| 742 |
* With @idx == 0 this function returns the Distinguished Name (DN) of |
| 743 |
* the certificate issuer which in most cases is a CA. The format of |
| 744 |
* the returned string is in accordance with RFC-2253. NULL is |
| 745 |
* returned if the DN is not available which is an error and should |
| 746 |
* have been catched by the certificate reading function. |
| 747 |
* |
| 748 |
* With @idx > 0 the function may be used to enumerate alternate |
| 749 |
* issuer names. The function returns NULL if there are no more |
| 750 |
* alternate names. The function does only return alternate names |
| 751 |
* which are recognized by libksba and ignores others. The format of |
| 752 |
* the returned name is either a RFC-2253 formated one which can be |
| 753 |
* detected by checking whether the first character is letter or |
| 754 |
* digit. rfc-2822 conform email addresses are returned enclosed in |
| 755 |
* angle brackets, the opening angle bracket should be used to |
| 756 |
* indicate this. Other formats are returned as an S-Expression in |
| 757 |
* canonical format, so a opening parenthesis may be used to detect |
| 758 |
* this encoding, the name may include binary null characters, so |
| 759 |
* strlen may return a length shorther than actually used, the real |
| 760 |
* length is implictly given by the structure of the S-Exp, an extra |
| 761 |
* null is appended for safety reasons. |
| 762 |
* |
| 763 |
* The caller must free the returned string using ksba_free() or the |
| 764 |
* function he has registered as a replacement. |
| 765 |
* |
| 766 |
* Return value: An allocated string or NULL for error. |
| 767 |
**/ |
| 768 |
char * |
| 769 |
ksba_cert_get_issuer (ksba_cert_t cert, int idx) |
| 770 |
{ |
| 771 |
gpg_error_t err; |
| 772 |
char *name; |
| 773 |
|
| 774 |
err = get_name (cert, idx, 0, &name); |
| 775 |
if (err) |
| 776 |
{ |
| 777 |
cert->last_error = err; |
| 778 |
return NULL; |
| 779 |
} |
| 780 |
return name; |
| 781 |
} |
| 782 |
|
| 783 |
/* See ..get_issuer */ |
| 784 |
char * |
| 785 |
ksba_cert_get_subject (ksba_cert_t cert, int idx) |
| 786 |
{ |
| 787 |
gpg_error_t err; |
| 788 |
char *name; |
| 789 |
|
| 790 |
err = get_name (cert, idx, 1, &name); |
| 791 |
if (err) |
| 792 |
{ |
| 793 |
cert->last_error = err; |
| 794 |
return NULL; |
| 795 |
} |
| 796 |
return name; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
/** |
| 802 |
* ksba_cert_get_valididy: |
| 803 |
* @cert: certificate object |
| 804 |
* @what: 0 for notBefore, 1 for notAfter |
| 805 |
* @timebuf: Returns the time. |
| 806 |
* |
| 807 |
* Return the validity object from the certificate. If no value is |
| 808 |
* available 0 is returned because we can safely assume that this is |
| 809 |
* not a valid date. |
| 810 |
* |
| 811 |
* Return value: The time value an 0 or an error code. |
| 812 |
**/ |
| 813 |
gpg_error_t |
| 814 |
ksba_cert_get_validity (ksba_cert_t cert, int what, ksba_isotime_t timebuf) |
| 815 |
{ |
| 816 |
AsnNode n, n2; |
| 817 |
|
| 818 |
if (!cert || what < 0 || what > 1) |
| 819 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 820 |
*timebuf = 0; |
| 821 |
if (!cert->initialized) |
| 822 |
return gpg_error (GPG_ERR_NO_DATA); |
| 823 |
|
| 824 |
n = _ksba_asn_find_node (cert->root, |
| 825 |
what == 0? "Certificate.tbsCertificate.validity.notBefore" |
| 826 |
: "Certificate.tbsCertificate.validity.notAfter"); |
| 827 |
if (!n) |
| 828 |
return 0; /* no value available */ |
| 829 |
|
| 830 |
/* Fixme: We should remove the choice node and don't use this ugly hack */ |
| 831 |
for (n2=n->down; n2; n2 = n2->right) |
| 832 |
{ |
| 833 |
if ((n2->type == TYPE_UTC_TIME || n2->type == TYPE_GENERALIZED_TIME) |
| 834 |
&& n2->off != -1) |
| 835 |
break; |
| 836 |
} |
| 837 |
n = n2; |
| 838 |
if (!n) |
| 839 |
return 0; /* no value available */ |
| 840 |
|
| 841 |
return_val_if_fail (n->off != -1, gpg_error (GPG_ERR_BUG)); |
| 842 |
|
| 843 |
return _ksba_asntime_to_iso (cert->image + n->off + n->nhdr, n->len, |
| 844 |
n->type == TYPE_UTC_TIME, timebuf); |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
ksba_sexp_t |
| 850 |
ksba_cert_get_public_key (ksba_cert_t cert) |
| 851 |
{ |
| 852 |
AsnNode n; |
| 853 |
gpg_error_t err; |
| 854 |
ksba_sexp_t string; |
| 855 |
|
| 856 |
if (!cert) |
| 857 |
return NULL; |
| 858 |
if (!cert->initialized) |
| 859 |
return NULL; |
| 860 |
|
| 861 |
n = _ksba_asn_find_node (cert->root, |
| 862 |
"Certificate" |
| 863 |
".tbsCertificate.subjectPublicKeyInfo"); |
| 864 |
if (!n) |
| 865 |
{ |
| 866 |
cert->last_error = gpg_error (GPG_ERR_NO_VALUE); |
| 867 |
return NULL; |
| 868 |
} |
| 869 |
|
| 870 |
err = _ksba_keyinfo_to_sexp (cert->image + n->off, n->nhdr + n->len, |
| 871 |
&string); |
| 872 |
if (err) |
| 873 |
{ |
| 874 |
cert->last_error = err; |
| 875 |
return NULL; |
| 876 |
} |
| 877 |
|
| 878 |
return string; |
| 879 |
} |
| 880 |
|
| 881 |
/* Return a pointer to the DER encoding of the actual public key |
| 882 |
(i.e. the bit string) in PTR and the length of that object in |
| 883 |
LENGTH. */ |
| 884 |
gpg_error_t |
| 885 |
_ksba_cert_get_public_key_ptr (ksba_cert_t cert, |
| 886 |
unsigned char const **ptr, size_t *length) |
| 887 |
{ |
| 888 |
asn_node_t n; |
| 889 |
|
| 890 |
if (!cert || !cert->initialized || !ptr || !length) |
| 891 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 892 |
|
| 893 |
n = _ksba_asn_find_node (cert->root, |
| 894 |
"Certificate.tbsCertificate.subjectPublicKeyInfo"); |
| 895 |
if (!n || !n->down || !n->down->right) |
| 896 |
return gpg_error (GPG_ERR_NO_VALUE); /* oops - should be there */ |
| 897 |
n = n->down->right; |
| 898 |
if (n->off == -1) |
| 899 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 900 |
*ptr = cert->image + n->off + n->nhdr; |
| 901 |
*length = n->len; |
| 902 |
/* Somehow we end up at the preceding NULL value, and not at a |
| 903 |
sequence, we hack it way here. */ |
| 904 |
if (*length && !**ptr) |
| 905 |
{ |
| 906 |
(*length)--; |
| 907 |
(*ptr)++; |
| 908 |
} |
| 909 |
|
| 910 |
return 0; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
ksba_sexp_t |
| 916 |
ksba_cert_get_sig_val (ksba_cert_t cert) |
| 917 |
{ |
| 918 |
AsnNode n, n2; |
| 919 |
gpg_error_t err; |
| 920 |
ksba_sexp_t string; |
| 921 |
|
| 922 |
if (!cert) |
| 923 |
return NULL; |
| 924 |
if (!cert->initialized) |
| 925 |
return NULL; |
| 926 |
|
| 927 |
n = _ksba_asn_find_node (cert->root, |
| 928 |
"Certificate.signatureAlgorithm"); |
| 929 |
if (!n) |
| 930 |
{ |
| 931 |
cert->last_error = gpg_error (GPG_ERR_NO_VALUE); |
| 932 |
return NULL; |
| 933 |
} |
| 934 |
if (n->off == -1) |
| 935 |
{ |
| 936 |
/* fputs ("ksba_cert_get_sig_val problem at node:\n", stderr); */ |
| 937 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 938 |
cert->last_error = gpg_error (GPG_ERR_NO_VALUE); |
| 939 |
return NULL; |
| 940 |
} |
| 941 |
|
| 942 |
n2 = n->right; |
| 943 |
err = _ksba_sigval_to_sexp (cert->image + n->off, |
| 944 |
n->nhdr + n->len |
| 945 |
+ ((!n2||n2->off == -1)? 0:(n2->nhdr+n2->len)), |
| 946 |
&string); |
| 947 |
if (err) |
| 948 |
{ |
| 949 |
cert->last_error = err; |
| 950 |
return NULL; |
| 951 |
} |
| 952 |
|
| 953 |
return string; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
/* Read all extensions into the cache */ |
| 958 |
static gpg_error_t |
| 959 |
read_extensions (ksba_cert_t cert) |
| 960 |
{ |
| 961 |
AsnNode start, n; |
| 962 |
int count; |
| 963 |
|
| 964 |
assert (!cert->cache.extns_valid); |
| 965 |
assert (!cert->cache.extns); |
| 966 |
|
| 967 |
start = _ksba_asn_find_node (cert->root, |
| 968 |
"Certificate.tbsCertificate.extensions.."); |
| 969 |
for (count=0, n=start; n; n = n->right) |
| 970 |
count++; |
| 971 |
if (!count) |
| 972 |
{ |
| 973 |
cert->cache.n_extns = 0; |
| 974 |
cert->cache.extns_valid = 1; |
| 975 |
return 0; /* no extensions at all */ |
| 976 |
} |
| 977 |
cert->cache.extns = xtrycalloc (count, sizeof *cert->cache.extns); |
| 978 |
if (!cert->cache.extns) |
| 979 |
return gpg_error (GPG_ERR_ENOMEM); |
| 980 |
cert->cache.n_extns = count; |
| 981 |
|
| 982 |
{ |
| 983 |
for (count=0; start; start = start->right, count++) |
| 984 |
{ |
| 985 |
n = start->down; |
| 986 |
if (!n || n->type != TYPE_OBJECT_ID) |
| 987 |
goto no_value; |
| 988 |
|
| 989 |
cert->cache.extns[count].oid = _ksba_oid_node_to_str (cert->image, n); |
| 990 |
if (!cert->cache.extns[count].oid) |
| 991 |
goto no_value; |
| 992 |
|
| 993 |
n = n->right; |
| 994 |
if (n && n->type == TYPE_BOOLEAN) |
| 995 |
{ |
| 996 |
if (n->off != -1 && n->len && cert->image[n->off + n->nhdr]) |
| 997 |
cert->cache.extns[count].crit = 1; |
| 998 |
n = n->right; |
| 999 |
} |
| 1000 |
|
| 1001 |
if (!n || n->type != TYPE_OCTET_STRING || n->off == -1) |
| 1002 |
goto no_value; |
| 1003 |
|
| 1004 |
cert->cache.extns[count].off = n->off + n->nhdr; |
| 1005 |
cert->cache.extns[count].len = n->len; |
| 1006 |
} |
| 1007 |
|
| 1008 |
assert (count == cert->cache.n_extns); |
| 1009 |
cert->cache.extns_valid = 1; |
| 1010 |
return 0; |
| 1011 |
|
| 1012 |
no_value: |
| 1013 |
for (count=0; count < cert->cache.n_extns; count++) |
| 1014 |
xfree (cert->cache.extns[count].oid); |
| 1015 |
xfree (cert->cache.extns); |
| 1016 |
cert->cache.extns = NULL; |
| 1017 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
/* Return information about the IDX nth extension */ |
| 1023 |
gpg_error_t |
| 1024 |
ksba_cert_get_extension (ksba_cert_t cert, int idx, |
| 1025 |
char const **r_oid, int *r_crit, |
| 1026 |
size_t *r_deroff, size_t *r_derlen) |
| 1027 |
{ |
| 1028 |
gpg_error_t err; |
| 1029 |
|
| 1030 |
if (!cert) |
| 1031 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1032 |
if (!cert->initialized) |
| 1033 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1034 |
|
| 1035 |
if (!cert->cache.extns_valid) |
| 1036 |
{ |
| 1037 |
err = read_extensions (cert); |
| 1038 |
if (err) |
| 1039 |
return err; |
| 1040 |
assert (cert->cache.extns_valid); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (idx == cert->cache.n_extns) |
| 1044 |
return gpg_error (GPG_ERR_EOF); /* No more extensions. */ |
| 1045 |
|
| 1046 |
if (idx < 0 || idx >= cert->cache.n_extns) |
| 1047 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1048 |
|
| 1049 |
if (r_oid) |
| 1050 |
*r_oid = cert->cache.extns[idx].oid; |
| 1051 |
if (r_crit) |
| 1052 |
*r_crit = cert->cache.extns[idx].crit; |
| 1053 |
if (r_deroff) |
| 1054 |
*r_deroff = cert->cache.extns[idx].off; |
| 1055 |
if (r_derlen) |
| 1056 |
*r_derlen = cert->cache.extns[idx].len; |
| 1057 |
return 0; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
/* Return information on the basicConstraint (2.5.19.19) of CERT. |
| 1063 |
R_CA receives true if this is a CA and only in that case R_PATHLEN |
| 1064 |
is set to the maximim certification path length or -1 if there is |
| 1065 |
nosuch limitation */ |
| 1066 |
gpg_error_t |
| 1067 |
ksba_cert_is_ca (ksba_cert_t cert, int *r_ca, int *r_pathlen) |
| 1068 |
{ |
| 1069 |
gpg_error_t err; |
| 1070 |
const char *oid; |
| 1071 |
int idx, crit; |
| 1072 |
size_t off, derlen, seqlen; |
| 1073 |
const unsigned char *der; |
| 1074 |
struct tag_info ti; |
| 1075 |
unsigned long value; |
| 1076 |
|
| 1077 |
/* set default values */ |
| 1078 |
if (r_ca) |
| 1079 |
*r_ca = 0; |
| 1080 |
if (r_pathlen) |
| 1081 |
*r_pathlen = -1; |
| 1082 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &oid, &crit, |
| 1083 |
&off, &derlen)); idx++) |
| 1084 |
{ |
| 1085 |
if (!strcmp (oid, oidstr_basicConstraints)) |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
if (gpg_err_code (err) == GPG_ERR_EOF) |
| 1089 |
return 0; /* no such constraint */ |
| 1090 |
if (err) |
| 1091 |
return err; |
| 1092 |
|
| 1093 |
/* check that there is only one */ |
| 1094 |
for (idx++; !(err=ksba_cert_get_extension (cert, idx, &oid, NULL, |
| 1095 |
NULL, NULL)); idx++) |
| 1096 |
{ |
| 1097 |
if (!strcmp (oid, oidstr_basicConstraints)) |
| 1098 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 1099 |
} |
| 1100 |
|
| 1101 |
der = cert->image + off; |
| 1102 |
|
| 1103 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1104 |
if (err) |
| 1105 |
return err; |
| 1106 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1107 |
&& ti.is_constructed) ) |
| 1108 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1109 |
if (ti.ndef) |
| 1110 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1111 |
seqlen = ti.length; |
| 1112 |
if (seqlen > derlen) |
| 1113 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1114 |
if (!seqlen) |
| 1115 |
return 0; /* an empty sequence is allowed because both elements |
| 1116 |
are optional */ |
| 1117 |
|
| 1118 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1119 |
if (err) |
| 1120 |
return err; |
| 1121 |
if (seqlen < ti.nhdr) |
| 1122 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1123 |
seqlen -= ti.nhdr; |
| 1124 |
if (seqlen < ti.length) |
| 1125 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1126 |
seqlen -= ti.length; |
| 1127 |
|
| 1128 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_BOOLEAN) |
| 1129 |
{ |
| 1130 |
if (ti.length != 1) |
| 1131 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 1132 |
if (r_ca) |
| 1133 |
*r_ca = !!*der; |
| 1134 |
der++; derlen--; |
| 1135 |
if (!seqlen) |
| 1136 |
return 0; /* ready (no pathlength) */ |
| 1137 |
|
| 1138 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1139 |
if (err) |
| 1140 |
return err; |
| 1141 |
if (seqlen < ti.nhdr) |
| 1142 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1143 |
seqlen -= ti.nhdr; |
| 1144 |
if (seqlen < ti.length) |
| 1145 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1146 |
seqlen -= ti.length; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (!(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_INTEGER)) |
| 1150 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1151 |
|
| 1152 |
for (value=0; ti.length; ti.length--) |
| 1153 |
{ |
| 1154 |
value <<= 8; |
| 1155 |
value |= (*der++) & 0xff; |
| 1156 |
derlen--; |
| 1157 |
} |
| 1158 |
if (r_pathlen) |
| 1159 |
*r_pathlen = value; |
| 1160 |
|
| 1161 |
/* if the extension is marked as critical and any stuff is still |
| 1162 |
left we better return an error */ |
| 1163 |
if (crit && seqlen) |
| 1164 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1165 |
|
| 1166 |
return 0; |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
/* Get the key usage flags. The function returns Ksba_No_Data if no |
| 1172 |
key usage is specified. */ |
| 1173 |
gpg_error_t |
| 1174 |
ksba_cert_get_key_usage (ksba_cert_t cert, unsigned int *r_flags) |
| 1175 |
{ |
| 1176 |
gpg_error_t err; |
| 1177 |
const char *oid; |
| 1178 |
int idx, crit; |
| 1179 |
size_t off, derlen; |
| 1180 |
const unsigned char *der; |
| 1181 |
struct tag_info ti; |
| 1182 |
unsigned int bits, mask; |
| 1183 |
int i, unused, full; |
| 1184 |
|
| 1185 |
if (!r_flags) |
| 1186 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1187 |
*r_flags = 0; |
| 1188 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &oid, &crit, |
| 1189 |
&off, &derlen)); idx++) |
| 1190 |
{ |
| 1191 |
if (!strcmp (oid, oidstr_keyUsage)) |
| 1192 |
break; |
| 1193 |
} |
| 1194 |
if (gpg_err_code (err) == GPG_ERR_EOF |
| 1195 |
|| gpg_err_code (err) == GPG_ERR_NO_VALUE) |
| 1196 |
return gpg_error (GPG_ERR_NO_DATA); /* no key usage */ |
| 1197 |
if (err) |
| 1198 |
return err; |
| 1199 |
|
| 1200 |
/* check that there is only one */ |
| 1201 |
for (idx++; !(err=ksba_cert_get_extension (cert, idx, &oid, NULL, |
| 1202 |
NULL, NULL)); idx++) |
| 1203 |
{ |
| 1204 |
if (!strcmp (oid, oidstr_keyUsage)) |
| 1205 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 1206 |
} |
| 1207 |
|
| 1208 |
der = cert->image + off; |
| 1209 |
|
| 1210 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1211 |
if (err) |
| 1212 |
return err; |
| 1213 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_BIT_STRING |
| 1214 |
&& !ti.is_constructed) ) |
| 1215 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1216 |
if (ti.ndef) |
| 1217 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1218 |
if (!ti.length || ti.length > derlen) |
| 1219 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); /* number of unused bits missing */ |
| 1220 |
unused = *der++; derlen--; |
| 1221 |
ti.length--; |
| 1222 |
if ((!ti.length && unused) || unused/8 > ti.length) |
| 1223 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 1224 |
|
| 1225 |
full = ti.length - (unused+7)/8; |
| 1226 |
unused %= 8; |
| 1227 |
mask = 0; |
| 1228 |
for (i=1; unused; i <<= 1, unused--) |
| 1229 |
mask |= i; |
| 1230 |
|
| 1231 |
/* the first octet */ |
| 1232 |
if (!ti.length) |
| 1233 |
return 0; /* no bits set */ |
| 1234 |
bits = *der++; derlen--; ti.length--; |
| 1235 |
if (full) |
| 1236 |
full--; |
| 1237 |
else { |
| 1238 |
bits &= ~mask; |
| 1239 |
mask = 0; |
| 1240 |
} |
| 1241 |
if (bits & 0x80) |
| 1242 |
*r_flags |= KSBA_KEYUSAGE_DIGITAL_SIGNATURE; |
| 1243 |
if (bits & 0x40) |
| 1244 |
*r_flags |= KSBA_KEYUSAGE_NON_REPUDIATION; |
| 1245 |
if (bits & 0x20) |
| 1246 |
*r_flags |= KSBA_KEYUSAGE_KEY_ENCIPHERMENT; |
| 1247 |
if (bits & 0x10) |
| 1248 |
*r_flags |= KSBA_KEYUSAGE_DATA_ENCIPHERMENT; |
| 1249 |
if (bits & 0x08) |
| 1250 |
*r_flags |= KSBA_KEYUSAGE_KEY_AGREEMENT; |
| 1251 |
if (bits & 0x04) |
| 1252 |
*r_flags |= KSBA_KEYUSAGE_KEY_CERT_SIGN; |
| 1253 |
if (bits & 0x02) |
| 1254 |
*r_flags |= KSBA_KEYUSAGE_CRL_SIGN; |
| 1255 |
if (bits & 0x01) |
| 1256 |
*r_flags |= KSBA_KEYUSAGE_ENCIPHER_ONLY; |
| 1257 |
|
| 1258 |
/* the second octet */ |
| 1259 |
if (!ti.length) |
| 1260 |
return 0; /* no bits set */ |
| 1261 |
bits = *der++; derlen--; ti.length--; |
| 1262 |
if (full) |
| 1263 |
full--; |
| 1264 |
else { |
| 1265 |
bits &= mask; |
| 1266 |
mask = ~0; |
| 1267 |
} |
| 1268 |
if (bits & 0x80) |
| 1269 |
*r_flags |= KSBA_KEYUSAGE_DECIPHER_ONLY; |
| 1270 |
|
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
/* Note, that this helper is also used for ext_key_usage. */ |
| 1277 |
static gpg_error_t |
| 1278 |
append_cert_policy (char **policies, const char *oid, int crit) |
| 1279 |
{ |
| 1280 |
char *p; |
| 1281 |
|
| 1282 |
if (!*policies) |
| 1283 |
{ |
| 1284 |
*policies = xtrymalloc (strlen (oid) + 4); |
| 1285 |
if (!*policies) |
| 1286 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1287 |
p = *policies; |
| 1288 |
} |
| 1289 |
else |
| 1290 |
{ |
| 1291 |
char *tmp = xtryrealloc (*policies, |
| 1292 |
strlen(*policies) + 1 + strlen (oid) + 4); |
| 1293 |
if (!tmp) |
| 1294 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1295 |
*policies = tmp; |
| 1296 |
p = stpcpy (tmp+strlen (tmp), "\n");; |
| 1297 |
} |
| 1298 |
|
| 1299 |
strcpy (stpcpy (p, oid), crit? ":C:": ":N:"); |
| 1300 |
return 0; |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
/* Return a string with the certificatePolicies delimited by |
| 1305 |
linefeeds. The return values may be extended to carry more |
| 1306 |
information er line, so the caller should only use the first |
| 1307 |
white-space delimited token per line. The function returns |
| 1308 |
GPG_ERR_NO_DATA when this extension is not used. Caller must free |
| 1309 |
the returned value. */ |
| 1310 |
gpg_error_t |
| 1311 |
ksba_cert_get_cert_policies (ksba_cert_t cert, char **r_policies) |
| 1312 |
{ |
| 1313 |
gpg_error_t err; |
| 1314 |
const char *oid; |
| 1315 |
int idx, crit; |
| 1316 |
size_t off, derlen, seqlen; |
| 1317 |
const unsigned char *der; |
| 1318 |
struct tag_info ti; |
| 1319 |
|
| 1320 |
if (!cert || !r_policies) |
| 1321 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1322 |
*r_policies = NULL; |
| 1323 |
|
| 1324 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &oid, &crit, |
| 1325 |
&off, &derlen)); idx++) |
| 1326 |
{ |
| 1327 |
if (!strcmp (oid, oidstr_certificatePolicies)) |
| 1328 |
{ |
| 1329 |
char *suboid; |
| 1330 |
|
| 1331 |
der = cert->image + off; |
| 1332 |
|
| 1333 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1334 |
if (err) |
| 1335 |
goto leave; |
| 1336 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1337 |
&& ti.is_constructed) ) |
| 1338 |
{ |
| 1339 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1340 |
goto leave; |
| 1341 |
} |
| 1342 |
if (ti.ndef) |
| 1343 |
{ |
| 1344 |
err = gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1345 |
goto leave; |
| 1346 |
} |
| 1347 |
seqlen = ti.length; |
| 1348 |
if (seqlen > derlen) |
| 1349 |
{ |
| 1350 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1351 |
goto leave; |
| 1352 |
} |
| 1353 |
while (seqlen) |
| 1354 |
{ |
| 1355 |
size_t seqseqlen; |
| 1356 |
|
| 1357 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1358 |
if (err) |
| 1359 |
goto leave; |
| 1360 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1361 |
&& ti.is_constructed) ) |
| 1362 |
{ |
| 1363 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1364 |
goto leave; |
| 1365 |
} |
| 1366 |
if (ti.ndef) |
| 1367 |
{ |
| 1368 |
err = gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1369 |
goto leave; |
| 1370 |
} |
| 1371 |
if (ti.length > derlen) |
| 1372 |
{ |
| 1373 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1374 |
goto leave; |
| 1375 |
} |
| 1376 |
if (!ti.length) |
| 1377 |
{ |
| 1378 |
/* We do not accept an empty inner SEQ */ |
| 1379 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1380 |
goto leave; |
| 1381 |
} |
| 1382 |
if (ti.nhdr+ti.length > seqlen) |
| 1383 |
{ |
| 1384 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1385 |
goto leave; |
| 1386 |
} |
| 1387 |
seqlen -= ti.nhdr + ti.length; |
| 1388 |
seqseqlen = ti.length; |
| 1389 |
|
| 1390 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1391 |
if (err) |
| 1392 |
goto leave; |
| 1393 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OBJECT_ID)) |
| 1394 |
{ |
| 1395 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1396 |
goto leave; |
| 1397 |
} |
| 1398 |
if (ti.length > derlen) |
| 1399 |
{ |
| 1400 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1401 |
goto leave; |
| 1402 |
} |
| 1403 |
if (ti.nhdr+ti.length > seqseqlen) |
| 1404 |
{ |
| 1405 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1406 |
goto leave; |
| 1407 |
} |
| 1408 |
seqseqlen -= ti.nhdr; |
| 1409 |
|
| 1410 |
suboid = ksba_oid_to_str (der, ti.length); |
| 1411 |
if (!suboid) |
| 1412 |
{ |
| 1413 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 1414 |
goto leave; |
| 1415 |
} |
| 1416 |
der += ti.length; |
| 1417 |
derlen -= ti.length; |
| 1418 |
seqseqlen -= ti.length; |
| 1419 |
|
| 1420 |
err = append_cert_policy (r_policies, suboid, crit); |
| 1421 |
xfree (suboid); |
| 1422 |
if (err) |
| 1423 |
goto leave; |
| 1424 |
|
| 1425 |
/* skip the rest of the seq which is more or less optional */ |
| 1426 |
der += seqseqlen; |
| 1427 |
derlen -= seqseqlen; |
| 1428 |
} |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
if (gpg_err_code (err) == GPG_ERR_EOF) |
| 1433 |
err = 0; |
| 1434 |
if (!*r_policies || gpg_err_code (err) == GPG_ERR_NO_VALUE) |
| 1435 |
err = gpg_error (GPG_ERR_NO_DATA); |
| 1436 |
|
| 1437 |
leave: |
| 1438 |
if (err) |
| 1439 |
{ |
| 1440 |
xfree (*r_policies); |
| 1441 |
*r_policies = NULL; |
| 1442 |
} |
| 1443 |
return err; |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
/* Return a string with the extendedKeyUsageOIDs delimited by |
| 1448 |
linefeeds. The return values may be extended to carry more |
| 1449 |
information per line, so the caller should only use the first |
| 1450 |
white-space delimited token per line. The function returns |
| 1451 |
GPG_ERR_NO_DATA when this extension is not used. Caller must free |
| 1452 |
the returned value. */ |
| 1453 |
gpg_error_t |
| 1454 |
ksba_cert_get_ext_key_usages (ksba_cert_t cert, char **result) |
| 1455 |
{ |
| 1456 |
gpg_error_t err; |
| 1457 |
const char *oid; |
| 1458 |
int idx, crit; |
| 1459 |
size_t off, derlen; |
| 1460 |
const unsigned char *der; |
| 1461 |
struct tag_info ti; |
| 1462 |
|
| 1463 |
if (!cert || !result) |
| 1464 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1465 |
*result = NULL; |
| 1466 |
|
| 1467 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &oid, &crit, |
| 1468 |
&off, &derlen)); idx++) |
| 1469 |
{ |
| 1470 |
if (!strcmp (oid, oidstr_extKeyUsage)) |
| 1471 |
{ |
| 1472 |
char *suboid; |
| 1473 |
|
| 1474 |
der = cert->image + off; |
| 1475 |
|
| 1476 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1477 |
if (err) |
| 1478 |
goto leave; |
| 1479 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1480 |
&& ti.is_constructed) ) |
| 1481 |
{ |
| 1482 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1483 |
goto leave; |
| 1484 |
} |
| 1485 |
if (ti.ndef) |
| 1486 |
{ |
| 1487 |
err = gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1488 |
goto leave; |
| 1489 |
} |
| 1490 |
if (ti.length > derlen) |
| 1491 |
{ |
| 1492 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1493 |
goto leave; |
| 1494 |
} |
| 1495 |
while (derlen) |
| 1496 |
{ |
| 1497 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1498 |
if (err) |
| 1499 |
goto leave; |
| 1500 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OBJECT_ID)) |
| 1501 |
{ |
| 1502 |
err = gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1503 |
goto leave; |
| 1504 |
} |
| 1505 |
if (ti.ndef) |
| 1506 |
{ |
| 1507 |
err = gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1508 |
goto leave; |
| 1509 |
} |
| 1510 |
if (ti.length > derlen) |
| 1511 |
{ |
| 1512 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 1513 |
goto leave; |
| 1514 |
} |
| 1515 |
|
| 1516 |
suboid = ksba_oid_to_str (der, ti.length); |
| 1517 |
if (!suboid) |
| 1518 |
{ |
| 1519 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 1520 |
goto leave; |
| 1521 |
} |
| 1522 |
der += ti.length; |
| 1523 |
derlen -= ti.length; |
| 1524 |
|
| 1525 |
err = append_cert_policy (result, suboid, crit); |
| 1526 |
xfree (suboid); |
| 1527 |
if (err) |
| 1528 |
goto leave; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
if (gpg_err_code (err) == GPG_ERR_EOF) |
| 1534 |
err = 0; |
| 1535 |
if (!*result || gpg_err_code (err) == GPG_ERR_NO_VALUE) |
| 1536 |
err = gpg_error (GPG_ERR_NO_DATA); |
| 1537 |
|
| 1538 |
leave: |
| 1539 |
if (err) |
| 1540 |
{ |
| 1541 |
xfree (*result); |
| 1542 |
*result = NULL; |
| 1543 |
} |
| 1544 |
return err; |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
/* Helper function for ksba_cert_get_crl_dist_point */ |
| 1551 |
static gpg_error_t |
| 1552 |
parse_distribution_point (const unsigned char *der, size_t derlen, |
| 1553 |
ksba_name_t *distpoint, ksba_name_t *issuer, |
| 1554 |
ksba_crl_reason_t *reason) |
| 1555 |
{ |
| 1556 |
gpg_error_t err; |
| 1557 |
struct tag_info ti; |
| 1558 |
|
| 1559 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1560 |
if (err) |
| 1561 |
return err; |
| 1562 |
if (ti.class != CLASS_CONTEXT) |
| 1563 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1564 |
if (ti.ndef) |
| 1565 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1566 |
if (derlen < ti.length) |
| 1567 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1568 |
|
| 1569 |
if (ti.tag == 0 && derlen) |
| 1570 |
{ /* distributionPointName */ |
| 1571 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1572 |
if (err) |
| 1573 |
return err; |
| 1574 |
if (ti.class != CLASS_CONTEXT) |
| 1575 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1576 |
if (ti.ndef) |
| 1577 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1578 |
if (derlen < ti.nhdr) |
| 1579 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1580 |
if (derlen < ti.length) |
| 1581 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1582 |
|
| 1583 |
if (ti.tag == 0) |
| 1584 |
{ |
| 1585 |
if (distpoint) |
| 1586 |
{ |
| 1587 |
err = _ksba_name_new_from_der (distpoint, der, ti.length); |
| 1588 |
if (err) |
| 1589 |
return err; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
else |
| 1593 |
{ |
| 1594 |
/* We don't support nameRelativeToCRLIssuer yet*/ |
| 1595 |
} |
| 1596 |
der += ti.length; |
| 1597 |
derlen -= ti.length; |
| 1598 |
|
| 1599 |
if (!derlen) |
| 1600 |
return 0; |
| 1601 |
|
| 1602 |
/* read the next optional element */ |
| 1603 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1604 |
if (err) |
| 1605 |
return err; |
| 1606 |
if (ti.class != CLASS_CONTEXT) |
| 1607 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1608 |
if (ti.ndef) |
| 1609 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1610 |
if (derlen < ti.length) |
| 1611 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1612 |
} |
| 1613 |
|
| 1614 |
if (ti.tag == 1 && derlen) |
| 1615 |
{ /* reasonFlags */ |
| 1616 |
unsigned int bits, mask; |
| 1617 |
int i, unused, full; |
| 1618 |
|
| 1619 |
if (!ti.length || ti.length > derlen) |
| 1620 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 1621 |
unused = *der++; derlen--; |
| 1622 |
ti.length--; |
| 1623 |
if ((!ti.length && unused) || unused/8 > ti.length) |
| 1624 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 1625 |
|
| 1626 |
full = ti.length - (unused+7)/8; |
| 1627 |
unused %= 8; |
| 1628 |
mask = 0; |
| 1629 |
for (i=1; unused; i <<= 1, unused--) |
| 1630 |
mask |= i; |
| 1631 |
|
| 1632 |
/* we are only required to look at the first octect */ |
| 1633 |
if (ti.length && reason) |
| 1634 |
{ |
| 1635 |
bits = *der; |
| 1636 |
if (full) |
| 1637 |
full--; |
| 1638 |
else { |
| 1639 |
bits &= ~mask; |
| 1640 |
mask = 0; |
| 1641 |
} |
| 1642 |
|
| 1643 |
if (bits & 0x80) |
| 1644 |
*reason |= KSBA_CRLREASON_UNSPECIFIED; |
| 1645 |
if (bits & 0x40) |
| 1646 |
*reason |= KSBA_CRLREASON_KEY_COMPROMISE; |
| 1647 |
if (bits & 0x20) |
| 1648 |
*reason |= KSBA_CRLREASON_CA_COMPROMISE; |
| 1649 |
if (bits & 0x10) |
| 1650 |
*reason |= KSBA_CRLREASON_AFFILIATION_CHANGED; |
| 1651 |
if (bits & 0x08) |
| 1652 |
*reason |= KSBA_CRLREASON_SUPERSEDED; |
| 1653 |
if (bits & 0x04) |
| 1654 |
*reason |= KSBA_CRLREASON_CESSATION_OF_OPERATION; |
| 1655 |
if (bits & 0x02) |
| 1656 |
*reason |= KSBA_CRLREASON_CERTIFICATE_HOLD; |
| 1657 |
} |
| 1658 |
|
| 1659 |
der += ti.length; |
| 1660 |
derlen -= ti.length; |
| 1661 |
|
| 1662 |
if (!derlen) |
| 1663 |
return 0; |
| 1664 |
|
| 1665 |
/* read the next optional element */ |
| 1666 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1667 |
if (err) |
| 1668 |
return err; |
| 1669 |
if (ti.class != CLASS_CONTEXT) |
| 1670 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1671 |
if (ti.ndef) |
| 1672 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1673 |
if (derlen < ti.length) |
| 1674 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1675 |
} |
| 1676 |
|
| 1677 |
if (ti.tag == 2 && derlen) |
| 1678 |
{ /* crlIssuer */ |
| 1679 |
if (issuer) |
| 1680 |
{ |
| 1681 |
err = _ksba_name_new_from_der (issuer, der, ti.length); |
| 1682 |
if (err) |
| 1683 |
return err; |
| 1684 |
} |
| 1685 |
|
| 1686 |
der += ti.length; |
| 1687 |
derlen -= ti.length; |
| 1688 |
} |
| 1689 |
|
| 1690 |
if (derlen) |
| 1691 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1692 |
|
| 1693 |
return 0; |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* Return the CRLDistPoints given in the cert extension. IDX should |
| 1697 |
be iterated started from 0 until the function returns -1. |
| 1698 |
R_DISTPOINT returns a ksba_name_t object with the distribution point |
| 1699 |
name(s) the return value may be NULL to indicate that this name is |
| 1700 |
not available. R_ISSUER returns the CRL issuer; if the returned |
| 1701 |
value is NULL the caller should assume that the CRL issuer is the |
| 1702 |
same as the certificate issuer. R_REASON returns the reason for |
| 1703 |
the CRL. This is a bit encoded value with no bit set if this has |
| 1704 |
not been specified in the cert. |
| 1705 |
|
| 1706 |
The caller may pass NULL to any of the pointer arguments if he is |
| 1707 |
not interested in this value. The return values for R_DISTPOINT |
| 1708 |
and R_ISSUER must be released by the caller using |
| 1709 |
ksba_name_release(). */ |
| 1710 |
|
| 1711 |
gpg_error_t |
| 1712 |
ksba_cert_get_crl_dist_point (ksba_cert_t cert, int idx, |
| 1713 |
ksba_name_t *r_distpoint, |
| 1714 |
ksba_name_t *r_issuer, |
| 1715 |
ksba_crl_reason_t *r_reason) |
| 1716 |
{ |
| 1717 |
gpg_error_t err; |
| 1718 |
const char *oid; |
| 1719 |
size_t off, derlen; |
| 1720 |
int myidx, crit; |
| 1721 |
|
| 1722 |
if (r_distpoint) |
| 1723 |
*r_distpoint = NULL; |
| 1724 |
if (r_issuer) |
| 1725 |
*r_issuer = NULL; |
| 1726 |
if (r_reason) |
| 1727 |
*r_reason = 0; |
| 1728 |
|
| 1729 |
for (myidx=0; !(err=ksba_cert_get_extension (cert, myidx, &oid, &crit, |
| 1730 |
&off, &derlen)); myidx++) |
| 1731 |
{ |
| 1732 |
if (!strcmp (oid, oidstr_crlDistributionPoints)) |
| 1733 |
{ |
| 1734 |
const unsigned char *der; |
| 1735 |
struct tag_info ti; |
| 1736 |
size_t seqlen; |
| 1737 |
|
| 1738 |
der = cert->image + off; |
| 1739 |
|
| 1740 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1741 |
if (err) |
| 1742 |
return err; |
| 1743 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1744 |
&& ti.is_constructed) ) |
| 1745 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1746 |
if (ti.ndef) |
| 1747 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1748 |
seqlen = ti.length; |
| 1749 |
if (seqlen > derlen) |
| 1750 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1751 |
|
| 1752 |
/* Note: an empty sequence is actually not allowed but we |
| 1753 |
better don't care */ |
| 1754 |
|
| 1755 |
while (seqlen) |
| 1756 |
{ |
| 1757 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1758 |
if (err) |
| 1759 |
return err; |
| 1760 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1761 |
&& ti.is_constructed) ) |
| 1762 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1763 |
if (derlen < ti.length) |
| 1764 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1765 |
if (seqlen < ti.nhdr) |
| 1766 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1767 |
seqlen -= ti.nhdr; |
| 1768 |
if (seqlen < ti.length) |
| 1769 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1770 |
|
| 1771 |
if (idx) |
| 1772 |
{ /* skip because we are not yet at the desired index */ |
| 1773 |
der += ti.length; |
| 1774 |
derlen -= ti.length; |
| 1775 |
seqlen -= ti.length; |
| 1776 |
idx--; |
| 1777 |
continue; |
| 1778 |
} |
| 1779 |
|
| 1780 |
if (!ti.length) |
| 1781 |
return 0; |
| 1782 |
|
| 1783 |
err = parse_distribution_point (der, ti.length, |
| 1784 |
r_distpoint, r_issuer, r_reason); |
| 1785 |
if (err && r_distpoint) |
| 1786 |
{ |
| 1787 |
ksba_name_release (*r_distpoint); |
| 1788 |
*r_distpoint = NULL; |
| 1789 |
} |
| 1790 |
if (err && r_issuer) |
| 1791 |
{ |
| 1792 |
ksba_name_release (*r_issuer); |
| 1793 |
*r_issuer = NULL; |
| 1794 |
} |
| 1795 |
if (err && r_reason) |
| 1796 |
*r_reason = 0; |
| 1797 |
|
| 1798 |
return err; |
| 1799 |
} |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
return err; |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
/* Return the authorityKeyIdentifier in R_NAME and R_SERIAL or/and in |
| 1808 |
R_KEYID. GPG_ERR_NO_DATA is returned if no authorityKeyIdentifier |
| 1809 |
or only one using the keyIdentifier method is available and R_KEYID |
| 1810 |
is NULL. */ |
| 1811 |
gpg_error_t |
| 1812 |
ksba_cert_get_auth_key_id (ksba_cert_t cert, |
| 1813 |
ksba_sexp_t *r_keyid, |
| 1814 |
ksba_name_t *r_name, |
| 1815 |
ksba_sexp_t *r_serial) |
| 1816 |
{ |
| 1817 |
gpg_error_t err; |
| 1818 |
const char *oid; |
| 1819 |
size_t off, derlen; |
| 1820 |
const unsigned char *der; |
| 1821 |
const unsigned char *keyid_der = NULL; |
| 1822 |
size_t keyid_derlen = 0; |
| 1823 |
int idx, crit; |
| 1824 |
struct tag_info ti; |
| 1825 |
char numbuf[30]; |
| 1826 |
size_t numbuflen; |
| 1827 |
|
| 1828 |
if (r_keyid) |
| 1829 |
*r_keyid = NULL; |
| 1830 |
if (!r_name || !r_serial) |
| 1831 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1832 |
*r_name = NULL; |
| 1833 |
*r_serial = NULL; |
| 1834 |
|
| 1835 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &oid, &crit, |
| 1836 |
&off, &derlen)); idx++) |
| 1837 |
{ |
| 1838 |
if (!strcmp (oid, oidstr_authorityKeyIdentifier)) |
| 1839 |
break; |
| 1840 |
} |
| 1841 |
if (gpg_err_code (err) == GPG_ERR_EOF |
| 1842 |
|| gpg_err_code (err) == GPG_ERR_NO_VALUE) |
| 1843 |
return gpg_error (GPG_ERR_NO_DATA); /* not available */ |
| 1844 |
if (err) |
| 1845 |
return err; |
| 1846 |
|
| 1847 |
/* check that there is only one */ |
| 1848 |
for (idx++; !(err=ksba_cert_get_extension (cert, idx, &oid, NULL, |
| 1849 |
NULL, NULL)); idx++) |
| 1850 |
{ |
| 1851 |
if (!strcmp (oid, oidstr_authorityKeyIdentifier)) |
| 1852 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 1853 |
} |
| 1854 |
|
| 1855 |
der = cert->image + off; |
| 1856 |
|
| 1857 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1858 |
if (err) |
| 1859 |
return err; |
| 1860 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1861 |
&& ti.is_constructed) ) |
| 1862 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1863 |
if (ti.ndef) |
| 1864 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1865 |
if (ti.length > derlen) |
| 1866 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1867 |
|
| 1868 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1869 |
if (err) |
| 1870 |
return err; |
| 1871 |
if (ti.class != CLASS_CONTEXT) |
| 1872 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1873 |
if (ti.ndef) |
| 1874 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1875 |
if (derlen < ti.length) |
| 1876 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1877 |
|
| 1878 |
if (ti.tag == 0) |
| 1879 |
{ /* keyIdentifier: Save it away and skip over it. */ |
| 1880 |
keyid_der = der; |
| 1881 |
keyid_derlen = ti.length; |
| 1882 |
|
| 1883 |
der += ti.length; |
| 1884 |
derlen -= ti.length; |
| 1885 |
/* If the keyid has been requested but no other data follows, we |
| 1886 |
directly jump to the end. */ |
| 1887 |
if (r_keyid && !derlen) |
| 1888 |
goto build_keyid; |
| 1889 |
if (!derlen) |
| 1890 |
return gpg_error (GPG_ERR_NO_DATA); /* not available */ |
| 1891 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1892 |
if (err) |
| 1893 |
return err; |
| 1894 |
if (ti.class != CLASS_CONTEXT) |
| 1895 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1896 |
if (ti.ndef) |
| 1897 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1898 |
if (derlen < ti.length) |
| 1899 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1900 |
} |
| 1901 |
|
| 1902 |
if (ti.tag != 1 || !derlen) |
| 1903 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1904 |
|
| 1905 |
err = _ksba_name_new_from_der (r_name, der, ti.length); |
| 1906 |
if (err) |
| 1907 |
return err; |
| 1908 |
|
| 1909 |
der += ti.length; |
| 1910 |
derlen -= ti.length; |
| 1911 |
|
| 1912 |
/* fixme: we should release r_name before returning on error */ |
| 1913 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 1914 |
if (err) |
| 1915 |
return err; |
| 1916 |
if (ti.class != CLASS_CONTEXT) |
| 1917 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* we expected a tag */ |
| 1918 |
if (ti.ndef) |
| 1919 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 1920 |
if (derlen < ti.length) |
| 1921 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1922 |
|
| 1923 |
if (ti.tag != 2 || !derlen) |
| 1924 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1925 |
|
| 1926 |
if (ti.length > MAX_SERIALNO_LENGTH) |
| 1927 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1928 |
sprintf (numbuf,"(%u:", (unsigned int)ti.length); |
| 1929 |
numbuflen = strlen (numbuf); |
| 1930 |
*r_serial = xtrymalloc (numbuflen + ti.length + 2); |
| 1931 |
if (!*r_serial) |
| 1932 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1933 |
strcpy (*r_serial, numbuf); |
| 1934 |
memcpy (*r_serial+numbuflen, der, ti.length); |
| 1935 |
(*r_serial)[numbuflen + ti.length] = ')'; |
| 1936 |
(*r_serial)[numbuflen + ti.length + 1] = 0; |
| 1937 |
|
| 1938 |
build_keyid: |
| 1939 |
if (r_keyid && keyid_der && keyid_derlen) |
| 1940 |
{ |
| 1941 |
if (keyid_derlen > MAX_KEYID_DER_LENGTH) |
| 1942 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 1943 |
sprintf (numbuf,"(%u:", (unsigned int)keyid_derlen); |
| 1944 |
numbuflen = strlen (numbuf); |
| 1945 |
*r_keyid = xtrymalloc (numbuflen + keyid_derlen + 2); |
| 1946 |
if (!*r_keyid) |
| 1947 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1948 |
strcpy (*r_keyid, numbuf); |
| 1949 |
memcpy (*r_keyid+numbuflen, keyid_der, keyid_derlen); |
| 1950 |
(*r_keyid)[numbuflen + keyid_derlen] = ')'; |
| 1951 |
(*r_keyid)[numbuflen + keyid_derlen + 1] = 0; |
| 1952 |
} |
| 1953 |
return 0; |
| 1954 |
} |
| 1955 |
|
| 1956 |
|
| 1957 |
/* Return a simple octet string extension at the object identifier OID |
| 1958 |
from certificate CERT. The data is return as a simple S-expression |
| 1959 |
and stored at R_DATA. Returns 0 on success or an error code. |
| 1960 |
common error codes are: GPG_ERR_NO_DATA if no such extension is |
| 1961 |
available, GPG_ERR_DUP_VALUE if more than one is available. If |
| 1962 |
R_CRIT is not NULL, the critical extension flag will be stored at |
| 1963 |
that address. */ |
| 1964 |
static gpg_error_t |
| 1965 |
get_simple_octet_string_ext (ksba_cert_t cert, const char *oid, |
| 1966 |
int *r_crit, ksba_sexp_t *r_data) |
| 1967 |
{ |
| 1968 |
gpg_error_t err; |
| 1969 |
const char *tmpoid; |
| 1970 |
size_t off, derlen; |
| 1971 |
const unsigned char *der; |
| 1972 |
int idx, crit; |
| 1973 |
struct tag_info ti; |
| 1974 |
char numbuf[30]; |
| 1975 |
size_t numbuflen; |
| 1976 |
|
| 1977 |
if (!r_data) |
| 1978 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1979 |
*r_data = NULL; |
| 1980 |
|
| 1981 |
for (idx=0; !(err=ksba_cert_get_extension (cert, idx, &tmpoid, &crit, |
| 1982 |
&off, &derlen)); idx++) |
| 1983 |
{ |
| 1984 |
if (!strcmp (tmpoid, oid)) |
| 1985 |
break; |
| 1986 |
} |
| 1987 |
if (err) |
| 1988 |
{ |
| 1989 |
if (gpg_err_code (err) == GPG_ERR_EOF |
| 1990 |
|| gpg_err_code (err) == GPG_ERR_NO_VALUE) |
| 1991 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1992 |
return err; |
| 1993 |
} |
| 1994 |
|
| 1995 |
/* Check that there is only one */ |
| 1996 |
for (idx++; !(err=ksba_cert_get_extension (cert, idx, &tmpoid, NULL, |
| 1997 |
NULL, NULL)); idx++) |
| 1998 |
{ |
| 1999 |
if (!strcmp (tmpoid, oid)) |
| 2000 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 2001 |
} |
| 2002 |
|
| 2003 |
der = cert->image + off; |
| 2004 |
|
| 2005 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 2006 |
if (err) |
| 2007 |
return err; |
| 2008 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 2009 |
&& !ti.is_constructed) ) |
| 2010 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2011 |
if (ti.ndef) |
| 2012 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 2013 |
if (ti.length > derlen) |
| 2014 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2015 |
if (ti.length != derlen) |
| 2016 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); /* Garbage follows. */ |
| 2017 |
|
| 2018 |
if (ti.length > MAX_CERT_EXT_LENGTH) |
| 2019 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2020 |
sprintf (numbuf,"(%u:", (unsigned int)ti.length); |
| 2021 |
numbuflen = strlen (numbuf); |
| 2022 |
*r_data = xtrymalloc (numbuflen + ti.length + 2); |
| 2023 |
if (!*r_data) |
| 2024 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2025 |
strcpy (*r_data, numbuf); |
| 2026 |
memcpy (*r_data+numbuflen, der, ti.length); |
| 2027 |
(*r_data)[numbuflen + ti.length] = ')'; |
| 2028 |
(*r_data)[numbuflen + ti.length + 1] = 0; |
| 2029 |
if (r_crit) |
| 2030 |
*r_crit = crit; |
| 2031 |
return 0; |
| 2032 |
} |
| 2033 |
|
| 2034 |
|
| 2035 |
/* Return the subjectKeyIdentifier extension as a simple allocated |
| 2036 |
S-expression at the address of R_KEYID. 0 is returned on success, |
| 2037 |
GPG_ERR_NO_DATA if no such extension is available or any other |
| 2038 |
error code. If R_CRIT is not passed as NULL, the critical flag of |
| 2039 |
this is extension is stored there. */ |
| 2040 |
gpg_error_t |
| 2041 |
ksba_cert_get_subj_key_id (ksba_cert_t cert, int *r_crit, ksba_sexp_t *r_keyid) |
| 2042 |
{ |
| 2043 |
return get_simple_octet_string_ext (cert, oidstr_subjectKeyIdentifier, |
| 2044 |
r_crit, r_keyid); |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
/* MODE 0 := authorityInfoAccess |
| 2050 |
1 := subjectInfoAccess |
| 2051 |
|
| 2052 |
Caller must release METHOD and LOCATION if the function returned |
| 2053 |
with success; on error both variables will point to NULL. |
| 2054 |
*/ |
| 2055 |
static gpg_error_t |
| 2056 |
get_info_access (ksba_cert_t cert, int idx, int mode, |
| 2057 |
char **method, ksba_name_t *location) |
| 2058 |
{ |
| 2059 |
gpg_error_t err; |
| 2060 |
const char *oid; |
| 2061 |
size_t off, derlen; |
| 2062 |
int myidx, crit; |
| 2063 |
|
| 2064 |
*method = NULL; |
| 2065 |
*location = NULL; |
| 2066 |
|
| 2067 |
if (!cert || !cert->initialized) |
| 2068 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2069 |
if (idx < 0) |
| 2070 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2071 |
|
| 2072 |
for (myidx=0; !(err=ksba_cert_get_extension (cert, myidx, &oid, &crit, |
| 2073 |
&off, &derlen)); myidx++) |
| 2074 |
{ |
| 2075 |
if (!strcmp (oid,(mode == 0)? oidstr_authorityInfoAccess |
| 2076 |
: oidstr_subjectInfoAccess) ) |
| 2077 |
{ |
| 2078 |
const unsigned char *der; |
| 2079 |
struct tag_info ti; |
| 2080 |
size_t seqlen; |
| 2081 |
|
| 2082 |
der = cert->image + off; |
| 2083 |
|
| 2084 |
/* What we are going to parse is: |
| 2085 |
* |
| 2086 |
* AuthorityInfoAccessSyntax ::= |
| 2087 |
* SEQUENCE SIZE (1..MAX) OF AccessDescription |
| 2088 |
* |
| 2089 |
* AccessDescription ::= SEQUENCE { |
| 2090 |
* accessMethod OBJECT IDENTIFIER, |
| 2091 |
* accessLocation GeneralName } |
| 2092 |
*/ |
| 2093 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 2094 |
if (err) |
| 2095 |
return err; |
| 2096 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 2097 |
&& ti.is_constructed) ) |
| 2098 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2099 |
if (ti.ndef) |
| 2100 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 2101 |
seqlen = ti.length; |
| 2102 |
if (seqlen > derlen) |
| 2103 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2104 |
|
| 2105 |
/* Note: an empty sequence is actually not allowed but we |
| 2106 |
better don't care. */ |
| 2107 |
|
| 2108 |
while (seqlen) |
| 2109 |
{ |
| 2110 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 2111 |
if (err) |
| 2112 |
return err; |
| 2113 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 2114 |
&& ti.is_constructed) ) |
| 2115 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2116 |
if (derlen < ti.length) |
| 2117 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2118 |
if (seqlen < ti.nhdr) |
| 2119 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2120 |
seqlen -= ti.nhdr; |
| 2121 |
if (seqlen < ti.length) |
| 2122 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2123 |
|
| 2124 |
if (idx) |
| 2125 |
{ /* Skip because we are not yet at the desired index. */ |
| 2126 |
der += ti.length; |
| 2127 |
derlen -= ti.length; |
| 2128 |
seqlen -= ti.length; |
| 2129 |
idx--; |
| 2130 |
continue; |
| 2131 |
} |
| 2132 |
/* We only need the next object, thus we can (and |
| 2133 |
actually need to) limit the DERLEN to the length of |
| 2134 |
the current sequence. */ |
| 2135 |
derlen = ti.length; |
| 2136 |
if (!derlen) |
| 2137 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2138 |
|
| 2139 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 2140 |
if (err) |
| 2141 |
return err; |
| 2142 |
|
| 2143 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OBJECT_ID |
| 2144 |
&& !ti.is_constructed)) |
| 2145 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 2146 |
if (ti.ndef) |
| 2147 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 2148 |
if (derlen < ti.length) |
| 2149 |
return gpg_error (GPG_ERR_BAD_BER); |
| 2150 |
|
| 2151 |
*method = ksba_oid_to_str (der, ti.length); |
| 2152 |
if (!*method) |
| 2153 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2154 |
der += ti.length; |
| 2155 |
derlen -= ti.length; |
| 2156 |
|
| 2157 |
err = _ksba_name_new_from_der (location, der, derlen); |
| 2158 |
if (err) |
| 2159 |
{ |
| 2160 |
ksba_free (*method); |
| 2161 |
*method = NULL; |
| 2162 |
return err; |
| 2163 |
} |
| 2164 |
return 0; |
| 2165 |
} |
| 2166 |
} |
| 2167 |
} |
| 2168 |
|
| 2169 |
return err; |
| 2170 |
} |
| 2171 |
|
| 2172 |
|
| 2173 |
/* Return the authorityInfoAccess attributes. IDX should be iterated |
| 2174 |
starting from 0 until the function returns GPG_ERR_EOF. R_METHOD |
| 2175 |
returns an allocated string with the OID of one item and R_LOCATION |
| 2176 |
return the GeneralName for that OID. The return values for |
| 2177 |
R_METHOD and R_LOCATION must be released by the caller unless the |
| 2178 |
function returned an error; the function will however make sure |
| 2179 |
that R_METHOD and R_LOCATION will point to NULL if the function |
| 2180 |
returns an error. See RFC 5280, section 4.2.2.1 */ |
| 2181 |
gpg_error_t |
| 2182 |
ksba_cert_get_authority_info_access (ksba_cert_t cert, int idx, |
| 2183 |
char **r_method, ksba_name_t *r_location) |
| 2184 |
{ |
| 2185 |
if (!r_method || !r_location) |
| 2186 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2187 |
return get_info_access (cert, idx, 0, r_method, r_location); |
| 2188 |
} |
| 2189 |
|
| 2190 |
|
| 2191 |
/* Return the subjectInfoAccess attributes. IDX should be iterated |
| 2192 |
starting from 0 until the function returns GPG_ERR_EOF. R_METHOD |
| 2193 |
returns an allocated string with the OID of one item and R_LOCATION |
| 2194 |
return the GeneralName for that OID. The return values for |
| 2195 |
R_METHOD and R_LOCATION must be released by the caller unless the |
| 2196 |
function returned an error; the function will however make sure |
| 2197 |
that R_METHOD and R_LOCATION will point to NULL if the function |
| 2198 |
returns an error. See RFC 5280, section 4.2.2.2 */ |
| 2199 |
gpg_error_t |
| 2200 |
ksba_cert_get_subject_info_access (ksba_cert_t cert, int idx, |
| 2201 |
char **r_method, ksba_name_t *r_location) |
| 2202 |
{ |
| 2203 |
if (!r_method || !r_location) |
| 2204 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2205 |
return get_info_access (cert, idx, 1, r_method, r_location); |
| 2206 |
} |