]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ts/ts_verify_ctx.c
Add and use OPENSSL_zalloc
[thirdparty/openssl.git] / crypto / ts / ts_verify_ctx.c
CommitLineData
c7235be6 1/* crypto/ts/ts_verify_ctx.c */
0f113f3e
MC
2/*
3 * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4 * 2003.
c7235be6
UM
5 */
6/* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
c7235be6
UM
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
b39fc560 60#include "internal/cryptlib.h"
c7235be6
UM
61#include <openssl/objects.h>
62#include <openssl/ts.h>
63
64TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
0f113f3e 65{
b51bce94 66 TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
b196e7d9 67
b51bce94 68 if (!ctx)
0f113f3e
MC
69 TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
70 return ctx;
71}
c7235be6
UM
72
73void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
0f113f3e
MC
74{
75 OPENSSL_assert(ctx != NULL);
16f8d4eb 76 memset(ctx, 0, sizeof(*ctx));
0f113f3e 77}
c7235be6
UM
78
79void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
0f113f3e
MC
80{
81 if (!ctx)
82 return;
c7235be6 83
0f113f3e
MC
84 TS_VERIFY_CTX_cleanup(ctx);
85 OPENSSL_free(ctx);
86}
c7235be6
UM
87
88void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
0f113f3e
MC
89{
90 if (!ctx)
91 return;
c7235be6 92
0f113f3e
MC
93 X509_STORE_free(ctx->store);
94 sk_X509_pop_free(ctx->certs, X509_free);
c7235be6 95
0f113f3e 96 ASN1_OBJECT_free(ctx->policy);
c7235be6 97
0f113f3e
MC
98 X509_ALGOR_free(ctx->md_alg);
99 OPENSSL_free(ctx->imprint);
c7235be6 100
0f113f3e 101 BIO_free_all(ctx->data);
c7235be6 102
0f113f3e 103 ASN1_INTEGER_free(ctx->nonce);
c7235be6 104
0f113f3e
MC
105 GENERAL_NAME_free(ctx->tsa_name);
106
107 TS_VERIFY_CTX_init(ctx);
108}
c7235be6
UM
109
110TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
0f113f3e
MC
111{
112 TS_VERIFY_CTX *ret = ctx;
113 ASN1_OBJECT *policy;
114 TS_MSG_IMPRINT *imprint;
115 X509_ALGOR *md_alg;
116 ASN1_OCTET_STRING *msg;
117 const ASN1_INTEGER *nonce;
118
119 OPENSSL_assert(req != NULL);
120 if (ret)
121 TS_VERIFY_CTX_cleanup(ret);
75ebbd9a 122 else if ((ret = TS_VERIFY_CTX_new()) == NULL)
0f113f3e
MC
123 return NULL;
124
125 /* Setting flags. */
126 ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
127
128 /* Setting policy. */
129 if ((policy = TS_REQ_get_policy_id(req)) != NULL) {
75ebbd9a 130 if ((ret->policy = OBJ_dup(policy)) == NULL)
0f113f3e
MC
131 goto err;
132 } else
133 ret->flags &= ~TS_VFY_POLICY;
134
135 /* Setting md_alg, imprint and imprint_len. */
136 imprint = TS_REQ_get_msg_imprint(req);
137 md_alg = TS_MSG_IMPRINT_get_algo(imprint);
75ebbd9a 138 if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
0f113f3e
MC
139 goto err;
140 msg = TS_MSG_IMPRINT_get_msg(imprint);
141 ret->imprint_len = ASN1_STRING_length(msg);
75ebbd9a 142 if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
0f113f3e
MC
143 goto err;
144 memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
145
146 /* Setting nonce. */
147 if ((nonce = TS_REQ_get_nonce(req)) != NULL) {
75ebbd9a 148 if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
0f113f3e
MC
149 goto err;
150 } else
151 ret->flags &= ~TS_VFY_NONCE;
152
153 return ret;
c7235be6 154 err:
0f113f3e
MC
155 if (ctx)
156 TS_VERIFY_CTX_cleanup(ctx);
157 else
158 TS_VERIFY_CTX_free(ret);
159 return NULL;
160}