1.\" $OpenBSD: BIO_f_md.3,v 1.12 2022/12/18 19:35:36 schwarze Exp $ 2.\" full merge up to: OpenSSL 99d63d46 Oct 26 13:56:48 2016 -0400 3.\" 4.\" This file was written by Dr. Stephen Henson <steve@openssl.org>. 5.\" Copyright (c) 2000, 2006, 2009, 2016 The OpenSSL Project. 6.\" All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in 17.\" the documentation and/or other materials provided with the 18.\" distribution. 19.\" 20.\" 3. All advertising materials mentioning features or use of this 21.\" software must display the following acknowledgment: 22.\" "This product includes software developed by the OpenSSL Project 23.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24.\" 25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26.\" endorse or promote products derived from this software without 27.\" prior written permission. For written permission, please contact 28.\" openssl-core@openssl.org. 29.\" 30.\" 5. Products derived from this software may not be called "OpenSSL" 31.\" nor may "OpenSSL" appear in their names without prior written 32.\" permission of the OpenSSL Project. 33.\" 34.\" 6. Redistributions of any form whatsoever must retain the following 35.\" acknowledgment: 36.\" "This product includes software developed by the OpenSSL Project 37.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38.\" 39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50.\" OF THE POSSIBILITY OF SUCH DAMAGE. 51.\" 52.Dd $Mdocdate: December 18 2022 $ 53.Dt BIO_F_MD 3 54.Os 55.Sh NAME 56.Nm BIO_f_md , 57.Nm BIO_set_md , 58.Nm BIO_get_md , 59.Nm BIO_get_md_ctx 60.Nd message digest BIO filter 61.Sh SYNOPSIS 62.In openssl/bio.h 63.In openssl/evp.h 64.Ft const BIO_METHOD * 65.Fo BIO_f_md 66.Fa void 67.Fc 68.Ft int 69.Fo BIO_set_md 70.Fa "BIO *b" 71.Fa "EVP_MD *md" 72.Fc 73.Ft int 74.Fo BIO_get_md 75.Fa "BIO *b" 76.Fa "EVP_MD **mdp" 77.Fc 78.Ft int 79.Fo BIO_get_md_ctx 80.Fa "BIO *b" 81.Fa "EVP_MD_CTX **mdcp" 82.Fc 83.Sh DESCRIPTION 84.Fn BIO_f_md 85returns the message digest BIO method. 86This is a filter BIO that digests any data passed through it. 87It is a BIO wrapper for the digest routines 88.Xr EVP_DigestInit 3 , 89.Xr EVP_DigestUpdate 3 , 90and 91.Xr EVP_DigestFinal 3 . 92.Pp 93Any data written or read through a digest BIO using 94.Xr BIO_read 3 95and 96.Xr BIO_write 3 97is digested. 98.Pp 99.Xr BIO_gets 3 , 100if its 101.Sy size 102parameter is large enough, 103finishes the digest calculation and returns the digest value. 104.Xr BIO_puts 3 105is 106not supported. 107.Pp 108.Xr BIO_reset 3 109reinitialises a digest BIO. 110.Pp 111.Fn BIO_set_md 112sets the message digest of BIO 113.Fa b 114to 115.Fa md : 116this must be called to initialize a digest BIO 117before any data is passed through it. 118It is a 119.Xr BIO_ctrl 3 120macro. 121.Pp 122.Fn BIO_get_md 123places a pointer to the digest BIOs digest method in 124.Fa mdp . 125It is a 126.Xr BIO_ctrl 3 127macro. 128.Pp 129.Fn BIO_get_md_ctx 130returns the digest BIOs context in 131.Fa mdcp . 132.Pp 133The context returned by 134.Fn BIO_get_md_ctx 135can be used in calls to 136.Xr EVP_DigestFinal 3 137and also in the signature routines 138.Xr EVP_SignFinal 3 139and 140.Xr EVP_VerifyFinal 3 . 141.Pp 142The context returned by 143.Fn BIO_get_md_ctx 144is an internal context structure. 145Changes made to this context will affect the digest BIO itself, and 146the context pointer will become invalid when the digest BIO is freed. 147.Pp 148When a chain containing a message digest BIO is copied with 149.Xr BIO_dup_chain 3 , 150.Xr EVP_MD_CTX_copy_ex 3 151is called internally to automatically copy the message digest context 152from the existing BIO object to the new one, 153and the init flag that can be retrieved with 154.Xr BIO_get_init 3 155is set to 1. 156.Pp 157After the digest has been retrieved from a digest BIO, 158it must be reinitialized by calling 159.Xr BIO_reset 3 160or 161.Fn BIO_set_md 162before any more data is passed through it. 163.Pp 164If an application needs to call 165.Xr BIO_gets 3 166or 167.Xr BIO_puts 3 168through a chain containing digest BIOs, 169then this can be done by prepending a buffering BIO. 170.Pp 171Calling 172.Fn BIO_get_md_ctx 173will return the context and initialize the 174.Vt BIO 175state. 176This allows applications to initialize the context externally 177if the standard calls such as 178.Fn BIO_set_md 179are not sufficiently flexible. 180.Sh RETURN VALUES 181.Fn BIO_f_md 182returns the digest BIO method. 183.Pp 184.Fn BIO_set_md , 185.Fn BIO_get_md , 186and 187.Fn BIO_get_md_ctx 188return 1 for success and 0 for failure. 189.Sh EXAMPLES 190The following example creates a BIO chain containing a SHA-1 and MD5 191digest BIO and passes the string "Hello World" through it. 192Error checking has been omitted for clarity. 193.Bd -literal -offset 2n 194BIO *bio, *mdtmp; 195const char message[] = "Hello World"; 196bio = BIO_new(BIO_s_null()); 197mdtmp = BIO_new(BIO_f_md()); 198BIO_set_md(mdtmp, EVP_sha1()); 199/* 200 * For BIO_push() we want to append the sink BIO 201 * and keep a note of the start of the chain. 202 */ 203bio = BIO_push(mdtmp, bio); 204mdtmp = BIO_new(BIO_f_md()); 205BIO_set_md(mdtmp, EVP_md5()); 206bio = BIO_push(mdtmp, bio); 207/* Note: mdtmp can now be discarded */ 208BIO_write(bio, message, strlen(message)); 209.Ed 210.Pp 211The next example digests data by reading through a chain instead: 212.Bd -literal -offset 2n 213BIO *bio, *mdtmp; 214char buf[1024]; 215int rdlen; 216 217bio = BIO_new_file(file, "rb"); 218mdtmp = BIO_new(BIO_f_md()); 219BIO_set_md(mdtmp, EVP_sha1()); 220bio = BIO_push(mdtmp, bio); 221mdtmp = BIO_new(BIO_f_md()); 222BIO_set_md(mdtmp, EVP_md5()); 223bio = BIO_push(mdtmp, bio); 224do { 225 rdlen = BIO_read(bio, buf, sizeof(buf)); 226 /* Might want to do something with the data here */ 227} while (rdlen > 0); 228.Ed 229.Pp 230This next example retrieves the message digests from a BIO chain 231and outputs them. 232This could be used with the examples above. 233.Bd -literal -offset 2n 234BIO *mdtmp; 235unsigned char mdbuf[EVP_MAX_MD_SIZE]; 236int mdlen; 237int i; 238 239mdtmp = bio; /* Assume bio has previously been set up */ 240do { 241 EVP_MD *md; 242 mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD); 243 if (!mdtmp) 244 break; 245 BIO_get_md(mdtmp, &md); 246 printf("%s digest", OBJ_nid2sn(EVP_MD_type(md))); 247 mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE); 248 for(i = 0; i < mdlen; i++) 249 printf(":%02X", mdbuf[i]); 250 printf("\en"); 251 mdtmp = BIO_next(mdtmp); 252} while(mdtmp); 253BIO_free_all(bio); 254.Ed 255.Sh SEE ALSO 256.Xr BIO_new 3 , 257.Xr EVP_DigestInit 3 258.Sh HISTORY 259.Fn BIO_f_md , 260.Fn BIO_set_md , 261and 262.Fn BIO_get_md 263first appeared in SSLeay 0.6.0. 264.Fn BIO_get_md_ctx 265first appeared in SSLeay 0.8.1. 266These functions have been available since 267.Ox 2.4 . 268.Pp 269Before OpenSSL 1.0.0, the call to 270.Fn BIO_get_md_ctx 271would only work if the 272.Vt BIO 273had been initialized, for example by calling 274.Fn BIO_set_md . 275.Sh BUGS 276The lack of support for 277.Xr BIO_puts 3 278and the non-standard behaviour of 279.Xr BIO_gets 3 280could be regarded as anomalous. 281It could be argued that 282.Xr BIO_gets 3 283and 284.Xr BIO_puts 3 285should be passed to the next BIO in the chain and digest the data 286passed through and that digests should be retrieved using a separate 287.Xr BIO_ctrl 3 288call. 289