1238384Sjkim/* crypto/ts/ts_verify_ctx.c */
2280297Sjkim/*
3280297Sjkim * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4280297Sjkim * 2003.
5238384Sjkim */
6238384Sjkim/* ====================================================================
7238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8238384Sjkim *
9238384Sjkim * Redistribution and use in source and binary forms, with or without
10238384Sjkim * modification, are permitted provided that the following conditions
11238384Sjkim * are met:
12238384Sjkim *
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer.
15238384Sjkim *
16238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
17238384Sjkim *    notice, this list of conditions and the following disclaimer in
18238384Sjkim *    the documentation and/or other materials provided with the
19238384Sjkim *    distribution.
20238384Sjkim *
21238384Sjkim * 3. All advertising materials mentioning features or use of this
22238384Sjkim *    software must display the following acknowledgment:
23238384Sjkim *    "This product includes software developed by the OpenSSL Project
24238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25238384Sjkim *
26238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27238384Sjkim *    endorse or promote products derived from this software without
28238384Sjkim *    prior written permission. For written permission, please contact
29238384Sjkim *    licensing@OpenSSL.org.
30238384Sjkim *
31238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
32238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
33238384Sjkim *    permission of the OpenSSL Project.
34238384Sjkim *
35238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
36238384Sjkim *    acknowledgment:
37238384Sjkim *    "This product includes software developed by the OpenSSL Project
38238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39238384Sjkim *
40238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
52238384Sjkim * ====================================================================
53238384Sjkim *
54238384Sjkim * This product includes cryptographic software written by Eric Young
55238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
56238384Sjkim * Hudson (tjh@cryptsoft.com).
57238384Sjkim *
58238384Sjkim */
59238384Sjkim
60238384Sjkim#include "cryptlib.h"
61238384Sjkim#include <openssl/objects.h>
62238384Sjkim#include <openssl/ts.h>
63238384Sjkim
64238384SjkimTS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
65280297Sjkim{
66280297Sjkim    TS_VERIFY_CTX *ctx =
67280297Sjkim        (TS_VERIFY_CTX *)OPENSSL_malloc(sizeof(TS_VERIFY_CTX));
68280297Sjkim    if (ctx)
69280297Sjkim        memset(ctx, 0, sizeof(TS_VERIFY_CTX));
70280297Sjkim    else
71280297Sjkim        TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
72280297Sjkim    return ctx;
73280297Sjkim}
74238384Sjkim
75238384Sjkimvoid TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
76280297Sjkim{
77280297Sjkim    OPENSSL_assert(ctx != NULL);
78280297Sjkim    memset(ctx, 0, sizeof(TS_VERIFY_CTX));
79280297Sjkim}
80238384Sjkim
81238384Sjkimvoid TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
82280297Sjkim{
83280297Sjkim    if (!ctx)
84280297Sjkim        return;
85238384Sjkim
86280297Sjkim    TS_VERIFY_CTX_cleanup(ctx);
87280297Sjkim    OPENSSL_free(ctx);
88280297Sjkim}
89238384Sjkim
90238384Sjkimvoid TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
91280297Sjkim{
92280297Sjkim    if (!ctx)
93280297Sjkim        return;
94238384Sjkim
95280297Sjkim    X509_STORE_free(ctx->store);
96280297Sjkim    sk_X509_pop_free(ctx->certs, X509_free);
97238384Sjkim
98280297Sjkim    ASN1_OBJECT_free(ctx->policy);
99238384Sjkim
100280297Sjkim    X509_ALGOR_free(ctx->md_alg);
101280297Sjkim    OPENSSL_free(ctx->imprint);
102238384Sjkim
103280297Sjkim    BIO_free_all(ctx->data);
104238384Sjkim
105280297Sjkim    ASN1_INTEGER_free(ctx->nonce);
106238384Sjkim
107280297Sjkim    GENERAL_NAME_free(ctx->tsa_name);
108238384Sjkim
109280297Sjkim    TS_VERIFY_CTX_init(ctx);
110280297Sjkim}
111280297Sjkim
112238384SjkimTS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
113280297Sjkim{
114280297Sjkim    TS_VERIFY_CTX *ret = ctx;
115280297Sjkim    ASN1_OBJECT *policy;
116280297Sjkim    TS_MSG_IMPRINT *imprint;
117280297Sjkim    X509_ALGOR *md_alg;
118280297Sjkim    ASN1_OCTET_STRING *msg;
119280297Sjkim    const ASN1_INTEGER *nonce;
120238384Sjkim
121280297Sjkim    OPENSSL_assert(req != NULL);
122280297Sjkim    if (ret)
123280297Sjkim        TS_VERIFY_CTX_cleanup(ret);
124280297Sjkim    else if (!(ret = TS_VERIFY_CTX_new()))
125280297Sjkim        return NULL;
126238384Sjkim
127280297Sjkim    /* Setting flags. */
128280297Sjkim    ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
129238384Sjkim
130280297Sjkim    /* Setting policy. */
131280297Sjkim    if ((policy = TS_REQ_get_policy_id(req)) != NULL) {
132280297Sjkim        if (!(ret->policy = OBJ_dup(policy)))
133280297Sjkim            goto err;
134280297Sjkim    } else
135280297Sjkim        ret->flags &= ~TS_VFY_POLICY;
136238384Sjkim
137280297Sjkim    /* Setting md_alg, imprint and imprint_len. */
138280297Sjkim    imprint = TS_REQ_get_msg_imprint(req);
139280297Sjkim    md_alg = TS_MSG_IMPRINT_get_algo(imprint);
140280297Sjkim    if (!(ret->md_alg = X509_ALGOR_dup(md_alg)))
141280297Sjkim        goto err;
142280297Sjkim    msg = TS_MSG_IMPRINT_get_msg(imprint);
143280297Sjkim    ret->imprint_len = ASN1_STRING_length(msg);
144280297Sjkim    if (!(ret->imprint = OPENSSL_malloc(ret->imprint_len)))
145280297Sjkim        goto err;
146280297Sjkim    memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
147238384Sjkim
148280297Sjkim    /* Setting nonce. */
149280297Sjkim    if ((nonce = TS_REQ_get_nonce(req)) != NULL) {
150280297Sjkim        if (!(ret->nonce = ASN1_INTEGER_dup(nonce)))
151280297Sjkim            goto err;
152280297Sjkim    } else
153280297Sjkim        ret->flags &= ~TS_VFY_NONCE;
154238384Sjkim
155280297Sjkim    return ret;
156238384Sjkim err:
157280297Sjkim    if (ctx)
158280297Sjkim        TS_VERIFY_CTX_cleanup(ctx);
159280297Sjkim    else
160280297Sjkim        TS_VERIFY_CTX_free(ret);
161280297Sjkim    return NULL;
162280297Sjkim}
163