XRootD
XrdCryptoBasic.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C r y p t o B a s i c. h h */
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 /* Generic buffer for crypto functions needed in XrdCrypto */
32 /* Different crypto implementation (OpenSSL, Botan, ...) available as plug-in */
33 /* */
34 /* ************************************************************************** */
35 
36 #include <cstdio>
37 #include <cstring>
38 
39 #include "XrdSut/XrdSutAux.hh"
42 
43 // ---------------------------------------------------------------------------//
44 //
45 // Basic crypto buffer implementation
46 //
47 // ---------------------------------------------------------------------------//
48 
49 //_____________________________________________________________________________
50 XrdCryptoBasic::XrdCryptoBasic(const char *t, int l, const char *b)
51 {
52  // Basic constructor.
53  // This class has responsibility over both its buffers.
54 
55  type = 0;
56  membuf = 0;
57  lenbuf = 0;
58  //
59  // Fill in the type, if any
60  if (t) {
61  int tl = strlen(t);
62  if (tl) {
63  type = new char[tl+1];
64  if (type) {
65  memcpy(type,t,tl);
66  type[tl] = 0;
67  }
68  }
69  }
70  //
71  // Fill the buffer and length
72  if (l > 0) {
73  membuf = new char[l];
74  if (membuf) {
75  lenbuf = l;
76  if (b)
77  memcpy(membuf,b,l);
78  else
79  memset(membuf,0,l);
80  }
81  }
82 }
83 
84 //_____________________________________________________________________________
86 {
87  // Return pointer to a bucket created using the internal buffer
88  // Type is not copied.
89  // The bucket is responsible for the allocated memory
90 
91  XrdSutBucket *buck = (XrdSutBucket *)0;
92 
93  if (Length()) {
94  char *nbuf = new char[Length()];
95  if (nbuf) {
96  memcpy(nbuf,Buffer(),Length());
97  buck = new XrdSutBucket(nbuf,Length());
98  }
99  }
100 
101  return buck;
102 }
103 
104 //_____________________________________________________________________________
106 {
107  // Return the internal buffer as a hexadecimal string
108  static char out[XrdSutMAXBUF];
109 
110  int lmax = XrdSutMAXBUF / 2 - 1 ;
111  int lconv = (Length() > lmax) ? lmax : Length();
112 
113  if (!XrdSutToHex(Buffer(),lconv,&out[0]))
114  return &out[0];
115  return 0;
116 }
117 
118 //_____________________________________________________________________________
119 int XrdCryptoBasic::FromHex(const char *hex)
120 {
121  // Set a binary buffer from a null-terminated hexadecimal string
122  // Returns 0 in case of success, -1 otherwise.
123 
124  if (!hex)
125  return -1;
126 
127  // Determine length
128  int lhex = strlen(hex);
129  int lout = lhex / 2;
130  if (lout * 2 < lhex) lout++;
131 
132  // Allocate buffer
133  char *bout = new char[lout];
134  if (bout) {
135  if (XrdSutFromHex(hex, bout, lout) != 0) {
136  delete[] bout;
137  return -1;
138  }
139  UseBuffer(lout,bout);
140  return 0;
141  }
142 
143  // Failure
144  return -1;
145 }
146 
147 //_____________________________________________________________________________
149 {
150  // Truncate or enlarge the data buffer length to l; new bytes are filled
151  // with 0 in case of enlargement
152  // Returns 0 in case of success, -1 in case of error (in buffer allocation).
153 
154  if (l > 0) {
155  //
156  // Create new buffer
157  char *newbuf = new char[l];
158  if (newbuf) {
159  //
160  // Save existing info
161  memcpy(newbuf,membuf,l);
162  //
163  // Reset additional bytes, if any
164  if (l > lenbuf)
165  memset(newbuf+lenbuf,0,(l-lenbuf));
166  //
167  // Release old buffer
168  delete[] membuf;
169  //
170  // Set the new length and buffer
171  lenbuf = l;
172  membuf = newbuf;
173  } else
174  return -1;
175  } else {
176  //
177  // Release existing buffer, if any
178  if (membuf)
179  delete[] membuf;
180  lenbuf = 0;
181  membuf = 0;
182  }
183 
184  return 0;
185 }
186 
187 //_____________________________________________________________________________
188 int XrdCryptoBasic::SetBuffer(int l, const char *b)
189 {
190  // Substitute buffer with the l bytes at b.
191  // Returns 0 in case of success, -1 in case of error (in buffer allocation).
192 
193  if (l > 0) {
194  //
195  // Allocate new buffer
196  char *tmpbuf = new char[l];
197  if (tmpbuf) {
198  if (b)
199  memcpy(tmpbuf,b,l);
200  else
201  memset(tmpbuf,0,l);
202  if (membuf)
203  delete[] membuf;
204  lenbuf = l;
205  membuf = tmpbuf;
206  } else
207  return -1;
208  } else {
209  //
210  // Release existing buffer, if any
211  if (membuf)
212  delete[] membuf;
213  lenbuf = 0;
214  membuf = 0;
215  }
216 
217  return 0;
218 }
219 
220 //_____________________________________________________________________________
221 int XrdCryptoBasic::SetType(const char *t)
222 {
223  // Substitute type with the string at t.
224  // Returns 0 in case of success, -1 in case of error (in buffer allocation).
225 
226  if (t) {
227  //
228  // Allocate new buffer
229  int tl = strlen(t);
230  char *tmpbuf = new char[tl+1];
231  if (tmpbuf) {
232  strcpy(tmpbuf,t);
233  delete[] type;
234  type = tmpbuf;
235  } else
236  return -1;
237  } else {
238  //
239  // Release existing buffer, if any
240  if (type)
241  delete[] type;
242  type = 0;
243  }
244 
245  return 0;
246 }
int XrdSutToHex(const char *in, int lin, char *out)
Definition: XrdSutAux.cc:241
int XrdSutFromHex(const char *in, char *out, int &lout)
Definition: XrdSutAux.cc:274
#define XrdSutMAXBUF
Definition: XrdSutAux.hh:48
virtual int FromHex(const char *hex)
virtual XrdSutBucket * AsBucket()
virtual int SetBuffer(int l, const char *b)
virtual int SetLength(int l)
virtual int Length() const
char * AsHexString()
virtual char * Buffer() const
XrdCryptoBasic(const char *t=0, int l=0, const char *b=0)
virtual int SetType(const char *t)
virtual void UseBuffer(int l, const char *b)
@ hex
Definition: XrdSysTrace.hh:42