Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/md.c

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <errno.h>
5 #include <openssl/buffer.h>
6 #include <openssl/evp.h>
7 #include <openssl/pkcs7.h>
8
9 #include "internal.h"
10 #include "../internal.h"
11
12
13 static int md_new(BIO *b) {
14 GUARD_PTR(b);
15 EVP_MD_CTX *ctx;
16
17 ctx = EVP_MD_CTX_new();
18 if (ctx == NULL) {
19 return 0;
20 }
21
22 BIO_set_data(b, ctx);
23
24 return 1;
25 }
26
27 static int md_free(BIO *b) {
28 GUARD_PTR(b);
29 EVP_MD_CTX_free(BIO_get_data(b));
30 BIO_set_data(b, NULL);
31 BIO_set_init(b, 0);
32
33 return 1;
34 }
35
36 static int md_read(BIO *b, char *out, int outl) {
37 GUARD_PTR(b);
38 GUARD_PTR(out);
39 int ret = 0;
40 EVP_MD_CTX *ctx;
41 BIO *next;
42
43 ctx = BIO_get_data(b);
44 next = BIO_next(b);
45
46 if ((ctx == NULL) || (next == NULL) || outl <= 0) {
47 return 0;
48 }
49
50 ret = BIO_read(next, out, outl);
51 if (ret > 0) {
52 if (EVP_DigestUpdate(ctx, (unsigned char *)out, ret) <= 0) {
53 ret = -1;
54 }
55 }
56
57 BIO_clear_retry_flags(b);
58 BIO_copy_next_retry(b);
59 return ret;
60 }
61
62 static int md_write(BIO *b, const char *in, int inl) {
63 GUARD_PTR(b);
64 GUARD_PTR(in);
65 int ret = 0;
66 EVP_MD_CTX *ctx;
67 BIO *next;
68
69 ctx = BIO_get_data(b);
70 next = BIO_next(b);
71
72 if ((ctx == NULL) || (next == NULL) || inl <= 0) {
73 return 0;
74 }
75
76 ret = BIO_write(next, in, inl);
77 if (ret > 0) {
78 if (EVP_DigestUpdate(ctx, (const unsigned char *)in, ret) <= 0) {
79 ret = -1;
80 }
81 }
82
83 BIO_clear_retry_flags(b);
84 BIO_copy_next_retry(b);
85 return ret;
86 }
87
88 static long md_ctrl(BIO *b, int cmd, long num, void *ptr) {
89 GUARD_PTR(b);
90 EVP_MD_CTX *ctx, **pctx;
91 EVP_MD *md;
92 long ret = 1;
93 BIO *next;
94
95 ctx = BIO_get_data(b);
96 next = BIO_next(b);
97
98 switch (cmd) {
99 case BIO_CTRL_RESET:
100 if (BIO_get_init(b)) {
101 ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL);
102 } else {
103 ret = 0;
104 }
105 if (ret > 0) {
106 ret = BIO_ctrl(next, cmd, num, ptr);
107 }
108 break;
109 case BIO_C_GET_MD_CTX:
110 GUARD_PTR(ptr);
111 pctx = ptr;
112 *pctx = ctx;
113 BIO_set_init(b, 1);
114 break;
115 case BIO_C_SET_MD:
116 GUARD_PTR(ptr);
117 md = ptr;
118 ret = EVP_DigestInit_ex(ctx, md, NULL);
119 if (ret > 0) {
120 BIO_set_init(b, 1);
121 }
122 break;
123 case BIO_C_GET_MD:
124 GUARD_PTR(ptr);
125 if (BIO_get_init(b)) {
126 const EVP_MD **ppmd = ptr;
127 *ppmd = EVP_MD_CTX_md(ctx);
128 } else {
129 ret = 0;
130 }
131 break;
132 // |BIO_C_SET_MD_CTX| and |BIO_set_md_ctx| are not implemented due to
133 // complexity with |BIO_f_md|'s automatic |EVP_MD_CTX| allocation.
134 // OpenSSL users would need to manually free the existing |EVP_MD_CTX|
135 // before setting a new one.
136 // Open an issue to AWS-LC if this functionality is ever needed.
137 case BIO_C_SET_MD_CTX:
138 // |BIO_C_DO_STATE_MACHINE| is used by |BIO_do_handshake| for most |BIO|s in
139 // OpenSSL. This functionality is only properly supported by |BIO_s_connect|
140 // within AWS-LC.
141 case BIO_C_DO_STATE_MACHINE:
142 // |BIO_CTRL_DUP| is used by |BIO_dup_state| for most |BIO|s in OpenSSL,
143 // which AWS-LC doesn't have support for. We can implement support here (or
144 // with a direct function call) if we ever have the need to do so.
145 case BIO_CTRL_DUP:
146 // |BIO_CTRL_GET/SET_CALLBACK| are routed to by |BIO_get/set_info_callback|
147 // in OpenSSL, which AWS-LC doesn't have support for. We can implement
148 // support here (or with a direct function call) if we ever have the need to
149 // do so.
150 case BIO_CTRL_GET_CALLBACK:
151 case BIO_CTRL_SET_CALLBACK:
152 OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB);
153 return 0;
154 default:
155 ret = BIO_ctrl(next, cmd, num, ptr);
156 break;
157 }
158 return ret;
159 }
160
161 static int md_gets(BIO *b, char *buf, int size) {
162 GUARD_PTR(b);
163 GUARD_PTR(buf);
164 EVP_MD_CTX *ctx;
165 unsigned int ret;
166
167 ctx = BIO_get_data(b);
168
169 if (((size_t)size) < EVP_MD_CTX_size(ctx)) {
170 return 0;
171 }
172
173 if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0) {
174 return -1;
175 }
176
177 return ret;
178 }
179
180 static const BIO_METHOD methods_md = {
181 BIO_TYPE_MD, // type
182 "message digest", // name
183 md_write, // bwrite
184 md_read, // bread
185 NULL, // bputs
186 md_gets, // bgets
187 md_ctrl, // ctrl
188 md_new, // create
189 md_free, // destroy
190 NULL, // callback_ctrl
191 };
192
193 const BIO_METHOD *BIO_f_md(void) { return &methods_md; }
194
195 int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **ctx) {
196 return BIO_ctrl(b, BIO_C_GET_MD_CTX, 0, ctx);
197 }
198
199 int BIO_set_md(BIO *b, const EVP_MD *md) {
200 return BIO_ctrl(b, BIO_C_SET_MD, 0, (EVP_MD*)md);
201 }
202
203 int BIO_get_md(BIO *b, EVP_MD **md) {
204 return BIO_ctrl(b, BIO_C_GET_MD, 0, md);
205 }