1238384Sjkim/* crypto/ts/ts_verify_ctx.c */
2238384Sjkim/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3238384Sjkim * project 2003.
4238384Sjkim */
5238384Sjkim/* ====================================================================
6238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7238384Sjkim *
8238384Sjkim * Redistribution and use in source and binary forms, with or without
9238384Sjkim * modification, are permitted provided that the following conditions
10238384Sjkim * are met:
11238384Sjkim *
12238384Sjkim * 1. Redistributions of source code must retain the above copyright
13238384Sjkim *    notice, this list of conditions and the following disclaimer.
14238384Sjkim *
15238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16238384Sjkim *    notice, this list of conditions and the following disclaimer in
17238384Sjkim *    the documentation and/or other materials provided with the
18238384Sjkim *    distribution.
19238384Sjkim *
20238384Sjkim * 3. All advertising materials mentioning features or use of this
21238384Sjkim *    software must display the following acknowledgment:
22238384Sjkim *    "This product includes software developed by the OpenSSL Project
23238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24238384Sjkim *
25238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26238384Sjkim *    endorse or promote products derived from this software without
27238384Sjkim *    prior written permission. For written permission, please contact
28238384Sjkim *    licensing@OpenSSL.org.
29238384Sjkim *
30238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
31238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
32238384Sjkim *    permission of the OpenSSL Project.
33238384Sjkim *
34238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
35238384Sjkim *    acknowledgment:
36238384Sjkim *    "This product includes software developed by the OpenSSL Project
37238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38238384Sjkim *
39238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
51238384Sjkim * ====================================================================
52238384Sjkim *
53238384Sjkim * This product includes cryptographic software written by Eric Young
54238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
55238384Sjkim * Hudson (tjh@cryptsoft.com).
56238384Sjkim *
57238384Sjkim */
58238384Sjkim
59238384Sjkim#include "cryptlib.h"
60238384Sjkim#include <openssl/objects.h>
61238384Sjkim#include <openssl/ts.h>
62238384Sjkim
63238384SjkimTS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
64238384Sjkim	{
65238384Sjkim	TS_VERIFY_CTX *ctx =
66238384Sjkim		(TS_VERIFY_CTX *) OPENSSL_malloc(sizeof(TS_VERIFY_CTX));
67238384Sjkim	if (ctx)
68238384Sjkim		memset(ctx, 0, sizeof(TS_VERIFY_CTX));
69238384Sjkim	else
70238384Sjkim		TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
71238384Sjkim	return ctx;
72238384Sjkim	}
73238384Sjkim
74238384Sjkimvoid TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
75238384Sjkim	{
76238384Sjkim	OPENSSL_assert(ctx != NULL);
77238384Sjkim	memset(ctx, 0, sizeof(TS_VERIFY_CTX));
78238384Sjkim	}
79238384Sjkim
80238384Sjkimvoid TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
81238384Sjkim	{
82238384Sjkim	if (!ctx) return;
83238384Sjkim
84238384Sjkim	TS_VERIFY_CTX_cleanup(ctx);
85238384Sjkim	OPENSSL_free(ctx);
86238384Sjkim	}
87238384Sjkim
88238384Sjkimvoid TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
89238384Sjkim	{
90238384Sjkim	if (!ctx) return;
91238384Sjkim
92238384Sjkim	X509_STORE_free(ctx->store);
93238384Sjkim	sk_X509_pop_free(ctx->certs, X509_free);
94238384Sjkim
95238384Sjkim	ASN1_OBJECT_free(ctx->policy);
96238384Sjkim
97238384Sjkim	X509_ALGOR_free(ctx->md_alg);
98238384Sjkim	OPENSSL_free(ctx->imprint);
99238384Sjkim
100238384Sjkim	BIO_free_all(ctx->data);
101238384Sjkim
102238384Sjkim	ASN1_INTEGER_free(ctx->nonce);
103238384Sjkim
104238384Sjkim	GENERAL_NAME_free(ctx->tsa_name);
105238384Sjkim
106238384Sjkim	TS_VERIFY_CTX_init(ctx);
107238384Sjkim	}
108238384Sjkim
109238384SjkimTS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
110238384Sjkim	{
111238384Sjkim	TS_VERIFY_CTX *ret = ctx;
112238384Sjkim	ASN1_OBJECT *policy;
113238384Sjkim	TS_MSG_IMPRINT *imprint;
114238384Sjkim	X509_ALGOR *md_alg;
115238384Sjkim	ASN1_OCTET_STRING *msg;
116238384Sjkim	const ASN1_INTEGER *nonce;
117238384Sjkim
118238384Sjkim	OPENSSL_assert(req != NULL);
119238384Sjkim	if (ret)
120238384Sjkim		TS_VERIFY_CTX_cleanup(ret);
121238384Sjkim	else
122238384Sjkim		if (!(ret = TS_VERIFY_CTX_new())) return NULL;
123238384Sjkim
124238384Sjkim	/* Setting flags. */
125238384Sjkim	ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
126238384Sjkim
127238384Sjkim	/* Setting policy. */
128238384Sjkim	if ((policy = TS_REQ_get_policy_id(req)) != NULL)
129238384Sjkim		{
130238384Sjkim		if (!(ret->policy = OBJ_dup(policy))) goto err;
131238384Sjkim		}
132238384Sjkim	else
133238384Sjkim		ret->flags &= ~TS_VFY_POLICY;
134238384Sjkim
135238384Sjkim	/* Setting md_alg, imprint and imprint_len. */
136238384Sjkim	imprint = TS_REQ_get_msg_imprint(req);
137238384Sjkim	md_alg = TS_MSG_IMPRINT_get_algo(imprint);
138238384Sjkim	if (!(ret->md_alg = X509_ALGOR_dup(md_alg))) goto err;
139238384Sjkim	msg = TS_MSG_IMPRINT_get_msg(imprint);
140238384Sjkim	ret->imprint_len = ASN1_STRING_length(msg);
141238384Sjkim	if (!(ret->imprint = OPENSSL_malloc(ret->imprint_len))) goto err;
142238384Sjkim	memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
143238384Sjkim
144238384Sjkim	/* Setting nonce. */
145238384Sjkim	if ((nonce = TS_REQ_get_nonce(req)) != NULL)
146238384Sjkim		{
147238384Sjkim		if (!(ret->nonce = ASN1_INTEGER_dup(nonce))) goto err;
148238384Sjkim		}
149238384Sjkim	else
150238384Sjkim		ret->flags &= ~TS_VFY_NONCE;
151238384Sjkim
152238384Sjkim	return ret;
153238384Sjkim err:
154238384Sjkim	if (ctx)
155238384Sjkim		TS_VERIFY_CTX_cleanup(ctx);
156238384Sjkim	else
157238384Sjkim		TS_VERIFY_CTX_free(ret);
158238384Sjkim	return NULL;
159238384Sjkim	}
160