1*e71b7053SJung-uk Kim=pod 2*e71b7053SJung-uk Kim 3*e71b7053SJung-uk Kim=head1 NAME 4*e71b7053SJung-uk Kim 5*e71b7053SJung-uk KimBIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO filter 6*e71b7053SJung-uk Kim 7*e71b7053SJung-uk Kim=head1 SYNOPSIS 8*e71b7053SJung-uk Kim 9*e71b7053SJung-uk Kim=for comment multiple includes 10*e71b7053SJung-uk Kim 11*e71b7053SJung-uk Kim #include <openssl/bio.h> 12*e71b7053SJung-uk Kim #include <openssl/evp.h> 13*e71b7053SJung-uk Kim 14*e71b7053SJung-uk Kim const BIO_METHOD *BIO_f_md(void); 15*e71b7053SJung-uk Kim int BIO_set_md(BIO *b, EVP_MD *md); 16*e71b7053SJung-uk Kim int BIO_get_md(BIO *b, EVP_MD **mdp); 17*e71b7053SJung-uk Kim int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **mdcp); 18*e71b7053SJung-uk Kim 19*e71b7053SJung-uk Kim=head1 DESCRIPTION 20*e71b7053SJung-uk Kim 21*e71b7053SJung-uk KimBIO_f_md() returns the message digest BIO method. This is a filter 22*e71b7053SJung-uk KimBIO that digests any data passed through it, it is a BIO wrapper 23*e71b7053SJung-uk Kimfor the digest routines EVP_DigestInit(), EVP_DigestUpdate() 24*e71b7053SJung-uk Kimand EVP_DigestFinal(). 25*e71b7053SJung-uk Kim 26*e71b7053SJung-uk KimAny data written or read through a digest BIO using BIO_read_ex() and 27*e71b7053SJung-uk KimBIO_write_ex() is digested. 28*e71b7053SJung-uk Kim 29*e71b7053SJung-uk KimBIO_gets(), if its B<size> parameter is large enough finishes the 30*e71b7053SJung-uk Kimdigest calculation and returns the digest value. BIO_puts() is 31*e71b7053SJung-uk Kimnot supported. 32*e71b7053SJung-uk Kim 33*e71b7053SJung-uk KimBIO_reset() reinitialises a digest BIO. 34*e71b7053SJung-uk Kim 35*e71b7053SJung-uk KimBIO_set_md() sets the message digest of BIO B<b> to B<md>: this 36*e71b7053SJung-uk Kimmust be called to initialize a digest BIO before any data is 37*e71b7053SJung-uk Kimpassed through it. It is a BIO_ctrl() macro. 38*e71b7053SJung-uk Kim 39*e71b7053SJung-uk KimBIO_get_md() places the a pointer to the digest BIOs digest method 40*e71b7053SJung-uk Kimin B<mdp>, it is a BIO_ctrl() macro. 41*e71b7053SJung-uk Kim 42*e71b7053SJung-uk KimBIO_get_md_ctx() returns the digest BIOs context into B<mdcp>. 43*e71b7053SJung-uk Kim 44*e71b7053SJung-uk Kim=head1 NOTES 45*e71b7053SJung-uk Kim 46*e71b7053SJung-uk KimThe context returned by BIO_get_md_ctx() can be used in calls 47*e71b7053SJung-uk Kimto EVP_DigestFinal() and also the signature routines EVP_SignFinal() 48*e71b7053SJung-uk Kimand EVP_VerifyFinal(). 49*e71b7053SJung-uk Kim 50*e71b7053SJung-uk KimThe context returned by BIO_get_md_ctx() is an internal context 51*e71b7053SJung-uk Kimstructure. Changes made to this context will affect the digest 52*e71b7053SJung-uk KimBIO itself and the context pointer will become invalid when the digest 53*e71b7053SJung-uk KimBIO is freed. 54*e71b7053SJung-uk Kim 55*e71b7053SJung-uk KimAfter the digest has been retrieved from a digest BIO it must be 56*e71b7053SJung-uk Kimreinitialized by calling BIO_reset(), or BIO_set_md() before any more 57*e71b7053SJung-uk Kimdata is passed through it. 58*e71b7053SJung-uk Kim 59*e71b7053SJung-uk KimIf an application needs to call BIO_gets() or BIO_puts() through 60*e71b7053SJung-uk Kima chain containing digest BIOs then this can be done by prepending 61*e71b7053SJung-uk Kima buffering BIO. 62*e71b7053SJung-uk Kim 63*e71b7053SJung-uk KimCalling BIO_get_md_ctx() will return the context and initialize the BIO 64*e71b7053SJung-uk Kimstate. This allows applications to initialize the context externally 65*e71b7053SJung-uk Kimif the standard calls such as BIO_set_md() are not sufficiently flexible. 66*e71b7053SJung-uk Kim 67*e71b7053SJung-uk Kim=head1 RETURN VALUES 68*e71b7053SJung-uk Kim 69*e71b7053SJung-uk KimBIO_f_md() returns the digest BIO method. 70*e71b7053SJung-uk Kim 71*e71b7053SJung-uk KimBIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and 72*e71b7053SJung-uk Kim0 for failure. 73*e71b7053SJung-uk Kim 74*e71b7053SJung-uk Kim=head1 EXAMPLES 75*e71b7053SJung-uk Kim 76*e71b7053SJung-uk KimThe following example creates a BIO chain containing an SHA1 and MD5 77*e71b7053SJung-uk Kimdigest BIO and passes the string "Hello World" through it. Error 78*e71b7053SJung-uk Kimchecking has been omitted for clarity. 79*e71b7053SJung-uk Kim 80*e71b7053SJung-uk Kim BIO *bio, *mdtmp; 81*e71b7053SJung-uk Kim char message[] = "Hello World"; 82*e71b7053SJung-uk Kim 83*e71b7053SJung-uk Kim bio = BIO_new(BIO_s_null()); 84*e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 85*e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_sha1()); 86*e71b7053SJung-uk Kim /* 87*e71b7053SJung-uk Kim * For BIO_push() we want to append the sink BIO and keep a note of 88*e71b7053SJung-uk Kim * the start of the chain. 89*e71b7053SJung-uk Kim */ 90*e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 91*e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 92*e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_md5()); 93*e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 94*e71b7053SJung-uk Kim /* Note: mdtmp can now be discarded */ 95*e71b7053SJung-uk Kim BIO_write(bio, message, strlen(message)); 96*e71b7053SJung-uk Kim 97*e71b7053SJung-uk KimThe next example digests data by reading through a chain instead: 98*e71b7053SJung-uk Kim 99*e71b7053SJung-uk Kim BIO *bio, *mdtmp; 100*e71b7053SJung-uk Kim char buf[1024]; 101*e71b7053SJung-uk Kim int rdlen; 102*e71b7053SJung-uk Kim 103*e71b7053SJung-uk Kim bio = BIO_new_file(file, "rb"); 104*e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 105*e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_sha1()); 106*e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 107*e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 108*e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_md5()); 109*e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 110*e71b7053SJung-uk Kim do { 111*e71b7053SJung-uk Kim rdlen = BIO_read(bio, buf, sizeof(buf)); 112*e71b7053SJung-uk Kim /* Might want to do something with the data here */ 113*e71b7053SJung-uk Kim } while (rdlen > 0); 114*e71b7053SJung-uk Kim 115*e71b7053SJung-uk KimThis next example retrieves the message digests from a BIO chain and 116*e71b7053SJung-uk Kimoutputs them. This could be used with the examples above. 117*e71b7053SJung-uk Kim 118*e71b7053SJung-uk Kim BIO *mdtmp; 119*e71b7053SJung-uk Kim unsigned char mdbuf[EVP_MAX_MD_SIZE]; 120*e71b7053SJung-uk Kim int mdlen; 121*e71b7053SJung-uk Kim int i; 122*e71b7053SJung-uk Kim 123*e71b7053SJung-uk Kim mdtmp = bio; /* Assume bio has previously been set up */ 124*e71b7053SJung-uk Kim do { 125*e71b7053SJung-uk Kim EVP_MD *md; 126*e71b7053SJung-uk Kim 127*e71b7053SJung-uk Kim mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD); 128*e71b7053SJung-uk Kim if (!mdtmp) 129*e71b7053SJung-uk Kim break; 130*e71b7053SJung-uk Kim BIO_get_md(mdtmp, &md); 131*e71b7053SJung-uk Kim printf("%s digest", OBJ_nid2sn(EVP_MD_type(md))); 132*e71b7053SJung-uk Kim mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE); 133*e71b7053SJung-uk Kim for (i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]); 134*e71b7053SJung-uk Kim printf("\n"); 135*e71b7053SJung-uk Kim mdtmp = BIO_next(mdtmp); 136*e71b7053SJung-uk Kim } while (mdtmp); 137*e71b7053SJung-uk Kim 138*e71b7053SJung-uk Kim BIO_free_all(bio); 139*e71b7053SJung-uk Kim 140*e71b7053SJung-uk Kim=head1 BUGS 141*e71b7053SJung-uk Kim 142*e71b7053SJung-uk KimThe lack of support for BIO_puts() and the non standard behaviour of 143*e71b7053SJung-uk KimBIO_gets() could be regarded as anomalous. It could be argued that BIO_gets() 144*e71b7053SJung-uk Kimand BIO_puts() should be passed to the next BIO in the chain and digest 145*e71b7053SJung-uk Kimthe data passed through and that digests should be retrieved using a 146*e71b7053SJung-uk Kimseparate BIO_ctrl() call. 147*e71b7053SJung-uk Kim 148*e71b7053SJung-uk Kim=head1 HISTORY 149*e71b7053SJung-uk Kim 150*e71b7053SJung-uk KimBefore OpenSSL 1.0.0., the call to BIO_get_md_ctx() would only work if the 151*e71b7053SJung-uk KimBIO was initialized first. 152*e71b7053SJung-uk Kim 153*e71b7053SJung-uk Kim=head1 COPYRIGHT 154*e71b7053SJung-uk Kim 155*e71b7053SJung-uk KimCopyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. 156*e71b7053SJung-uk Kim 157*e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 158*e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 159*e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 160*e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 161*e71b7053SJung-uk Kim 162*e71b7053SJung-uk Kim=cut 163