-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCryptogsiX509Chain.cc
287 lines (254 loc) · 9.92 KB
/
XrdCryptogsiX509Chain.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
/******************************************************************************/
/* */
/* X r d C r y p t o g s i X 5 0 9 C h a i n . c c */
/* */
/* (c) 2014 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. */
/* */
/******************************************************************************/
#include <cstring>
#include <ctime>
#include "XrdCrypto/XrdCryptoFactory.hh"
#include "XrdCrypto/XrdCryptogsiX509Chain.hh"
#include "XrdCrypto/XrdCryptoTrace.hh"
// ---------------------------------------------------------------------------//
// //
// XrdCryptogsiX509Chain (was XrdCryptosslgsiX509Chain) //
// //
// Enforce GSI policies on X509 certificate chains //
// //
// ---------------------------------------------------------------------------//
//___________________________________________________________________________
bool XrdCryptogsiX509Chain::Verify(EX509ChainErr &errcode, x509ChainVerifyOpt_t *vopt)
{
// Verify the chain
EPNAME("X509Chain::Verify");
errcode = kNone;
// There must be at least a CA and a { EEC or subCA }.
if (size < 2) {
DEBUG("Nothing to verify (size: "<<size<<")");
return 0;
}
if (QTRACE(Dump)) { Dump(); }
//
// Reorder if needed
if (Reorder() != 0) {
errcode = kInconsistent;
lastError = ":";
lastError += X509ChainError(errcode);
return 0;
}
//
// Verification options
int opt = (vopt) ? vopt->opt : 0;
int when = (vopt) ? vopt->when : (int)time(0);
int plen = (vopt) ? vopt->pathlen : -1;
XrdCryptoX509Crl *crl = (vopt) ? vopt->crl : 0;
//
// Global path depth length consistency check
if (plen > -1 && plen < size) {
errcode = kTooMany;
lastError = "checking path depth: ";
lastError += X509ChainError(errcode);
}
//
// Check the first certificate: it MUST be of CA type, valid,
// self-signed
XrdCryptoX509ChainNode *node = begin;
XrdCryptoX509 *xcer = node->Cert(); // Certificate under exam
XrdCryptoX509 *xsig = xcer; // Signing certificate
if (statusCA == kUnknown) {
if (!XrdCryptoX509Chain::Verify(errcode, "CA: ",
XrdCryptoX509::kCA, when, xcer, xsig))
return 0;
statusCA = kValid;
} else if (statusCA == kAbsent || statusCA == kInvalid) {
errcode = kNoCA;
lastError = X509ChainError(errcode);
return 0;
}
//
// Update the max path depth len
if (plen > -1)
plen -= 1;
//
// Check sub-CA's certificate, if any
while (node->Next() && node->Next()->Cert()->type == XrdCryptoX509::kCA) {
xsig = xcer;
node = node->Next();
xcer = node->Cert();
if (!XrdCryptoX509Chain::Verify(errcode, "Sub-CA: ",
XrdCryptoX509::kCA,
when, xcer, xsig, crl))
return 0;
//
// Update the max path depth len
if (plen > -1)
plen -= 1;
}
//
// If subCA verification case we are done
if (opt & kOptsCheckSubCA) return 1;
//
// Check the end-point entity certificate
if (!node->Next() || // We expect somethign else if not in subCA checking mode
(node->Next() && node->Next()->Cert()->type != XrdCryptoX509::kEEC)) {
errcode = kNoEEC;
lastError = X509ChainError(errcode);
return 0;
}
//
// Check the end-point entity certificate
xsig = xcer;
node = node->Next();
xcer = node->Cert();
if (!XrdCryptoX509Chain::Verify(errcode, "EEC: ",
XrdCryptoX509::kUnknown,
when, xcer, xsig, crl))
return 0;
//
// Update the max path depth len
if (plen > -1)
plen -= 1;
//
// Only one end-point entity certificate
if (node->Next() && node->Next()->Cert()->type == XrdCryptoX509::kEEC) {
errcode = kTooManyEEC;
lastError = X509ChainError(errcode);
return 0;
}
//
// There are proxy certificates
xsig = xcer;
node = node->Next();
while (node && (plen == -1 || plen > 0)) {
// Attache to certificate
xcer = node->Cert();
//
// Must be a recognized proxy certificate
if (xcer && xcer->type != XrdCryptoX509::kProxy) {
errcode = kInvalidProxy;
lastError = X509ChainError(errcode);
return 0;
}
// Proxy subject name must follow some rules
if (!SubjectOK(errcode, xcer))
return 0;
// Check if ProxyCertInfo extension is there (required by RFC3820)
int pxplen = -1; bool b;
if (opt & kOptsRfc3820) {
const void *extdata = xcer->GetExtension(gsiProxyCertInfo_OID);
if (!extdata) extdata = xcer->GetExtension(gsiProxyCertInfo_OLD_OID);
if (!extdata || !cfact || !(cfact && (*(cfact->ProxyCertInfo()))(extdata, pxplen, &b))) {
errcode = kMissingExtension;
lastError = "rfc3820: ";
lastError += X509ChainError(errcode);
return 0;
}
}
// Update plen, if needed
if (plen == -1) {
plen = (pxplen > -1) ? pxplen : plen;
} else {
plen--;
// Aply stricter rules if required
plen = (pxplen > -1 && pxplen < plen) ? pxplen : plen;
}
// Standard verification
if (!XrdCryptoX509Chain::Verify(errcode, "Proxy: ",
XrdCryptoX509::kProxy, when, xcer, xsig))
return 0;
// Get next
xsig = xcer;
node = node->Next();
}
// We are done (successfully!)
return 1;
}
//___________________________________________________________________________
bool XrdCryptogsiX509Chain::SubjectOK(EX509ChainErr &errcode, XrdCryptoX509 *xcer)
{
// Apply GSI rules for proxy subject names
// Check inputs
if (!xcer) {
errcode = kNoCertificate;
lastError = "subject check:";
lastError += X509ChainError(errcode);
return 0;
}
// This applies only to proxies
if (xcer->type != XrdCryptoX509::kProxy)
return 1;
// Pointers to names
if (!(xcer->Subject()) || !(xcer->Issuer())) {
errcode = kInvalidNames;
lastError = "subject check:";
lastError += X509ChainError(errcode);
return 0;
}
// Subject name must start with issuer name.
// We need the length of the common part between issuer and subject.
// We allow proxies issued by other proxies. In such cases we must
// ignore the last '/CN=' in the issuer name; this explains the need
// for the following gymnastic.
int ilen = strlen(xcer->Issuer());
if (strncmp(xcer->Subject(), xcer->Issuer(), ilen)) {
// Check if the issuer is a proxy: ignore the last 'CN='
char *pcn = (char *) strstr(xcer->Issuer(), "/CN=");
if (pcn) {
char *pcnn = 0;
while ((pcnn = (char *) strstr(pcn+1,"/CN=")))
pcn = pcnn;
ilen = (int)(pcn - xcer->Issuer());
}
if (strncmp(xcer->Subject() + ilen,"/CN=",4)) {
errcode = kInvalidNames;
lastError = "proxy subject check: found additional chars :";
lastError += X509ChainError(errcode);
return 0;
}
if (strncmp(xcer->Subject(), xcer->Issuer(), ilen)) {
errcode = kInvalidNames;
lastError = "proxy issuer check: issuer not found in subject :";
lastError += X509ChainError(errcode);
return 0;
}
}
// A common name must be appendend
char *pp = (char *)strstr(xcer->Subject()+ilen, "CN=");
if (!pp) {
errcode = kInvalidNames;
lastError = "proxy subject check: no appended 'CN='";
lastError += X509ChainError(errcode);
return 0;
}
// But only one
pp = strstr(pp+strlen("CN="), "CN=");
if (pp) {
errcode = kInvalidNames;
lastError = "proxy subject check: too many appended 'CN='s";
lastError += X509ChainError(errcode);
return 0;
}
// We are done
return 1;
}