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