1BIO_f_md(3) OpenSSL BIO_f_md(3)
2
3
4
6 BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO
7 filter
8
10 #include <openssl/bio.h>
11 #include <openssl/evp.h>
12
13 BIO_METHOD * BIO_f_md(void);
14 int BIO_set_md(BIO *b,EVP_MD *md);
15 int BIO_get_md(BIO *b,EVP_MD **mdp);
16 int BIO_get_md_ctx(BIO *b,EVP_MD_CTX **mdcp);
17
19 BIO_f_md() returns the message digest BIO method. This is a filter BIO
20 that digests any data passed through it, it is a BIO wrapper for the
21 digest routines EVP_DigestInit(), EVP_DigestUpdate() and
22 EVP_DigestFinal().
23
24 Any data written or read through a digest BIO using BIO_read() and
25 BIO_write() is digested.
26
27 BIO_gets(), if its size parameter is large enough finishes the digest
28 calculation and returns the digest value. BIO_puts() is not supported.
29
30 BIO_reset() reinitialises a digest BIO.
31
32 BIO_set_md() sets the message digest of BIO b to md: this must be
33 called to initialize a digest BIO before any data is passed through it.
34 It is a BIO_ctrl() macro.
35
36 BIO_get_md() places the a pointer to the digest BIOs digest method in
37 mdp, it is a BIO_ctrl() macro.
38
39 BIO_get_md_ctx() returns the digest BIOs context into mdcp.
40
42 The context returned by BIO_get_md_ctx() can be used in calls to
43 EVP_DigestFinal() and also the signature routines EVP_SignFinal() and
44 EVP_VerifyFinal().
45
46 The context returned by BIO_get_md_ctx() is an internal context
47 structure. Changes made to this context will affect the digest BIO
48 itself and the context pointer will become invalid when the digest BIO
49 is freed.
50
51 After the digest has been retrieved from a digest BIO it must be
52 reinitialized by calling BIO_reset(), or BIO_set_md() before any more
53 data is passed through it.
54
55 If an application needs to call BIO_gets() or BIO_puts() through a
56 chain containing digest BIOs then this can be done by prepending a
57 buffering BIO.
58
59 Before OpenSSL 1.0.0 the call to BIO_get_md_ctx() would only work if
60 the BIO had been initialized for example by calling BIO_set_md() ). In
61 OpenSSL 1.0.0 and later the context is always returned and the BIO is
62 state is set to initialized. This allows applications to initialize the
63 context externally if the standard calls such as BIO_set_md() are not
64 sufficiently flexible.
65
67 BIO_f_md() returns the digest BIO method.
68
69 BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and 0
70 for failure.
71
73 The following example creates a BIO chain containing an SHA1 and MD5
74 digest BIO and passes the string "Hello World" through it. Error
75 checking has been omitted for clarity.
76
77 BIO *bio, *mdtmp;
78 char message[] = "Hello World";
79 bio = BIO_new(BIO_s_null());
80 mdtmp = BIO_new(BIO_f_md());
81 BIO_set_md(mdtmp, EVP_sha1());
82 /* For BIO_push() we want to append the sink BIO and keep a note of
83 * the start of the chain.
84 */
85 bio = BIO_push(mdtmp, bio);
86 mdtmp = BIO_new(BIO_f_md());
87 BIO_set_md(mdtmp, EVP_md5());
88 bio = BIO_push(mdtmp, bio);
89 /* Note: mdtmp can now be discarded */
90 BIO_write(bio, message, strlen(message));
91
92 The next example digests data by reading through a chain instead:
93
94 BIO *bio, *mdtmp;
95 char buf[1024];
96 int rdlen;
97 bio = BIO_new_file(file, "rb");
98 mdtmp = BIO_new(BIO_f_md());
99 BIO_set_md(mdtmp, EVP_sha1());
100 bio = BIO_push(mdtmp, bio);
101 mdtmp = BIO_new(BIO_f_md());
102 BIO_set_md(mdtmp, EVP_md5());
103 bio = BIO_push(mdtmp, bio);
104 do {
105 rdlen = BIO_read(bio, buf, sizeof(buf));
106 /* Might want to do something with the data here */
107 } while(rdlen > 0);
108
109 This next example retrieves the message digests from a BIO chain and
110 outputs them. This could be used with the examples above.
111
112 BIO *mdtmp;
113 unsigned char mdbuf[EVP_MAX_MD_SIZE];
114 int mdlen;
115 int i;
116 mdtmp = bio; /* Assume bio has previously been set up */
117 do {
118 EVP_MD *md;
119 mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
120 if(!mdtmp) break;
121 BIO_get_md(mdtmp, &md);
122 printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
123 mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
124 for(i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
125 printf("\n");
126 mdtmp = BIO_next(mdtmp);
127 } while(mdtmp);
128
129 BIO_free_all(bio);
130
132 The lack of support for BIO_puts() and the non standard behaviour of
133 BIO_gets() could be regarded as anomalous. It could be argued that
134 BIO_gets() and BIO_puts() should be passed to the next BIO in the chain
135 and digest the data passed through and that digests should be retrieved
136 using a separate BIO_ctrl() call.
137
139 TBA
140
141
142
1431.0.2o 2020-08-01 BIO_f_md(3)