Blame crypto/ts/ts_verify_ctx.c

Packit c4476c
/*
Packit c4476c
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include "internal/cryptlib.h"
Packit c4476c
#include <openssl/objects.h>
Packit c4476c
#include <openssl/ts.h>
Packit c4476c
#include "ts_local.h"
Packit c4476c
Packit c4476c
TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
Packit c4476c
{
Packit c4476c
    TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
Packit c4476c
Packit c4476c
    if (ctx == NULL)
Packit c4476c
        TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
Packit c4476c
    return ctx;
Packit c4476c
}
Packit c4476c
Packit c4476c
void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
Packit c4476c
{
Packit c4476c
    OPENSSL_assert(ctx != NULL);
Packit c4476c
    memset(ctx, 0, sizeof(*ctx));
Packit c4476c
}
Packit c4476c
Packit c4476c
void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
Packit c4476c
{
Packit c4476c
    if (!ctx)
Packit c4476c
        return;
Packit c4476c
Packit c4476c
    TS_VERIFY_CTX_cleanup(ctx);
Packit c4476c
    OPENSSL_free(ctx);
Packit c4476c
}
Packit c4476c
Packit c4476c
int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
Packit c4476c
{
Packit c4476c
    ctx->flags |= f;
Packit c4476c
    return ctx->flags;
Packit c4476c
}
Packit c4476c
Packit c4476c
int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
Packit c4476c
{
Packit c4476c
    ctx->flags = f;
Packit c4476c
    return ctx->flags;
Packit c4476c
}
Packit c4476c
Packit c4476c
BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
Packit c4476c
{
Packit c4476c
    ctx->data = b;
Packit c4476c
    return ctx->data;
Packit c4476c
}
Packit c4476c
Packit c4476c
X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
Packit c4476c
{
Packit c4476c
    ctx->store = s;
Packit c4476c
    return ctx->store;
Packit c4476c
}
Packit c4476c
Packit c4476c
STACK_OF(X509) *TS_VERIFY_CTS_set_certs(TS_VERIFY_CTX *ctx,
Packit c4476c
                                        STACK_OF(X509) *certs)
Packit c4476c
{
Packit c4476c
    ctx->certs = certs;
Packit c4476c
    return ctx->certs;
Packit c4476c
}
Packit c4476c
Packit c4476c
unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
Packit c4476c
                                         unsigned char *hexstr, long len)
Packit c4476c
{
Packit c4476c
    ctx->imprint = hexstr;
Packit c4476c
    ctx->imprint_len = len;
Packit c4476c
    return ctx->imprint;
Packit c4476c
}
Packit c4476c
Packit c4476c
void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
Packit c4476c
{
Packit c4476c
    if (!ctx)
Packit c4476c
        return;
Packit c4476c
Packit c4476c
    X509_STORE_free(ctx->store);
Packit c4476c
    sk_X509_pop_free(ctx->certs, X509_free);
Packit c4476c
Packit c4476c
    ASN1_OBJECT_free(ctx->policy);
Packit c4476c
Packit c4476c
    X509_ALGOR_free(ctx->md_alg);
Packit c4476c
    OPENSSL_free(ctx->imprint);
Packit c4476c
Packit c4476c
    BIO_free_all(ctx->data);
Packit c4476c
Packit c4476c
    ASN1_INTEGER_free(ctx->nonce);
Packit c4476c
Packit c4476c
    GENERAL_NAME_free(ctx->tsa_name);
Packit c4476c
Packit c4476c
    TS_VERIFY_CTX_init(ctx);
Packit c4476c
}
Packit c4476c
Packit c4476c
TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
Packit c4476c
{
Packit c4476c
    TS_VERIFY_CTX *ret = ctx;
Packit c4476c
    ASN1_OBJECT *policy;
Packit c4476c
    TS_MSG_IMPRINT *imprint;
Packit c4476c
    X509_ALGOR *md_alg;
Packit c4476c
    ASN1_OCTET_STRING *msg;
Packit c4476c
    const ASN1_INTEGER *nonce;
Packit c4476c
Packit c4476c
    OPENSSL_assert(req != NULL);
Packit c4476c
    if (ret)
Packit c4476c
        TS_VERIFY_CTX_cleanup(ret);
Packit c4476c
    else if ((ret = TS_VERIFY_CTX_new()) == NULL)
Packit c4476c
        return NULL;
Packit c4476c
Packit c4476c
    ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
Packit c4476c
Packit c4476c
    if ((policy = req->policy_id) != NULL) {
Packit c4476c
        if ((ret->policy = OBJ_dup(policy)) == NULL)
Packit c4476c
            goto err;
Packit c4476c
    } else
Packit c4476c
        ret->flags &= ~TS_VFY_POLICY;
Packit c4476c
Packit c4476c
    imprint = req->msg_imprint;
Packit c4476c
    md_alg = imprint->hash_algo;
Packit c4476c
    if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    msg = imprint->hashed_msg;
Packit c4476c
    ret->imprint_len = ASN1_STRING_length(msg);
Packit c4476c
    if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
Packit c4476c
Packit c4476c
    if ((nonce = req->nonce) != NULL) {
Packit c4476c
        if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
Packit c4476c
            goto err;
Packit c4476c
    } else
Packit c4476c
        ret->flags &= ~TS_VFY_NONCE;
Packit c4476c
Packit c4476c
    return ret;
Packit c4476c
 err:
Packit c4476c
    if (ctx)
Packit c4476c
        TS_VERIFY_CTX_cleanup(ctx);
Packit c4476c
    else
Packit c4476c
        TS_VERIFY_CTX_free(ret);
Packit c4476c
    return NULL;
Packit c4476c
}