XRootD
XrdCryptoRSA.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C r y p t o R S A . 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 /* Abstract interface for RSA PKI functionality. */
32 /* Allows to plug-in modules based on different crypto implementation */
33 /* (OpenSSL, Botan, ...) */
34 /* */
35 /* ************************************************************************** */
36 
37 #include <cstring>
38 
40 
41 const char *XrdCryptoRSA::cstatus[3] = { "Invalid", "Public", "Complete" };
42 
43 //_____________________________________________________________________________
45 {
46  // Check key validity
47  ABSTRACTMETHOD("XrdCryptoRSA::Dump");
48 }
49 
50 //_____________________________________________________________________________
52 {
53  // Return underlying key in raw format
54  ABSTRACTMETHOD("XrdCryptoRSA::Opaque");
55  return (XrdCryptoRSAdata)0;
56 }
57 
58 //_____________________________________________________________________________
60 {
61  // Get length of output
62  ABSTRACTMETHOD("XrdCryptoRSA::GetOutlen");
63  return 0;
64 }
65 
66 //_____________________________________________________________________________
68 {
69  // Get length of public key export form
70  ABSTRACTMETHOD("XrdCryptoRSA::GetPublen");
71  return 0;
72 }
73 
74 //_____________________________________________________________________________
76 {
77  // Get length of private key export form
78  ABSTRACTMETHOD("XrdCryptoRSA::GetPrilen");
79  return 0;
80 }
81 
82 //_____________________________________________________________________________
83 int XrdCryptoRSA::ImportPublic(const char *, int)
84 {
85  // Abstract method to import a public key
86  ABSTRACTMETHOD("XrdCryptoRSA::ImportPublic");
87  return -1;
88 }
89 
90 //_____________________________________________________________________________
92 {
93  // Abstract method to export the public key
94  ABSTRACTMETHOD("XrdCryptoRSA::ExportPublic");
95  return -1;
96 }
97 
98 //_____________________________________________________________________________
99 int XrdCryptoRSA::ImportPrivate(const char *, int)
100 {
101  // Abstract method to import a private key
102  ABSTRACTMETHOD("XrdCryptoRSA::ImportPrivate");
103  return -1;
104 }
105 
106 //_____________________________________________________________________________
108 {
109  // Abstract method to export the private key
110  ABSTRACTMETHOD("XrdCryptoRSA::ExportPrivate");
111  return -1;
112 }
113 
114 //_____________________________________________________________________________
116 {
117  // Export the public key into string s
118 
119  int newlen = GetPublen();
120  if (newlen > 0) {
121  char *newbuf = new char[newlen+1];
122  if (newbuf) {
123  memset(newbuf, 0, newlen+1);
124  if (ExportPublic(newbuf,newlen+1) > -1) {
125  s = (const char *)newbuf;
126  delete[] newbuf;
127  return 0;
128  }
129  delete[] newbuf;
130  }
131  }
132  return -1;
133 }
134 
135 //_____________________________________________________________________________
137 {
138  // Export the private key into string s
139 
140  int newlen = GetPrilen();
141  if (newlen > 0) {
142  char *newbuf = new char[newlen+1];
143  if (newbuf) {
144  memset(newbuf, 0, newlen+1);
145  if (ExportPrivate(newbuf,newlen+1) > -1) {
146  s = (const char *)newbuf;
147  delete[] newbuf;
148  return 0;
149  }
150  delete[] newbuf;
151  }
152  }
153  return -1;
154 }
155 
156 //_____________________________________________________________________________
157 int XrdCryptoRSA::EncryptPrivate(const char *, int, char *, int)
158 {
159  // Abstract method to encrypt using the private key
160  ABSTRACTMETHOD("XrdCryptoRSA::EncryptPrivate");
161  return -1;
162 }
163 
164 //_____________________________________________________________________________
165 int XrdCryptoRSA::EncryptPublic(const char *, int, char *, int)
166 {
167  // Abstract method to encrypt using the public key
168  ABSTRACTMETHOD("XrdCryptoRSA::EncryptPublic");
169  return -1;
170 }
171 
172 //_____________________________________________________________________________
173 int XrdCryptoRSA::DecryptPrivate(const char *, int, char *, int)
174 {
175  // Abstract method to decrypt using the private key
176  ABSTRACTMETHOD("XrdCryptoRSA::DecryptPrivate");
177  return -1;
178 }
179 
180 //_____________________________________________________________________________
181 int XrdCryptoRSA::DecryptPublic(const char *, int, char *, int)
182 {
183  // Abstract method to decrypt using the public key
184  ABSTRACTMETHOD("XrdCryptoRSA::DecryptPublic");
185  return -1;
186 }
187 
188 //_____________________________________________________________________________
190 {
191  // Encrypt bucket bck using the private key
192  // Return new bucket size, or -1 in case of error
193  int snew = -1;
194 
195  int sz = GetOutlen(bck.size);
196  char *newbuf = new char[sz];
197  if (newbuf) {
198  memset(newbuf, 0, sz);
199  snew = EncryptPrivate(bck.buffer,bck.size,newbuf,sz);
200  if (snew > -1)
201  bck.Update(newbuf,snew);
202  else
203  delete[] newbuf;
204  }
205  return snew;
206 }
207 
208 //_____________________________________________________________________________
210 {
211  // Encrypt bucket bck using the public key
212  // Return new bucket size, or -1 in case of error
213  int snew = -1;
214 
215  int sz = GetOutlen(bck.size);
216  char *newbuf = new char[sz];
217  if (newbuf) {
218  memset(newbuf, 0, sz);
219  snew = EncryptPublic(bck.buffer,bck.size,newbuf,sz);
220  if (snew > -1)
221  bck.Update(newbuf,snew);
222  else
223  delete[] newbuf;
224  }
225  return snew;
226 }
227 
228 //_____________________________________________________________________________
230 {
231  // Decrypt bucket bck using the private key
232  // Return new bucket size, or -1 in case of error
233  int snew = -1;
234 
235  int sz = GetOutlen(bck.size);
236  char *newbuf = new char[sz];
237  if (newbuf) {
238  memset(newbuf, 0, sz);
239  snew = DecryptPrivate(bck.buffer,bck.size,newbuf,sz);
240  if (snew > -1)
241  bck.Update(newbuf,snew);
242  else
243  delete[] newbuf;
244  }
245  return snew;
246 }
247 
248 //_____________________________________________________________________________
250 {
251  // Decrypt bucket bck using the public key
252  // Return new bucket size, or -1 in case of error
253  int snew = -1;
254 
255  int sz = GetOutlen(bck.size);
256  char *newbuf = new char[sz];
257  if (newbuf) {
258  memset(newbuf, 0, sz);
259  snew = DecryptPublic(bck.buffer,bck.size,newbuf,sz);
260  if (snew > -1)
261  bck.Update(newbuf,snew);
262  else
263  delete[] newbuf;
264  }
265  return snew;
266 }
#define ABSTRACTMETHOD(x)
Definition: XrdCryptoAux.hh:41
void * XrdCryptoRSAdata
Definition: XrdCryptoRSA.hh:43
virtual int ImportPublic(const char *in, int lin)
Definition: XrdCryptoRSA.cc:83
virtual int ExportPrivate(char *out, int lout)
virtual int EncryptPublic(const char *in, int lin, char *out, int lout)
virtual XrdCryptoRSAdata Opaque()
Definition: XrdCryptoRSA.cc:51
virtual int EncryptPrivate(const char *in, int lin, char *out, int lout)
virtual int GetOutlen(int lin)
Definition: XrdCryptoRSA.cc:59
virtual int GetPublen()
Definition: XrdCryptoRSA.cc:67
virtual int ImportPrivate(const char *in, int lin)
Definition: XrdCryptoRSA.cc:99
virtual int DecryptPublic(const char *in, int lin, char *out, int lout)
virtual void Dump()
Definition: XrdCryptoRSA.cc:44
virtual int GetPrilen()
Definition: XrdCryptoRSA.cc:75
virtual int ExportPublic(char *out, int lout)
Definition: XrdCryptoRSA.cc:91
virtual int DecryptPrivate(const char *in, int lin, char *out, int lout)
kXR_int32 size
Definition: XrdSutBucket.hh:47
void Update(char *nb=0, int ns=0, int ty=0)
Definition: XrdSutBucket.cc:95