INSTALL.md: Restore $ as command prompt indicator
[oweals/openssl.git] / crypto / ts / ts_verify_ctx.c
1 /*
2  * Copyright 2006-2020 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
15 DEFINE_STACK_OF(X509)
16
17 TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
18 {
19     TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
20
21     if (ctx == NULL)
22         TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
23     return ctx;
24 }
25
26 void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
27 {
28     OPENSSL_assert(ctx != NULL);
29     memset(ctx, 0, sizeof(*ctx));
30 }
31
32 void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
33 {
34     if (!ctx)
35         return;
36
37     TS_VERIFY_CTX_cleanup(ctx);
38     OPENSSL_free(ctx);
39 }
40
41 int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
42 {
43     ctx->flags |= f;
44     return ctx->flags;
45 }
46
47 int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
48 {
49     ctx->flags = f;
50     return ctx->flags;
51 }
52
53 BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
54 {
55     ctx->data = b;
56     return ctx->data;
57 }
58
59 X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
60 {
61     ctx->store = s;
62     return ctx->store;
63 }
64
65 STACK_OF(X509) *TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx,
66                                         STACK_OF(X509) *certs)
67 {
68     ctx->certs = certs;
69     return ctx->certs;
70 }
71
72 unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
73                                          unsigned char *hexstr, long len)
74 {
75     ctx->imprint = hexstr;
76     ctx->imprint_len = len;
77     return ctx->imprint;
78 }
79
80 void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
81 {
82     if (!ctx)
83         return;
84
85     X509_STORE_free(ctx->store);
86     sk_X509_pop_free(ctx->certs, X509_free);
87
88     ASN1_OBJECT_free(ctx->policy);
89
90     X509_ALGOR_free(ctx->md_alg);
91     OPENSSL_free(ctx->imprint);
92
93     BIO_free_all(ctx->data);
94
95     ASN1_INTEGER_free(ctx->nonce);
96
97     GENERAL_NAME_free(ctx->tsa_name);
98
99     TS_VERIFY_CTX_init(ctx);
100 }
101
102 TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
103 {
104     TS_VERIFY_CTX *ret = ctx;
105     ASN1_OBJECT *policy;
106     TS_MSG_IMPRINT *imprint;
107     X509_ALGOR *md_alg;
108     ASN1_OCTET_STRING *msg;
109     const ASN1_INTEGER *nonce;
110
111     OPENSSL_assert(req != NULL);
112     if (ret)
113         TS_VERIFY_CTX_cleanup(ret);
114     else if ((ret = TS_VERIFY_CTX_new()) == NULL)
115         return NULL;
116
117     ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
118
119     if ((policy = req->policy_id) != NULL) {
120         if ((ret->policy = OBJ_dup(policy)) == NULL)
121             goto err;
122     } else
123         ret->flags &= ~TS_VFY_POLICY;
124
125     imprint = req->msg_imprint;
126     md_alg = imprint->hash_algo;
127     if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
128         goto err;
129     msg = imprint->hashed_msg;
130     ret->imprint_len = ASN1_STRING_length(msg);
131     if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
132         goto err;
133     memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
134
135     if ((nonce = req->nonce) != NULL) {
136         if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
137             goto err;
138     } else
139         ret->flags &= ~TS_VFY_NONCE;
140
141     return ret;
142  err:
143     if (ctx)
144         TS_VERIFY_CTX_cleanup(ctx);
145     else
146         TS_VERIFY_CTX_free(ret);
147     return NULL;
148 }