1*bb933e2fSbeck /* $OpenBSD: ts_verify_ctx.c,v 1.14 2023/07/07 07:25:21 beck Exp $ */
2f1535dc8Sdjm /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3f1535dc8Sdjm * project 2003.
4f1535dc8Sdjm */
5f1535dc8Sdjm /* ====================================================================
6f1535dc8Sdjm * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7f1535dc8Sdjm *
8f1535dc8Sdjm * Redistribution and use in source and binary forms, with or without
9f1535dc8Sdjm * modification, are permitted provided that the following conditions
10f1535dc8Sdjm * are met:
11f1535dc8Sdjm *
12f1535dc8Sdjm * 1. Redistributions of source code must retain the above copyright
13f1535dc8Sdjm * notice, this list of conditions and the following disclaimer.
14f1535dc8Sdjm *
15f1535dc8Sdjm * 2. Redistributions in binary form must reproduce the above copyright
16f1535dc8Sdjm * notice, this list of conditions and the following disclaimer in
17f1535dc8Sdjm * the documentation and/or other materials provided with the
18f1535dc8Sdjm * distribution.
19f1535dc8Sdjm *
20f1535dc8Sdjm * 3. All advertising materials mentioning features or use of this
21f1535dc8Sdjm * software must display the following acknowledgment:
22f1535dc8Sdjm * "This product includes software developed by the OpenSSL Project
23f1535dc8Sdjm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f1535dc8Sdjm *
25f1535dc8Sdjm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f1535dc8Sdjm * endorse or promote products derived from this software without
27f1535dc8Sdjm * prior written permission. For written permission, please contact
28f1535dc8Sdjm * licensing@OpenSSL.org.
29f1535dc8Sdjm *
30f1535dc8Sdjm * 5. Products derived from this software may not be called "OpenSSL"
31f1535dc8Sdjm * nor may "OpenSSL" appear in their names without prior written
32f1535dc8Sdjm * permission of the OpenSSL Project.
33f1535dc8Sdjm *
34f1535dc8Sdjm * 6. Redistributions of any form whatsoever must retain the following
35f1535dc8Sdjm * acknowledgment:
36f1535dc8Sdjm * "This product includes software developed by the OpenSSL Project
37f1535dc8Sdjm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f1535dc8Sdjm *
39f1535dc8Sdjm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f1535dc8Sdjm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f1535dc8Sdjm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f1535dc8Sdjm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f1535dc8Sdjm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f1535dc8Sdjm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f1535dc8Sdjm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f1535dc8Sdjm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f1535dc8Sdjm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f1535dc8Sdjm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f1535dc8Sdjm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f1535dc8Sdjm * OF THE POSSIBILITY OF SUCH DAMAGE.
51f1535dc8Sdjm * ====================================================================
52f1535dc8Sdjm *
53f1535dc8Sdjm * This product includes cryptographic software written by Eric Young
54f1535dc8Sdjm * (eay@cryptsoft.com). This product includes software written by Tim
55f1535dc8Sdjm * Hudson (tjh@cryptsoft.com).
56f1535dc8Sdjm *
57f1535dc8Sdjm */
58f1535dc8Sdjm
59a8913c44Sjsing #include <string.h>
60a8913c44Sjsing
61b6ab114eSjsing #include <openssl/err.h>
62f1535dc8Sdjm #include <openssl/objects.h>
63f1535dc8Sdjm #include <openssl/ts.h>
64f1535dc8Sdjm
650cea7bbeStb #include "ts_local.h"
660cea7bbeStb
67ec7cdc12Sjsing TS_VERIFY_CTX *
TS_VERIFY_CTX_new(void)68ec7cdc12Sjsing TS_VERIFY_CTX_new(void)
69f1535dc8Sdjm {
7066415b63Stedu TS_VERIFY_CTX *ctx = calloc(1, sizeof(TS_VERIFY_CTX));
71ec7cdc12Sjsing
7266415b63Stedu if (!ctx)
735067ae9fSbeck TSerror(ERR_R_MALLOC_FAILURE);
7466415b63Stedu
75f1535dc8Sdjm return ctx;
76f1535dc8Sdjm }
77*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_new);
78f1535dc8Sdjm
79ec7cdc12Sjsing void
TS_VERIFY_CTX_free(TS_VERIFY_CTX * ctx)80ec7cdc12Sjsing TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
81f1535dc8Sdjm {
82ec7cdc12Sjsing if (!ctx)
83ec7cdc12Sjsing return;
84f1535dc8Sdjm
85f1535dc8Sdjm TS_VERIFY_CTX_cleanup(ctx);
866f3a6cb1Sbeck free(ctx);
87f1535dc8Sdjm }
88*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_free);
89f1535dc8Sdjm
90ec7cdc12Sjsing void
TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX * ctx)91ec7cdc12Sjsing TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
92f1535dc8Sdjm {
93ec7cdc12Sjsing if (!ctx)
94ec7cdc12Sjsing return;
95f1535dc8Sdjm
96f1535dc8Sdjm X509_STORE_free(ctx->store);
97f1535dc8Sdjm sk_X509_pop_free(ctx->certs, X509_free);
98f1535dc8Sdjm
99f1535dc8Sdjm ASN1_OBJECT_free(ctx->policy);
100f1535dc8Sdjm
101f1535dc8Sdjm X509_ALGOR_free(ctx->md_alg);
1026f3a6cb1Sbeck free(ctx->imprint);
103f1535dc8Sdjm
104f1535dc8Sdjm BIO_free_all(ctx->data);
105f1535dc8Sdjm
106f1535dc8Sdjm ASN1_INTEGER_free(ctx->nonce);
107f1535dc8Sdjm
108f1535dc8Sdjm GENERAL_NAME_free(ctx->tsa_name);
109f1535dc8Sdjm
11047468d46Stb memset(ctx, 0, sizeof(*ctx));
111f1535dc8Sdjm }
112*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_cleanup);
113f1535dc8Sdjm
1144f19ead7Stb /*
1154f19ead7Stb * XXX: The following accessors demonstrate the amount of care and thought that
1164f19ead7Stb * went into OpenSSL 1.1 API design and the review thereof: for whatever reason
1174f19ead7Stb * these functions return what was passed in. Correct memory management is left
1184f19ead7Stb * as an exercise for the reader... Unfortunately, careful consumers like
1194f19ead7Stb * openssl-ruby assume this behavior, so we're stuck with this insanity. The
1204f19ead7Stb * cherry on top is the TS_VERIFY_CTS_set_certs() [sic!] function that made it
1214f19ead7Stb * into the public API.
1224f19ead7Stb *
1234f19ead7Stb * Outstanding job, R$ and tjh, A+.
1244f19ead7Stb */
1254f19ead7Stb
1264f19ead7Stb int
TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX * ctx,int flags)1274f19ead7Stb TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int flags)
1284f19ead7Stb {
1294f19ead7Stb ctx->flags |= flags;
1304f19ead7Stb
1314f19ead7Stb return ctx->flags;
1324f19ead7Stb }
133*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_add_flags);
1344f19ead7Stb
1354f19ead7Stb int
TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX * ctx,int flags)1364f19ead7Stb TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int flags)
1374f19ead7Stb {
1384f19ead7Stb ctx->flags = flags;
1394f19ead7Stb
1404f19ead7Stb return ctx->flags;
1414f19ead7Stb }
142*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_set_flags);
1434f19ead7Stb
1444f19ead7Stb BIO *
TS_VERIFY_CTX_set_data(TS_VERIFY_CTX * ctx,BIO * bio)1454f19ead7Stb TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *bio)
1464f19ead7Stb {
1474f19ead7Stb ctx->data = bio;
1484f19ead7Stb
1494f19ead7Stb return ctx->data;
1504f19ead7Stb }
151*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_set_data);
1524f19ead7Stb
1534f19ead7Stb X509_STORE *
TS_VERIFY_CTX_set_store(TS_VERIFY_CTX * ctx,X509_STORE * store)1544f19ead7Stb TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *store)
1554f19ead7Stb {
1564f19ead7Stb ctx->store = store;
1574f19ead7Stb
1584f19ead7Stb return ctx->store;
1594f19ead7Stb }
160*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_set_store);
1614f19ead7Stb
STACK_OF(X509)1624f19ead7Stb STACK_OF(X509) *
1634f19ead7Stb TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx, STACK_OF(X509) *certs)
1644f19ead7Stb {
1654f19ead7Stb ctx->certs = certs;
1664f19ead7Stb
1674f19ead7Stb return ctx->certs;
1684f19ead7Stb }
169*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_set_certs);
1704f19ead7Stb
1714f19ead7Stb unsigned char *
TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX * ctx,unsigned char * imprint,long imprint_len)1724f19ead7Stb TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx, unsigned char *imprint,
1734f19ead7Stb long imprint_len)
1744f19ead7Stb {
1754f19ead7Stb free(ctx->imprint);
1764f19ead7Stb
1774f19ead7Stb ctx->imprint = imprint;
1784f19ead7Stb ctx->imprint_len = imprint_len;
1794f19ead7Stb
1804f19ead7Stb return ctx->imprint;
1814f19ead7Stb }
182*bb933e2fSbeck LCRYPTO_ALIAS(TS_VERIFY_CTX_set_imprint);
1834f19ead7Stb
184ec7cdc12Sjsing TS_VERIFY_CTX *
TS_REQ_to_TS_VERIFY_CTX(TS_REQ * req,TS_VERIFY_CTX * ctx)185ec7cdc12Sjsing TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
186f1535dc8Sdjm {
187f1535dc8Sdjm TS_VERIFY_CTX *ret = ctx;
188f1535dc8Sdjm ASN1_OBJECT *policy;
189f1535dc8Sdjm TS_MSG_IMPRINT *imprint;
190f1535dc8Sdjm X509_ALGOR *md_alg;
191f1535dc8Sdjm ASN1_OCTET_STRING *msg;
192f1535dc8Sdjm const ASN1_INTEGER *nonce;
193f1535dc8Sdjm
194f1535dc8Sdjm if (ret)
195f1535dc8Sdjm TS_VERIFY_CTX_cleanup(ret);
196ec7cdc12Sjsing else if (!(ret = TS_VERIFY_CTX_new()))
197ec7cdc12Sjsing return NULL;
198f1535dc8Sdjm
199f1535dc8Sdjm /* Setting flags. */
200f1535dc8Sdjm ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
201f1535dc8Sdjm
202f1535dc8Sdjm /* Setting policy. */
203ec7cdc12Sjsing if ((policy = TS_REQ_get_policy_id(req)) != NULL) {
204ec7cdc12Sjsing if (!(ret->policy = OBJ_dup(policy)))
205ec7cdc12Sjsing goto err;
206ec7cdc12Sjsing } else
207f1535dc8Sdjm ret->flags &= ~TS_VFY_POLICY;
208f1535dc8Sdjm
209f1535dc8Sdjm /* Setting md_alg, imprint and imprint_len. */
210f1535dc8Sdjm imprint = TS_REQ_get_msg_imprint(req);
211f1535dc8Sdjm md_alg = TS_MSG_IMPRINT_get_algo(imprint);
212ec7cdc12Sjsing if (!(ret->md_alg = X509_ALGOR_dup(md_alg)))
213ec7cdc12Sjsing goto err;
214f1535dc8Sdjm msg = TS_MSG_IMPRINT_get_msg(imprint);
215f1535dc8Sdjm ret->imprint_len = ASN1_STRING_length(msg);
216ec7cdc12Sjsing if (!(ret->imprint = malloc(ret->imprint_len)))
217ec7cdc12Sjsing goto err;
218f1535dc8Sdjm memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
219f1535dc8Sdjm
220f1535dc8Sdjm /* Setting nonce. */
221ec7cdc12Sjsing if ((nonce = TS_REQ_get_nonce(req)) != NULL) {
222ec7cdc12Sjsing if (!(ret->nonce = ASN1_INTEGER_dup(nonce)))
223ec7cdc12Sjsing goto err;
224ec7cdc12Sjsing } else
225f1535dc8Sdjm ret->flags &= ~TS_VFY_NONCE;
226f1535dc8Sdjm
227f1535dc8Sdjm return ret;
228ec7cdc12Sjsing
229f1535dc8Sdjm err:
230f1535dc8Sdjm if (ctx)
231f1535dc8Sdjm TS_VERIFY_CTX_cleanup(ctx);
232f1535dc8Sdjm else
233f1535dc8Sdjm TS_VERIFY_CTX_free(ret);
234f1535dc8Sdjm return NULL;
235f1535dc8Sdjm }
236*bb933e2fSbeck LCRYPTO_ALIAS(TS_REQ_to_TS_VERIFY_CTX);
237