BIO_f_md.pod 4.7 KB

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