| 1 |
/* crl.c - CRL parser |
| 2 |
* Copyright (C) 2002, 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 |
|
| 40 |
#include "convert.h" |
| 41 |
#include "keyinfo.h" |
| 42 |
#include "der-encoder.h" |
| 43 |
#include "ber-help.h" |
| 44 |
#include "ber-decoder.h" |
| 45 |
#include "crl.h" |
| 46 |
#include "stringbuf.h" |
| 47 |
|
| 48 |
|
| 49 |
static const char oidstr_crlNumber[] = "2.5.29.20"; |
| 50 |
static const char oidstr_crlReason[] = "2.5.29.21"; |
| 51 |
#if 0 |
| 52 |
static const char oidstr_issuingDistributionPoint[] = "2.5.29.28"; |
| 53 |
#endif |
| 54 |
static const char oidstr_certificateIssuer[] = "2.5.29.29"; |
| 55 |
static const char oidstr_authorityKeyIdentifier[] = "2.5.29.35"; |
| 56 |
|
| 57 |
/* We better buffer the hashing. */ |
| 58 |
static inline void |
| 59 |
do_hash (ksba_crl_t crl, const void *buffer, size_t length) |
| 60 |
{ |
| 61 |
while (length) |
| 62 |
{ |
| 63 |
size_t n = length; |
| 64 |
|
| 65 |
if (crl->hashbuf.used + n > sizeof crl->hashbuf.buffer) |
| 66 |
n = sizeof crl->hashbuf.buffer - crl->hashbuf.used; |
| 67 |
memcpy (crl->hashbuf.buffer+crl->hashbuf.used, buffer, n); |
| 68 |
crl->hashbuf.used += n; |
| 69 |
if (crl->hashbuf.used == sizeof crl->hashbuf.buffer) |
| 70 |
{ |
| 71 |
if (crl->hash_fnc) |
| 72 |
crl->hash_fnc (crl->hash_fnc_arg, |
| 73 |
crl->hashbuf.buffer, crl->hashbuf.used); |
| 74 |
crl->hashbuf.used = 0; |
| 75 |
} |
| 76 |
buffer = (const char *)buffer + n; |
| 77 |
length -= n; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#define HASH(a,b) do_hash (crl, (a), (b)) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* ksba_crl_new: |
| 87 |
* |
| 88 |
* Create a new and empty CRL object |
| 89 |
* |
| 90 |
* Return value: A CRL object or an error code. |
| 91 |
**/ |
| 92 |
gpg_error_t |
| 93 |
ksba_crl_new (ksba_crl_t *r_crl) |
| 94 |
{ |
| 95 |
*r_crl = xtrycalloc (1, sizeof **r_crl); |
| 96 |
if (!*r_crl) |
| 97 |
return gpg_error_from_errno (errno); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* ksba_crl_release: |
| 104 |
* @crl: A CRL object |
| 105 |
* |
| 106 |
* Release a CRL object. |
| 107 |
**/ |
| 108 |
void |
| 109 |
ksba_crl_release (ksba_crl_t crl) |
| 110 |
{ |
| 111 |
if (!crl) |
| 112 |
return; |
| 113 |
xfree (crl->algo.oid); |
| 114 |
xfree (crl->algo.parm); |
| 115 |
|
| 116 |
_ksba_asn_release_nodes (crl->issuer.root); |
| 117 |
xfree (crl->issuer.image); |
| 118 |
|
| 119 |
xfree (crl->item.serial); |
| 120 |
|
| 121 |
xfree (crl->sigval); |
| 122 |
while (crl->extension_list) |
| 123 |
{ |
| 124 |
crl_extn_t tmp = crl->extension_list->next; |
| 125 |
xfree (crl->extension_list->oid); |
| 126 |
xfree (crl->extension_list); |
| 127 |
crl->extension_list = tmp; |
| 128 |
} |
| 129 |
|
| 130 |
xfree (crl); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
gpg_error_t |
| 135 |
ksba_crl_set_reader (ksba_crl_t crl, ksba_reader_t r) |
| 136 |
{ |
| 137 |
if (!crl || !r) |
| 138 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 139 |
|
| 140 |
crl->reader = r; |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
/* Provide a hash function so that we are able to hash the data */ |
| 145 |
void |
| 146 |
ksba_crl_set_hash_function (ksba_crl_t crl, |
| 147 |
void (*hash_fnc)(void *, const void *, size_t), |
| 148 |
void *hash_fnc_arg) |
| 149 |
{ |
| 150 |
if (crl) |
| 151 |
{ |
| 152 |
crl->hash_fnc = hash_fnc; |
| 153 |
crl->hash_fnc_arg = hash_fnc_arg; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
access functions |
| 161 |
*/ |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* ksba_crl_get_digest_algo: |
| 166 |
* @cms: CMS object |
| 167 |
* |
| 168 |
* Figure out the the digest algorithm used for the signature and return |
| 169 |
* its OID. |
| 170 |
* |
| 171 |
* Return value: NULL if the signature algorithm is not yet available |
| 172 |
* or there is a mismatched between "tbsCertList.signature" and |
| 173 |
* "signatureAlgorithm"; on success the OID is returned which is valid |
| 174 |
* as long as the CRL object is valid. |
| 175 |
**/ |
| 176 |
const char * |
| 177 |
ksba_crl_get_digest_algo (ksba_crl_t crl) |
| 178 |
{ |
| 179 |
if (!crl) |
| 180 |
return NULL; |
| 181 |
|
| 182 |
/* fixme: implement the described check */ |
| 183 |
|
| 184 |
return crl->algo.oid; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* ksba_crl_get_issuer: |
| 190 |
* @cms: CMS object |
| 191 |
* @r_issuer: returns the issuer |
| 192 |
* |
| 193 |
* This functions returns the issuer of the CRL. The caller must |
| 194 |
* release the returned object. |
| 195 |
* |
| 196 |
* Return value: 0 on success or an error code |
| 197 |
**/ |
| 198 |
gpg_error_t |
| 199 |
ksba_crl_get_issuer (ksba_crl_t crl, char **r_issuer) |
| 200 |
{ |
| 201 |
gpg_error_t err; |
| 202 |
AsnNode n; |
| 203 |
const unsigned char *image; |
| 204 |
|
| 205 |
if (!crl || !r_issuer) |
| 206 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 207 |
if (!crl->issuer.root) |
| 208 |
return gpg_error (GPG_ERR_NO_DATA); |
| 209 |
|
| 210 |
n = crl->issuer.root; |
| 211 |
image = crl->issuer.image; |
| 212 |
|
| 213 |
if (!n || !n->down) |
| 214 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 215 |
n = n->down; /* dereference the choice node */ |
| 216 |
|
| 217 |
if (n->off == -1) |
| 218 |
{ |
| 219 |
/* fputs ("get_issuer problem at node:\n", stderr); */ |
| 220 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 221 |
return gpg_error (GPG_ERR_GENERAL); |
| 222 |
} |
| 223 |
err = _ksba_dn_to_str (image, n, r_issuer); |
| 224 |
|
| 225 |
return err; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/* Return the CRL extension in OID, CRITICAL, DER and DERLEN. The |
| 230 |
caller should iterate IDX from 0 upwards until GPG_ERR_EOF is |
| 231 |
returned. Note, that the returned values are valid as long as the |
| 232 |
context is valid and no new parsing has been started. */ |
| 233 |
gpg_error_t |
| 234 |
ksba_crl_get_extension (ksba_crl_t crl, int idx, |
| 235 |
char const **oid, int *critical, |
| 236 |
unsigned char const **der, size_t *derlen) |
| 237 |
{ |
| 238 |
crl_extn_t e; |
| 239 |
|
| 240 |
if (!crl) |
| 241 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 242 |
if (idx < 0) |
| 243 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 244 |
|
| 245 |
for (e=crl->extension_list; e && idx; e = e->next, idx-- ) |
| 246 |
; |
| 247 |
if (!e) |
| 248 |
return gpg_error (GPG_ERR_EOF); |
| 249 |
|
| 250 |
if (oid) |
| 251 |
*oid = e->oid; |
| 252 |
if (critical) |
| 253 |
*critical = e->critical; |
| 254 |
if (der) |
| 255 |
*der = e->der; |
| 256 |
if (derlen) |
| 257 |
*derlen = e->derlen; |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/* Return the authorityKeyIdentifier in r_name and r_serial or in |
| 264 |
r_keyID. GPG_ERR_NO_DATA is returned if no authorityKeyIdentifier |
| 265 |
or only one using the keyIdentifier method is available and R_KEYID |
| 266 |
is NULL. |
| 267 |
|
| 268 |
FIXME: This function shares a lot of code with the one in cert.c |
| 269 |
*/ |
| 270 |
gpg_error_t |
| 271 |
ksba_crl_get_auth_key_id (ksba_crl_t crl, |
| 272 |
ksba_sexp_t *r_keyid, |
| 273 |
ksba_name_t *r_name, |
| 274 |
ksba_sexp_t *r_serial) |
| 275 |
{ |
| 276 |
gpg_error_t err; |
| 277 |
size_t derlen; |
| 278 |
const unsigned char *der; |
| 279 |
const unsigned char *keyid_der = NULL; |
| 280 |
size_t keyid_derlen = 0; |
| 281 |
struct tag_info ti; |
| 282 |
char numbuf[30]; |
| 283 |
size_t numbuflen; |
| 284 |
crl_extn_t e; |
| 285 |
|
| 286 |
if (r_keyid) |
| 287 |
*r_keyid = NULL; |
| 288 |
if (!crl || !r_name || !r_serial) |
| 289 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 290 |
*r_name = NULL; |
| 291 |
*r_serial = NULL; |
| 292 |
|
| 293 |
for (e=crl->extension_list; e; e = e->next) |
| 294 |
if (!strcmp (e->oid, oidstr_authorityKeyIdentifier)) |
| 295 |
break; |
| 296 |
if (!e) |
| 297 |
return gpg_error (GPG_ERR_NO_DATA); /* not available */ |
| 298 |
|
| 299 |
/* Check that there is only one */ |
| 300 |
{ |
| 301 |
crl_extn_t e2; |
| 302 |
|
| 303 |
for (e2 = e->next; e2; e2 = e2->next) |
| 304 |
if (!strcmp (e2->oid, oidstr_authorityKeyIdentifier)) |
| 305 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 306 |
} |
| 307 |
|
| 308 |
der = e->der; |
| 309 |
derlen = e->derlen; |
| 310 |
|
| 311 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 312 |
if (err) |
| 313 |
return err; |
| 314 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 315 |
&& ti.is_constructed) ) |
| 316 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 317 |
if (ti.ndef) |
| 318 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 319 |
if (ti.length > derlen) |
| 320 |
return gpg_error (GPG_ERR_BAD_BER); |
| 321 |
|
| 322 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 323 |
if (err) |
| 324 |
return err; |
| 325 |
if (ti.class != CLASS_CONTEXT) |
| 326 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); /* We expected a tag. */ |
| 327 |
if (ti.ndef) |
| 328 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 329 |
if (derlen < ti.length) |
| 330 |
return gpg_error (GPG_ERR_BAD_BER); |
| 331 |
|
| 332 |
if (ti.tag == 0) |
| 333 |
{ /* keyIdentifier: Just save it away for later use. */ |
| 334 |
keyid_der = der; |
| 335 |
keyid_derlen = ti.length; |
| 336 |
|
| 337 |
der += ti.length; |
| 338 |
derlen -= ti.length; |
| 339 |
/* If the keyid has been requested but no other data follows, we |
| 340 |
directly jump to the end. */ |
| 341 |
if (r_keyid && !derlen) |
| 342 |
goto build_keyid; |
| 343 |
if (!derlen) |
| 344 |
return gpg_error (GPG_ERR_NO_DATA); /* not available */ |
| 345 |
|
| 346 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 347 |
if (err) |
| 348 |
return err; |
| 349 |
if (ti.class != CLASS_CONTEXT) |
| 350 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); /* we expected a tag */ |
| 351 |
if (ti.ndef) |
| 352 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 353 |
if (derlen < ti.length) |
| 354 |
return gpg_error (GPG_ERR_BAD_BER); |
| 355 |
} |
| 356 |
|
| 357 |
if (ti.tag != 1 || !derlen) |
| 358 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 359 |
|
| 360 |
err = _ksba_name_new_from_der (r_name, der, ti.length); |
| 361 |
if (err) |
| 362 |
return err; |
| 363 |
|
| 364 |
der += ti.length; |
| 365 |
derlen -= ti.length; |
| 366 |
|
| 367 |
/* Fixme: we should release r_name before returning on error */ |
| 368 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 369 |
if (err) |
| 370 |
return err; |
| 371 |
if (ti.class != CLASS_CONTEXT) |
| 372 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); /* we expected a tag */ |
| 373 |
if (ti.ndef) |
| 374 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); |
| 375 |
if (derlen < ti.length) |
| 376 |
return gpg_error (GPG_ERR_BAD_BER); |
| 377 |
|
| 378 |
if (ti.tag != 2 || !derlen) |
| 379 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 380 |
|
| 381 |
if (ti.length > MAX_SERIALNO_LENGTH) |
| 382 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 383 |
sprintf (numbuf,"(%u:", (unsigned int)ti.length); |
| 384 |
numbuflen = strlen (numbuf); |
| 385 |
*r_serial = xtrymalloc (numbuflen + ti.length + 2); |
| 386 |
if (!*r_serial) |
| 387 |
return gpg_error_from_errno (errno); |
| 388 |
strcpy (*r_serial, numbuf); |
| 389 |
memcpy (*r_serial+numbuflen, der, ti.length); |
| 390 |
(*r_serial)[numbuflen + ti.length] = ')'; |
| 391 |
(*r_serial)[numbuflen + ti.length + 1] = 0; |
| 392 |
|
| 393 |
build_keyid: |
| 394 |
if (r_keyid && keyid_der && keyid_derlen) |
| 395 |
{ |
| 396 |
if (keyid_derlen > MAX_KEYID_DER_LENGTH) |
| 397 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 398 |
sprintf (numbuf,"(%u:", (unsigned int)keyid_derlen); |
| 399 |
numbuflen = strlen (numbuf); |
| 400 |
*r_keyid = xtrymalloc (numbuflen + keyid_derlen + 2); |
| 401 |
if (!*r_keyid) |
| 402 |
return gpg_error (GPG_ERR_ENOMEM); |
| 403 |
strcpy (*r_keyid, numbuf); |
| 404 |
memcpy (*r_keyid+numbuflen, keyid_der, keyid_derlen); |
| 405 |
(*r_keyid)[numbuflen + keyid_derlen] = ')'; |
| 406 |
(*r_keyid)[numbuflen + keyid_derlen + 1] = 0; |
| 407 |
} |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
/* Return the optional crlNumber in NUMBER or GPG_ERR_NO_DATA if it is |
| 413 |
not available. Caller must release NUMBER if the fuction retruned |
| 414 |
with success. */ |
| 415 |
gpg_error_t |
| 416 |
ksba_crl_get_crl_number (ksba_crl_t crl, ksba_sexp_t *number) |
| 417 |
{ |
| 418 |
gpg_error_t err; |
| 419 |
size_t derlen; |
| 420 |
const unsigned char *der; |
| 421 |
struct tag_info ti; |
| 422 |
char numbuf[30]; |
| 423 |
size_t numbuflen; |
| 424 |
crl_extn_t e; |
| 425 |
|
| 426 |
if (!crl || !number) |
| 427 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 428 |
*number = NULL; |
| 429 |
|
| 430 |
for (e=crl->extension_list; e; e = e->next) |
| 431 |
if (!strcmp (e->oid, oidstr_crlNumber)) |
| 432 |
break; |
| 433 |
if (!e) |
| 434 |
return gpg_error (GPG_ERR_NO_DATA); /* not available */ |
| 435 |
|
| 436 |
/* Check that there is only one. */ |
| 437 |
{ |
| 438 |
crl_extn_t e2; |
| 439 |
|
| 440 |
for (e2 = e->next; e2; e2 = e2->next) |
| 441 |
if (!strcmp (e2->oid, oidstr_crlNumber)) |
| 442 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 443 |
} |
| 444 |
|
| 445 |
der = e->der; |
| 446 |
derlen = e->derlen; |
| 447 |
|
| 448 |
err = parse_integer (&der, &derlen, &ti); |
| 449 |
if (err) |
| 450 |
return err; |
| 451 |
|
| 452 |
if (ti.length > MAX_CRL_NUMBER_LENGTH) |
| 453 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 454 |
sprintf (numbuf,"(%u:", (unsigned int)ti.length); |
| 455 |
numbuflen = strlen (numbuf); |
| 456 |
*number = xtrymalloc (numbuflen + ti.length + 2); |
| 457 |
if (!*number) |
| 458 |
return gpg_error_from_errno (errno); |
| 459 |
strcpy (*number, numbuf); |
| 460 |
memcpy (*number+numbuflen, der, ti.length); |
| 461 |
(*number)[numbuflen + ti.length] = ')'; |
| 462 |
(*number)[numbuflen + ti.length + 1] = 0; |
| 463 |
|
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
/** |
| 471 |
* ksba_crl_get_update_times: |
| 472 |
* @crl: CRL object |
| 473 |
* @this: Returns the thisUpdate value |
| 474 |
* @next: Returns the nextUpdate value. |
| 475 |
* |
| 476 |
* THIS and NEXT may be given as NULL if the value is not required. |
| 477 |
* Return value: 0 on success or an error code |
| 478 |
**/ |
| 479 |
gpg_error_t |
| 480 |
ksba_crl_get_update_times (ksba_crl_t crl, |
| 481 |
ksba_isotime_t this, |
| 482 |
ksba_isotime_t next) |
| 483 |
{ |
| 484 |
if (this) |
| 485 |
*this = 0; |
| 486 |
if (next) |
| 487 |
*next = 0; |
| 488 |
if (!crl) |
| 489 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 490 |
if (!*crl->this_update) |
| 491 |
return gpg_error (GPG_ERR_INV_TIME); |
| 492 |
if (this) |
| 493 |
_ksba_copy_time (this, crl->this_update); |
| 494 |
if (next) |
| 495 |
_ksba_copy_time (next, crl->next_update); |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* ksba_crl_get_item: |
| 501 |
* @crl: CRL object |
| 502 |
* @r_serial: Returns a S-exp with the serial number; caller must free. |
| 503 |
* @r_revocation_date: Returns the recocation date |
| 504 |
* @r_reason: Return the reason for revocation |
| 505 |
* |
| 506 |
* Return the serial number, revocation time and reason of the current |
| 507 |
* item. Any of these arguments may be passed as %NULL if the value |
| 508 |
* is not of interest. This function should be used after the parse |
| 509 |
* function came back with %KSBA_SR_GOT_ITEM. For efficiency reasons |
| 510 |
* the function should be called only once, the implementation may |
| 511 |
* return an error for the second call. |
| 512 |
* |
| 513 |
* Return value: 0 in success or an error code. |
| 514 |
**/ |
| 515 |
gpg_error_t |
| 516 |
ksba_crl_get_item (ksba_crl_t crl, ksba_sexp_t *r_serial, |
| 517 |
ksba_isotime_t r_revocation_date, |
| 518 |
ksba_crl_reason_t *r_reason) |
| 519 |
{ |
| 520 |
if (r_revocation_date) |
| 521 |
*r_revocation_date = 0; |
| 522 |
|
| 523 |
if (!crl) |
| 524 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 525 |
|
| 526 |
if (r_serial) |
| 527 |
{ |
| 528 |
if (!crl->item.serial) |
| 529 |
return gpg_error (GPG_ERR_NO_DATA); |
| 530 |
*r_serial = crl->item.serial; |
| 531 |
crl->item.serial = NULL; |
| 532 |
} |
| 533 |
if (r_revocation_date) |
| 534 |
_ksba_copy_time (r_revocation_date, crl->item.revocation_date); |
| 535 |
if (r_reason) |
| 536 |
*r_reason = crl->item.reason; |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
/** |
| 543 |
* ksba_crl_get_sig_val: |
| 544 |
* @crl: CRL object |
| 545 |
* |
| 546 |
* Return the actual signature in a format suitable to be used as |
| 547 |
* input to Libgcrypt's verification function. The caller must free |
| 548 |
* the returned string. For a rsaPSS signed CRLs this function may |
| 549 |
* also be called right after rsaPSS has been detected using |
| 550 |
* ksba_crl_get_digest_algo and before the the signature value can be |
| 551 |
* retrieved. In this case an S-expression of the form |
| 552 |
* |
| 553 |
* (sig-val (hash-algo OID)(salt-length N)) |
| 554 |
* |
| 555 |
* is returned. The caller should extract the actual to be used hash |
| 556 |
* algorithm from that S-expression. Note that after the actual |
| 557 |
* signature as been seen, a similar S-expression is returned but in |
| 558 |
* this case also with the (rsa(s XXX)) list. |
| 559 |
* |
| 560 |
* Return value: NULL or a string with an S-Exp. |
| 561 |
**/ |
| 562 |
ksba_sexp_t |
| 563 |
ksba_crl_get_sig_val (ksba_crl_t crl) |
| 564 |
{ |
| 565 |
ksba_sexp_t p; |
| 566 |
|
| 567 |
if (!crl) |
| 568 |
return NULL; |
| 569 |
|
| 570 |
if (!crl->sigval |
| 571 |
&& crl->algo.oid && !strcmp (crl->algo.oid, "1.2.840.113549.1.1.10") |
| 572 |
&& crl->algo.parm && crl->algo.parmlen) |
| 573 |
{ |
| 574 |
char *pss_hash; |
| 575 |
unsigned int salt_length; |
| 576 |
struct stringbuf sb; |
| 577 |
|
| 578 |
if (_ksba_keyinfo_get_pss_info (crl->algo.parm, crl->algo.parmlen, |
| 579 |
&pss_hash, &salt_length)) |
| 580 |
return NULL; |
| 581 |
|
| 582 |
init_stringbuf (&sb, 100); |
| 583 |
put_stringbuf (&sb,"(7:sig-val(5:flags3:pss)(9:hash-algo"); |
| 584 |
put_stringbuf_sexp (&sb, pss_hash); |
| 585 |
put_stringbuf (&sb, ")(11:salt-length"); |
| 586 |
put_stringbuf_uint (&sb, salt_length); |
| 587 |
put_stringbuf (&sb, "))"); |
| 588 |
|
| 589 |
return get_stringbuf (&sb); |
| 590 |
} |
| 591 |
|
| 592 |
if (!crl->sigval) |
| 593 |
return NULL; |
| 594 |
|
| 595 |
p = crl->sigval; |
| 596 |
crl->sigval = NULL; |
| 597 |
return p; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
/* |
| 603 |
Parser functions |
| 604 |
*/ |
| 605 |
|
| 606 |
/* read one byte */ |
| 607 |
static int |
| 608 |
read_byte (ksba_reader_t reader) |
| 609 |
{ |
| 610 |
unsigned char buf; |
| 611 |
size_t nread; |
| 612 |
int rc; |
| 613 |
|
| 614 |
do |
| 615 |
rc = ksba_reader_read (reader, &buf, 1, &nread); |
| 616 |
while (!rc && !nread); |
| 617 |
return rc? -1: buf; |
| 618 |
} |
| 619 |
|
| 620 |
/* read COUNT bytes into buffer. Return 0 on success */ |
| 621 |
static int |
| 622 |
read_buffer (ksba_reader_t reader, char *buffer, size_t count) |
| 623 |
{ |
| 624 |
size_t nread; |
| 625 |
|
| 626 |
while (count) |
| 627 |
{ |
| 628 |
if (ksba_reader_read (reader, buffer, count, &nread)) |
| 629 |
return -1; |
| 630 |
buffer += nread; |
| 631 |
count -= nread; |
| 632 |
} |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
/* Create a new decoder and run it for the given element */ |
| 637 |
/* Fixme: this code is duplicated from cms-parser.c */ |
| 638 |
static gpg_error_t |
| 639 |
create_and_run_decoder (ksba_reader_t reader, const char *elem_name, |
| 640 |
AsnNode *r_root, |
| 641 |
unsigned char **r_image, size_t *r_imagelen) |
| 642 |
{ |
| 643 |
gpg_error_t err; |
| 644 |
ksba_asn_tree_t crl_tree; |
| 645 |
BerDecoder decoder; |
| 646 |
|
| 647 |
err = ksba_asn_create_tree ("tmttv2", &crl_tree); |
| 648 |
if (err) |
| 649 |
return err; |
| 650 |
|
| 651 |
decoder = _ksba_ber_decoder_new (); |
| 652 |
if (!decoder) |
| 653 |
{ |
| 654 |
ksba_asn_tree_release (crl_tree); |
| 655 |
return gpg_error (GPG_ERR_ENOMEM); |
| 656 |
} |
| 657 |
|
| 658 |
err = _ksba_ber_decoder_set_reader (decoder, reader); |
| 659 |
if (err) |
| 660 |
{ |
| 661 |
ksba_asn_tree_release (crl_tree); |
| 662 |
_ksba_ber_decoder_release (decoder); |
| 663 |
return err; |
| 664 |
} |
| 665 |
|
| 666 |
err = _ksba_ber_decoder_set_module (decoder, crl_tree); |
| 667 |
if (err) |
| 668 |
{ |
| 669 |
ksba_asn_tree_release (crl_tree); |
| 670 |
_ksba_ber_decoder_release (decoder); |
| 671 |
return err; |
| 672 |
} |
| 673 |
|
| 674 |
err = _ksba_ber_decoder_decode (decoder, elem_name, 0, |
| 675 |
r_root, r_image, r_imagelen); |
| 676 |
|
| 677 |
_ksba_ber_decoder_release (decoder); |
| 678 |
ksba_asn_tree_release (crl_tree); |
| 679 |
return err; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
/* Parse the extension in the buffer DER or length DERLEN and return |
| 684 |
the result in OID, CRITICAL, OFF and LEN. */ |
| 685 |
static gpg_error_t |
| 686 |
parse_one_extension (const unsigned char *der, size_t derlen, |
| 687 |
char **oid, int *critical, size_t *off, size_t *len) |
| 688 |
{ |
| 689 |
gpg_error_t err; |
| 690 |
struct tag_info ti; |
| 691 |
const unsigned char *start = der; |
| 692 |
|
| 693 |
*oid = NULL; |
| 694 |
*critical = 0; |
| 695 |
*off = *len = 0; |
| 696 |
|
| 697 |
/* |
| 698 |
Extension ::= SEQUENCE { |
| 699 |
extnID OBJECT IDENTIFIER, |
| 700 |
critical BOOLEAN DEFAULT FALSE, |
| 701 |
extnValue OCTET STRING } |
| 702 |
*/ |
| 703 |
err = parse_sequence (&der, &derlen, &ti); |
| 704 |
if (err) |
| 705 |
goto failure; |
| 706 |
|
| 707 |
err = parse_object_id_into_str (&der, &derlen, oid); |
| 708 |
if (err) |
| 709 |
goto failure; |
| 710 |
|
| 711 |
err = _ksba_ber_parse_tl (&der, &derlen, &ti); |
| 712 |
if (err) |
| 713 |
goto failure; |
| 714 |
if (ti.length > derlen) |
| 715 |
goto bad_ber; |
| 716 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_BOOLEAN |
| 717 |
&& !ti.is_constructed) |
| 718 |
{ |
| 719 |
if (ti.length != 1) |
| 720 |
goto bad_ber; |
| 721 |
*critical = !!*der; |
| 722 |
parse_skip (&der, &derlen, &ti); |
| 723 |
} |
| 724 |
else |
| 725 |
{ /* Undo that read. */ |
| 726 |
der -= ti.nhdr; |
| 727 |
derlen += ti.nhdr; |
| 728 |
} |
| 729 |
|
| 730 |
err = parse_octet_string (&der, &derlen, &ti); |
| 731 |
if (err) |
| 732 |
goto failure; |
| 733 |
*off = der - start; |
| 734 |
*len = ti.length; |
| 735 |
|
| 736 |
return 0; |
| 737 |
|
| 738 |
bad_ber: |
| 739 |
err = gpg_error (GPG_ERR_BAD_BER); |
| 740 |
failure: |
| 741 |
xfree (*oid); |
| 742 |
*oid = NULL; |
| 743 |
return err; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
/* Store an extension into the context. */ |
| 748 |
static gpg_error_t |
| 749 |
store_one_extension (ksba_crl_t crl, const unsigned char *der, size_t derlen) |
| 750 |
{ |
| 751 |
gpg_error_t err; |
| 752 |
char *oid; |
| 753 |
int critical; |
| 754 |
size_t off, len; |
| 755 |
crl_extn_t e; |
| 756 |
|
| 757 |
err = parse_one_extension (der, derlen, &oid, &critical, &off, &len); |
| 758 |
if (err) |
| 759 |
return err; |
| 760 |
e = xtrymalloc (sizeof *e + len - 1); |
| 761 |
if (!e) |
| 762 |
{ |
| 763 |
err = gpg_error_from_errno (errno); |
| 764 |
xfree (oid); |
| 765 |
return err; |
| 766 |
} |
| 767 |
e->oid = oid; |
| 768 |
e->critical = critical; |
| 769 |
e->derlen = len; |
| 770 |
memcpy (e->der, der + off, len); |
| 771 |
e->next = crl->extension_list; |
| 772 |
crl->extension_list = e; |
| 773 |
|
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
/* Parse the fixed block at the beginning. We use a custom parser |
| 780 |
here because our BER decoder is not yet able to stop at certain |
| 781 |
points */ |
| 782 |
static gpg_error_t |
| 783 |
parse_to_next_update (ksba_crl_t crl) |
| 784 |
{ |
| 785 |
gpg_error_t err; |
| 786 |
struct tag_info ti; |
| 787 |
unsigned long outer_len, tbs_len; |
| 788 |
int outer_ndef, tbs_ndef; |
| 789 |
int c; |
| 790 |
unsigned char tmpbuf[500]; /* for OID or algorithmIdentifier */ |
| 791 |
size_t nread; |
| 792 |
|
| 793 |
/* read the outer sequence */ |
| 794 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 795 |
if (err) |
| 796 |
return err; |
| 797 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 798 |
&& ti.is_constructed) ) |
| 799 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 800 |
outer_len = ti.length; |
| 801 |
outer_ndef = ti.ndef; |
| 802 |
if (!outer_ndef && outer_len < 10) |
| 803 |
return gpg_error (GPG_ERR_TOO_SHORT); |
| 804 |
|
| 805 |
/* read the tbs sequence */ |
| 806 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 807 |
if (err) |
| 808 |
return err; |
| 809 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 810 |
&& ti.is_constructed) ) |
| 811 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 812 |
HASH (ti.buf, ti.nhdr); |
| 813 |
if (!outer_ndef) |
| 814 |
{ |
| 815 |
if (outer_len < ti.nhdr) |
| 816 |
return gpg_error (GPG_ERR_BAD_BER); /* Triplet header larger |
| 817 |
than outer sequence */ |
| 818 |
outer_len -= ti.nhdr; |
| 819 |
if (!ti.ndef && outer_len < ti.length) |
| 820 |
return gpg_error (GPG_ERR_BAD_BER); /* Triplet larger than |
| 821 |
outer sequence */ |
| 822 |
outer_len -= ti.length; |
| 823 |
} |
| 824 |
tbs_len = ti.length; |
| 825 |
tbs_ndef = ti.ndef; |
| 826 |
if (!tbs_ndef && tbs_len < 10) |
| 827 |
return gpg_error (GPG_ERR_TOO_SHORT); |
| 828 |
|
| 829 |
/* read the optional version integer */ |
| 830 |
crl->crl_version = -1; |
| 831 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 832 |
if (err) |
| 833 |
return err; |
| 834 |
if ( ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_INTEGER) |
| 835 |
{ |
| 836 |
if ( ti.is_constructed || !ti.length ) |
| 837 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 838 |
HASH (ti.buf, ti.nhdr); |
| 839 |
if (!tbs_ndef) |
| 840 |
{ |
| 841 |
if (tbs_len < ti.nhdr) |
| 842 |
return gpg_error (GPG_ERR_BAD_BER); |
| 843 |
tbs_len -= ti.nhdr; |
| 844 |
if (tbs_len < ti.length) |
| 845 |
return gpg_error (GPG_ERR_BAD_BER); |
| 846 |
tbs_len -= ti.length; |
| 847 |
} |
| 848 |
/* fixme: we should also check the outer data length here and in |
| 849 |
the follwing code. It might however be easier to to this at |
| 850 |
the end of this sequence. */ |
| 851 |
if (ti.length != 1) |
| 852 |
return gpg_error (GPG_ERR_UNSUPPORTED_CRL_VERSION); |
| 853 |
if ( (c=read_byte (crl->reader)) == -1) |
| 854 |
{ |
| 855 |
err = ksba_reader_error (crl->reader); |
| 856 |
return err? err : gpg_error (GPG_ERR_GENERAL); |
| 857 |
} |
| 858 |
if ( !(c == 0 || c == 1) ) |
| 859 |
return gpg_error (GPG_ERR_UNSUPPORTED_CRL_VERSION); |
| 860 |
{ |
| 861 |
unsigned char tmp = c; |
| 862 |
HASH (&tmp, 1); |
| 863 |
} |
| 864 |
crl->crl_version = c; |
| 865 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 866 |
if (err) |
| 867 |
return err; |
| 868 |
} |
| 869 |
|
| 870 |
/* read the algorithm identifier */ |
| 871 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 872 |
&& ti.is_constructed) ) |
| 873 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 874 |
if (!tbs_ndef) |
| 875 |
{ |
| 876 |
if (tbs_len < ti.nhdr) |
| 877 |
return gpg_error (GPG_ERR_BAD_BER); |
| 878 |
tbs_len -= ti.nhdr; |
| 879 |
if (!ti.ndef && tbs_len < ti.length) |
| 880 |
return gpg_error (GPG_ERR_BAD_BER); |
| 881 |
tbs_len -= ti.length; |
| 882 |
} |
| 883 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 884 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 885 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 886 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 887 |
if (err) |
| 888 |
return err; |
| 889 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 890 |
|
| 891 |
xfree (crl->algo.oid); crl->algo.oid = NULL; |
| 892 |
xfree (crl->algo.parm); crl->algo.parm = NULL; |
| 893 |
err = _ksba_parse_algorithm_identifier2 (tmpbuf, ti.nhdr+ti.length, &nread, |
| 894 |
&crl->algo.oid, |
| 895 |
&crl->algo.parm, |
| 896 |
&crl->algo.parmlen); |
| 897 |
if (err) |
| 898 |
return err; |
| 899 |
assert (nread <= ti.nhdr + ti.length); |
| 900 |
if (nread < ti.nhdr + ti.length) |
| 901 |
return gpg_error (GPG_ERR_TOO_SHORT); |
| 902 |
|
| 903 |
|
| 904 |
/* read the name */ |
| 905 |
{ |
| 906 |
unsigned long n = ksba_reader_tell (crl->reader); |
| 907 |
err = create_and_run_decoder (crl->reader, |
| 908 |
"TMTTv2.CertificateList.tbsCertList.issuer", |
| 909 |
&crl->issuer.root, |
| 910 |
&crl->issuer.image, |
| 911 |
&crl->issuer.imagelen); |
| 912 |
if (err) |
| 913 |
return err; |
| 914 |
/* imagelen might be larger than the valid data (due to read ahead). |
| 915 |
So we need to get the count from the reader */ |
| 916 |
n = ksba_reader_tell (crl->reader) - n; |
| 917 |
if (n > crl->issuer.imagelen) |
| 918 |
return gpg_error (GPG_ERR_BUG); |
| 919 |
HASH (crl->issuer.image, n); |
| 920 |
|
| 921 |
if (!tbs_ndef) |
| 922 |
{ |
| 923 |
if (tbs_len < n) |
| 924 |
return gpg_error (GPG_ERR_BAD_BER); |
| 925 |
tbs_len -= n; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
/* Read the thisUpdate time. */ |
| 930 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 931 |
if (err) |
| 932 |
return err; |
| 933 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 934 |
&& (ti.tag == TYPE_UTC_TIME || ti.tag == TYPE_GENERALIZED_TIME) |
| 935 |
&& !ti.is_constructed) ) |
| 936 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 937 |
if (!tbs_ndef) |
| 938 |
{ |
| 939 |
if (tbs_len < ti.nhdr) |
| 940 |
return gpg_error (GPG_ERR_BAD_BER); |
| 941 |
tbs_len -= ti.nhdr; |
| 942 |
if (!ti.ndef && tbs_len < ti.length) |
| 943 |
return gpg_error (GPG_ERR_BAD_BER); |
| 944 |
tbs_len -= ti.length; |
| 945 |
} |
| 946 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 947 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 948 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 949 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 950 |
if (err) |
| 951 |
return err; |
| 952 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 953 |
_ksba_asntime_to_iso (tmpbuf+ti.nhdr, ti.length, |
| 954 |
ti.tag == TYPE_UTC_TIME, crl->this_update); |
| 955 |
|
| 956 |
/* Read the optional nextUpdate time. */ |
| 957 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 958 |
if (err) |
| 959 |
return err; |
| 960 |
if ( ti.class == CLASS_UNIVERSAL |
| 961 |
&& (ti.tag == TYPE_UTC_TIME || ti.tag == TYPE_GENERALIZED_TIME) |
| 962 |
&& !ti.is_constructed ) |
| 963 |
{ |
| 964 |
if (!tbs_ndef) |
| 965 |
{ |
| 966 |
if (tbs_len < ti.nhdr) |
| 967 |
return gpg_error (GPG_ERR_BAD_BER); |
| 968 |
tbs_len -= ti.nhdr; |
| 969 |
if (!ti.ndef && tbs_len < ti.length) |
| 970 |
return gpg_error (GPG_ERR_BAD_BER); |
| 971 |
tbs_len -= ti.length; |
| 972 |
} |
| 973 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 974 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 975 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 976 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 977 |
if (err) |
| 978 |
return err; |
| 979 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 980 |
_ksba_asntime_to_iso (tmpbuf+ti.nhdr, ti.length, |
| 981 |
ti.tag == TYPE_UTC_TIME, crl->next_update); |
| 982 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 983 |
if (err) |
| 984 |
return err; |
| 985 |
} |
| 986 |
|
| 987 |
/* Read the first sequence tag of the optional SEQ of SEQ. */ |
| 988 |
if (tbs_ndef || tbs_len) |
| 989 |
{ |
| 990 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 991 |
&& ti.is_constructed ) |
| 992 |
{ /* yes, there is one */ |
| 993 |
HASH (ti.buf, ti.nhdr); |
| 994 |
if (!tbs_ndef) |
| 995 |
{ |
| 996 |
if (tbs_len < ti.nhdr) |
| 997 |
return gpg_error (GPG_ERR_BAD_BER); |
| 998 |
tbs_len -= ti.nhdr; |
| 999 |
if (!ti.ndef && tbs_len < ti.length) |
| 1000 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1001 |
tbs_len -= ti.length; |
| 1002 |
} |
| 1003 |
crl->state.have_seqseq = 1; |
| 1004 |
crl->state.seqseq_ndef = ti.ndef; |
| 1005 |
crl->state.seqseq_len = ti.length; |
| 1006 |
/* and read the next */ |
| 1007 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1008 |
if (err) |
| 1009 |
return err; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* We need to save some stuff for the next round. */ |
| 1014 |
crl->state.ti = ti; |
| 1015 |
crl->state.outer_ndef = outer_ndef; |
| 1016 |
crl->state.outer_len = outer_len; |
| 1017 |
crl->state.tbs_ndef = tbs_ndef; |
| 1018 |
crl->state.tbs_len = tbs_len; |
| 1019 |
|
| 1020 |
return 0; |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
/* Store an entry extension into the current item. */ |
| 1026 |
static gpg_error_t |
| 1027 |
store_one_entry_extension (ksba_crl_t crl, |
| 1028 |
const unsigned char *der, size_t derlen) |
| 1029 |
{ |
| 1030 |
gpg_error_t err; |
| 1031 |
char *oid; |
| 1032 |
int critical; |
| 1033 |
size_t off, len; |
| 1034 |
|
| 1035 |
err = parse_one_extension (der, derlen, &oid, &critical, &off, &len); |
| 1036 |
if (err) |
| 1037 |
return err; |
| 1038 |
if (!strcmp (oid, oidstr_crlReason)) |
| 1039 |
{ |
| 1040 |
struct tag_info ti; |
| 1041 |
const unsigned char *buf = der+off; |
| 1042 |
size_t mylen = len; |
| 1043 |
|
| 1044 |
err = parse_enumerated (&buf, &mylen, &ti, 1); |
| 1045 |
if (err) |
| 1046 |
return err; |
| 1047 |
/* Note that we OR the values so that in case this extension is |
| 1048 |
repeated we can track all reason codes. */ |
| 1049 |
switch (*buf) |
| 1050 |
{ |
| 1051 |
case 0: crl->item.reason |= KSBA_CRLREASON_UNSPECIFIED; break; |
| 1052 |
case 1: crl->item.reason |= KSBA_CRLREASON_KEY_COMPROMISE; break; |
| 1053 |
case 2: crl->item.reason |= KSBA_CRLREASON_CA_COMPROMISE; break; |
| 1054 |
case 3: crl->item.reason |= KSBA_CRLREASON_AFFILIATION_CHANGED; break; |
| 1055 |
case 4: crl->item.reason |= KSBA_CRLREASON_SUPERSEDED; break; |
| 1056 |
case 5: crl->item.reason |= KSBA_CRLREASON_CESSATION_OF_OPERATION; |
| 1057 |
break; |
| 1058 |
case 6: crl->item.reason |= KSBA_CRLREASON_CERTIFICATE_HOLD; break; |
| 1059 |
case 8: crl->item.reason |= KSBA_CRLREASON_REMOVE_FROM_CRL; break; |
| 1060 |
case 9: crl->item.reason |= KSBA_CRLREASON_PRIVILEGE_WITHDRAWN; break; |
| 1061 |
case 10: crl->item.reason |= KSBA_CRLREASON_AA_COMPROMISE; break; |
| 1062 |
default: crl->item.reason |= KSBA_CRLREASON_OTHER; break; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
if (!strcmp (oid, oidstr_certificateIssuer)) |
| 1066 |
{ |
| 1067 |
/* FIXME: We need to implement this. */ |
| 1068 |
} |
| 1069 |
else if (critical) |
| 1070 |
err = gpg_error (GPG_ERR_UNKNOWN_CRIT_EXTN); |
| 1071 |
|
| 1072 |
xfree (oid); |
| 1073 |
|
| 1074 |
return err; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* Parse the revokedCertificates SEQUENCE of SEQUENCE using a custom |
| 1078 |
parser for efficiency and return after each entry */ |
| 1079 |
static gpg_error_t |
| 1080 |
parse_crl_entry (ksba_crl_t crl, int *got_entry) |
| 1081 |
{ |
| 1082 |
gpg_error_t err; |
| 1083 |
struct tag_info ti = crl->state.ti; |
| 1084 |
unsigned long seqseq_len= crl->state.seqseq_len; |
| 1085 |
int seqseq_ndef = crl->state.seqseq_ndef; |
| 1086 |
unsigned long len; |
| 1087 |
int ndef; |
| 1088 |
unsigned char tmpbuf[4096]; /* for time, serial number and extensions */ |
| 1089 |
char numbuf[22]; |
| 1090 |
int numbuflen; |
| 1091 |
|
| 1092 |
/* Check the length to see whether we are at the end of the seq but do |
| 1093 |
this only when we know that we have this optional seq of seq. */ |
| 1094 |
if (!crl->state.have_seqseq) |
| 1095 |
return 0; /* ready (no entries at all) */ |
| 1096 |
|
| 1097 |
if (!seqseq_ndef && !seqseq_len) |
| 1098 |
return 0; /* ready */ |
| 1099 |
|
| 1100 |
/* if this is not a SEQUENCE the CRL is invalid */ |
| 1101 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1102 |
&& ti.is_constructed) ) |
| 1103 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1104 |
HASH (ti.buf, ti.nhdr); |
| 1105 |
if (!seqseq_ndef) |
| 1106 |
{ |
| 1107 |
if (seqseq_len < ti.nhdr) |
| 1108 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1109 |
seqseq_len -= ti.nhdr; |
| 1110 |
if (!ti.ndef && seqseq_len < ti.length) |
| 1111 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1112 |
seqseq_len -= ti.length; |
| 1113 |
} |
| 1114 |
ndef = ti.ndef; |
| 1115 |
len = ti.length; |
| 1116 |
|
| 1117 |
/* get the serial number */ |
| 1118 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1119 |
if (err) |
| 1120 |
return err; |
| 1121 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_INTEGER |
| 1122 |
&& !ti.is_constructed) ) |
| 1123 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1124 |
if (!ndef) |
| 1125 |
{ |
| 1126 |
if (len < ti.nhdr) |
| 1127 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1128 |
len -= ti.nhdr; |
| 1129 |
if (!ti.ndef && len < ti.length) |
| 1130 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1131 |
len -= ti.length; |
| 1132 |
} |
| 1133 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 1134 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1135 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 1136 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 1137 |
if (err) |
| 1138 |
return err; |
| 1139 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 1140 |
|
| 1141 |
if (ti.length > MAX_SERIALNO_LENGTH) |
| 1142 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1143 |
xfree (crl->item.serial); |
| 1144 |
sprintf (numbuf,"(%u:", (unsigned int)ti.length); |
| 1145 |
numbuflen = strlen (numbuf); |
| 1146 |
crl->item.serial = xtrymalloc (numbuflen + ti.length + 2); |
| 1147 |
if (!crl->item.serial) |
| 1148 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1149 |
strcpy (crl->item.serial, numbuf); |
| 1150 |
memcpy (crl->item.serial+numbuflen, tmpbuf+ti.nhdr, ti.length); |
| 1151 |
crl->item.serial[numbuflen + ti.length] = ')'; |
| 1152 |
crl->item.serial[numbuflen + ti.length + 1] = 0; |
| 1153 |
crl->item.reason = 0; |
| 1154 |
|
| 1155 |
/* get the revocation time */ |
| 1156 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1157 |
if (err) |
| 1158 |
return err; |
| 1159 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 1160 |
&& (ti.tag == TYPE_UTC_TIME || ti.tag == TYPE_GENERALIZED_TIME) |
| 1161 |
&& !ti.is_constructed) ) |
| 1162 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1163 |
if (!ndef) |
| 1164 |
{ |
| 1165 |
if (len < ti.nhdr) |
| 1166 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1167 |
len -= ti.nhdr; |
| 1168 |
if (!ti.ndef && len < ti.length) |
| 1169 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1170 |
len -= ti.length; |
| 1171 |
} |
| 1172 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 1173 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1174 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 1175 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 1176 |
if (err) |
| 1177 |
return err; |
| 1178 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 1179 |
|
| 1180 |
_ksba_asntime_to_iso (tmpbuf+ti.nhdr, ti.length, |
| 1181 |
ti.tag == TYPE_UTC_TIME, crl->item.revocation_date); |
| 1182 |
|
| 1183 |
/* if there is still space we must parse the optional entryExtensions */ |
| 1184 |
if (ndef) |
| 1185 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1186 |
else if (len) |
| 1187 |
{ |
| 1188 |
/* read the outer sequence */ |
| 1189 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1190 |
if (err) |
| 1191 |
return err; |
| 1192 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 1193 |
&& ti.tag == TYPE_SEQUENCE && ti.is_constructed) ) |
| 1194 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1195 |
if (ti.ndef) |
| 1196 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1197 |
HASH (ti.buf, ti.nhdr); |
| 1198 |
if (len < ti.nhdr) |
| 1199 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1200 |
len -= ti.nhdr; |
| 1201 |
if (len < ti.length) |
| 1202 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1203 |
|
| 1204 |
/* now loop over the extensions */ |
| 1205 |
while (len) |
| 1206 |
{ |
| 1207 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1208 |
if (err) |
| 1209 |
return err; |
| 1210 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 1211 |
&& ti.tag == TYPE_SEQUENCE && ti.is_constructed) ) |
| 1212 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1213 |
if (ti.ndef) |
| 1214 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1215 |
if (len < ti.nhdr) |
| 1216 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1217 |
len -= ti.nhdr; |
| 1218 |
if (len < ti.length) |
| 1219 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1220 |
len -= ti.length; |
| 1221 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 1222 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1223 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 1224 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 1225 |
if (err) |
| 1226 |
return err; |
| 1227 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 1228 |
err = store_one_entry_extension (crl, tmpbuf, ti.nhdr+ti.length); |
| 1229 |
if (err) |
| 1230 |
return err; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
/* read ahead */ |
| 1235 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1236 |
if (err) |
| 1237 |
return err; |
| 1238 |
|
| 1239 |
*got_entry = 1; |
| 1240 |
|
| 1241 |
/* Fixme: the seqseq length is not correct if any element was ndef'd */ |
| 1242 |
crl->state.ti = ti; |
| 1243 |
crl->state.seqseq_ndef = seqseq_ndef; |
| 1244 |
crl->state.seqseq_len = seqseq_len; |
| 1245 |
|
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
/* This function is used when a [0] tag was encountered to read the |
| 1251 |
crlExtensions */ |
| 1252 |
static gpg_error_t |
| 1253 |
parse_crl_extensions (ksba_crl_t crl) |
| 1254 |
{ |
| 1255 |
gpg_error_t err; |
| 1256 |
struct tag_info ti = crl->state.ti; |
| 1257 |
unsigned long ext_len, len; |
| 1258 |
unsigned char tmpbuf[4096]; /* for extensions */ |
| 1259 |
|
| 1260 |
/* if we do not have a tag [0] we are done with this */ |
| 1261 |
if (!(ti.class == CLASS_CONTEXT && ti.tag == 0 && ti.is_constructed)) |
| 1262 |
return 0; |
| 1263 |
if (ti.ndef) |
| 1264 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1265 |
HASH (ti.buf, ti.nhdr); |
| 1266 |
ext_len = ti.length; |
| 1267 |
|
| 1268 |
/* read the outer sequence */ |
| 1269 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1270 |
if (err) |
| 1271 |
return err; |
| 1272 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 1273 |
&& ti.tag == TYPE_SEQUENCE && ti.is_constructed) ) |
| 1274 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1275 |
if (ti.ndef) |
| 1276 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1277 |
HASH (ti.buf, ti.nhdr); |
| 1278 |
if (ext_len < ti.nhdr) |
| 1279 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1280 |
ext_len -= ti.nhdr; |
| 1281 |
if (ext_len < ti.length) |
| 1282 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1283 |
len = ti.length; |
| 1284 |
|
| 1285 |
/* now loop over the extensions */ |
| 1286 |
while (len) |
| 1287 |
{ |
| 1288 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1289 |
if (err) |
| 1290 |
return err; |
| 1291 |
if ( !(ti.class == CLASS_UNIVERSAL |
| 1292 |
&& ti.tag == TYPE_SEQUENCE && ti.is_constructed) ) |
| 1293 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1294 |
if (ti.ndef) |
| 1295 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1296 |
if (len < ti.nhdr) |
| 1297 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1298 |
len -= ti.nhdr; |
| 1299 |
if (len < ti.length) |
| 1300 |
return gpg_error (GPG_ERR_BAD_BER); |
| 1301 |
len -= ti.length; |
| 1302 |
if (ti.nhdr + ti.length >= DIM(tmpbuf)) |
| 1303 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1304 |
/* fixme use a larger buffer if the extension does not fit into tmpbuf */ |
| 1305 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 1306 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 1307 |
if (err) |
| 1308 |
return err; |
| 1309 |
HASH (tmpbuf, ti.nhdr+ti.length); |
| 1310 |
err = store_one_extension (crl, tmpbuf, ti.nhdr+ti.length); |
| 1311 |
if (err) |
| 1312 |
return err; |
| 1313 |
} |
| 1314 |
|
| 1315 |
/* read ahead */ |
| 1316 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1317 |
if (err) |
| 1318 |
return err; |
| 1319 |
|
| 1320 |
crl->state.ti = ti; |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* Parse the signatureAlgorithm and the signature */ |
| 1325 |
static gpg_error_t |
| 1326 |
parse_signature (ksba_crl_t crl) |
| 1327 |
{ |
| 1328 |
gpg_error_t err; |
| 1329 |
struct tag_info ti = crl->state.ti; |
| 1330 |
unsigned char tmpbuf[2048]; /* for the sig algo and bitstr */ |
| 1331 |
size_t n, n2; |
| 1332 |
|
| 1333 |
/* We do read the stuff into a temporary buffer so that we can apply |
| 1334 |
our parsing function for this structure */ |
| 1335 |
|
| 1336 |
/* read the algorithmIdentifier sequence */ |
| 1337 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 1338 |
&& ti.is_constructed) ) |
| 1339 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1340 |
if (ti.ndef) |
| 1341 |
return gpg_error (GPG_ERR_UNSUPPORTED_ENCODING); |
| 1342 |
n = ti.nhdr + ti.length; |
| 1343 |
if (n >= DIM(tmpbuf)) |
| 1344 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1345 |
memcpy (tmpbuf, ti.buf, ti.nhdr); |
| 1346 |
err = read_buffer (crl->reader, tmpbuf+ti.nhdr, ti.length); |
| 1347 |
if (err) |
| 1348 |
return err; |
| 1349 |
|
| 1350 |
/* and append the bit string */ |
| 1351 |
err = _ksba_ber_read_tl (crl->reader, &ti); |
| 1352 |
if (err) |
| 1353 |
return err; |
| 1354 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_BIT_STRING |
| 1355 |
&& !ti.is_constructed) ) |
| 1356 |
return gpg_error (GPG_ERR_INV_CRL_OBJ); |
| 1357 |
n2 = ti.nhdr + ti.length; |
| 1358 |
if (n + n2 >= DIM(tmpbuf) || (n + n2) < n) |
| 1359 |
return gpg_error (GPG_ERR_TOO_LARGE); |
| 1360 |
memcpy (tmpbuf+n, ti.buf, ti.nhdr); |
| 1361 |
err = read_buffer (crl->reader, tmpbuf+n+ti.nhdr, ti.length); |
| 1362 |
if (err) |
| 1363 |
return err; |
| 1364 |
|
| 1365 |
/* now parse it */ |
| 1366 |
xfree (crl->sigval); crl->sigval = NULL; |
| 1367 |
return _ksba_sigval_to_sexp (tmpbuf, n + n2, &crl->sigval); |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
/* The actual parser which should be used with a new CRL object and |
| 1372 |
run in a loop until the the KSBA_SR_READY is encountered */ |
| 1373 |
gpg_error_t |
| 1374 |
ksba_crl_parse (ksba_crl_t crl, ksba_stop_reason_t *r_stopreason) |
| 1375 |
{ |
| 1376 |
enum { |
| 1377 |
sSTART, |
| 1378 |
sCRLENTRY, |
| 1379 |
sCRLEXT, |
| 1380 |
sERROR |
| 1381 |
} state = sERROR; |
| 1382 |
ksba_stop_reason_t stop_reason; |
| 1383 |
gpg_error_t err = 0; |
| 1384 |
int got_entry = 0; |
| 1385 |
|
| 1386 |
if (!crl || !r_stopreason) |
| 1387 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1388 |
|
| 1389 |
if (!crl->any_parse_done) |
| 1390 |
{ /* first time initialization of the stop reason */ |
| 1391 |
*r_stopreason = 0; |
| 1392 |
crl->any_parse_done = 1; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/* Calculate state from last reason */ |
| 1396 |
stop_reason = *r_stopreason; |
| 1397 |
*r_stopreason = KSBA_SR_RUNNING; |
| 1398 |
switch (stop_reason) |
| 1399 |
{ |
| 1400 |
case 0: |
| 1401 |
state = sSTART; |
| 1402 |
break; |
| 1403 |
case KSBA_SR_BEGIN_ITEMS: |
| 1404 |
case KSBA_SR_GOT_ITEM: |
| 1405 |
state = sCRLENTRY; |
| 1406 |
break; |
| 1407 |
case KSBA_SR_END_ITEMS: |
| 1408 |
state = sCRLEXT; |
| 1409 |
break; |
| 1410 |
case KSBA_SR_RUNNING: |
| 1411 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 1412 |
break; |
| 1413 |
default: |
| 1414 |
err = gpg_error (GPG_ERR_BUG); |
| 1415 |
break; |
| 1416 |
} |
| 1417 |
if (err) |
| 1418 |
return err; |
| 1419 |
|
| 1420 |
/* Do the action */ |
| 1421 |
switch (state) |
| 1422 |
{ |
| 1423 |
case sSTART: |
| 1424 |
err = parse_to_next_update (crl); |
| 1425 |
break; |
| 1426 |
case sCRLENTRY: |
| 1427 |
err = parse_crl_entry (crl, &got_entry); |
| 1428 |
break; |
| 1429 |
case sCRLEXT: |
| 1430 |
err = parse_crl_extensions (crl); |
| 1431 |
if (!err) |
| 1432 |
{ |
| 1433 |
if (crl->hash_fnc && crl->hashbuf.used) |
| 1434 |
crl->hash_fnc (crl->hash_fnc_arg, |
| 1435 |
crl->hashbuf.buffer, crl->hashbuf.used); |
| 1436 |
crl->hashbuf.used = 0; |
| 1437 |
err = parse_signature (crl); |
| 1438 |
} |
| 1439 |
break; |
| 1440 |
default: |
| 1441 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 1442 |
break; |
| 1443 |
} |
| 1444 |
if (err) |
| 1445 |
return err; |
| 1446 |
|
| 1447 |
/* Calculate new stop reason */ |
| 1448 |
switch (state) |
| 1449 |
{ |
| 1450 |
case sSTART: |
| 1451 |
stop_reason = KSBA_SR_BEGIN_ITEMS; |
| 1452 |
break; |
| 1453 |
case sCRLENTRY: |
| 1454 |
stop_reason = got_entry? KSBA_SR_GOT_ITEM : KSBA_SR_END_ITEMS; |
| 1455 |
break; |
| 1456 |
case sCRLEXT: |
| 1457 |
stop_reason = KSBA_SR_READY; |
| 1458 |
break; |
| 1459 |
default: |
| 1460 |
break; |
| 1461 |
} |
| 1462 |
|
| 1463 |
*r_stopreason = stop_reason; |
| 1464 |
return 0; |
| 1465 |
} |