XRootD
Loading...
Searching...
No Matches
XrdCryptosslMsgDigest.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d C r y p t o M s g D i g e s t . c c */
4/* */
5/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Gerri Ganis for CERN */
7/* */
8/* This file is part of the XRootD software suite. */
9/* */
10/* XRootD is free software: you can redistribute it and/or modify it under */
11/* the terms of the GNU Lesser General Public License as published by the */
12/* Free Software Foundation, either version 3 of the License, or (at your */
13/* option) any later version. */
14/* */
15/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
16/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
17/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
18/* License for more details. */
19/* */
20/* You should have received a copy of the GNU Lesser General Public License */
21/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
22/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
23/* */
24/* The copyright holder's institutional names and contributor's names may not */
25/* be used to endorse or promote products derived from this software without */
26/* specific prior written permission of the institution or contributor. */
27/******************************************************************************/
28
29/* ************************************************************************** */
30/* */
31/* OpenSSL implementation of XrdCryptoMsgDigest */
32/* */
33/* ************************************************************************** */
34
38
39//_____________________________________________________________________________
41 : XrdCryptoMsgDigest(), valid(0), mdctx(0)
42{
43 // Constructor.
44 // Init the message digest calculation
45
46 SetType(0);
47 Init(dgst);
48}
49
50//_____________________________________________________________________________
52{
53 // Destructor.
54
55 if (valid) {
56 unsigned char mdval[EVP_MAX_MD_SIZE];
57 EVP_DigestFinal_ex(mdctx, mdval, 0);
58 EVP_MD_CTX_destroy(mdctx);
59 }
60}
61
62//_____________________________________________________________________________
64{
65 // Check if the specified MD is supported
66
67 return (EVP_get_digestbyname(dgst) != 0);
68}
69
70//_____________________________________________________________________________
71int XrdCryptosslMsgDigest::Init(const char *dgst)
72{
73 // Initialize the buffer for the message digest calculation
74 EPNAME("MsgDigest::Init");
75
76 // We use the input digest type; or the old one; or the default, sha-256
77 if (dgst) {
78 SetType(dgst);
79 } else if (!Type()) {
80 SetType("sha256");
81 }
82
83 // Get message digest handle
84 const EVP_MD *md = 0;
85 if (!(md = EVP_get_digestbyname(Type()))) {
86 PRINT("EROOR: cannot get msg digest by name");
87 return -1;
88 }
89
90 // Init digest machine
91 mdctx = EVP_MD_CTX_create();
92 if (!EVP_DigestInit_ex(mdctx, md, NULL)) {
93 PRINT("ERROR: cannot initialize digest");
94 EVP_MD_CTX_destroy(mdctx);
95 return -1;
96 }
97
98 // Successful initialization
99 valid = 1;
100
101 // OK
102 return 0;
103}
104
105//_____________________________________________________________________________
106int XrdCryptosslMsgDigest::Reset(const char *dgst)
107{
108 // Re-Init the message digest calculation
109 if (valid) {
110 unsigned char mdval[EVP_MAX_MD_SIZE];
111 EVP_DigestFinal_ex(mdctx, mdval, 0);
112 SetBuffer(0,0);
113 EVP_MD_CTX_destroy(mdctx);
114 }
115 valid = 0;
116 Init(dgst);
117 if (!valid) return -1;
118
119 return 0;
120}
121
122//_____________________________________________________________________________
123int XrdCryptosslMsgDigest::Update(const char *b, int l)
124{
125 // Update message digest with the MD of l bytes at b.
126 // Create the internal buffer if needed (first call)
127 // Returns -1 if unsuccessful (digest not initialized), 0 otherwise.
128
129 if (Type()) {
130 EVP_DigestUpdate(mdctx, (char *)b, l);
131 return 0;
132 }
133 return -1;
134}
135
136//_____________________________________________________________________________
138{
139 // Finalize message digest calculation.
140 // Finalize the operation
141 // Returns -1 if unsuccessful (digest not initialized), 0 otherwise.
142 EPNAME("MsgDigest::Final");
143
144 // MD outputs in these variables
145 unsigned char mdval[EVP_MAX_MD_SIZE] = {0};
146 unsigned int mdlen = 0;
147
148 if (Type()) {
149 // Finalize what we have
150 if (EVP_DigestFinal_ex(mdctx, mdval, &mdlen) == 1) {
151 // Save result
152 SetBuffer(mdlen,(const char *)mdval);
153 // Notify, if requested
154 DEBUG("result length is "<<mdlen <<
155 " bytes (hex: " << AsHexString() <<")");
156 return 0;
157 } else {
158 PRINT("ERROR: problems finalizing digest");
159 }
160 }
161 return -1;
162}
#define DEBUG(x)
#define EPNAME(x)
#define PRINT(y)
virtual char * Type() const
virtual int SetBuffer(int l, const char *b)
virtual int SetType(const char *t)
static bool IsSupported(const char *dgst)
int Reset(const char *dgst=0)
XrdCryptosslMsgDigest(const char *dgst)
int Update(const char *b, int l)