]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/bio_md.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/buffer.h>
13 #include <openssl/evp.h>
14 #include "internal/bio.h"
17 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
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
);
28 static const BIO_METHOD methods_md
= {
31 /* TODO: Convert to new style write function */
34 /* TODO: Convert to new style read function */
45 const BIO_METHOD
*BIO_f_md(void)
50 static int md_new(BIO
*bi
)
54 ctx
= EVP_MD_CTX_new();
59 BIO_set_data(bi
, ctx
);
64 static int md_free(BIO
*a
)
68 EVP_MD_CTX_free(BIO_get_data(a
));
69 BIO_set_data(a
, NULL
);
75 static int md_read(BIO
*b
, char *out
, int outl
)
84 ctx
= BIO_get_data(b
);
87 if ((ctx
== NULL
) || (next
== NULL
))
90 ret
= BIO_read(next
, out
, outl
);
91 if (BIO_get_init(b
)) {
93 if (EVP_DigestUpdate(ctx
, (unsigned char *)out
,
94 (unsigned int)ret
) <= 0)
98 BIO_clear_retry_flags(b
);
99 BIO_copy_next_retry(b
);
103 static int md_write(BIO
*b
, const char *in
, int inl
)
109 if ((in
== NULL
) || (inl
<= 0))
112 ctx
= BIO_get_data(b
);
114 if ((ctx
!= NULL
) && (next
!= NULL
))
115 ret
= BIO_write(next
, in
, inl
);
117 if (BIO_get_init(b
)) {
119 if (!EVP_DigestUpdate(ctx
, (const unsigned char *)in
,
120 (unsigned int)ret
)) {
121 BIO_clear_retry_flags(b
);
127 BIO_clear_retry_flags(b
);
128 BIO_copy_next_retry(b
);
133 static long md_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
135 EVP_MD_CTX
*ctx
, *dctx
, **pctx
;
142 ctx
= BIO_get_data(b
);
148 ret
= EVP_DigestInit_ex(ctx
, EVP_MD_CTX_md(ctx
), NULL
);
152 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
155 if (BIO_get_init(b
)) {
157 *ppmd
= EVP_MD_CTX_md(ctx
);
161 case BIO_C_GET_MD_CTX
:
166 case BIO_C_SET_MD_CTX
:
168 BIO_set_data(b
, ptr
);
172 case BIO_C_DO_STATE_MACHINE
:
173 BIO_clear_retry_flags(b
);
174 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
175 BIO_copy_next_retry(b
);
180 ret
= EVP_DigestInit_ex(ctx
, md
, NULL
);
186 dctx
= BIO_get_data(dbio
);
187 if (!EVP_MD_CTX_copy_ex(dctx
, ctx
))
192 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
198 static long md_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
207 return BIO_callback_ctrl(next
, cmd
, fp
);
210 static int md_gets(BIO
*bp
, char *buf
, int size
)
215 ctx
= BIO_get_data(bp
);
217 if (size
< EVP_MD_CTX_size(ctx
))
220 if (EVP_DigestFinal_ex(ctx
, (unsigned char *)buf
, &ret
) <= 0)