xref: /freebsd/crypto/openssl/crypto/evp/bio_md.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-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 <stdio.h>
11 #include <errno.h>
12 #include <openssl/buffer.h>
13 #include <openssl/evp.h>
14 #include "internal/bio.h"
15 
16 /*
17  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18  */
19 
20 static int md_write(BIO *h, char const *buf, int num);
21 static int md_read(BIO *h, char *buf, int size);
22 static int md_gets(BIO *h, char *str, int size);
23 static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
24 static int md_new(BIO *h);
25 static int md_free(BIO *data);
26 static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
27 
28 static const BIO_METHOD methods_md = {
29     BIO_TYPE_MD,
30     "message digest",
31     bwrite_conv,
32     md_write,
33     bread_conv,
34     md_read,
35     NULL, /* md_puts, */
36     md_gets,
37     md_ctrl,
38     md_new,
39     md_free,
40     md_callback_ctrl,
41 };
42 
BIO_f_md(void)43 const BIO_METHOD *BIO_f_md(void)
44 {
45     return &methods_md;
46 }
47 
md_new(BIO * bi)48 static int md_new(BIO *bi)
49 {
50     EVP_MD_CTX *ctx;
51 
52     ctx = EVP_MD_CTX_new();
53     if (ctx == NULL)
54         return 0;
55 
56     BIO_set_init(bi, 1);
57     BIO_set_data(bi, ctx);
58 
59     return 1;
60 }
61 
md_free(BIO * a)62 static int md_free(BIO *a)
63 {
64     if (a == NULL)
65         return 0;
66     EVP_MD_CTX_free(BIO_get_data(a));
67     BIO_set_data(a, NULL);
68     BIO_set_init(a, 0);
69 
70     return 1;
71 }
72 
md_read(BIO * b,char * out,int outl)73 static int md_read(BIO *b, char *out, int outl)
74 {
75     int ret = 0;
76     EVP_MD_CTX *ctx;
77     BIO *next;
78 
79     if (out == NULL)
80         return 0;
81 
82     ctx = BIO_get_data(b);
83     next = BIO_next(b);
84 
85     if ((ctx == NULL) || (next == NULL))
86         return 0;
87 
88     ret = BIO_read(next, out, outl);
89     if (BIO_get_init(b)) {
90         if (ret > 0) {
91             if (EVP_DigestUpdate(ctx, (unsigned char *)out,
92                     (unsigned int)ret)
93                 <= 0)
94                 return -1;
95         }
96     }
97     BIO_clear_retry_flags(b);
98     BIO_copy_next_retry(b);
99     return ret;
100 }
101 
md_write(BIO * b,const char * in,int inl)102 static int md_write(BIO *b, const char *in, int inl)
103 {
104     int ret = 0;
105     EVP_MD_CTX *ctx;
106     BIO *next;
107 
108     if ((in == NULL) || (inl <= 0))
109         return 0;
110 
111     ctx = BIO_get_data(b);
112     next = BIO_next(b);
113     if ((ctx != NULL) && (next != NULL))
114         ret = BIO_write(next, in, inl);
115 
116     if (BIO_get_init(b)) {
117         if (ret > 0) {
118             if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
119                     (unsigned int)ret)) {
120                 BIO_clear_retry_flags(b);
121                 return 0;
122             }
123         }
124     }
125     if (next != NULL) {
126         BIO_clear_retry_flags(b);
127         BIO_copy_next_retry(b);
128     }
129     return ret;
130 }
131 
md_ctrl(BIO * b,int cmd,long num,void * ptr)132 static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
133 {
134     EVP_MD_CTX *ctx, *dctx, **pctx;
135     const EVP_MD **ppmd;
136     EVP_MD *md;
137     long ret = 1;
138     BIO *dbio, *next;
139 
140     ctx = BIO_get_data(b);
141     next = BIO_next(b);
142 
143     switch (cmd) {
144     case BIO_CTRL_RESET:
145         if (BIO_get_init(b))
146             ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL);
147         else
148             ret = 0;
149         if (ret > 0)
150             ret = BIO_ctrl(next, cmd, num, ptr);
151         break;
152     case BIO_C_GET_MD:
153         if (BIO_get_init(b)) {
154             ppmd = ptr;
155             *ppmd = EVP_MD_CTX_get0_md(ctx);
156         } else
157             ret = 0;
158         break;
159     case BIO_C_GET_MD_CTX:
160         pctx = ptr;
161         *pctx = ctx;
162         BIO_set_init(b, 1);
163         break;
164     case BIO_C_SET_MD_CTX:
165         if (BIO_get_init(b))
166             BIO_set_data(b, ptr);
167         else
168             ret = 0;
169         break;
170     case BIO_C_DO_STATE_MACHINE:
171         BIO_clear_retry_flags(b);
172         ret = BIO_ctrl(next, cmd, num, ptr);
173         BIO_copy_next_retry(b);
174         break;
175 
176     case BIO_C_SET_MD:
177         md = ptr;
178         ret = EVP_DigestInit_ex(ctx, md, NULL);
179         if (ret > 0)
180             BIO_set_init(b, 1);
181         break;
182     case BIO_CTRL_DUP:
183         dbio = ptr;
184         dctx = BIO_get_data(dbio);
185         if (!EVP_MD_CTX_copy_ex(dctx, ctx))
186             return 0;
187         BIO_set_init(b, 1);
188         break;
189     default:
190         ret = BIO_ctrl(next, cmd, num, ptr);
191         break;
192     }
193     return ret;
194 }
195 
md_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)196 static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
197 {
198     BIO *next;
199 
200     next = BIO_next(b);
201 
202     if (next == NULL)
203         return 0;
204 
205     return BIO_callback_ctrl(next, cmd, fp);
206 }
207 
md_gets(BIO * bp,char * buf,int size)208 static int md_gets(BIO *bp, char *buf, int size)
209 {
210     EVP_MD_CTX *ctx;
211     unsigned int ret;
212 
213     ctx = BIO_get_data(bp);
214 
215     if (size < EVP_MD_CTX_get_size(ctx))
216         return 0;
217 
218     if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
219         return -1;
220 
221     return (int)ret;
222 }
223