BIO_f_md.pod 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. =pod
  2. =head1 NAME
  3. BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO filter
  4. =for comment multiple includes
  5. =head1 SYNOPSIS
  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() and
  18. BIO_write() 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. /* For BIO_push() we want to append the sink BIO and keep a note of
  60. * the start of the chain.
  61. */
  62. bio = BIO_push(mdtmp, bio);
  63. mdtmp = BIO_new(BIO_f_md());
  64. BIO_set_md(mdtmp, EVP_md5());
  65. bio = BIO_push(mdtmp, bio);
  66. /* Note: mdtmp can now be discarded */
  67. BIO_write(bio, message, strlen(message));
  68. The next example digests data by reading through a chain instead:
  69. BIO *bio, *mdtmp;
  70. char buf[1024];
  71. int rdlen;
  72. bio = BIO_new_file(file, "rb");
  73. mdtmp = BIO_new(BIO_f_md());
  74. BIO_set_md(mdtmp, EVP_sha1());
  75. bio = BIO_push(mdtmp, bio);
  76. mdtmp = BIO_new(BIO_f_md());
  77. BIO_set_md(mdtmp, EVP_md5());
  78. bio = BIO_push(mdtmp, bio);
  79. do {
  80. rdlen = BIO_read(bio, buf, sizeof(buf));
  81. /* Might want to do something with the data here */
  82. } while (rdlen > 0);
  83. This next example retrieves the message digests from a BIO chain and
  84. outputs them. This could be used with the examples above.
  85. BIO *mdtmp;
  86. unsigned char mdbuf[EVP_MAX_MD_SIZE];
  87. int mdlen;
  88. int i;
  89. mdtmp = bio; /* Assume bio has previously been set up */
  90. do {
  91. EVP_MD *md;
  92. mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
  93. if (!mdtmp) break;
  94. BIO_get_md(mdtmp, &md);
  95. printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
  96. mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
  97. for (i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
  98. printf("\n");
  99. mdtmp = BIO_next(mdtmp);
  100. } while (mdtmp);
  101. BIO_free_all(bio);
  102. =head1 BUGS
  103. The lack of support for BIO_puts() and the non standard behaviour of
  104. BIO_gets() could be regarded as anomalous. It could be argued that BIO_gets()
  105. and BIO_puts() should be passed to the next BIO in the chain and digest
  106. the data passed through and that digests should be retrieved using a
  107. separate BIO_ctrl() call.
  108. =head1 HISTORY
  109. Before OpenSSL 1.0.0., the call to BIO_get_md_ctx() would only work if the
  110. BIO was initialized first.
  111. =head1 COPYRIGHT
  112. Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  113. Licensed under the OpenSSL license (the "License"). You may not use
  114. this file except in compliance with the License. You can obtain a copy
  115. in the file LICENSE in the source distribution or at
  116. L<https://www.openssl.org/source/license.html>.
  117. =cut