reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
DigestStream.cxx
Go to the documentation of this file.
1// For testing - also uncomment this in testDigestStream.cxx to test non-SSL MD5 and SHA1
2//#undef USE_SSL
3
5
6// Remove warning about 'this' use in initiator list - pointer is only stored
7#if defined(WIN32) && !defined(__GNUC__)
8#pragma warning( disable : 4355 ) // using this in base member initializer list
9#endif
10
11#ifdef USE_SSL
12 #include <openssl/evp.h>
13#else
14 #include "rutil/vmd5.hxx"
15 #include "rutil/Sha1.hxx"
16#endif
17
18using namespace resip;
19
21 mDigestType(digestType),
22 mContext(nullptr),
23 mBytesTaken(0),
24#ifdef USE_SSL
25 mFinalBin(EVP_MAX_MD_SIZE, Data::Preallocate)
26#else
27 mFinalBin(16, Data::Preallocate)
28#endif
29{
30 int ret = 0;
31 switch (digestType)
32 {
33 case MD5:
34#ifdef USE_SSL
35 mContext = EVP_MD_CTX_new();
36 EVP_MD_CTX_init((EVP_MD_CTX*)mContext);
37 ret = EVP_DigestInit_ex((EVP_MD_CTX*)mContext, EVP_md5(), nullptr);
38#else
39 mContext = new MD5Context;
40 MD5Init((MD5Context*)mContext);
41 ret = 1;
42#endif
43 break;
44 case SHA1:
45#ifdef USE_SSL
46 mContext = EVP_MD_CTX_new();
47 EVP_MD_CTX_init((EVP_MD_CTX*)mContext);
48 ret = EVP_DigestInit_ex((EVP_MD_CTX*)mContext, EVP_sha1(), nullptr);
49#else
50 mContext = new Sha1();
51 ret = 1;
52#endif
53 break;
54#ifdef USE_SSL
55 case SHA256:
56 mContext = EVP_MD_CTX_new();
57 EVP_MD_CTX_init((EVP_MD_CTX*)mContext);
58 ret = EVP_DigestInit_ex((EVP_MD_CTX*)mContext, EVP_sha256(), nullptr);
59 break;
60 case SHA512:
61 mContext = EVP_MD_CTX_new();
62 EVP_MD_CTX_init((EVP_MD_CTX*)mContext);
63 ret = EVP_DigestInit_ex((EVP_MD_CTX*)mContext, EVP_sha512(), nullptr);
64 break;
65 case SHA512_256:
66 mContext = EVP_MD_CTX_new();
67 EVP_MD_CTX_init((EVP_MD_CTX*)mContext);
68 ret = EVP_DigestInit_ex((EVP_MD_CTX*)mContext, EVP_sha512_256(), nullptr);
69 break;
70#endif
71 default:
72 resip_assert(false);
73 break;
74 }
75 if (!ret)
76 {
77 throw std::runtime_error("Failed to initialize digest context");
78 }
79 setp(mBuf, mBuf + sizeof(mBuf));
80}
81
83{
84#ifdef USE_SSL
85 EVP_MD_CTX_free((EVP_MD_CTX*)mContext);
86#else
87 if (mDigestType == MD5)
88 {
89 delete (MD5Context*)mContext;
90 }
91 else if (mDigestType == SHA1)
92 {
93 delete (resip::Sha1*)mContext;
94 }
95#endif
96}
97
98int
100{
101 if (!mContext) return -1;
102
103 size_t len = pptr() - pbase();
104 if (len > 0)
105 {
106#ifdef USE_SSL
107 EVP_DigestUpdate((EVP_MD_CTX*)mContext, reinterpret_cast <unsigned const char*>(pbase()), len);
108#else
109 if (mDigestType == MD5)
110 {
111 MD5Update((MD5Context*)mContext, reinterpret_cast <unsigned const char*>(pbase()), (unsigned int)len);
112 }
113 else if (mDigestType == SHA1)
114 {
115 ((Sha1*)mContext)->update(std::string(pbase(), len));
116 }
117#endif
118 // reset the put buffer
119 setp(mBuf, mBuf + sizeof(mBuf));
120 mBytesTaken += len;
121 }
122 return 0;
123}
124
125int
127{
128 sync();
129 if (c != -1)
130 {
131 mBuf[0] = c;
132 pbump(1);
133 return c;
134 }
135 return 0;
136}
137
138Data
140{
141 return getBin().hex();
142}
143
144const Data&
146{
147 // If not already calculated, then calcuate it
148 if (mFinalBin.size() == 0)
149 {
150#ifdef USE_SSL
151 unsigned int len = EVP_MAX_MD_SIZE;
152 EVP_DigestFinal_ex((EVP_MD_CTX*)mContext, (unsigned char*)mFinalBin.getBuf(len), &len);
153 mFinalBin.truncate(len);
154#else
155 if (mDigestType == MD5)
156 {
157 MD5Final((unsigned char*)mFinalBin.getBuf(16), (MD5Context*)mContext);
158 }
159 else if (mDigestType == SHA1)
160 {
161 mFinalBin = ((Sha1*)mContext)->finalBin();
162 }
163#endif
164 }
165 return mFinalBin;
166}
167
169 : DigestBuffer(digestType), std::ostream(this)
170{
171}
172
176
177Data
179{
180 flush();
181 return DigestBuffer::getHex();
182}
183
184const Data&
186{
187 flush();
188 return DigestBuffer::getBin();
189}
190
191size_t
196
197static Data md5Name("MD5");
198static Data sha1Name("SHA1");
199#ifdef USE_SSL
200static Data sha256Name("SHA-256");
201static Data sha512Name("SHA-512");
202static Data sha512_256Name("SHA-512-256");
203#endif
204const Data&
206{
207 // Note: This name formatting aligns with what is needed in the SIP
208 // Digest authentication header 'algorithm' parameter (where it makes sense).
209 switch (digestType)
210 {
211 case MD5:
212 return md5Name;
213 case SHA1:
214 return sha1Name;
215#ifdef USE_SSL
216 case SHA256:
217 return sha256Name;
218 case SHA512:
219 return sha512Name;
220 case SHA512_256:
221 return sha512_256Name;
222#endif
223 default:
224 resip_assert(false);
225 return Data::Empty;
226 }
227}
228
229
230/* ====================================================================
231 * The Vovida Software License, Version 1.0
232 *
233 * Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
234 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
235 *
236 * Redistribution and use in source and binary forms, with or without
237 * modification, are permitted provided that the following conditions
238 * are met:
239 *
240 * 1. Redistributions of source code must retain the above copyright
241 * notice, this list of conditions and the following disclaimer.
242 *
243 * 2. Redistributions in binary form must reproduce the above copyright
244 * notice, this list of conditions and the following disclaimer in
245 * the documentation and/or other materials provided with the
246 * distribution.
247 *
248 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
249 * and "Vovida Open Communication Application Library (VOCAL)" must
250 * not be used to endorse or promote products derived from this
251 * software without prior written permission. For written
252 * permission, please contact vocal@vovida.org.
253 *
254 * 4. Products derived from this software may not be called "VOCAL", nor
255 * may "VOCAL" appear in their name, without prior written
256 * permission of Vovida Networks, Inc.
257 *
258 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
259 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
260 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
261 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
262 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
263 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
264 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
265 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
266 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
267 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
269 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
270 * DAMAGE.
271 *
272 * ====================================================================
273 *
274 * This software consists of voluntary contributions made by Vovida
275 * Networks, Inc. and many individuals on behalf of Vovida Networks,
276 * Inc. For more information on Vovida Networks, Inc., please see
277 * <http://www.vovida.org/>.
278 *
279 */
static Data sha256Name("SHA-256")
static Data sha512Name("SHA-512")
static Data md5Name("MD5")
static Data sha512_256Name("SHA-512-256")
static Data sha1Name("SHA1")
#define resip_assert(x)
This file needs to work for both C and C++ code.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
static const Data Empty
Constant that represents a zero-length data.
Definition Data.hxx:882
char * getBuf(size_type length)
Set size to be exactly {length}, extending buffer if needed.
Definition Data.cxx:723
size_type size() const noexcept
Returns the number of bytes in this Data.
Definition Data.hxx:599
size_type truncate(size_type len)
Shortens the size of this Data.
Definition Data.cxx:928
Data hex() const
Returns a hexadecimal representation of the contents of this Data.
Definition Data.cxx:1577
Implementation of std::streambuf used to back the DigestStream.
virtual int overflow(int c=-1)
const Data & getBin()
DigestBuffer(DigestType digestType=MD5)
Data getHex()
Calls flush() on itself and returns the Digest data in hex format.
static const Data & getDigestName(DigestType digestType)
const Data & getBin()
DigestStream(DigestType digestType=MD5)
dcm! – add UnusedChecking(_enum) below;
void MD5Final(unsigned char digest[16], struct MD5Context *context)
Definition vmd5.cxx:116
void MD5Init(struct MD5Context *context)
Definition vmd5.cxx:58
void MD5Update(struct MD5Context *context, md5byte const *buf, unsigned len)
Definition vmd5.cxx:74