1 /*
2 * Copyright 2006-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/objects.h>
12 #include <openssl/ts.h>
13 #include "ts_local.h"
14
TS_VERIFY_CTX_new(void)15 TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
16 {
17 TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
18
19 return ctx;
20 }
21
TS_VERIFY_CTX_init(TS_VERIFY_CTX * ctx)22 void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
23 {
24 OPENSSL_assert(ctx != NULL);
25 memset(ctx, 0, sizeof(*ctx));
26 }
27
TS_VERIFY_CTX_free(TS_VERIFY_CTX * ctx)28 void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
29 {
30 if (!ctx)
31 return;
32
33 TS_VERIFY_CTX_cleanup(ctx);
34 OPENSSL_free(ctx);
35 }
36
TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX * ctx,int f)37 int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
38 {
39 ctx->flags |= f;
40 return ctx->flags;
41 }
42
TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX * ctx,int f)43 int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
44 {
45 ctx->flags = f;
46 return ctx->flags;
47 }
48
49 #ifndef OPENSSL_NO_DEPRECATED_3_4
TS_VERIFY_CTX_set_data(TS_VERIFY_CTX * ctx,BIO * b)50 BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
51 {
52 ctx->data = b;
53 return ctx->data;
54 }
55 #endif
56
TS_VERIFY_CTX_set0_data(TS_VERIFY_CTX * ctx,BIO * b)57 int TS_VERIFY_CTX_set0_data(TS_VERIFY_CTX *ctx, BIO *b)
58 {
59 BIO_free_all(ctx->data);
60 ctx->data = b;
61 return 1;
62 }
63
64 #ifndef OPENSSL_NO_DEPRECATED_3_4
TS_VERIFY_CTX_set_store(TS_VERIFY_CTX * ctx,X509_STORE * s)65 X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
66 {
67 ctx->store = s;
68 return ctx->store;
69 }
70 #endif
71
TS_VERIFY_CTX_set0_store(TS_VERIFY_CTX * ctx,X509_STORE * s)72 int TS_VERIFY_CTX_set0_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
73 {
74 X509_STORE_free(ctx->store);
75 ctx->store = s;
76 return 1;
77 }
78
79 #ifndef OPENSSL_NO_DEPRECATED_3_4
STACK_OF(X509)80 STACK_OF(X509) *TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx,
81 STACK_OF(X509) *certs)
82 {
83 ctx->certs = certs;
84 return ctx->certs;
85 }
86 #endif
87
TS_VERIFY_CTX_set0_certs(TS_VERIFY_CTX * ctx,STACK_OF (X509)* certs)88 int TS_VERIFY_CTX_set0_certs(TS_VERIFY_CTX *ctx, STACK_OF(X509) *certs)
89 {
90 OSSL_STACK_OF_X509_free(ctx->certs);
91 ctx->certs = certs;
92 return 1;
93 }
94
95 #ifndef OPENSSL_NO_DEPRECATED_3_4
TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX * ctx,unsigned char * hexstr,long len)96 unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
97 unsigned char *hexstr, long len)
98 {
99 OPENSSL_free(ctx->imprint);
100 ctx->imprint = hexstr;
101 ctx->imprint_len = len;
102 return ctx->imprint;
103 }
104 #endif
105
TS_VERIFY_CTX_set0_imprint(TS_VERIFY_CTX * ctx,unsigned char * hexstr,long len)106 int TS_VERIFY_CTX_set0_imprint(TS_VERIFY_CTX *ctx,
107 unsigned char *hexstr, long len)
108 {
109 OPENSSL_free(ctx->imprint);
110 ctx->imprint = hexstr;
111 ctx->imprint_len = len;
112 return 1;
113 }
114
TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX * ctx)115 void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
116 {
117 if (!ctx)
118 return;
119
120 X509_STORE_free(ctx->store);
121 OSSL_STACK_OF_X509_free(ctx->certs);
122
123 ASN1_OBJECT_free(ctx->policy);
124
125 X509_ALGOR_free(ctx->md_alg);
126 OPENSSL_free(ctx->imprint);
127
128 BIO_free_all(ctx->data);
129
130 ASN1_INTEGER_free(ctx->nonce);
131
132 GENERAL_NAME_free(ctx->tsa_name);
133
134 TS_VERIFY_CTX_init(ctx);
135 }
136
TS_REQ_to_TS_VERIFY_CTX(TS_REQ * req,TS_VERIFY_CTX * ctx)137 TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
138 {
139 TS_VERIFY_CTX *ret = ctx;
140 ASN1_OBJECT *policy;
141 TS_MSG_IMPRINT *imprint;
142 X509_ALGOR *md_alg;
143 ASN1_OCTET_STRING *msg;
144 const ASN1_INTEGER *nonce;
145
146 OPENSSL_assert(req != NULL);
147 if (ret)
148 TS_VERIFY_CTX_cleanup(ret);
149 else if ((ret = TS_VERIFY_CTX_new()) == NULL)
150 return NULL;
151
152 ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
153
154 if ((policy = req->policy_id) != NULL) {
155 if ((ret->policy = OBJ_dup(policy)) == NULL)
156 goto err;
157 } else
158 ret->flags &= ~TS_VFY_POLICY;
159
160 imprint = req->msg_imprint;
161 md_alg = imprint->hash_algo;
162 if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
163 goto err;
164 msg = imprint->hashed_msg;
165 ret->imprint_len = ASN1_STRING_length(msg);
166 if (ret->imprint_len <= 0)
167 goto err;
168 if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
169 goto err;
170 memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
171
172 if ((nonce = req->nonce) != NULL) {
173 if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
174 goto err;
175 } else
176 ret->flags &= ~TS_VFY_NONCE;
177
178 return ret;
179 err:
180 if (ctx)
181 TS_VERIFY_CTX_cleanup(ctx);
182 else
183 TS_VERIFY_CTX_free(ret);
184 return NULL;
185 }
186