ts_rsp_verify.c 23.2 KB
Newer Older
1
/* crypto/ts/ts_resp_verify.c */
2 3 4
/*
 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
 * 2002.
5 6 7 8 9 10 11 12 13
 */
/* ====================================================================
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
61
#include "internal/cryptlib.h"
62 63 64
#include <openssl/objects.h>
#include <openssl/ts.h>
#include <openssl/pkcs7.h>
R
Rich Salz 已提交
65
#include "ts_lcl.h"
66 67 68

/* Private function declarations. */

R
Rich Salz 已提交
69
static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
70
                          X509 *signer, STACK_OF(X509) **chain);
R
Rich Salz 已提交
71
static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
72
                                  STACK_OF(X509) *chain);
R
Rich Salz 已提交
73 74
static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si);
static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert);
75
static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert);
R
Rich Salz 已提交
76
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
77
                                    PKCS7 *token, TS_TST_INFO *tst_info);
R
Rich Salz 已提交
78 79 80 81
static int ts_check_status_info(TS_RESP *response);
static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text);
static int ts_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info);
static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
82 83
                              X509_ALGOR **md_alg,
                              unsigned char **imprint, unsigned *imprint_len);
R
Rich Salz 已提交
84
static int ts_check_imprints(X509_ALGOR *algor_a,
85 86
                             unsigned char *imprint_a, unsigned len_a,
                             TS_TST_INFO *tst_info);
R
Rich Salz 已提交
87 88 89
static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info);
static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer);
static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names,
90
                        GENERAL_NAME *name);
91 92 93

/*
 * Local mapping between response codes and descriptions.
94
 * Don't forget to change TS_STATUS_BUF_SIZE when modifying
95 96
 * the elements of this array.
 */
R
Rich Salz 已提交
97
static const char *ts_status_text[] = { "granted",
98 99 100 101 102 103
    "grantedWithMods",
    "rejection",
    "waiting",
    "revocationWarning",
    "revocationNotification"
};
104

R
Rich Salz 已提交
105
#define TS_STATUS_TEXT_SIZE     OSSL_NELEM(ts_status_text)
106 107 108 109 110

/*
 * This must be greater or equal to the sum of the strings in TS_status_text
 * plus the number of its elements.
 */
111 112 113 114 115
#define TS_STATUS_BUF_SIZE      256

static struct {
    int code;
    const char *text;
R
Rich Salz 已提交
116 117 118 119 120 121 122 123 124
} ts_failure_info[] = {
    {TS_INFO_BAD_ALG, "badAlg"},
    {TS_INFO_BAD_REQUEST, "badRequest"},
    {TS_INFO_BAD_DATA_FORMAT, "badDataFormat"},
    {TS_INFO_TIME_NOT_AVAILABLE, "timeNotAvailable"},
    {TS_INFO_UNACCEPTED_POLICY, "unacceptedPolicy"},
    {TS_INFO_UNACCEPTED_EXTENSION, "unacceptedExtension"},
    {TS_INFO_ADD_INFO_NOT_AVAILABLE, "addInfoNotAvailable"},
    {TS_INFO_SYSTEM_FAILURE, "systemFailure"}
125 126
};

R
Rich Salz 已提交
127
#define TS_FAILURE_INFO_SIZE    OSSL_NELEM(ts_failure_info)
128 129 130

/* Functions for verifying a signed TS_TST_INFO structure. */

131
/*-
132
 * This function carries out the following tasks:
133 134 135 136 137 138 139 140 141
 *      - Checks if there is one and only one signer.
 *      - Search for the signing certificate in 'certs' and in the response.
 *      - Check the extended key usage and key usage fields of the signer
 *      certificate (done by the path validation).
 *      - Build and validate the certificate path.
 *      - Check if the certificate path meets the requirements of the
 *      SigningCertificate ESS signed attribute.
 *      - Verify the signature value.
 *      - Returns the signer certificate in 'signer', if 'signer' is not NULL.
142 143
 */
int TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                             X509_STORE *store, X509 **signer_out)
{
    STACK_OF(PKCS7_SIGNER_INFO) *sinfos = NULL;
    PKCS7_SIGNER_INFO *si;
    STACK_OF(X509) *signers = NULL;
    X509 *signer;
    STACK_OF(X509) *chain = NULL;
    char buf[4096];
    int i, j = 0, ret = 0;
    BIO *p7bio = NULL;

    /* Some sanity checks first. */
    if (!token) {
        TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_INVALID_NULL_POINTER);
        goto err;
    }

    /* Check for the correct content type */
    if (!PKCS7_type_is_signed(token)) {
        TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_WRONG_CONTENT_TYPE);
        goto err;
    }

    /* Check if there is one and only one signer. */
    sinfos = PKCS7_get_signer_info(token);
    if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) {
        TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_THERE_MUST_BE_ONE_SIGNER);
        goto err;
    }
    si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0);

    /* Check for no content: no data to verify signature. */
    if (PKCS7_get_detached(token)) {
        TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_NO_CONTENT);
        goto err;
    }

    /*
     * Get hold of the signer certificate, search only internal certificates
     * if it was requested.
     */
    signers = PKCS7_get0_signers(token, certs, 0);
    if (!signers || sk_X509_num(signers) != 1)
        goto err;
    signer = sk_X509_value(signers, 0);

    /* Now verify the certificate. */
R
Rich Salz 已提交
191
    if (!ts_verify_cert(store, certs, signer, &chain))
192 193 194 195 196
        goto err;

    /*
     * Check if the signer certificate is consistent with the ESS extension.
     */
R
Rich Salz 已提交
197
    if (!ts_check_signing_certs(si, chain))
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        goto err;

    /* Creating the message digest. */
    p7bio = PKCS7_dataInit(token, NULL);

    /* We now have to 'read' from p7bio to calculate digests etc. */
    while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0) ;

    /* Verifying the signature. */
    j = PKCS7_signatureVerify(p7bio, token, si, signer);
    if (j <= 0) {
        TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_SIGNATURE_FAILURE);
        goto err;
    }

    /* Return the signer certificate if needed. */
    if (signer_out) {
        *signer_out = signer;
D
Dr. Stephen Henson 已提交
216
        X509_up_ref(signer);
217 218 219
    }

    ret = 1;
220 221

 err:
222 223 224
    BIO_free_all(p7bio);
    sk_X509_pop_free(chain, X509_free);
    sk_X509_free(signers);
225

226 227
    return ret;
}
228 229 230 231 232

/*
 * The certificate chain is returned in chain. Caller is responsible for
 * freeing the vector.
 */
R
Rich Salz 已提交
233
static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                          X509 *signer, STACK_OF(X509) **chain)
{
    X509_STORE_CTX cert_ctx;
    int i;
    int ret = 1;

    /* chain is an out argument. */
    *chain = NULL;
    X509_STORE_CTX_init(&cert_ctx, store, signer, untrusted);
    X509_STORE_CTX_set_purpose(&cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);
    i = X509_verify_cert(&cert_ctx);
    if (i <= 0) {
        int j = X509_STORE_CTX_get_error(&cert_ctx);
        TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);
        ERR_add_error_data(2, "Verify error:",
                           X509_verify_cert_error_string(j));
        ret = 0;
    } else {
        /* Get a copy of the certificate chain. */
        *chain = X509_STORE_CTX_get1_chain(&cert_ctx);
    }

    X509_STORE_CTX_cleanup(&cert_ctx);

    return ret;
}

R
Rich Salz 已提交
261
static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
262 263
                                  STACK_OF(X509) *chain)
{
R
Rich Salz 已提交
264
    ESS_SIGNING_CERT *ss = ess_get_signing_cert(si);
265 266 267 268 269 270 271 272 273 274
    STACK_OF(ESS_CERT_ID) *cert_ids = NULL;
    X509 *cert;
    int i = 0;
    int ret = 0;

    if (!ss)
        goto err;
    cert_ids = ss->cert_ids;
    /* The signer certificate must be the first in cert_ids. */
    cert = sk_X509_value(chain, 0);
R
Rich Salz 已提交
275
    if (ts_find_cert(cert_ids, cert) != 0)
276 277 278 279 280 281 282 283 284 285
        goto err;

    /*
     * Check the other certificates of the chain if there are more than one
     * certificate ids in cert_ids.
     */
    if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
        /* All the certificates of the chain must be in cert_ids. */
        for (i = 1; i < sk_X509_num(chain); ++i) {
            cert = sk_X509_value(chain, i);
R
Rich Salz 已提交
286
            if (ts_find_cert(cert_ids, cert) < 0)
287 288 289 290
                goto err;
        }
    }
    ret = 1;
291
 err:
292 293 294 295 296 297
    if (!ret)
        TSerr(TS_F_TS_CHECK_SIGNING_CERTS,
              TS_R_ESS_SIGNING_CERTIFICATE_ERROR);
    ESS_SIGNING_CERT_free(ss);
    return ret;
}
298

R
Rich Salz 已提交
299
static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si)
300 301 302 303 304 305 306 307 308
{
    ASN1_TYPE *attr;
    const unsigned char *p;
    attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate);
    if (!attr)
        return NULL;
    p = attr->value.sequence->data;
    return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
}
309 310

/* Returns < 0 if certificate is not found, certificate index otherwise. */
R
Rich Salz 已提交
311
static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
{
    int i;

    if (!cert_ids || !cert)
        return -1;

    /* Recompute SHA1 hash of certificate if necessary (side effect). */
    X509_check_purpose(cert, -1, 0);

    /* Look for cert in the cert_ids vector. */
    for (i = 0; i < sk_ESS_CERT_ID_num(cert_ids); ++i) {
        ESS_CERT_ID *cid = sk_ESS_CERT_ID_value(cert_ids, i);

        /* Check the SHA-1 hash first. */
        if (cid->hash->length == sizeof(cert->sha1_hash)
            && !memcmp(cid->hash->data, cert->sha1_hash,
                       sizeof(cert->sha1_hash))) {
            /* Check the issuer/serial as well if specified. */
            ESS_ISSUER_SERIAL *is = cid->issuer_serial;
331
            if (!is || !ts_issuer_serial_cmp(is, cert))
332 333 334 335 336 337
                return i;
        }
    }

    return -1;
}
338

339
static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert)
340 341
{
    GENERAL_NAME *issuer;
342

343
    if (!is || !cert || sk_GENERAL_NAME_num(is->issuer) != 1)
344
        return -1;
345

346 347 348
    /* Check the issuer first. It must be a directory name. */
    issuer = sk_GENERAL_NAME_value(is->issuer, 0);
    if (issuer->type != GEN_DIRNAME
349
        || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)))
350
        return -1;
351

352
    /* Check the serial number, too. */
353
    if (ASN1_INTEGER_cmp(is->serial, X509_get_serialNumber(cert)))
354
        return -1;
355

356 357
    return 0;
}
358

359
/*-
360
 * Verifies whether 'response' contains a valid response with regards
361
 * to the settings of the context:
362 363
 *      - Gives an error message if the TS_TST_INFO is not present.
 *      - Calls _TS_RESP_verify_token to verify the token content.
364 365
 */
int TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response)
366
{
R
Rich Salz 已提交
367 368
    PKCS7 *token = response->token;
    TS_TST_INFO *tst_info = response->tst_info;
369
    int ret = 0;
370

371
    /* Check if we have a successful TS_TST_INFO object in place. */
R
Rich Salz 已提交
372
    if (!ts_check_status_info(response))
373
        goto err;
374

375
    /* Check the contents of the time stamp token. */
R
Rich Salz 已提交
376
    if (!int_ts_RESP_verify_token(ctx, token, tst_info))
377
        goto err;
378

379
    ret = 1;
380
 err:
381 382
    return ret;
}
383 384 385

/*
 * Tries to extract a TS_TST_INFO structure from the PKCS7 token and
B
Bodo Möller 已提交
386
 * calls the internal int_TS_RESP_verify_token function for verifying it.
387 388
 */
int TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token)
389 390 391 392
{
    TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token);
    int ret = 0;
    if (tst_info) {
R
Rich Salz 已提交
393
        ret = int_ts_RESP_verify_token(ctx, token, tst_info);
394 395 396 397
        TS_TST_INFO_free(tst_info);
    }
    return ret;
}
398

399
/*-
400
 * Verifies whether the 'token' contains a valid time stamp token
401 402
 * with regards to the settings of the context. Only those checks are
 * carried out that are specified in the context:
403 404 405 406 407 408 409
 *      - Verifies the signature of the TS_TST_INFO.
 *      - Checks the version number of the response.
 *      - Check if the requested and returned policies math.
 *      - Check if the message imprints are the same.
 *      - Check if the nonces are the same.
 *      - Check if the TSA name matches the signer.
 *      - Check if the TSA name is the expected TSA.
410
 */
R
Rich Salz 已提交
411
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
412 413 414
                                    PKCS7 *token, TS_TST_INFO *tst_info)
{
    X509 *signer = NULL;
R
Rich Salz 已提交
415
    GENERAL_NAME *tsa_name = tst_info->tsa;
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    X509_ALGOR *md_alg = NULL;
    unsigned char *imprint = NULL;
    unsigned imprint_len = 0;
    int ret = 0;

    /* Verify the signature. */
    if ((ctx->flags & TS_VFY_SIGNATURE)
        && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
        goto err;

    /* Check version number of response. */
    if ((ctx->flags & TS_VFY_VERSION)
        && TS_TST_INFO_get_version(tst_info) != 1) {
        TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);
        goto err;
    }

    /* Check policies. */
    if ((ctx->flags & TS_VFY_POLICY)
R
Rich Salz 已提交
435
        && !ts_check_policy(ctx->policy, tst_info))
436 437 438 439
        goto err;

    /* Check message imprints. */
    if ((ctx->flags & TS_VFY_IMPRINT)
R
Rich Salz 已提交
440
        && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
441 442 443 444 445
                              tst_info))
        goto err;

    /* Compute and check message imprints. */
    if ((ctx->flags & TS_VFY_DATA)
R
Rich Salz 已提交
446
        && (!ts_compute_imprint(ctx->data, tst_info,
447
                                &md_alg, &imprint, &imprint_len)
R
Rich Salz 已提交
448
            || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))
449 450 451 452
        goto err;

    /* Check nonces. */
    if ((ctx->flags & TS_VFY_NONCE)
R
Rich Salz 已提交
453
        && !ts_check_nonces(ctx->nonce, tst_info))
454 455 456 457
        goto err;

    /* Check whether TSA name and signer certificate match. */
    if ((ctx->flags & TS_VFY_SIGNER)
R
Rich Salz 已提交
458
        && tsa_name && !ts_check_signer_name(tsa_name, signer)) {
459 460 461 462 463 464
        TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);
        goto err;
    }

    /* Check whether the TSA is the expected one. */
    if ((ctx->flags & TS_VFY_TSA_NAME)
R
Rich Salz 已提交
465
        && !ts_check_signer_name(ctx->tsa_name, signer)) {
466 467 468 469 470
        TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);
        goto err;
    }

    ret = 1;
471
 err:
472 473 474 475 476
    X509_free(signer);
    X509_ALGOR_free(md_alg);
    OPENSSL_free(imprint);
    return ret;
}
477

R
Rich Salz 已提交
478
static int ts_check_status_info(TS_RESP *response)
479
{
R
Rich Salz 已提交
480
    TS_STATUS_INFO *info = response->status_info;
481 482 483 484 485 486 487 488 489 490 491
    long status = ASN1_INTEGER_get(info->status);
    const char *status_text = NULL;
    char *embedded_status_text = NULL;
    char failure_text[TS_STATUS_BUF_SIZE] = "";

    /* Check if everything went fine. */
    if (status == 0 || status == 1)
        return 1;

    /* There was an error, get the description in status_text. */
    if (0 <= status && status < (long)TS_STATUS_TEXT_SIZE)
R
Rich Salz 已提交
492
        status_text = ts_status_text[status];
493 494 495 496 497
    else
        status_text = "unknown code";

    /* Set the embedded_status_text to the returned description. */
    if (sk_ASN1_UTF8STRING_num(info->text) > 0
R
Rich Salz 已提交
498
        && (embedded_status_text = ts_get_status_text(info->text)) == NULL)
499 500 501 502 503 504 505 506
        return 0;

    /* Filling in failure_text with the failure information. */
    if (info->failure_info) {
        int i;
        int first = 1;
        for (i = 0; i < (int)TS_FAILURE_INFO_SIZE; ++i) {
            if (ASN1_BIT_STRING_get_bit(info->failure_info,
R
Rich Salz 已提交
507
                                        ts_failure_info[i].code)) {
508 509 510 511
                if (!first)
                    strcpy(failure_text, ",");
                else
                    first = 0;
R
Rich Salz 已提交
512
                strcat(failure_text, ts_failure_info[i].text);
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
            }
        }
    }
    if (failure_text[0] == '\0')
        strcpy(failure_text, "unspecified");

    /* Making up the error string. */
    TSerr(TS_F_TS_CHECK_STATUS_INFO, TS_R_NO_TIME_STAMP_TOKEN);
    ERR_add_error_data(6,
                       "status code: ", status_text,
                       ", status text: ", embedded_status_text ?
                       embedded_status_text : "unspecified",
                       ", failure codes: ", failure_text);
    OPENSSL_free(embedded_status_text);

    return 0;
}
530

R
Rich Salz 已提交
531
static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
532 533 534 535 536 537 538 539 540 541 542 543 544
{
    int i;
    unsigned int length = 0;
    char *result = NULL;
    char *p;

    /* Determine length first. */
    for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
        ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
        length += ASN1_STRING_length(current);
        length += 1;            /* separator character */
    }
    /* Allocate memory (closing '\0' included). */
545
    if ((result = OPENSSL_malloc(length)) == NULL) {
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
        TSerr(TS_F_TS_GET_STATUS_TEXT, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    /* Concatenate the descriptions. */
    for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) {
        ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
        length = ASN1_STRING_length(current);
        if (i > 0)
            *p++ = '/';
        strncpy(p, (const char *)ASN1_STRING_data(current), length);
        p += length;
    }
    /* We do have space for this, too. */
    *p = '\0';

    return result;
}
563

R
Rich Salz 已提交
564
static int ts_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info)
565
{
R
Rich Salz 已提交
566
    ASN1_OBJECT *resp_oid = tst_info->policy_id;
567

568 569 570 571
    if (OBJ_cmp(req_oid, resp_oid) != 0) {
        TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);
        return 0;
    }
572

573 574
    return 1;
}
575

R
Rich Salz 已提交
576
static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
577 578 579
                              X509_ALGOR **md_alg,
                              unsigned char **imprint, unsigned *imprint_len)
{
R
Rich Salz 已提交
580 581
    TS_MSG_IMPRINT *msg_imprint = tst_info->msg_imprint;
    X509_ALGOR *md_alg_resp = msg_imprint->hash_algo;
582 583 584 585 586 587 588 589 590
    const EVP_MD *md;
    EVP_MD_CTX md_ctx;
    unsigned char buffer[4096];
    int length;

    *md_alg = NULL;
    *imprint = NULL;

    /* Return the MD algorithm of the response. */
591
    if ((*md_alg = X509_ALGOR_dup(md_alg_resp)) == NULL)
592 593 594
        goto err;

    /* Getting the MD object. */
595
    if ((md = EVP_get_digestbyobj((*md_alg)->algorithm)) == NULL) {
596 597 598 599 600 601 602 603 604
        TSerr(TS_F_TS_COMPUTE_IMPRINT, TS_R_UNSUPPORTED_MD_ALGORITHM);
        goto err;
    }

    /* Compute message digest. */
    length = EVP_MD_size(md);
    if (length < 0)
        goto err;
    *imprint_len = length;
605
    if ((*imprint = OPENSSL_malloc(*imprint_len)) == NULL) {
606 607 608 609 610 611 612 613 614 615 616 617 618 619
        TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    if (!EVP_DigestInit(&md_ctx, md))
        goto err;
    while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) {
        if (!EVP_DigestUpdate(&md_ctx, buffer, length))
            goto err;
    }
    if (!EVP_DigestFinal(&md_ctx, *imprint, NULL))
        goto err;

    return 1;
620
 err:
621 622 623 624 625 626 627
    X509_ALGOR_free(*md_alg);
    OPENSSL_free(*imprint);
    *imprint_len = 0;
    *imprint = 0;
    return 0;
}

R
Rich Salz 已提交
628
static int ts_check_imprints(X509_ALGOR *algor_a,
629 630 631
                             unsigned char *imprint_a, unsigned len_a,
                             TS_TST_INFO *tst_info)
{
R
Rich Salz 已提交
632 633
    TS_MSG_IMPRINT *b = tst_info->msg_imprint;
    X509_ALGOR *algor_b = b->hash_algo;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    int ret = 0;

    /* algor_a is optional. */
    if (algor_a) {
        /* Compare algorithm OIDs. */
        if (OBJ_cmp(algor_a->algorithm, algor_b->algorithm))
            goto err;

        /* The parameter must be NULL in both. */
        if ((algor_a->parameter
             && ASN1_TYPE_get(algor_a->parameter) != V_ASN1_NULL)
            || (algor_b->parameter
                && ASN1_TYPE_get(algor_b->parameter) != V_ASN1_NULL))
            goto err;
    }

    /* Compare octet strings. */
    ret = len_a == (unsigned)ASN1_STRING_length(b->hashed_msg) &&
        memcmp(imprint_a, ASN1_STRING_data(b->hashed_msg), len_a) == 0;
653
 err:
654 655 656 657
    if (!ret)
        TSerr(TS_F_TS_CHECK_IMPRINTS, TS_R_MESSAGE_IMPRINT_MISMATCH);
    return ret;
}
658

R
Rich Salz 已提交
659
static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info)
660
{
R
Rich Salz 已提交
661
    const ASN1_INTEGER *b = tst_info->nonce;
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

    /* Error if nonce is missing. */
    if (!b) {
        TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_NOT_RETURNED);
        return 0;
    }

    /* No error if a nonce is returned without being requested. */
    if (ASN1_INTEGER_cmp(a, b) != 0) {
        TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_MISMATCH);
        return 0;
    }

    return 1;
}

/*
 * Check if the specified TSA name matches either the subject or one of the
 * subject alternative names of the TSA certificate.
 */
R
Rich Salz 已提交
682
static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)
683 684 685 686 687 688 689
{
    STACK_OF(GENERAL_NAME) *gen_names = NULL;
    int idx = -1;
    int found = 0;

    /* Check the subject name first. */
    if (tsa_name->type == GEN_DIRNAME
690
        && X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0)
691 692 693 694
        return 1;

    /* Check all the alternative names. */
    gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
695
    while (gen_names != NULL) {
R
Rich Salz 已提交
696
        found = ts_find_name(gen_names, tsa_name) >= 0;
697 698
        if (found)
            break;
699 700 701 702 703
        /*
         * Get the next subject alternative name, although there should be no
         * more than one.
         */
        GENERAL_NAMES_free(gen_names);
704
        gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
705
    }
R
Rich Salz 已提交
706
    GENERAL_NAMES_free(gen_names);
707 708 709

    return found;
}
710 711

/* Returns 1 if name is in gen_names, 0 otherwise. */
R
Rich Salz 已提交
712
static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name)
713 714 715 716 717 718 719 720
{
    int i, found;
    for (i = 0, found = 0; !found && i < sk_GENERAL_NAME_num(gen_names); ++i) {
        GENERAL_NAME *current = sk_GENERAL_NAME_value(gen_names, i);
        found = GENERAL_NAME_cmp(current, name) == 0;
    }
    return found ? i - 1 : -1;
}