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