-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCryptoX509.cc
301 lines (263 loc) · 10.6 KB
/
XrdCryptoX509.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/******************************************************************************/
/* */
/* X r d C r y p t o X 5 0 9 . c c */
/* */
/* (c) 2005 G. Ganis , CERN */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/******************************************************************************/
/* ************************************************************************** */
/* */
/* Abstract interface for X509 certificates. */
/* Allows to plug-in modules based on different crypto implementation */
/* (OpenSSL, Botan, ...) */
/* */
/* ************************************************************************** */
#include <time.h>
#include "XrdCrypto/XrdCryptoX509.hh"
#include "XrdCrypto/XrdCryptoTrace.hh"
const char *XrdCryptoX509::ctype[4] = { "Unknown", "CA", "EEC", "Proxy" };
#define kAllowedSkew 600
//_____________________________________________________________________________
void XrdCryptoX509::Dump()
{
// Dump content
EPNAME("X509::Dump");
// Time strings
struct tm tst;
char stbeg[256] = {0};
time_t tbeg = NotBefore();
localtime_r(&tbeg,&tst);
asctime_r(&tst,stbeg);
stbeg[strlen(stbeg)-1] = 0;
char stend[256] = {0};
time_t tend = NotAfter();
localtime_r(&tend,&tst);
asctime_r(&tst,stend);
stend[strlen(stend)-1] = 0;
PRINT("+++++++++++++++ X509 dump +++++++++++++++++++++++");
PRINT("+");
PRINT("+ File: "<<ParentFile());
PRINT("+");
PRINT("+ Type: "<<Type());
PRINT("+ Serial Number: "<<SerialNumber());
PRINT("+ Subject: "<<Subject());
PRINT("+ Subject hash: "<<SubjectHash(0));
PRINT("+ Issuer: "<<Issuer());
PRINT("+ Issuer hash: "<<IssuerHash(0));
PRINT("+");
if (IsExpired()) {
PRINT("+ Validity: (expired!)");
} else {
PRINT("+ Validity:");
}
PRINT("+ NotBefore: "<<tbeg<<" UTC - "<<stbeg);
PRINT("+ NotAfter: "<<tend<<" UTC - "<<stend);
PRINT("+");
if (PKI()) {
PRINT("+ PKI: "<<PKI()->Status());
} else {
PRINT("+ PKI: missing");
}
PRINT("+");
PRINT("+++++++++++++++++++++++++++++++++++++++++++++++++");
}
//_____________________________________________________________________________
int XrdCryptoX509::BitStrength()
{
// Return number of bits in key
ABSTRACTMETHOD("XrdCryptoX509::BitStrength");
return -1;
}
//_____________________________________________________________________________
bool XrdCryptoX509::IsValid(int when)
{
// Check validity at local time 'when'. Use when =0 (default) to check
// at present time.
int now = (when > 0) ? when : (int)time(0);
// Correct for time zone (certificate times are UTC plus, eventually, DST
now -= XrdCryptoTZCorr();
return (now >= (NotBefore()-kAllowedSkew) && now <= NotAfter());
}
//_____________________________________________________________________________
bool XrdCryptoX509::IsExpired(int when)
{
// Check expiration at UTC time 'when'. Use when =0 (default) to check
// at present time.
int now = (when > 0) ? when : (int)time(0);
// Correct for time zone (certificate times are UTC plus, eventually, DST
now -= XrdCryptoTZCorr();
return (now > NotAfter());
}
//_____________________________________________________________________________
time_t XrdCryptoX509::NotBefore()
{
// Begin-validity time in secs since Epoch
ABSTRACTMETHOD("XrdCryptoX509::NotBefore");
return -1;
}
//_____________________________________________________________________________
time_t XrdCryptoX509::NotAfter()
{
// End-validity time in secs since Epoch
ABSTRACTMETHOD("XrdCryptoX509::NotAfter");
return -1;
}
//_____________________________________________________________________________
const char *XrdCryptoX509::Subject()
{
// Return subject name
ABSTRACTMETHOD("XrdCryptoX509::Subject");
return (const char *)0;
}
//_____________________________________________________________________________
const char *XrdCryptoX509::ParentFile()
{
// Return parent file name
ABSTRACTMETHOD("XrdCryptoX509::ParentFile");
return (const char *)0;
}
//_____________________________________________________________________________
const char *XrdCryptoX509::Issuer()
{
// Return issuer name
ABSTRACTMETHOD("XrdCryptoX509::Issuer");
return (const char *)0;
}
//_____________________________________________________________________________
const char *XrdCryptoX509::SubjectHash(int)
{
// Return subject name
ABSTRACTMETHOD("XrdCryptoX509::SubjectHash");
return (const char *)0;
}
//_____________________________________________________________________________
const char *XrdCryptoX509::IssuerHash(int)
{
// Return issuer name
ABSTRACTMETHOD("XrdCryptoX509::IssuerHash");
return (const char *)0;
}
//_____________________________________________________________________________
XrdCryptoX509data XrdCryptoX509::Opaque()
{
// Return underlying certificate in raw format
ABSTRACTMETHOD("XrdCryptoX509::Opaque");
return (XrdCryptoX509data)0;
}
//_____________________________________________________________________________
XrdCryptoRSA *XrdCryptoX509::PKI()
{
// Return PKI key of the certificate
ABSTRACTMETHOD("XrdCryptoX509::PKI");
return (XrdCryptoRSA *)0;
}
//_____________________________________________________________________________
void XrdCryptoX509::SetPKI(XrdCryptoX509data)
{
// Set PKI
ABSTRACTMETHOD("XrdCryptoX509::SetPKI");
}
//_____________________________________________________________________________
kXR_int64 XrdCryptoX509::SerialNumber()
{
// Return issuer name
ABSTRACTMETHOD("XrdCryptoX509::SerialNumber");
return -1;
}
//_____________________________________________________________________________
XrdOucString XrdCryptoX509::SerialNumberString()
{
// Return issuer name
ABSTRACTMETHOD("XrdCryptoX509::SerialNumberString");
return XrdOucString("");
}
//_____________________________________________________________________________
XrdCryptoX509data XrdCryptoX509::GetExtension(const char *)
{
// Return issuer name
ABSTRACTMETHOD("XrdCryptoX509::GetExtension");
return (XrdCryptoX509data)0;
}
//_____________________________________________________________________________
XrdSutBucket *XrdCryptoX509::Export()
{
// EXport in form of bucket
ABSTRACTMETHOD("XrdCryptoX509::Export");
return (XrdSutBucket *)0;
}
//_____________________________________________________________________________
bool XrdCryptoX509::Verify(XrdCryptoX509 *)
{
// Verify certificate signature with pub key of ref cert
ABSTRACTMETHOD("XrdCryptoX509::Verify");
return 0;
}
//_____________________________________________________________________________
int XrdCryptoX509::DumpExtensions(bool)
{
// Dump extensions, if any
ABSTRACTMETHOD("XrdCryptoX509::DumpExtensions");
return -1;
}
//_____________________________________________________________________________
bool XrdCryptoX509::MatchHostnames(const char * match_pattern, const char * hostname)
{
// Compare two hostnames and see if they are the same, including wildcards.
//
// For example,
//
// - foo.example.com and foo.example.com are considered equal.
// - bar.example.com and foo.example.com are not equal.
// - *.example.com and foo.example.com are equal.
// - *.example.com and foo.bar.example.com are NOT equal (wildcard applies to a single label).
// - FOO.example.com and foo.EXAMPLE.COM are equal (comparison is not case sensitive).
// - F*.com and foo.com are equal
//
// Returns true if the hostnames are considered a match
XrdOucString mpatt(match_pattern), hname(hostname);
// Not empty
if (!mpatt.length() || !hname.length()) return false;
// Create a lowercase copy of both hostnames
mpatt.lower(0);
hname.lower(0);
// Are they equal?
if (mpatt == hname) return true;
bool theydomatch = false;
// Get first token of both strings
int mfrom = -1, hfrom = -1;
XrdOucString mfirst, hfirst;
if (((mfrom = mpatt.tokenize(mfirst, mfrom, '.')) != -1) &&
((hfrom = hname.tokenize(hfirst, hfrom, '.')) != -1)) {
if (hfirst.matches(mfirst.c_str())) {
// First tokens matches, the rest should match without wildcards
mpatt.erasefromstart(mfrom);
hname.erasefromstart(hfrom);
if ((hname == mpatt) ||
(!hname.length() && !mpatt.length())) theydomatch = true;
}
}
return theydomatch;
}