BIO_f_md.pod 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <openssl/bio.h>
  6. #include <openssl/evp.h>
  7. BIO_METHOD * BIO_f_md(void);
  8. int BIO_set_md(BIO *b,EVP_MD *md);
  9. int BIO_get_md(BIO *b,EVP_MD **mdp);
  10. int BIO_get_md_ctx(BIO *b,EVP_MD_CTX **mdcp);
  11. =head1 DESCRIPTION
  12. BIO_f_md() returns the message digest BIO method. This is a filter
  13. BIO that digests any data passed through it, it is a BIO wrapper
  14. for the digest routines EVP_DigestInit(), EVP_DigestUpdate()
  15. and EVP_DigestFinal().
  16. Any data written or read through a digest BIO using BIO_read() and
  17. BIO_write() is digested.
  18. BIO_gets(), if its B<size> parameter is large enough finishes the
  19. digest calculation and returns the digest value. BIO_puts() is
  20. not supported.
  21. BIO_reset() reinitialises a digest BIO.
  22. BIO_set_md() sets the message digest of BIO B<b> to B<md>: this
  23. must be called to initialize a digest BIO before any data is
  24. passed through it. It is a BIO_ctrl() macro.
  25. BIO_get_md() places the a pointer to the digest BIOs digest method
  26. in B<mdp>, it is a BIO_ctrl() macro.
  27. BIO_get_md_ctx() returns the digest BIOs context into B<mdcp>.
  28. =head1 NOTES
  29. The context returned by BIO_get_md_ctx() can be used in calls
  30. to EVP_DigestFinal() and also the signature routines EVP_SignFinal()
  31. and EVP_VerifyFinal().
  32. The context returned by BIO_get_md_ctx() is an internal context
  33. structure. Changes made to this context will affect the digest
  34. BIO itself and the context pointer will become invalid when the digest
  35. BIO is freed.
  36. After the digest has been retrieved from a digest BIO it must be
  37. reinitialized by calling BIO_reset(), or BIO_set_md() before any more
  38. data is passed through it.
  39. If an application needs to call BIO_gets() or BIO_puts() through
  40. a chain containing digest BIOs then this can be done by prepending
  41. a buffering BIO.
  42. Before OpenSSL 0.9.9 the call to BIO_get_md_ctx() would only work if the BIO
  43. had been initialized for example by calling BIO_set_md() ). In OpenSSL
  44. 0.9.9 and later the context is always returned and the BIO is state is set
  45. to initialized. This allows applications to initialize the context externally
  46. if the standard calls such as BIO_set_md() are not sufficiently flexible.
  47. =head1 RETURN VALUES
  48. BIO_f_md() returns the digest BIO method.
  49. BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and
  50. 0 for failure.
  51. =head1 EXAMPLES
  52. The following example creates a BIO chain containing an SHA1 and MD5
  53. digest BIO and passes the string "Hello World" through it. Error
  54. checking has been omitted for clarity.
  55. BIO *bio, *mdtmp;
  56. char message[] = "Hello World";
  57. bio = BIO_new(BIO_s_null());
  58. mdtmp = BIO_new(BIO_f_md());
  59. BIO_set_md(mdtmp, EVP_sha1());
  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) break;
  95. BIO_get_md(mdtmp, &md);
  96. printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
  97. mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
  98. for(i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
  99. printf("\n");
  100. mdtmp = BIO_next(mdtmp);
  101. } while(mdtmp);
  102. BIO_free_all(bio);
  103. =head1 BUGS
  104. The lack of support for BIO_puts() and the non standard behaviour of
  105. BIO_gets() could be regarded as anomalous. It could be argued that BIO_gets()
  106. and BIO_puts() should be passed to the next BIO in the chain and digest
  107. the data passed through and that digests should be retrieved using a
  108. separate BIO_ctrl() call.
  109. =head1 SEE ALSO
  110. TBA