ts_verify_ctx.c 3.4 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9
 */

10
#include "internal/cryptlib.h"
11 12
#include <openssl/objects.h>
#include <openssl/ts.h>
13
#include "ts_local.h"
14 15

TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
16
{
R
Rich Salz 已提交
17
    TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
R
Rich Salz 已提交
18

19
    if (ctx == NULL)
20 21 22
        TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
    return ctx;
}
23 24

void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
25 26
{
    OPENSSL_assert(ctx != NULL);
27
    memset(ctx, 0, sizeof(*ctx));
28
}
29 30

void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
31 32 33
{
    if (!ctx)
        return;
34

35 36 37
    TS_VERIFY_CTX_cleanup(ctx);
    OPENSSL_free(ctx);
}
38

R
Rich Salz 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
{
    ctx->flags |= f;
    return ctx->flags;
}

int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
{
    ctx->flags = f;
    return ctx->flags;
}

BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
{
    ctx->data = b;
    return ctx->data;
}

X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
{
    ctx->store = s;
    return ctx->store;
}

STACK_OF(X509) *TS_VERIFY_CTS_set_certs(TS_VERIFY_CTX *ctx,
                                        STACK_OF(X509) *certs)
{
    ctx->certs = certs;
    return ctx->certs;
}

unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
                                         unsigned char *hexstr, long len)
{
    ctx->imprint = hexstr;
    ctx->imprint_len = len;
    return ctx->imprint;
}

78
void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
79 80 81
{
    if (!ctx)
        return;
82

83 84
    X509_STORE_free(ctx->store);
    sk_X509_pop_free(ctx->certs, X509_free);
85

86
    ASN1_OBJECT_free(ctx->policy);
87

88 89
    X509_ALGOR_free(ctx->md_alg);
    OPENSSL_free(ctx->imprint);
90

91
    BIO_free_all(ctx->data);
92

93
    ASN1_INTEGER_free(ctx->nonce);
94

95 96 97 98
    GENERAL_NAME_free(ctx->tsa_name);

    TS_VERIFY_CTX_init(ctx);
}
99 100

TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
101 102 103 104 105 106 107 108 109 110 111
{
    TS_VERIFY_CTX *ret = ctx;
    ASN1_OBJECT *policy;
    TS_MSG_IMPRINT *imprint;
    X509_ALGOR *md_alg;
    ASN1_OCTET_STRING *msg;
    const ASN1_INTEGER *nonce;

    OPENSSL_assert(req != NULL);
    if (ret)
        TS_VERIFY_CTX_cleanup(ret);
112
    else if ((ret = TS_VERIFY_CTX_new()) == NULL)
113 114 115 116
        return NULL;

    ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);

R
Rich Salz 已提交
117
    if ((policy = req->policy_id) != NULL) {
118
        if ((ret->policy = OBJ_dup(policy)) == NULL)
119 120 121 122
            goto err;
    } else
        ret->flags &= ~TS_VFY_POLICY;

R
Rich Salz 已提交
123 124
    imprint = req->msg_imprint;
    md_alg = imprint->hash_algo;
125
    if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
126
        goto err;
R
Rich Salz 已提交
127
    msg = imprint->hashed_msg;
128
    ret->imprint_len = ASN1_STRING_length(msg);
129
    if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
130
        goto err;
131
    memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
132

R
Rich Salz 已提交
133
    if ((nonce = req->nonce) != NULL) {
134
        if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
135 136 137 138 139
            goto err;
    } else
        ret->flags &= ~TS_VFY_NONCE;

    return ret;
140
 err:
141 142 143 144 145 146
    if (ctx)
        TS_VERIFY_CTX_cleanup(ctx);
    else
        TS_VERIFY_CTX_free(ret);
    return NULL;
}