Blame doc/man3/BIO_f_md.pod

Packit Service 084de1
=pod
Packit Service 084de1
Packit Service 084de1
=head1 NAME
Packit Service 084de1
Packit Service 084de1
BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO filter
Packit Service 084de1
Packit Service 084de1
=head1 SYNOPSIS
Packit Service 084de1
Packit Service 084de1
=for comment multiple includes
Packit Service 084de1
Packit Service 084de1
 #include <openssl/bio.h>
Packit Service 084de1
 #include <openssl/evp.h>
Packit Service 084de1
Packit Service 084de1
 const BIO_METHOD *BIO_f_md(void);
Packit Service 084de1
 int BIO_set_md(BIO *b, EVP_MD *md);
Packit Service 084de1
 int BIO_get_md(BIO *b, EVP_MD **mdp);
Packit Service 084de1
 int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **mdcp);
Packit Service 084de1
Packit Service 084de1
=head1 DESCRIPTION
Packit Service 084de1
Packit Service 084de1
BIO_f_md() returns the message digest BIO method. This is a filter
Packit Service 084de1
BIO that digests any data passed through it, it is a BIO wrapper
Packit Service 084de1
for the digest routines EVP_DigestInit(), EVP_DigestUpdate()
Packit Service 084de1
and EVP_DigestFinal().
Packit Service 084de1
Packit Service 084de1
Any data written or read through a digest BIO using BIO_read_ex() and
Packit Service 084de1
BIO_write_ex() is digested.
Packit Service 084de1
Packit Service 084de1
BIO_gets(), if its B<size> parameter is large enough finishes the
Packit Service 084de1
digest calculation and returns the digest value. BIO_puts() is
Packit Service 084de1
not supported.
Packit Service 084de1
Packit Service 084de1
BIO_reset() reinitialises a digest BIO.
Packit Service 084de1
Packit Service 084de1
BIO_set_md() sets the message digest of BIO B to B<md>: this
Packit Service 084de1
must be called to initialize a digest BIO before any data is
Packit Service 084de1
passed through it. It is a BIO_ctrl() macro.
Packit Service 084de1
Packit Service 084de1
BIO_get_md() places the a pointer to the digest BIOs digest method
Packit Service 084de1
in B<mdp>, it is a BIO_ctrl() macro.
Packit Service 084de1
Packit Service 084de1
BIO_get_md_ctx() returns the digest BIOs context into B<mdcp>.
Packit Service 084de1
Packit Service 084de1
=head1 NOTES
Packit Service 084de1
Packit Service 084de1
The context returned by BIO_get_md_ctx() can be used in calls
Packit Service 084de1
to EVP_DigestFinal() and also the signature routines EVP_SignFinal()
Packit Service 084de1
and EVP_VerifyFinal().
Packit Service 084de1
Packit Service 084de1
The context returned by BIO_get_md_ctx() is an internal context
Packit Service 084de1
structure. Changes made to this context will affect the digest
Packit Service 084de1
BIO itself and the context pointer will become invalid when the digest
Packit Service 084de1
BIO is freed.
Packit Service 084de1
Packit Service 084de1
After the digest has been retrieved from a digest BIO it must be
Packit Service 084de1
reinitialized by calling BIO_reset(), or BIO_set_md() before any more
Packit Service 084de1
data is passed through it.
Packit Service 084de1
Packit Service 084de1
If an application needs to call BIO_gets() or BIO_puts() through
Packit Service 084de1
a chain containing digest BIOs then this can be done by prepending
Packit Service 084de1
a buffering BIO.
Packit Service 084de1
Packit Service 084de1
Calling BIO_get_md_ctx() will return the context and initialize the BIO
Packit Service 084de1
state. This allows applications to initialize the context externally
Packit Service 084de1
if the standard calls such as BIO_set_md() are not sufficiently flexible.
Packit Service 084de1
Packit Service 084de1
=head1 RETURN VALUES
Packit Service 084de1
Packit Service 084de1
BIO_f_md() returns the digest BIO method.
Packit Service 084de1
Packit Service 084de1
BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and
Packit Service 084de1
0 for failure.
Packit Service 084de1
Packit Service 084de1
=head1 EXAMPLES
Packit Service 084de1
Packit Service 084de1
The following example creates a BIO chain containing an SHA1 and MD5
Packit Service 084de1
digest BIO and passes the string "Hello World" through it. Error
Packit Service 084de1
checking has been omitted for clarity.
Packit Service 084de1
Packit Service 084de1
 BIO *bio, *mdtmp;
Packit Service 084de1
 char message[] = "Hello World";
Packit Service 084de1
Packit Service 084de1
 bio = BIO_new(BIO_s_null());
Packit Service 084de1
 mdtmp = BIO_new(BIO_f_md());
Packit Service 084de1
 BIO_set_md(mdtmp, EVP_sha1());
Packit Service 084de1
 /*
Packit Service 084de1
  * For BIO_push() we want to append the sink BIO and keep a note of
Packit Service 084de1
  * the start of the chain.
Packit Service 084de1
  */
Packit Service 084de1
 bio = BIO_push(mdtmp, bio);
Packit Service 084de1
 mdtmp = BIO_new(BIO_f_md());
Packit Service 084de1
 BIO_set_md(mdtmp, EVP_md5());
Packit Service 084de1
 bio = BIO_push(mdtmp, bio);
Packit Service 084de1
 /* Note: mdtmp can now be discarded */
Packit Service 084de1
 BIO_write(bio, message, strlen(message));
Packit Service 084de1
Packit Service 084de1
The next example digests data by reading through a chain instead:
Packit Service 084de1
Packit Service 084de1
 BIO *bio, *mdtmp;
Packit Service 084de1
 char buf[1024];
Packit Service 084de1
 int rdlen;
Packit Service 084de1
Packit Service 084de1
 bio = BIO_new_file(file, "rb");
Packit Service 084de1
 mdtmp = BIO_new(BIO_f_md());
Packit Service 084de1
 BIO_set_md(mdtmp, EVP_sha1());
Packit Service 084de1
 bio = BIO_push(mdtmp, bio);
Packit Service 084de1
 mdtmp = BIO_new(BIO_f_md());
Packit Service 084de1
 BIO_set_md(mdtmp, EVP_md5());
Packit Service 084de1
 bio = BIO_push(mdtmp, bio);
Packit Service 084de1
 do {
Packit Service 084de1
     rdlen = BIO_read(bio, buf, sizeof(buf));
Packit Service 084de1
     /* Might want to do something with the data here */
Packit Service 084de1
 } while (rdlen > 0);
Packit Service 084de1
Packit Service 084de1
This next example retrieves the message digests from a BIO chain and
Packit Service 084de1
outputs them. This could be used with the examples above.
Packit Service 084de1
Packit Service 084de1
 BIO *mdtmp;
Packit Service 084de1
 unsigned char mdbuf[EVP_MAX_MD_SIZE];
Packit Service 084de1
 int mdlen;
Packit Service 084de1
 int i;
Packit Service 084de1
Packit Service 084de1
 mdtmp = bio;   /* Assume bio has previously been set up */
Packit Service 084de1
 do {
Packit Service 084de1
     EVP_MD *md;
Packit Service 084de1
Packit Service 084de1
     mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
Packit Service 084de1
     if (!mdtmp)
Packit Service 084de1
         break;
Packit Service 084de1
     BIO_get_md(mdtmp, &md);
Packit Service 084de1
     printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
Packit Service 084de1
     mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
Packit Service 084de1
     for (i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
Packit Service 084de1
     printf("\n");
Packit Service 084de1
     mdtmp = BIO_next(mdtmp);
Packit Service 084de1
 } while (mdtmp);
Packit Service 084de1
Packit Service 084de1
 BIO_free_all(bio);
Packit Service 084de1
Packit Service 084de1
=head1 BUGS
Packit Service 084de1
Packit Service 084de1
The lack of support for BIO_puts() and the non standard behaviour of
Packit Service 084de1
BIO_gets() could be regarded as anomalous. It could be argued that BIO_gets()
Packit Service 084de1
and BIO_puts() should be passed to the next BIO in the chain and digest
Packit Service 084de1
the data passed through and that digests should be retrieved using a
Packit Service 084de1
separate BIO_ctrl() call.
Packit Service 084de1
Packit Service 084de1
=head1 HISTORY
Packit Service 084de1
Packit Service 084de1
Before OpenSSL 1.0.0., the call to BIO_get_md_ctx() would only work if the
Packit Service 084de1
BIO was initialized first.
Packit Service 084de1
Packit Service 084de1
=head1 COPYRIGHT
Packit Service 084de1
Packit Service 084de1
Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
Packit Service 084de1
Packit Service 084de1
Licensed under the OpenSSL license (the "License").  You may not use
Packit Service 084de1
this file except in compliance with the License.  You can obtain a copy
Packit Service 084de1
in the file LICENSE in the source distribution or at
Packit Service 084de1
L<https://www.openssl.org/source/license.html>.
Packit Service 084de1
Packit Service 084de1
=cut