/src/nss/lib/ssl/sslauth.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | #include "nssrenam.h" /* for CERT_DecodeDERCertificate */ |
6 | | #include "cert.h" |
7 | | #include "secitem.h" |
8 | | #include "ssl.h" |
9 | | #include "sslimpl.h" |
10 | | #include "sslproto.h" |
11 | | #include "pk11func.h" |
12 | | #include "ocsp.h" |
13 | | |
14 | | /* NEED LOCKS IN HERE. */ |
15 | | CERTCertificate * |
16 | | SSL_PeerCertificate(PRFileDesc *fd) |
17 | 0 | { |
18 | 0 | sslSocket *ss; |
19 | |
|
20 | 0 | ss = ssl_FindSocket(fd); |
21 | 0 | if (!ss) { |
22 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in PeerCertificate", |
23 | 0 | SSL_GETPID(), fd)); |
24 | 0 | return 0; |
25 | 0 | } |
26 | 0 | if (ss->opt.useSecurity && ss->sec.peerCertDER.data) { |
27 | 0 | return CERT_NewTempCertificate(ss->dbHandle, &ss->sec.peerCertDER, |
28 | 0 | NULL, PR_FALSE, PR_TRUE); |
29 | 0 | } |
30 | 0 | return 0; |
31 | 0 | } |
32 | | |
33 | | /* NEED LOCKS IN HERE. */ |
34 | | SECStatus |
35 | | SSLExp_PeerCertificateChainDER(PRFileDesc *fd, SECItemArray **out) |
36 | 0 | { |
37 | 0 | sslSocket *ss; |
38 | 0 | ssl3CertNode *cur; |
39 | 0 | SECItemArray *chain; |
40 | 0 | unsigned int count; |
41 | 0 | unsigned int index; |
42 | 0 | SECStatus rv; |
43 | |
|
44 | 0 | ss = ssl_FindSocket(fd); |
45 | 0 | if (!ss) { |
46 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in PeerCertificateChainDER", |
47 | 0 | SSL_GETPID(), fd)); |
48 | 0 | return SECFailure; |
49 | 0 | } |
50 | 0 | if (!ss->opt.useSecurity || !ss->sec.peerCertDER.data) { |
51 | 0 | PORT_SetError(SSL_ERROR_NO_CERTIFICATE); |
52 | 0 | return SECFailure; |
53 | 0 | } |
54 | | |
55 | 0 | count = 1; // for ss->sec.peerCertDER |
56 | 0 | for (cur = ss->ssl3.peerCertChain; cur; cur = cur->next) { |
57 | 0 | ++count; |
58 | 0 | } |
59 | |
|
60 | 0 | chain = SECITEM_AllocArray(NULL, NULL, count); |
61 | 0 | if (chain == NULL) { |
62 | 0 | return SECFailure; /* error code set in SECITEM_AllocArray */ |
63 | 0 | } |
64 | | |
65 | 0 | index = 0; |
66 | 0 | rv = SECITEM_CopyItem(NULL, &chain->items[index++], &ss->sec.peerCertDER); |
67 | 0 | if (rv != SECSuccess) { |
68 | 0 | goto loser; /* error code set in SECITEM_CopyItem */ |
69 | 0 | } |
70 | | |
71 | 0 | for (cur = ss->ssl3.peerCertChain; cur; cur = cur->next) { |
72 | 0 | rv = SECITEM_CopyItem(NULL, &chain->items[index++], cur->derCert); |
73 | 0 | if (rv != SECSuccess) { |
74 | 0 | goto loser; /* error code set in SECITEM_CopyItem */ |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | 0 | *out = chain; |
79 | 0 | return SECSuccess; |
80 | | |
81 | 0 | loser: |
82 | 0 | SECITEM_FreeArray(chain, PR_TRUE); |
83 | 0 | return SECFailure; |
84 | 0 | } |
85 | | |
86 | | /* NEED LOCKS IN HERE. */ |
87 | | CERTCertList * |
88 | | SSL_PeerCertificateChain(PRFileDesc *fd) |
89 | 70.6k | { |
90 | 70.6k | sslSocket *ss; |
91 | 70.6k | CERTCertList *chain = NULL; |
92 | 70.6k | CERTCertificate *cert; |
93 | 70.6k | ssl3CertNode *cur; |
94 | | |
95 | 70.6k | ss = ssl_FindSocket(fd); |
96 | 70.6k | if (!ss) { |
97 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in PeerCertificateChain", |
98 | 0 | SSL_GETPID(), fd)); |
99 | 0 | return NULL; |
100 | 0 | } |
101 | 70.6k | if (!ss->opt.useSecurity || !ss->sec.peerCertDER.data) { |
102 | 0 | PORT_SetError(SSL_ERROR_NO_CERTIFICATE); |
103 | 0 | return NULL; |
104 | 0 | } |
105 | 70.6k | chain = CERT_NewCertList(); |
106 | 70.6k | if (!chain) { |
107 | 0 | return NULL; |
108 | 0 | } |
109 | 70.6k | cert = CERT_NewTempCertificate(ss->dbHandle, &ss->sec.peerCertDER, |
110 | 70.6k | NULL, PR_FALSE, PR_TRUE); |
111 | 70.6k | if (!cert || CERT_AddCertToListTail(chain, cert) != SECSuccess) { |
112 | 516 | goto loser; |
113 | 516 | } |
114 | 73.1k | for (cur = ss->ssl3.peerCertChain; cur; cur = cur->next) { |
115 | 3.67k | cert = CERT_NewTempCertificate(ss->dbHandle, cur->derCert, |
116 | 3.67k | NULL, PR_FALSE, PR_TRUE); |
117 | 3.67k | if (!cert || CERT_AddCertToListTail(chain, cert) != SECSuccess) { |
118 | 642 | goto loser; |
119 | 642 | } |
120 | 3.67k | } |
121 | 69.5k | return chain; |
122 | | |
123 | 1.15k | loser: |
124 | 1.15k | CERT_DestroyCertList(chain); |
125 | 1.15k | return NULL; |
126 | 70.1k | } |
127 | | |
128 | | /* NEED LOCKS IN HERE. */ |
129 | | CERTCertificate * |
130 | | SSL_LocalCertificate(PRFileDesc *fd) |
131 | 0 | { |
132 | 0 | sslSocket *ss; |
133 | |
|
134 | 0 | ss = ssl_FindSocket(fd); |
135 | 0 | if (!ss) { |
136 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in PeerCertificate", |
137 | 0 | SSL_GETPID(), fd)); |
138 | 0 | return NULL; |
139 | 0 | } |
140 | 0 | if (ss->opt.useSecurity) { |
141 | 0 | if (ss->sec.localCert) { |
142 | 0 | return CERT_DupCertificate(ss->sec.localCert); |
143 | 0 | } |
144 | 0 | if (ss->sec.ci.sid && ss->sec.ci.sid->localCert) { |
145 | 0 | return CERT_DupCertificate(ss->sec.ci.sid->localCert); |
146 | 0 | } |
147 | 0 | } |
148 | 0 | return NULL; |
149 | 0 | } |
150 | | |
151 | | /* NEED LOCKS IN HERE. */ |
152 | | SECStatus |
153 | | SSL_SecurityStatus(PRFileDesc *fd, int *op, char **cp, int *kp0, int *kp1, |
154 | | char **ip, char **sp) |
155 | 0 | { |
156 | 0 | sslSocket *ss; |
157 | |
|
158 | 0 | ss = ssl_FindSocket(fd); |
159 | 0 | if (!ss) { |
160 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SecurityStatus", |
161 | 0 | SSL_GETPID(), fd)); |
162 | 0 | return SECFailure; |
163 | 0 | } |
164 | | |
165 | 0 | if (cp) |
166 | 0 | *cp = 0; |
167 | 0 | if (kp0) |
168 | 0 | *kp0 = 0; |
169 | 0 | if (kp1) |
170 | 0 | *kp1 = 0; |
171 | 0 | if (ip) |
172 | 0 | *ip = 0; |
173 | 0 | if (sp) |
174 | 0 | *sp = 0; |
175 | 0 | if (op) { |
176 | 0 | *op = SSL_SECURITY_STATUS_OFF; |
177 | 0 | } |
178 | |
|
179 | 0 | if (ss->opt.useSecurity && ss->enoughFirstHsDone) { |
180 | 0 | const ssl3BulkCipherDef *bulkCipherDef; |
181 | 0 | PRBool isDes = PR_FALSE; |
182 | |
|
183 | 0 | bulkCipherDef = ssl_GetBulkCipherDef(ss->ssl3.hs.suite_def); |
184 | 0 | if (cp) { |
185 | 0 | *cp = PORT_Strdup(bulkCipherDef->short_name); |
186 | 0 | } |
187 | 0 | if (PORT_Strstr(bulkCipherDef->short_name, "DES")) { |
188 | 0 | isDes = PR_TRUE; |
189 | 0 | } |
190 | |
|
191 | 0 | if (kp0) { |
192 | 0 | *kp0 = bulkCipherDef->key_size * 8; |
193 | 0 | if (isDes) |
194 | 0 | *kp0 = (*kp0 * 7) / 8; |
195 | 0 | } |
196 | 0 | if (kp1) { |
197 | 0 | *kp1 = bulkCipherDef->secret_key_size * 8; |
198 | 0 | if (isDes) |
199 | 0 | *kp1 = (*kp1 * 7) / 8; |
200 | 0 | } |
201 | 0 | if (op) { |
202 | 0 | if (bulkCipherDef->key_size == 0) { |
203 | 0 | *op = SSL_SECURITY_STATUS_OFF; |
204 | 0 | } else if (bulkCipherDef->secret_key_size * 8 < 90) { |
205 | 0 | *op = SSL_SECURITY_STATUS_ON_LOW; |
206 | 0 | } else { |
207 | 0 | *op = SSL_SECURITY_STATUS_ON_HIGH; |
208 | 0 | } |
209 | 0 | } |
210 | |
|
211 | 0 | if (ip || sp) { |
212 | 0 | CERTCertificate *cert = NULL; |
213 | | |
214 | | /* Decode the peer's certificate just long enough to read the |
215 | | * issuer and subject names out of it. Nothing is added to any |
216 | | * certificate database. */ |
217 | 0 | if (ss->sec.peerCertDER.data) { |
218 | 0 | cert = CERT_DecodeDERCertificate(&ss->sec.peerCertDER, |
219 | 0 | PR_FALSE, NULL); |
220 | 0 | } |
221 | 0 | if (cert) { |
222 | 0 | if (ip) { |
223 | 0 | *ip = CERT_NameToAscii(&cert->issuer); |
224 | 0 | } |
225 | 0 | if (sp) { |
226 | 0 | *sp = CERT_NameToAscii(&cert->subject); |
227 | 0 | } |
228 | 0 | CERT_DestroyCertificate(cert); |
229 | 0 | } else { |
230 | 0 | if (ip) { |
231 | 0 | *ip = PORT_Strdup("no certificate"); |
232 | 0 | } |
233 | 0 | if (sp) { |
234 | 0 | *sp = PORT_Strdup("no certificate"); |
235 | 0 | } |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |
239 | |
|
240 | 0 | return SECSuccess; |
241 | 0 | } |
242 | | |
243 | | /************************************************************************/ |
244 | | |
245 | | /* NEED LOCKS IN HERE. */ |
246 | | SECStatus |
247 | | SSL_AuthCertificateHook(PRFileDesc *s, SSLAuthCertificate func, void *arg) |
248 | 61.2k | { |
249 | 61.2k | sslSocket *ss; |
250 | | |
251 | 61.2k | ss = ssl_FindSocket(s); |
252 | 61.2k | if (!ss) { |
253 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in AuthCertificateHook", |
254 | 0 | SSL_GETPID(), s)); |
255 | 0 | return SECFailure; |
256 | 0 | } |
257 | | |
258 | 61.2k | ss->authCertificate = func; |
259 | 61.2k | ss->authCertificateArg = arg; |
260 | | |
261 | 61.2k | return SECSuccess; |
262 | 61.2k | } |
263 | | |
264 | | /* NEED LOCKS IN HERE. */ |
265 | | SECStatus |
266 | | SSL_GetClientAuthDataHook(PRFileDesc *s, SSLGetClientAuthData func, |
267 | | void *arg) |
268 | 0 | { |
269 | 0 | sslSocket *ss; |
270 | |
|
271 | 0 | ss = ssl_FindSocket(s); |
272 | 0 | if (!ss) { |
273 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in GetClientAuthDataHook", |
274 | 0 | SSL_GETPID(), s)); |
275 | 0 | return SECFailure; |
276 | 0 | } |
277 | | |
278 | 0 | ss->getClientAuthData = func; |
279 | 0 | ss->getClientAuthDataArg = arg; |
280 | 0 | return SECSuccess; |
281 | 0 | } |
282 | | |
283 | | /* NEED LOCKS IN HERE. */ |
284 | | SECStatus |
285 | | SSL_SetPKCS11PinArg(PRFileDesc *s, void *arg) |
286 | 0 | { |
287 | 0 | sslSocket *ss; |
288 | |
|
289 | 0 | ss = ssl_FindSocket(s); |
290 | 0 | if (!ss) { |
291 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in GetClientAuthDataHook", |
292 | 0 | SSL_GETPID(), s)); |
293 | 0 | return SECFailure; |
294 | 0 | } |
295 | | |
296 | 0 | ss->pkcs11PinArg = arg; |
297 | 0 | return SECSuccess; |
298 | 0 | } |
299 | | |
300 | | /* This is the "default" authCert callback function. It is called when a |
301 | | * certificate message is received from the peer and the local application |
302 | | * has not registered an authCert callback function. |
303 | | */ |
304 | | SECStatus |
305 | | SSL_AuthCertificate(void *arg, PRFileDesc *fd, PRBool checkSig, PRBool isServer) |
306 | 0 | { |
307 | 0 | SECStatus rv; |
308 | 0 | CERTCertDBHandle *handle; |
309 | 0 | sslSocket *ss; |
310 | 0 | SECCertUsage certUsage; |
311 | 0 | const char *hostname = NULL; |
312 | 0 | SECItemArray *certStatusArray; |
313 | 0 | CERTCertificate *peerCert; |
314 | |
|
315 | 0 | ss = ssl_FindSocket(fd); |
316 | 0 | PORT_Assert(ss != NULL); |
317 | 0 | if (!ss) { |
318 | 0 | return SECFailure; |
319 | 0 | } |
320 | | |
321 | 0 | handle = (CERTCertDBHandle *)arg; |
322 | 0 | certStatusArray = &ss->sec.ci.sid->peerCertStatus; |
323 | | |
324 | | /* The handshake itself never instantiates a CERTCertificate for the peer, |
325 | | * but the classic certificate verification this callback performs is |
326 | | * defined in terms of one, so make one here. Applications that do their |
327 | | * own verification (Firefox, for one) install their own callback and never |
328 | | * reach this code. |
329 | | */ |
330 | 0 | if (!ss->sec.peerCertDER.data) { |
331 | 0 | PORT_SetError(SSL_ERROR_NO_CERTIFICATE); |
332 | 0 | return SECFailure; |
333 | 0 | } |
334 | 0 | peerCert = CERT_NewTempCertificate(handle, &ss->sec.peerCertDER, |
335 | 0 | NULL, PR_FALSE, PR_TRUE); |
336 | 0 | if (!peerCert) { |
337 | 0 | return SECFailure; |
338 | 0 | } |
339 | | |
340 | 0 | PRTime now = ssl_Time(ss); |
341 | 0 | if (certStatusArray->len) { |
342 | 0 | PORT_SetError(0); |
343 | 0 | if (CERT_CacheOCSPResponseFromSideChannel(handle, peerCert, now, |
344 | 0 | &certStatusArray->items[0], |
345 | 0 | ss->pkcs11PinArg) != |
346 | 0 | SECSuccess) { |
347 | 0 | PORT_Assert(PR_GetError() != 0); |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | | /* this may seem backwards, but isn't. */ |
352 | 0 | certUsage = isServer ? certUsageSSLClient : certUsageSSLServer; |
353 | | |
354 | | /* Calling SSL_PeerCertificateChain here ensures that all certs from the |
355 | | * peer's presented chain are in the database for path finding. |
356 | | */ |
357 | 0 | CERTCertList *peerChain = SSL_PeerCertificateChain(fd); |
358 | |
|
359 | 0 | rv = CERT_VerifyCert(handle, peerCert, checkSig, certUsage, |
360 | 0 | now, ss->pkcs11PinArg, NULL); |
361 | |
|
362 | 0 | CERT_DestroyCertList(peerChain); |
363 | |
|
364 | 0 | if (rv != SECSuccess || isServer) |
365 | 0 | goto done; |
366 | | |
367 | | /* cert is OK. This is the client side of an SSL connection. |
368 | | * Now check the name field in the cert against the desired hostname. |
369 | | * NB: This is our only defense against Man-In-The-Middle (MITM) attacks! |
370 | | */ |
371 | 0 | hostname = ss->url; |
372 | 0 | if (hostname && hostname[0]) |
373 | 0 | rv = CERT_VerifyCertName(peerCert, hostname); |
374 | 0 | else |
375 | 0 | rv = SECFailure; |
376 | 0 | if (rv != SECSuccess) |
377 | 0 | PORT_SetError(SSL_ERROR_BAD_CERT_DOMAIN); |
378 | |
|
379 | 0 | done: |
380 | 0 | CERT_DestroyCertificate(peerCert); |
381 | 0 | return rv; |
382 | 0 | } |