
#use wml::openssl-macros area=docs page=BIO_f_md

<title>Documents, BIO_f_md(3)</title>

<h1>BIO_f_md(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#EXAMPLES">EXAMPLES</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO
filter

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
 \#include &lt;openssl/evp.h&gt;
</PRE>
<PRE> BIO_METHOD *   BIO_f_md(void);
 int BIO_set_md(BIO *b,EVP_MD *md);
 int BIO_get_md(BIO *b,EVP_MD **mdp);
 int BIO_get_md_ctx(BIO *b,EVP_MD_CTX **mdcp);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_f_md()</CODE> returns the message digest BIO method. This is a
filter BIO that digests any data passed through it, it is a BIO wrapper for
the digest routines <CODE>EVP_DigestInit(),</CODE>
<CODE>EVP_DigestUpdate()</CODE> and <CODE>EVP_DigestFinal().</CODE>

</P>
<P>
Any data written or read through a digest BIO using <CODE>BIO_read()</CODE>
and <CODE>BIO_write()</CODE> is digested.

</P>
<P>
<CODE>BIO_gets(),</CODE> if its <STRONG>size</STRONG> parameter is large enough finishes the digest calculation and returns the
digest value. <CODE>BIO_puts()</CODE> is not supported.

</P>
<P>
<CODE>BIO_reset()</CODE> reinitialises a digest BIO.

</P>
<P>
<CODE>BIO_set_md()</CODE> sets the message digest of BIO <STRONG>b</STRONG> to <STRONG>md</STRONG>: this must be called to initialize a digest BIO before any data is passed
through it. It is a <CODE>BIO_ctrl()</CODE> macro.

</P>
<P>
<CODE>BIO_get_md()</CODE> places the a pointer to the digest BIOs digest
method in <STRONG>mdp</STRONG>, it is a <CODE>BIO_ctrl()</CODE> macro.

</P>
<P>
<CODE>BIO_get_md_ctx()</CODE> returns the digest BIOs context into <STRONG>mdcp</STRONG>.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
The context returned by <CODE>BIO_get_md_ctx()</CODE> can be used in calls
to <CODE>EVP_DigestFinal()</CODE> and also the signature routines
<CODE>EVP_SignFinal()</CODE> and <CODE>EVP_VerifyFinal().</CODE>

</P>
<P>
The context returned by <CODE>BIO_get_md_ctx()</CODE> is an internal
context structure. Changes made to this context will affect the digest BIO
itself and the context pointer will become invalid when the digest BIO is
freed.

</P>
<P>
After the digest has been retrieved from a digest BIO it must be
reinitialized by calling <CODE>BIO_reset(),</CODE> or
<CODE>BIO_set_md()</CODE> before any more data is passed through it.

</P>
<P>
If an application needs to call <CODE>BIO_gets()</CODE> or
<CODE>BIO_puts()</CODE> through a chain containing digest BIOs then this
can be done by prepending a buffering BIO.

</P>
<P>
Before OpenSSL 1.0.0 the call to <CODE>BIO_get_md_ctx()</CODE> would only
work if the BIO had been initialized for example by calling
<CODE>BIO_set_md()</CODE> ). In OpenSSL 1.0.0 and later the context is
always returned and the BIO is state is set to initialized. This allows
applications to initialize the context externally if the standard calls
such as <CODE>BIO_set_md()</CODE> are not sufficiently flexible.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>BIO_f_md()</CODE> returns the digest BIO method.

</P>
<P>
<CODE>BIO_set_md(),</CODE> <CODE>BIO_get_md()</CODE> and
<CODE>BIO_md_ctx()</CODE> return 1 for success and 0 for failure.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLES">EXAMPLES</A></H1>
<P>
The following example creates a BIO chain containing an SHA1 and MD5 digest
BIO and passes the string ``Hello World'' through it. Error checking has
been omitted for clarity.

</P>
<PRE> BIO *bio, *mdtmp;
 char message[] = &quot;Hello World&quot;;
 bio = BIO_new(BIO_s_null());
 mdtmp = BIO_new(BIO_f_md());
 BIO_set_md(mdtmp, EVP_sha1());
 /* For BIO_push() we want to append the sink BIO and keep a note of
  * the start of the chain.
  */
 bio = BIO_push(mdtmp, bio);
 mdtmp = BIO_new(BIO_f_md());
 BIO_set_md(mdtmp, EVP_md5());
 bio = BIO_push(mdtmp, bio);
 /* Note: mdtmp can now be discarded */
 BIO_write(bio, message, strlen(message));
</PRE>
<P>
The next example digests data by reading through a chain instead:

</P>
<PRE> BIO *bio, *mdtmp;
 char buf[1024];
 int rdlen;
 bio = BIO_new_file(file, &quot;rb&quot;);
 mdtmp = BIO_new(BIO_f_md());
 BIO_set_md(mdtmp, EVP_sha1());
 bio = BIO_push(mdtmp, bio);
 mdtmp = BIO_new(BIO_f_md());
 BIO_set_md(mdtmp, EVP_md5());
 bio = BIO_push(mdtmp, bio);
 do {
        rdlen = BIO_read(bio, buf, sizeof(buf));
        /* Might want to do something with the data here */
 } while(rdlen &gt; 0);
</PRE>
<P>
This next example retrieves the message digests from a BIO chain and
outputs them. This could be used with the examples above.

</P>
<PRE> BIO *mdtmp;
 unsigned char mdbuf[EVP_MAX_MD_SIZE];
 int mdlen;
 int i;
 mdtmp = bio;   /* Assume bio has previously been set up */
 do {
        EVP_MD *md;
        mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
        if(!mdtmp) break;
        BIO_get_md(mdtmp, &amp;md);
        printf(&quot;%s digest&quot;, OBJ_nid2sn(EVP_MD_type(md)));
        mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
        for(i = 0; i &lt; mdlen; i++) printf(&quot;:%02X&quot;, mdbuf[i]);
        printf(&quot;\n&quot;);
        mdtmp = BIO_next(mdtmp);
 } while(mdtmp);
</PRE>
<PRE> BIO_free_all(bio);
</PRE>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
The lack of support for <CODE>BIO_puts()</CODE> and the non standard
behaviour of <CODE>BIO_gets()</CODE> could be regarded as anomalous. It
could be argued that <CODE>BIO_gets()</CODE> and <CODE>BIO_puts()</CODE>
should be passed to the next BIO in the chain and digest the data passed
through and that digests should be retrieved using a separate
<CODE>BIO_ctrl()</CODE> call.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
TBA
</P>
:}


