OLD | NEW |
| (Empty) |
1 /* | |
2 * vtables (and methods that call through them) for the 4 types of | |
3 * SSLSockets supported. Only one type is still supported. | |
4 * Various other functions. | |
5 * | |
6 * This Source Code Form is subject to the terms of the Mozilla Public | |
7 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
9 #include "seccomon.h" | |
10 #include "cert.h" | |
11 #include "keyhi.h" | |
12 #include "ssl.h" | |
13 #include "sslimpl.h" | |
14 #include "sslproto.h" | |
15 #include "nspr.h" | |
16 #include "private/pprio.h" | |
17 #ifndef NO_PKCS11_BYPASS | |
18 #include "blapi.h" | |
19 #endif | |
20 #include "pk11pub.h" | |
21 #include "nss.h" | |
22 | |
23 /* This is a bodge to allow this code to be compiled against older NSS headers | |
24 * that don't contain the TLS 1.2 changes. */ | |
25 #ifndef CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 | |
26 #define CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 (CKM_NSS + 24) | |
27 #endif | |
28 | |
29 #define SET_ERROR_CODE /* reminder */ | |
30 | |
31 static const sslSocketOps ssl_default_ops = { /* No SSL. */ | |
32 ssl_DefConnect, | |
33 NULL, | |
34 ssl_DefBind, | |
35 ssl_DefListen, | |
36 ssl_DefShutdown, | |
37 ssl_DefClose, | |
38 ssl_DefRecv, | |
39 ssl_DefSend, | |
40 ssl_DefRead, | |
41 ssl_DefWrite, | |
42 ssl_DefGetpeername, | |
43 ssl_DefGetsockname | |
44 }; | |
45 | |
46 static const sslSocketOps ssl_secure_ops = { /* SSL. */ | |
47 ssl_SecureConnect, | |
48 NULL, | |
49 ssl_DefBind, | |
50 ssl_DefListen, | |
51 ssl_SecureShutdown, | |
52 ssl_SecureClose, | |
53 ssl_SecureRecv, | |
54 ssl_SecureSend, | |
55 ssl_SecureRead, | |
56 ssl_SecureWrite, | |
57 ssl_DefGetpeername, | |
58 ssl_DefGetsockname | |
59 }; | |
60 | |
61 /* | |
62 ** default settings for socket enables | |
63 */ | |
64 static sslOptions ssl_defaults = { | |
65 { siBuffer, NULL, 0 }, /* nextProtoNego */ | |
66 PR_TRUE, /* useSecurity */ | |
67 PR_FALSE, /* useSocks */ | |
68 PR_FALSE, /* requestCertificate */ | |
69 2, /* requireCertificate */ | |
70 PR_FALSE, /* handshakeAsClient */ | |
71 PR_FALSE, /* handshakeAsServer */ | |
72 PR_FALSE, /* enableSSL2 */ /* now defaults to off in NSS 3.13 */ | |
73 PR_FALSE, /* unusedBit9 */ | |
74 PR_FALSE, /* unusedBit10 */ | |
75 PR_FALSE, /* noCache */ | |
76 PR_FALSE, /* fdx */ | |
77 PR_FALSE, /* v2CompatibleHello */ /* now defaults to off in NSS 3.13 */ | |
78 PR_TRUE, /* detectRollBack */ | |
79 PR_FALSE, /* noStepDown */ | |
80 PR_FALSE, /* bypassPKCS11 */ | |
81 PR_FALSE, /* noLocks */ | |
82 PR_FALSE, /* enableSessionTickets */ | |
83 PR_FALSE, /* enableDeflate */ | |
84 2, /* enableRenegotiation (default: requires extension) */ | |
85 PR_FALSE, /* requireSafeNegotiation */ | |
86 PR_FALSE, /* enableFalseStart */ | |
87 PR_TRUE, /* cbcRandomIV */ | |
88 PR_FALSE, /* enableOCSPStapling */ | |
89 PR_TRUE, /* enableNPN */ | |
90 PR_FALSE, /* enableALPN */ | |
91 PR_FALSE, /* enableSignedCertTimestamps */ | |
92 PR_FALSE /* enableFallbackSCSV */ | |
93 }; | |
94 | |
95 /* | |
96 * default range of enabled SSL/TLS protocols | |
97 */ | |
98 static SSLVersionRange versions_defaults_stream = { | |
99 SSL_LIBRARY_VERSION_3_0, | |
100 SSL_LIBRARY_VERSION_TLS_1_0 | |
101 }; | |
102 | |
103 static SSLVersionRange versions_defaults_datagram = { | |
104 SSL_LIBRARY_VERSION_TLS_1_1, | |
105 SSL_LIBRARY_VERSION_TLS_1_1 | |
106 }; | |
107 | |
108 #define VERSIONS_DEFAULTS(variant) \ | |
109 (variant == ssl_variant_stream ? &versions_defaults_stream : \ | |
110 &versions_defaults_datagram) | |
111 | |
112 sslSessionIDLookupFunc ssl_sid_lookup; | |
113 sslSessionIDCacheFunc ssl_sid_cache; | |
114 sslSessionIDUncacheFunc ssl_sid_uncache; | |
115 | |
116 static PRBool ssl_inited = PR_FALSE; | |
117 static PRDescIdentity ssl_layer_id; | |
118 | |
119 PRBool locksEverDisabled; /* implicitly PR_FALSE */ | |
120 PRBool ssl_force_locks; /* implicitly PR_FALSE */ | |
121 int ssl_lock_readers = 1; /* default true. */ | |
122 char ssl_debug; | |
123 char ssl_trace; | |
124 FILE * ssl_trace_iob; | |
125 FILE * ssl_keylog_iob; | |
126 char lockStatus[] = "Locks are ENABLED. "; | |
127 #define LOCKSTATUS_OFFSET 10 /* offset of ENABLED */ | |
128 | |
129 /* SRTP_NULL_HMAC_SHA1_80 and SRTP_NULL_HMAC_SHA1_32 are not implemented. */ | |
130 static const PRUint16 srtpCiphers[] = { | |
131 SRTP_AES128_CM_HMAC_SHA1_80, | |
132 SRTP_AES128_CM_HMAC_SHA1_32, | |
133 0 | |
134 }; | |
135 | |
136 /* forward declarations. */ | |
137 static sslSocket *ssl_NewSocket(PRBool makeLocks, SSLProtocolVariant variant); | |
138 static SECStatus ssl_MakeLocks(sslSocket *ss); | |
139 static void ssl_SetDefaultsFromEnvironment(void); | |
140 static PRStatus ssl_PushIOLayer(sslSocket *ns, PRFileDesc *stack, | |
141 PRDescIdentity id); | |
142 | |
143 /************************************************************************/ | |
144 | |
145 /* | |
146 ** Lookup a socket structure from a file descriptor. | |
147 ** Only functions called through the PRIOMethods table should use this. | |
148 ** Other app-callable functions should use ssl_FindSocket. | |
149 */ | |
150 static sslSocket * | |
151 ssl_GetPrivate(PRFileDesc *fd) | |
152 { | |
153 sslSocket *ss; | |
154 | |
155 PORT_Assert(fd != NULL); | |
156 PORT_Assert(fd->methods->file_type == PR_DESC_LAYERED); | |
157 PORT_Assert(fd->identity == ssl_layer_id); | |
158 | |
159 if (fd->methods->file_type != PR_DESC_LAYERED || | |
160 fd->identity != ssl_layer_id) { | |
161 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR); | |
162 return NULL; | |
163 } | |
164 | |
165 ss = (sslSocket *)fd->secret; | |
166 ss->fd = fd; | |
167 return ss; | |
168 } | |
169 | |
170 /* This function tries to find the SSL layer in the stack. | |
171 * It searches for the first SSL layer at or below the argument fd, | |
172 * and failing that, it searches for the nearest SSL layer above the | |
173 * argument fd. It returns the private sslSocket from the found layer. | |
174 */ | |
175 sslSocket * | |
176 ssl_FindSocket(PRFileDesc *fd) | |
177 { | |
178 PRFileDesc *layer; | |
179 sslSocket *ss; | |
180 | |
181 PORT_Assert(fd != NULL); | |
182 PORT_Assert(ssl_layer_id != 0); | |
183 | |
184 layer = PR_GetIdentitiesLayer(fd, ssl_layer_id); | |
185 if (layer == NULL) { | |
186 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR); | |
187 return NULL; | |
188 } | |
189 | |
190 ss = (sslSocket *)layer->secret; | |
191 ss->fd = layer; | |
192 return ss; | |
193 } | |
194 | |
195 static sslSocket * | |
196 ssl_DupSocket(sslSocket *os) | |
197 { | |
198 sslSocket *ss; | |
199 SECStatus rv; | |
200 | |
201 ss = ssl_NewSocket((PRBool)(!os->opt.noLocks), os->protocolVariant); | |
202 if (ss) { | |
203 ss->opt = os->opt; | |
204 ss->opt.useSocks = PR_FALSE; | |
205 ss->vrange = os->vrange; | |
206 | |
207 ss->peerID = !os->peerID ? NULL : PORT_Strdup(os->peerID); | |
208 ss->url = !os->url ? NULL : PORT_Strdup(os->url); | |
209 | |
210 ss->ops = os->ops; | |
211 ss->rTimeout = os->rTimeout; | |
212 ss->wTimeout = os->wTimeout; | |
213 ss->cTimeout = os->cTimeout; | |
214 ss->dbHandle = os->dbHandle; | |
215 | |
216 /* copy ssl2&3 policy & prefs, even if it's not selected (yet) */ | |
217 ss->allowedByPolicy = os->allowedByPolicy; | |
218 ss->maybeAllowedByPolicy= os->maybeAllowedByPolicy; | |
219 ss->chosenPreference = os->chosenPreference; | |
220 PORT_Memcpy(ss->cipherSuites, os->cipherSuites, sizeof os->cipherSuites)
; | |
221 PORT_Memcpy(ss->ssl3.dtlsSRTPCiphers, os->ssl3.dtlsSRTPCiphers, | |
222 sizeof(PRUint16) * os->ssl3.dtlsSRTPCipherCount); | |
223 ss->ssl3.dtlsSRTPCipherCount = os->ssl3.dtlsSRTPCipherCount; | |
224 | |
225 if (os->cipherSpecs) { | |
226 ss->cipherSpecs = (unsigned char*)PORT_Alloc(os->sizeCipherSpecs); | |
227 if (ss->cipherSpecs) | |
228 PORT_Memcpy(ss->cipherSpecs, os->cipherSpecs, | |
229 os->sizeCipherSpecs); | |
230 ss->sizeCipherSpecs = os->sizeCipherSpecs; | |
231 ss->preferredCipher = os->preferredCipher; | |
232 } else { | |
233 ss->cipherSpecs = NULL; /* produced lazily */ | |
234 ss->sizeCipherSpecs = 0; | |
235 ss->preferredCipher = NULL; | |
236 } | |
237 if (ss->opt.useSecurity) { | |
238 /* This int should be SSLKEAType, but CC on Irix complains, | |
239 * during the for loop. | |
240 */ | |
241 int i; | |
242 sslServerCerts * oc = os->serverCerts; | |
243 sslServerCerts * sc = ss->serverCerts; | |
244 | |
245 for (i=kt_null; i < kt_kea_size; i++, oc++, sc++) { | |
246 if (oc->serverCert && oc->serverCertChain) { | |
247 sc->serverCert = CERT_DupCertificate(oc->serverCert); | |
248 sc->serverCertChain = CERT_DupCertList(oc->serverCertChain); | |
249 if (!sc->serverCertChain) | |
250 goto loser; | |
251 } else { | |
252 sc->serverCert = NULL; | |
253 sc->serverCertChain = NULL; | |
254 } | |
255 sc->serverKeyPair = oc->serverKeyPair ? | |
256 ssl3_GetKeyPairRef(oc->serverKeyPair) : NULL; | |
257 if (oc->serverKeyPair && !sc->serverKeyPair) | |
258 goto loser; | |
259 sc->serverKeyBits = oc->serverKeyBits; | |
260 ss->certStatusArray[i] = !os->certStatusArray[i] ? NULL : | |
261 SECITEM_DupArray(NULL, os->certStatusArray[i]); | |
262 } | |
263 ss->stepDownKeyPair = !os->stepDownKeyPair ? NULL : | |
264 ssl3_GetKeyPairRef(os->stepDownKeyPair); | |
265 ss->ephemeralECDHKeyPair = !os->ephemeralECDHKeyPair ? NULL : | |
266 ssl3_GetKeyPairRef(os->ephemeralECDHKeyPair); | |
267 /* | |
268 * XXX the preceding CERT_ and SECKEY_ functions can fail and return NULL. | |
269 * XXX We should detect this, and not just march on with NULL pointers. | |
270 */ | |
271 ss->authCertificate = os->authCertificate; | |
272 ss->authCertificateArg = os->authCertificateArg; | |
273 ss->getClientAuthData = os->getClientAuthData; | |
274 ss->getClientAuthDataArg = os->getClientAuthDataArg; | |
275 #ifdef NSS_PLATFORM_CLIENT_AUTH | |
276 ss->getPlatformClientAuthData = os->getPlatformClientAuthData; | |
277 ss->getPlatformClientAuthDataArg = os->getPlatformClientAuthDataArg; | |
278 #endif | |
279 ss->sniSocketConfig = os->sniSocketConfig; | |
280 ss->sniSocketConfigArg = os->sniSocketConfigArg; | |
281 ss->handleBadCert = os->handleBadCert; | |
282 ss->badCertArg = os->badCertArg; | |
283 ss->handshakeCallback = os->handshakeCallback; | |
284 ss->handshakeCallbackData = os->handshakeCallbackData; | |
285 ss->canFalseStartCallback = os->canFalseStartCallback; | |
286 ss->canFalseStartCallbackData = os->canFalseStartCallbackData; | |
287 ss->pkcs11PinArg = os->pkcs11PinArg; | |
288 ss->getChannelID = os->getChannelID; | |
289 ss->getChannelIDArg = os->getChannelIDArg; | |
290 | |
291 /* Create security data */ | |
292 rv = ssl_CopySecurityInfo(ss, os); | |
293 if (rv != SECSuccess) { | |
294 goto loser; | |
295 } | |
296 } | |
297 } | |
298 return ss; | |
299 | |
300 loser: | |
301 ssl_FreeSocket(ss); | |
302 return NULL; | |
303 } | |
304 | |
305 static void | |
306 ssl_DestroyLocks(sslSocket *ss) | |
307 { | |
308 /* Destroy locks. */ | |
309 if (ss->firstHandshakeLock) { | |
310 PZ_DestroyMonitor(ss->firstHandshakeLock); | |
311 ss->firstHandshakeLock = NULL; | |
312 } | |
313 if (ss->ssl3HandshakeLock) { | |
314 PZ_DestroyMonitor(ss->ssl3HandshakeLock); | |
315 ss->ssl3HandshakeLock = NULL; | |
316 } | |
317 if (ss->specLock) { | |
318 NSSRWLock_Destroy(ss->specLock); | |
319 ss->specLock = NULL; | |
320 } | |
321 | |
322 if (ss->recvLock) { | |
323 PZ_DestroyLock(ss->recvLock); | |
324 ss->recvLock = NULL; | |
325 } | |
326 if (ss->sendLock) { | |
327 PZ_DestroyLock(ss->sendLock); | |
328 ss->sendLock = NULL; | |
329 } | |
330 if (ss->xmitBufLock) { | |
331 PZ_DestroyMonitor(ss->xmitBufLock); | |
332 ss->xmitBufLock = NULL; | |
333 } | |
334 if (ss->recvBufLock) { | |
335 PZ_DestroyMonitor(ss->recvBufLock); | |
336 ss->recvBufLock = NULL; | |
337 } | |
338 } | |
339 | |
340 /* Caller holds any relevant locks */ | |
341 static void | |
342 ssl_DestroySocketContents(sslSocket *ss) | |
343 { | |
344 /* "i" should be of type SSLKEAType, but CC on IRIX complains during | |
345 * the for loop. | |
346 */ | |
347 int i; | |
348 | |
349 /* Free up socket */ | |
350 ssl_DestroySecurityInfo(&ss->sec); | |
351 | |
352 ssl3_DestroySSL3Info(ss); | |
353 | |
354 PORT_Free(ss->saveBuf.buf); | |
355 PORT_Free(ss->pendingBuf.buf); | |
356 ssl_DestroyGather(&ss->gs); | |
357 | |
358 if (ss->peerID != NULL) | |
359 PORT_Free(ss->peerID); | |
360 if (ss->url != NULL) | |
361 PORT_Free((void *)ss->url); /* CONST */ | |
362 if (ss->cipherSpecs) { | |
363 PORT_Free(ss->cipherSpecs); | |
364 ss->cipherSpecs = NULL; | |
365 ss->sizeCipherSpecs = 0; | |
366 } | |
367 | |
368 /* Clean up server configuration */ | |
369 for (i=kt_null; i < kt_kea_size; i++) { | |
370 sslServerCerts * sc = ss->serverCerts + i; | |
371 if (sc->serverCert != NULL) | |
372 CERT_DestroyCertificate(sc->serverCert); | |
373 if (sc->serverCertChain != NULL) | |
374 CERT_DestroyCertificateList(sc->serverCertChain); | |
375 if (sc->serverKeyPair != NULL) | |
376 ssl3_FreeKeyPair(sc->serverKeyPair); | |
377 if (ss->certStatusArray[i] != NULL) { | |
378 SECITEM_FreeArray(ss->certStatusArray[i], PR_TRUE); | |
379 ss->certStatusArray[i] = NULL; | |
380 } | |
381 } | |
382 if (ss->stepDownKeyPair) { | |
383 ssl3_FreeKeyPair(ss->stepDownKeyPair); | |
384 ss->stepDownKeyPair = NULL; | |
385 } | |
386 if (ss->ephemeralECDHKeyPair) { | |
387 ssl3_FreeKeyPair(ss->ephemeralECDHKeyPair); | |
388 ss->ephemeralECDHKeyPair = NULL; | |
389 } | |
390 SECITEM_FreeItem(&ss->opt.nextProtoNego, PR_FALSE); | |
391 PORT_Assert(!ss->xtnData.sniNameArr); | |
392 if (ss->xtnData.sniNameArr) { | |
393 PORT_Free(ss->xtnData.sniNameArr); | |
394 ss->xtnData.sniNameArr = NULL; | |
395 } | |
396 } | |
397 | |
398 /* | |
399 * free an sslSocket struct, and all the stuff that hangs off of it | |
400 */ | |
401 void | |
402 ssl_FreeSocket(sslSocket *ss) | |
403 { | |
404 /* Get every lock you can imagine! | |
405 ** Caller already holds these: | |
406 ** SSL_LOCK_READER(ss); | |
407 ** SSL_LOCK_WRITER(ss); | |
408 */ | |
409 ssl_Get1stHandshakeLock(ss); | |
410 ssl_GetRecvBufLock(ss); | |
411 ssl_GetSSL3HandshakeLock(ss); | |
412 ssl_GetXmitBufLock(ss); | |
413 ssl_GetSpecWriteLock(ss); | |
414 | |
415 ssl_DestroySocketContents(ss); | |
416 | |
417 /* Release all the locks acquired above. */ | |
418 SSL_UNLOCK_READER(ss); | |
419 SSL_UNLOCK_WRITER(ss); | |
420 ssl_Release1stHandshakeLock(ss); | |
421 ssl_ReleaseRecvBufLock(ss); | |
422 ssl_ReleaseSSL3HandshakeLock(ss); | |
423 ssl_ReleaseXmitBufLock(ss); | |
424 ssl_ReleaseSpecWriteLock(ss); | |
425 | |
426 ssl_DestroyLocks(ss); | |
427 | |
428 #ifdef DEBUG | |
429 PORT_Memset(ss, 0x1f, sizeof *ss); | |
430 #endif | |
431 PORT_Free(ss); | |
432 return; | |
433 } | |
434 | |
435 /************************************************************************/ | |
436 SECStatus | |
437 ssl_EnableNagleDelay(sslSocket *ss, PRBool enabled) | |
438 { | |
439 PRFileDesc * osfd = ss->fd->lower; | |
440 SECStatus rv = SECFailure; | |
441 PRSocketOptionData opt; | |
442 | |
443 opt.option = PR_SockOpt_NoDelay; | |
444 opt.value.no_delay = (PRBool)!enabled; | |
445 | |
446 if (osfd->methods->setsocketoption) { | |
447 rv = (SECStatus) osfd->methods->setsocketoption(osfd, &opt); | |
448 } else { | |
449 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
450 } | |
451 | |
452 return rv; | |
453 } | |
454 | |
455 static void | |
456 ssl_ChooseOps(sslSocket *ss) | |
457 { | |
458 ss->ops = ss->opt.useSecurity ? &ssl_secure_ops : &ssl_default_ops; | |
459 } | |
460 | |
461 /* Called from SSL_Enable (immediately below) */ | |
462 static SECStatus | |
463 PrepareSocket(sslSocket *ss) | |
464 { | |
465 SECStatus rv = SECSuccess; | |
466 | |
467 ssl_ChooseOps(ss); | |
468 return rv; | |
469 } | |
470 | |
471 SECStatus | |
472 SSL_Enable(PRFileDesc *fd, int which, PRBool on) | |
473 { | |
474 return SSL_OptionSet(fd, which, on); | |
475 } | |
476 | |
477 #ifndef NO_PKCS11_BYPASS | |
478 static const PRCallOnceType pristineCallOnce; | |
479 static PRCallOnceType setupBypassOnce; | |
480 | |
481 static SECStatus SSL_BypassShutdown(void* appData, void* nssData) | |
482 { | |
483 /* unload freeBL shared library from memory */ | |
484 BL_Unload(); | |
485 setupBypassOnce = pristineCallOnce; | |
486 return SECSuccess; | |
487 } | |
488 | |
489 static PRStatus SSL_BypassRegisterShutdown(void) | |
490 { | |
491 SECStatus rv = NSS_RegisterShutdown(SSL_BypassShutdown, NULL); | |
492 PORT_Assert(SECSuccess == rv); | |
493 return SECSuccess == rv ? PR_SUCCESS : PR_FAILURE; | |
494 } | |
495 #endif | |
496 | |
497 static PRStatus SSL_BypassSetup(void) | |
498 { | |
499 #ifdef NO_PKCS11_BYPASS | |
500 /* Guarantee binary compatibility */ | |
501 return PR_SUCCESS; | |
502 #else | |
503 return PR_CallOnce(&setupBypassOnce, &SSL_BypassRegisterShutdown); | |
504 #endif | |
505 } | |
506 | |
507 /* Implements the semantics for SSL_OptionSet(SSL_ENABLE_TLS, on) described in | |
508 * ssl.h in the section "SSL version range setting API". | |
509 */ | |
510 static void | |
511 ssl_EnableTLS(SSLVersionRange *vrange, PRBool on) | |
512 { | |
513 if (SSL3_ALL_VERSIONS_DISABLED(vrange)) { | |
514 if (on) { | |
515 vrange->min = SSL_LIBRARY_VERSION_TLS_1_0; | |
516 vrange->max = SSL_LIBRARY_VERSION_TLS_1_0; | |
517 } /* else don't change anything */ | |
518 return; | |
519 } | |
520 | |
521 if (on) { | |
522 /* Expand the range of enabled version to include TLS 1.0 */ | |
523 vrange->min = PR_MIN(vrange->min, SSL_LIBRARY_VERSION_TLS_1_0); | |
524 vrange->max = PR_MAX(vrange->max, SSL_LIBRARY_VERSION_TLS_1_0); | |
525 } else { | |
526 /* Disable all TLS versions, leaving only SSL 3.0 if it was enabled */ | |
527 if (vrange->min == SSL_LIBRARY_VERSION_3_0) { | |
528 vrange->max = SSL_LIBRARY_VERSION_3_0; | |
529 } else { | |
530 /* Only TLS was enabled, so now no versions are. */ | |
531 vrange->min = SSL_LIBRARY_VERSION_NONE; | |
532 vrange->max = SSL_LIBRARY_VERSION_NONE; | |
533 } | |
534 } | |
535 } | |
536 | |
537 /* Implements the semantics for SSL_OptionSet(SSL_ENABLE_SSL3, on) described in | |
538 * ssl.h in the section "SSL version range setting API". | |
539 */ | |
540 static void | |
541 ssl_EnableSSL3(SSLVersionRange *vrange, PRBool on) | |
542 { | |
543 if (SSL3_ALL_VERSIONS_DISABLED(vrange)) { | |
544 if (on) { | |
545 vrange->min = SSL_LIBRARY_VERSION_3_0; | |
546 vrange->max = SSL_LIBRARY_VERSION_3_0; | |
547 } /* else don't change anything */ | |
548 return; | |
549 } | |
550 | |
551 if (on) { | |
552 /* Expand the range of enabled versions to include SSL 3.0. We know | |
553 * SSL 3.0 or some version of TLS is already enabled at this point, so | |
554 * we don't need to change vrange->max. | |
555 */ | |
556 vrange->min = SSL_LIBRARY_VERSION_3_0; | |
557 } else { | |
558 /* Disable SSL 3.0, leaving TLS unaffected. */ | |
559 if (vrange->max > SSL_LIBRARY_VERSION_3_0) { | |
560 vrange->min = PR_MAX(vrange->min, SSL_LIBRARY_VERSION_TLS_1_0); | |
561 } else { | |
562 /* Only SSL 3.0 was enabled, so now no versions are. */ | |
563 vrange->min = SSL_LIBRARY_VERSION_NONE; | |
564 vrange->max = SSL_LIBRARY_VERSION_NONE; | |
565 } | |
566 } | |
567 } | |
568 | |
569 SECStatus | |
570 SSL_OptionSet(PRFileDesc *fd, PRInt32 which, PRBool on) | |
571 { | |
572 sslSocket *ss = ssl_FindSocket(fd); | |
573 SECStatus rv = SECSuccess; | |
574 PRBool holdingLocks; | |
575 | |
576 if (!ss) { | |
577 SSL_DBG(("%d: SSL[%d]: bad socket in Enable", SSL_GETPID(), fd)); | |
578 return SECFailure; | |
579 } | |
580 | |
581 holdingLocks = (!ss->opt.noLocks); | |
582 ssl_Get1stHandshakeLock(ss); | |
583 ssl_GetSSL3HandshakeLock(ss); | |
584 | |
585 switch (which) { | |
586 case SSL_SOCKS: | |
587 ss->opt.useSocks = PR_FALSE; | |
588 rv = PrepareSocket(ss); | |
589 if (on) { | |
590 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
591 rv = SECFailure; | |
592 } | |
593 break; | |
594 | |
595 case SSL_SECURITY: | |
596 ss->opt.useSecurity = on; | |
597 rv = PrepareSocket(ss); | |
598 break; | |
599 | |
600 case SSL_REQUEST_CERTIFICATE: | |
601 ss->opt.requestCertificate = on; | |
602 break; | |
603 | |
604 case SSL_REQUIRE_CERTIFICATE: | |
605 ss->opt.requireCertificate = on; | |
606 break; | |
607 | |
608 case SSL_HANDSHAKE_AS_CLIENT: | |
609 if ( ss->opt.handshakeAsServer && on ) { | |
610 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
611 rv = SECFailure; | |
612 break; | |
613 } | |
614 ss->opt.handshakeAsClient = on; | |
615 break; | |
616 | |
617 case SSL_HANDSHAKE_AS_SERVER: | |
618 if ( ss->opt.handshakeAsClient && on ) { | |
619 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
620 rv = SECFailure; | |
621 break; | |
622 } | |
623 ss->opt.handshakeAsServer = on; | |
624 break; | |
625 | |
626 case SSL_ENABLE_TLS: | |
627 if (IS_DTLS(ss)) { | |
628 if (on) { | |
629 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
630 rv = SECFailure; /* not allowed */ | |
631 } | |
632 break; | |
633 } | |
634 ssl_EnableTLS(&ss->vrange, on); | |
635 ss->preferredCipher = NULL; | |
636 if (ss->cipherSpecs) { | |
637 PORT_Free(ss->cipherSpecs); | |
638 ss->cipherSpecs = NULL; | |
639 ss->sizeCipherSpecs = 0; | |
640 } | |
641 break; | |
642 | |
643 case SSL_ENABLE_SSL3: | |
644 if (IS_DTLS(ss)) { | |
645 if (on) { | |
646 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
647 rv = SECFailure; /* not allowed */ | |
648 } | |
649 break; | |
650 } | |
651 ssl_EnableSSL3(&ss->vrange, on); | |
652 ss->preferredCipher = NULL; | |
653 if (ss->cipherSpecs) { | |
654 PORT_Free(ss->cipherSpecs); | |
655 ss->cipherSpecs = NULL; | |
656 ss->sizeCipherSpecs = 0; | |
657 } | |
658 break; | |
659 | |
660 case SSL_ENABLE_SSL2: | |
661 if (IS_DTLS(ss)) { | |
662 if (on) { | |
663 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
664 rv = SECFailure; /* not allowed */ | |
665 } | |
666 break; | |
667 } | |
668 ss->opt.enableSSL2 = on; | |
669 if (on) { | |
670 ss->opt.v2CompatibleHello = on; | |
671 } | |
672 ss->preferredCipher = NULL; | |
673 if (ss->cipherSpecs) { | |
674 PORT_Free(ss->cipherSpecs); | |
675 ss->cipherSpecs = NULL; | |
676 ss->sizeCipherSpecs = 0; | |
677 } | |
678 break; | |
679 | |
680 case SSL_NO_CACHE: | |
681 ss->opt.noCache = on; | |
682 break; | |
683 | |
684 case SSL_ENABLE_FDX: | |
685 if (on && ss->opt.noLocks) { | |
686 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
687 rv = SECFailure; | |
688 } | |
689 ss->opt.fdx = on; | |
690 break; | |
691 | |
692 case SSL_V2_COMPATIBLE_HELLO: | |
693 if (IS_DTLS(ss)) { | |
694 if (on) { | |
695 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
696 rv = SECFailure; /* not allowed */ | |
697 } | |
698 break; | |
699 } | |
700 ss->opt.v2CompatibleHello = on; | |
701 if (!on) { | |
702 ss->opt.enableSSL2 = on; | |
703 } | |
704 break; | |
705 | |
706 case SSL_ROLLBACK_DETECTION: | |
707 ss->opt.detectRollBack = on; | |
708 break; | |
709 | |
710 case SSL_NO_STEP_DOWN: | |
711 ss->opt.noStepDown = on; | |
712 if (on) | |
713 SSL_DisableExportCipherSuites(fd); | |
714 break; | |
715 | |
716 case SSL_BYPASS_PKCS11: | |
717 if (ss->handshakeBegun) { | |
718 PORT_SetError(PR_INVALID_STATE_ERROR); | |
719 rv = SECFailure; | |
720 } else { | |
721 if (PR_FALSE != on) { | |
722 if (PR_SUCCESS == SSL_BypassSetup() ) { | |
723 #ifdef NO_PKCS11_BYPASS | |
724 ss->opt.bypassPKCS11 = PR_FALSE; | |
725 #else | |
726 ss->opt.bypassPKCS11 = on; | |
727 #endif | |
728 } else { | |
729 rv = SECFailure; | |
730 } | |
731 } else { | |
732 ss->opt.bypassPKCS11 = PR_FALSE; | |
733 } | |
734 } | |
735 break; | |
736 | |
737 case SSL_NO_LOCKS: | |
738 if (on && ss->opt.fdx) { | |
739 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
740 rv = SECFailure; | |
741 } | |
742 if (on && ssl_force_locks) | |
743 on = PR_FALSE; /* silent override */ | |
744 ss->opt.noLocks = on; | |
745 if (on) { | |
746 locksEverDisabled = PR_TRUE; | |
747 strcpy(lockStatus + LOCKSTATUS_OFFSET, "DISABLED."); | |
748 } else if (!holdingLocks) { | |
749 rv = ssl_MakeLocks(ss); | |
750 if (rv != SECSuccess) { | |
751 ss->opt.noLocks = PR_TRUE; | |
752 } | |
753 } | |
754 break; | |
755 | |
756 case SSL_ENABLE_SESSION_TICKETS: | |
757 ss->opt.enableSessionTickets = on; | |
758 break; | |
759 | |
760 case SSL_ENABLE_DEFLATE: | |
761 ss->opt.enableDeflate = on; | |
762 break; | |
763 | |
764 case SSL_ENABLE_RENEGOTIATION: | |
765 ss->opt.enableRenegotiation = on; | |
766 break; | |
767 | |
768 case SSL_REQUIRE_SAFE_NEGOTIATION: | |
769 ss->opt.requireSafeNegotiation = on; | |
770 break; | |
771 | |
772 case SSL_ENABLE_FALSE_START: | |
773 ss->opt.enableFalseStart = on; | |
774 break; | |
775 | |
776 case SSL_CBC_RANDOM_IV: | |
777 ss->opt.cbcRandomIV = on; | |
778 break; | |
779 | |
780 case SSL_ENABLE_OCSP_STAPLING: | |
781 ss->opt.enableOCSPStapling = on; | |
782 break; | |
783 | |
784 case SSL_ENABLE_NPN: | |
785 ss->opt.enableNPN = on; | |
786 break; | |
787 | |
788 case SSL_ENABLE_ALPN: | |
789 ss->opt.enableALPN = on; | |
790 break; | |
791 | |
792 case SSL_ENABLE_SIGNED_CERT_TIMESTAMPS: | |
793 ss->opt.enableSignedCertTimestamps = on; | |
794 break; | |
795 | |
796 case SSL_ENABLE_FALLBACK_SCSV: | |
797 ss->opt.enableFallbackSCSV = on; | |
798 break; | |
799 | |
800 default: | |
801 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
802 rv = SECFailure; | |
803 } | |
804 | |
805 /* We can't use the macros for releasing the locks here, | |
806 * because ss->opt.noLocks might have changed just above. | |
807 * We must release these locks (monitors) here, if we aquired them above, | |
808 * regardless of the current value of ss->opt.noLocks. | |
809 */ | |
810 if (holdingLocks) { | |
811 PZ_ExitMonitor((ss)->ssl3HandshakeLock); | |
812 PZ_ExitMonitor((ss)->firstHandshakeLock); | |
813 } | |
814 | |
815 return rv; | |
816 } | |
817 | |
818 SECStatus | |
819 SSL_OptionGet(PRFileDesc *fd, PRInt32 which, PRBool *pOn) | |
820 { | |
821 sslSocket *ss = ssl_FindSocket(fd); | |
822 SECStatus rv = SECSuccess; | |
823 PRBool on = PR_FALSE; | |
824 | |
825 if (!pOn) { | |
826 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
827 return SECFailure; | |
828 } | |
829 if (!ss) { | |
830 SSL_DBG(("%d: SSL[%d]: bad socket in Enable", SSL_GETPID(), fd)); | |
831 *pOn = PR_FALSE; | |
832 return SECFailure; | |
833 } | |
834 | |
835 ssl_Get1stHandshakeLock(ss); | |
836 ssl_GetSSL3HandshakeLock(ss); | |
837 | |
838 switch (which) { | |
839 case SSL_SOCKS: on = PR_FALSE; break; | |
840 case SSL_SECURITY: on = ss->opt.useSecurity; break; | |
841 case SSL_REQUEST_CERTIFICATE: on = ss->opt.requestCertificate; break; | |
842 case SSL_REQUIRE_CERTIFICATE: on = ss->opt.requireCertificate; break; | |
843 case SSL_HANDSHAKE_AS_CLIENT: on = ss->opt.handshakeAsClient; break; | |
844 case SSL_HANDSHAKE_AS_SERVER: on = ss->opt.handshakeAsServer; break; | |
845 case SSL_ENABLE_TLS: | |
846 on = ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_0; | |
847 break; | |
848 case SSL_ENABLE_SSL3: | |
849 on = ss->vrange.min == SSL_LIBRARY_VERSION_3_0; | |
850 break; | |
851 case SSL_ENABLE_SSL2: on = ss->opt.enableSSL2; break; | |
852 case SSL_NO_CACHE: on = ss->opt.noCache; break; | |
853 case SSL_ENABLE_FDX: on = ss->opt.fdx; break; | |
854 case SSL_V2_COMPATIBLE_HELLO: on = ss->opt.v2CompatibleHello; break; | |
855 case SSL_ROLLBACK_DETECTION: on = ss->opt.detectRollBack; break; | |
856 case SSL_NO_STEP_DOWN: on = ss->opt.noStepDown; break; | |
857 case SSL_BYPASS_PKCS11: on = ss->opt.bypassPKCS11; break; | |
858 case SSL_NO_LOCKS: on = ss->opt.noLocks; break; | |
859 case SSL_ENABLE_SESSION_TICKETS: | |
860 on = ss->opt.enableSessionTickets; | |
861 break; | |
862 case SSL_ENABLE_DEFLATE: on = ss->opt.enableDeflate; break; | |
863 case SSL_ENABLE_RENEGOTIATION: | |
864 on = ss->opt.enableRenegotiation; break; | |
865 case SSL_REQUIRE_SAFE_NEGOTIATION: | |
866 on = ss->opt.requireSafeNegotiation; break; | |
867 case SSL_ENABLE_FALSE_START: on = ss->opt.enableFalseStart; break; | |
868 case SSL_CBC_RANDOM_IV: on = ss->opt.cbcRandomIV; break; | |
869 case SSL_ENABLE_OCSP_STAPLING: on = ss->opt.enableOCSPStapling; break; | |
870 case SSL_ENABLE_NPN: on = ss->opt.enableNPN; break; | |
871 case SSL_ENABLE_ALPN: on = ss->opt.enableALPN; break; | |
872 case SSL_ENABLE_SIGNED_CERT_TIMESTAMPS: | |
873 on = ss->opt.enableSignedCertTimestamps; | |
874 break; | |
875 case SSL_ENABLE_FALLBACK_SCSV: on = ss->opt.enableFallbackSCSV; break; | |
876 | |
877 default: | |
878 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
879 rv = SECFailure; | |
880 } | |
881 | |
882 ssl_ReleaseSSL3HandshakeLock(ss); | |
883 ssl_Release1stHandshakeLock(ss); | |
884 | |
885 *pOn = on; | |
886 return rv; | |
887 } | |
888 | |
889 SECStatus | |
890 SSL_OptionGetDefault(PRInt32 which, PRBool *pOn) | |
891 { | |
892 SECStatus rv = SECSuccess; | |
893 PRBool on = PR_FALSE; | |
894 | |
895 if (!pOn) { | |
896 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
897 return SECFailure; | |
898 } | |
899 | |
900 ssl_SetDefaultsFromEnvironment(); | |
901 | |
902 switch (which) { | |
903 case SSL_SOCKS: on = PR_FALSE; break; | |
904 case SSL_SECURITY: on = ssl_defaults.useSecurity; break; | |
905 case SSL_REQUEST_CERTIFICATE: on = ssl_defaults.requestCertificate; break; | |
906 case SSL_REQUIRE_CERTIFICATE: on = ssl_defaults.requireCertificate; break; | |
907 case SSL_HANDSHAKE_AS_CLIENT: on = ssl_defaults.handshakeAsClient; break; | |
908 case SSL_HANDSHAKE_AS_SERVER: on = ssl_defaults.handshakeAsServer; break; | |
909 case SSL_ENABLE_TLS: | |
910 on = versions_defaults_stream.max >= SSL_LIBRARY_VERSION_TLS_1_0; | |
911 break; | |
912 case SSL_ENABLE_SSL3: | |
913 on = versions_defaults_stream.min == SSL_LIBRARY_VERSION_3_0; | |
914 break; | |
915 case SSL_ENABLE_SSL2: on = ssl_defaults.enableSSL2; break; | |
916 case SSL_NO_CACHE: on = ssl_defaults.noCache; break; | |
917 case SSL_ENABLE_FDX: on = ssl_defaults.fdx; break; | |
918 case SSL_V2_COMPATIBLE_HELLO: on = ssl_defaults.v2CompatibleHello; break; | |
919 case SSL_ROLLBACK_DETECTION: on = ssl_defaults.detectRollBack; break; | |
920 case SSL_NO_STEP_DOWN: on = ssl_defaults.noStepDown; break; | |
921 case SSL_BYPASS_PKCS11: on = ssl_defaults.bypassPKCS11; break; | |
922 case SSL_NO_LOCKS: on = ssl_defaults.noLocks; break; | |
923 case SSL_ENABLE_SESSION_TICKETS: | |
924 on = ssl_defaults.enableSessionTickets; | |
925 break; | |
926 case SSL_ENABLE_DEFLATE: on = ssl_defaults.enableDeflate; break; | |
927 case SSL_ENABLE_RENEGOTIATION: | |
928 on = ssl_defaults.enableRenegotiation; break; | |
929 case SSL_REQUIRE_SAFE_NEGOTIATION: | |
930 on = ssl_defaults.requireSafeNegotiation; | |
931 break; | |
932 case SSL_ENABLE_FALSE_START: on = ssl_defaults.enableFalseStart; break; | |
933 case SSL_CBC_RANDOM_IV: on = ssl_defaults.cbcRandomIV; break; | |
934 case SSL_ENABLE_OCSP_STAPLING: | |
935 on = ssl_defaults.enableOCSPStapling; | |
936 break; | |
937 case SSL_ENABLE_NPN: on = ssl_defaults.enableNPN; break; | |
938 case SSL_ENABLE_ALPN: on = ssl_defaults.enableALPN; break; | |
939 case SSL_ENABLE_SIGNED_CERT_TIMESTAMPS: | |
940 on = ssl_defaults.enableSignedCertTimestamps; | |
941 break; | |
942 case SSL_ENABLE_FALLBACK_SCSV: | |
943 on = ssl_defaults.enableFallbackSCSV; | |
944 break; | |
945 | |
946 default: | |
947 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
948 rv = SECFailure; | |
949 } | |
950 | |
951 *pOn = on; | |
952 return rv; | |
953 } | |
954 | |
955 /* XXX Use Global Lock to protect this stuff. */ | |
956 SECStatus | |
957 SSL_EnableDefault(int which, PRBool on) | |
958 { | |
959 return SSL_OptionSetDefault(which, on); | |
960 } | |
961 | |
962 SECStatus | |
963 SSL_OptionSetDefault(PRInt32 which, PRBool on) | |
964 { | |
965 SECStatus status = ssl_Init(); | |
966 | |
967 if (status != SECSuccess) { | |
968 return status; | |
969 } | |
970 | |
971 ssl_SetDefaultsFromEnvironment(); | |
972 | |
973 switch (which) { | |
974 case SSL_SOCKS: | |
975 ssl_defaults.useSocks = PR_FALSE; | |
976 if (on) { | |
977 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
978 return SECFailure; | |
979 } | |
980 break; | |
981 | |
982 case SSL_SECURITY: | |
983 ssl_defaults.useSecurity = on; | |
984 break; | |
985 | |
986 case SSL_REQUEST_CERTIFICATE: | |
987 ssl_defaults.requestCertificate = on; | |
988 break; | |
989 | |
990 case SSL_REQUIRE_CERTIFICATE: | |
991 ssl_defaults.requireCertificate = on; | |
992 break; | |
993 | |
994 case SSL_HANDSHAKE_AS_CLIENT: | |
995 if ( ssl_defaults.handshakeAsServer && on ) { | |
996 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
997 return SECFailure; | |
998 } | |
999 ssl_defaults.handshakeAsClient = on; | |
1000 break; | |
1001 | |
1002 case SSL_HANDSHAKE_AS_SERVER: | |
1003 if ( ssl_defaults.handshakeAsClient && on ) { | |
1004 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1005 return SECFailure; | |
1006 } | |
1007 ssl_defaults.handshakeAsServer = on; | |
1008 break; | |
1009 | |
1010 case SSL_ENABLE_TLS: | |
1011 ssl_EnableTLS(&versions_defaults_stream, on); | |
1012 break; | |
1013 | |
1014 case SSL_ENABLE_SSL3: | |
1015 ssl_EnableSSL3(&versions_defaults_stream, on); | |
1016 break; | |
1017 | |
1018 case SSL_ENABLE_SSL2: | |
1019 ssl_defaults.enableSSL2 = on; | |
1020 if (on) { | |
1021 ssl_defaults.v2CompatibleHello = on; | |
1022 } | |
1023 break; | |
1024 | |
1025 case SSL_NO_CACHE: | |
1026 ssl_defaults.noCache = on; | |
1027 break; | |
1028 | |
1029 case SSL_ENABLE_FDX: | |
1030 if (on && ssl_defaults.noLocks) { | |
1031 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1032 return SECFailure; | |
1033 } | |
1034 ssl_defaults.fdx = on; | |
1035 break; | |
1036 | |
1037 case SSL_V2_COMPATIBLE_HELLO: | |
1038 ssl_defaults.v2CompatibleHello = on; | |
1039 if (!on) { | |
1040 ssl_defaults.enableSSL2 = on; | |
1041 } | |
1042 break; | |
1043 | |
1044 case SSL_ROLLBACK_DETECTION: | |
1045 ssl_defaults.detectRollBack = on; | |
1046 break; | |
1047 | |
1048 case SSL_NO_STEP_DOWN: | |
1049 ssl_defaults.noStepDown = on; | |
1050 if (on) | |
1051 SSL_DisableDefaultExportCipherSuites(); | |
1052 break; | |
1053 | |
1054 case SSL_BYPASS_PKCS11: | |
1055 if (PR_FALSE != on) { | |
1056 if (PR_SUCCESS == SSL_BypassSetup()) { | |
1057 #ifdef NO_PKCS11_BYPASS | |
1058 ssl_defaults.bypassPKCS11 = PR_FALSE; | |
1059 #else | |
1060 ssl_defaults.bypassPKCS11 = on; | |
1061 #endif | |
1062 } else { | |
1063 return SECFailure; | |
1064 } | |
1065 } else { | |
1066 ssl_defaults.bypassPKCS11 = PR_FALSE; | |
1067 } | |
1068 break; | |
1069 | |
1070 case SSL_NO_LOCKS: | |
1071 if (on && ssl_defaults.fdx) { | |
1072 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1073 return SECFailure; | |
1074 } | |
1075 if (on && ssl_force_locks) | |
1076 on = PR_FALSE; /* silent override */ | |
1077 ssl_defaults.noLocks = on; | |
1078 if (on) { | |
1079 locksEverDisabled = PR_TRUE; | |
1080 strcpy(lockStatus + LOCKSTATUS_OFFSET, "DISABLED."); | |
1081 } | |
1082 break; | |
1083 | |
1084 case SSL_ENABLE_SESSION_TICKETS: | |
1085 ssl_defaults.enableSessionTickets = on; | |
1086 break; | |
1087 | |
1088 case SSL_ENABLE_DEFLATE: | |
1089 ssl_defaults.enableDeflate = on; | |
1090 break; | |
1091 | |
1092 case SSL_ENABLE_RENEGOTIATION: | |
1093 ssl_defaults.enableRenegotiation = on; | |
1094 break; | |
1095 | |
1096 case SSL_REQUIRE_SAFE_NEGOTIATION: | |
1097 ssl_defaults.requireSafeNegotiation = on; | |
1098 break; | |
1099 | |
1100 case SSL_ENABLE_FALSE_START: | |
1101 ssl_defaults.enableFalseStart = on; | |
1102 break; | |
1103 | |
1104 case SSL_CBC_RANDOM_IV: | |
1105 ssl_defaults.cbcRandomIV = on; | |
1106 break; | |
1107 | |
1108 case SSL_ENABLE_OCSP_STAPLING: | |
1109 ssl_defaults.enableOCSPStapling = on; | |
1110 break; | |
1111 | |
1112 case SSL_ENABLE_NPN: | |
1113 ssl_defaults.enableNPN = on; | |
1114 break; | |
1115 | |
1116 case SSL_ENABLE_ALPN: | |
1117 ssl_defaults.enableALPN = on; | |
1118 break; | |
1119 | |
1120 case SSL_ENABLE_SIGNED_CERT_TIMESTAMPS: | |
1121 ssl_defaults.enableSignedCertTimestamps = on; | |
1122 break; | |
1123 | |
1124 case SSL_ENABLE_FALLBACK_SCSV: | |
1125 ssl_defaults.enableFallbackSCSV = on; | |
1126 break; | |
1127 | |
1128 default: | |
1129 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1130 return SECFailure; | |
1131 } | |
1132 return SECSuccess; | |
1133 } | |
1134 | |
1135 /* function tells us if the cipher suite is one that we no longer support. */ | |
1136 static PRBool | |
1137 ssl_IsRemovedCipherSuite(PRInt32 suite) | |
1138 { | |
1139 switch (suite) { | |
1140 case SSL_FORTEZZA_DMS_WITH_NULL_SHA: | |
1141 case SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA: | |
1142 case SSL_FORTEZZA_DMS_WITH_RC4_128_SHA: | |
1143 return PR_TRUE; | |
1144 default: | |
1145 return PR_FALSE; | |
1146 } | |
1147 } | |
1148 | |
1149 /* Part of the public NSS API. | |
1150 * Since this is a global (not per-socket) setting, we cannot use the | |
1151 * HandshakeLock to protect this. Probably want a global lock. | |
1152 */ | |
1153 SECStatus | |
1154 SSL_SetPolicy(long which, int policy) | |
1155 { | |
1156 if ((which & 0xfffe) == SSL_RSA_OLDFIPS_WITH_3DES_EDE_CBC_SHA) { | |
1157 /* one of the two old FIPS ciphers */ | |
1158 if (which == SSL_RSA_OLDFIPS_WITH_3DES_EDE_CBC_SHA) | |
1159 which = SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA; | |
1160 else if (which == SSL_RSA_OLDFIPS_WITH_DES_CBC_SHA) | |
1161 which = SSL_RSA_FIPS_WITH_DES_CBC_SHA; | |
1162 } | |
1163 if (ssl_IsRemovedCipherSuite(which)) | |
1164 return SECSuccess; | |
1165 return SSL_CipherPolicySet(which, policy); | |
1166 } | |
1167 | |
1168 SECStatus | |
1169 SSL_CipherPolicySet(PRInt32 which, PRInt32 policy) | |
1170 { | |
1171 SECStatus rv = ssl_Init(); | |
1172 | |
1173 if (rv != SECSuccess) { | |
1174 return rv; | |
1175 } | |
1176 | |
1177 if (ssl_IsRemovedCipherSuite(which)) { | |
1178 rv = SECSuccess; | |
1179 } else if (SSL_IS_SSL2_CIPHER(which)) { | |
1180 rv = ssl2_SetPolicy(which, policy); | |
1181 } else { | |
1182 rv = ssl3_SetPolicy((ssl3CipherSuite)which, policy); | |
1183 } | |
1184 return rv; | |
1185 } | |
1186 | |
1187 SECStatus | |
1188 SSL_CipherPolicyGet(PRInt32 which, PRInt32 *oPolicy) | |
1189 { | |
1190 SECStatus rv; | |
1191 | |
1192 if (!oPolicy) { | |
1193 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1194 return SECFailure; | |
1195 } | |
1196 if (ssl_IsRemovedCipherSuite(which)) { | |
1197 *oPolicy = SSL_NOT_ALLOWED; | |
1198 rv = SECSuccess; | |
1199 } else if (SSL_IS_SSL2_CIPHER(which)) { | |
1200 rv = ssl2_GetPolicy(which, oPolicy); | |
1201 } else { | |
1202 rv = ssl3_GetPolicy((ssl3CipherSuite)which, oPolicy); | |
1203 } | |
1204 return rv; | |
1205 } | |
1206 | |
1207 /* Part of the public NSS API. | |
1208 * Since this is a global (not per-socket) setting, we cannot use the | |
1209 * HandshakeLock to protect this. Probably want a global lock. | |
1210 * These changes have no effect on any sslSockets already created. | |
1211 */ | |
1212 SECStatus | |
1213 SSL_EnableCipher(long which, PRBool enabled) | |
1214 { | |
1215 if ((which & 0xfffe) == SSL_RSA_OLDFIPS_WITH_3DES_EDE_CBC_SHA) { | |
1216 /* one of the two old FIPS ciphers */ | |
1217 if (which == SSL_RSA_OLDFIPS_WITH_3DES_EDE_CBC_SHA) | |
1218 which = SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA; | |
1219 else if (which == SSL_RSA_OLDFIPS_WITH_DES_CBC_SHA) | |
1220 which = SSL_RSA_FIPS_WITH_DES_CBC_SHA; | |
1221 } | |
1222 if (ssl_IsRemovedCipherSuite(which)) | |
1223 return SECSuccess; | |
1224 return SSL_CipherPrefSetDefault(which, enabled); | |
1225 } | |
1226 | |
1227 SECStatus | |
1228 SSL_CipherPrefSetDefault(PRInt32 which, PRBool enabled) | |
1229 { | |
1230 SECStatus rv = ssl_Init(); | |
1231 | |
1232 if (rv != SECSuccess) { | |
1233 return rv; | |
1234 } | |
1235 | |
1236 if (ssl_IsRemovedCipherSuite(which)) | |
1237 return SECSuccess; | |
1238 if (enabled && ssl_defaults.noStepDown && SSL_IsExportCipherSuite(which)) { | |
1239 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
1240 return SECFailure; | |
1241 } | |
1242 if (SSL_IS_SSL2_CIPHER(which)) { | |
1243 rv = ssl2_CipherPrefSetDefault(which, enabled); | |
1244 } else { | |
1245 rv = ssl3_CipherPrefSetDefault((ssl3CipherSuite)which, enabled); | |
1246 } | |
1247 return rv; | |
1248 } | |
1249 | |
1250 SECStatus | |
1251 SSL_CipherPrefGetDefault(PRInt32 which, PRBool *enabled) | |
1252 { | |
1253 SECStatus rv; | |
1254 | |
1255 if (!enabled) { | |
1256 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1257 return SECFailure; | |
1258 } | |
1259 if (ssl_IsRemovedCipherSuite(which)) { | |
1260 *enabled = PR_FALSE; | |
1261 rv = SECSuccess; | |
1262 } else if (SSL_IS_SSL2_CIPHER(which)) { | |
1263 rv = ssl2_CipherPrefGetDefault(which, enabled); | |
1264 } else { | |
1265 rv = ssl3_CipherPrefGetDefault((ssl3CipherSuite)which, enabled); | |
1266 } | |
1267 return rv; | |
1268 } | |
1269 | |
1270 SECStatus | |
1271 SSL_CipherPrefSet(PRFileDesc *fd, PRInt32 which, PRBool enabled) | |
1272 { | |
1273 SECStatus rv; | |
1274 sslSocket *ss = ssl_FindSocket(fd); | |
1275 | |
1276 if (!ss) { | |
1277 SSL_DBG(("%d: SSL[%d]: bad socket in CipherPrefSet", SSL_GETPID(), fd)); | |
1278 return SECFailure; | |
1279 } | |
1280 if (ssl_IsRemovedCipherSuite(which)) | |
1281 return SECSuccess; | |
1282 if (enabled && ss->opt.noStepDown && SSL_IsExportCipherSuite(which)) { | |
1283 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
1284 return SECFailure; | |
1285 } | |
1286 if (SSL_IS_SSL2_CIPHER(which)) { | |
1287 rv = ssl2_CipherPrefSet(ss, which, enabled); | |
1288 } else { | |
1289 rv = ssl3_CipherPrefSet(ss, (ssl3CipherSuite)which, enabled); | |
1290 } | |
1291 return rv; | |
1292 } | |
1293 | |
1294 SECStatus | |
1295 SSL_CipherOrderSet(PRFileDesc *fd, const PRUint16 *ciphers, unsigned int len) | |
1296 { | |
1297 sslSocket *ss = ssl_FindSocket(fd); | |
1298 | |
1299 if (!ss) { | |
1300 SSL_DBG(("%d: SSL[%d]: bad socket in CipherOrderSet", SSL_GETPID(), | |
1301 fd)); | |
1302 return SECFailure; | |
1303 } | |
1304 return ssl3_CipherOrderSet(ss, ciphers, len); | |
1305 } | |
1306 | |
1307 SECStatus | |
1308 SSL_CipherPrefGet(PRFileDesc *fd, PRInt32 which, PRBool *enabled) | |
1309 { | |
1310 SECStatus rv; | |
1311 sslSocket *ss = ssl_FindSocket(fd); | |
1312 | |
1313 if (!enabled) { | |
1314 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1315 return SECFailure; | |
1316 } | |
1317 if (!ss) { | |
1318 SSL_DBG(("%d: SSL[%d]: bad socket in CipherPrefGet", SSL_GETPID(), fd)); | |
1319 *enabled = PR_FALSE; | |
1320 return SECFailure; | |
1321 } | |
1322 if (ssl_IsRemovedCipherSuite(which)) { | |
1323 *enabled = PR_FALSE; | |
1324 rv = SECSuccess; | |
1325 } else if (SSL_IS_SSL2_CIPHER(which)) { | |
1326 rv = ssl2_CipherPrefGet(ss, which, enabled); | |
1327 } else { | |
1328 rv = ssl3_CipherPrefGet(ss, (ssl3CipherSuite)which, enabled); | |
1329 } | |
1330 return rv; | |
1331 } | |
1332 | |
1333 SECStatus | |
1334 NSS_SetDomesticPolicy(void) | |
1335 { | |
1336 SECStatus status = SECSuccess; | |
1337 const PRUint16 *cipher; | |
1338 | |
1339 for (cipher = SSL_ImplementedCiphers; *cipher != 0; ++cipher) { | |
1340 status = SSL_SetPolicy(*cipher, SSL_ALLOWED); | |
1341 if (status != SECSuccess) | |
1342 break; | |
1343 } | |
1344 return status; | |
1345 } | |
1346 | |
1347 SECStatus | |
1348 NSS_SetExportPolicy(void) | |
1349 { | |
1350 return NSS_SetDomesticPolicy(); | |
1351 } | |
1352 | |
1353 SECStatus | |
1354 NSS_SetFrancePolicy(void) | |
1355 { | |
1356 return NSS_SetDomesticPolicy(); | |
1357 } | |
1358 | |
1359 SECStatus | |
1360 SSL_GetChannelBinding(PRFileDesc *fd, | |
1361 SSLChannelBindingType binding_type, | |
1362 unsigned char *out, | |
1363 unsigned int *outLen, | |
1364 unsigned int outLenMax) { | |
1365 sslSocket *ss = ssl_FindSocket(fd); | |
1366 | |
1367 if (!ss) { | |
1368 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_GetChannelBinding", | |
1369 SSL_GETPID(), fd)); | |
1370 return SECFailure; | |
1371 } | |
1372 | |
1373 if (binding_type != SSL_CHANNEL_BINDING_TLS_UNIQUE) { | |
1374 PORT_SetError(PR_INVALID_ARGUMENT_ERROR); | |
1375 return SECFailure; | |
1376 } | |
1377 | |
1378 return ssl3_GetTLSUniqueChannelBinding(ss, out, outLen, outLenMax); | |
1379 } | |
1380 | |
1381 | |
1382 /* LOCKS ??? XXX */ | |
1383 static PRFileDesc * | |
1384 ssl_ImportFD(PRFileDesc *model, PRFileDesc *fd, SSLProtocolVariant variant) | |
1385 { | |
1386 sslSocket * ns = NULL; | |
1387 PRStatus rv; | |
1388 PRNetAddr addr; | |
1389 SECStatus status = ssl_Init(); | |
1390 | |
1391 if (status != SECSuccess) { | |
1392 return NULL; | |
1393 } | |
1394 | |
1395 if (model == NULL) { | |
1396 /* Just create a default socket if we're given NULL for the model */ | |
1397 ns = ssl_NewSocket((PRBool)(!ssl_defaults.noLocks), variant); | |
1398 } else { | |
1399 sslSocket * ss = ssl_FindSocket(model); | |
1400 if (ss == NULL || ss->protocolVariant != variant) { | |
1401 SSL_DBG(("%d: SSL[%d]: bad model socket in ssl_ImportFD", | |
1402 SSL_GETPID(), model)); | |
1403 return NULL; | |
1404 } | |
1405 ns = ssl_DupSocket(ss); | |
1406 } | |
1407 if (ns == NULL) | |
1408 return NULL; | |
1409 | |
1410 rv = ssl_PushIOLayer(ns, fd, PR_TOP_IO_LAYER); | |
1411 if (rv != PR_SUCCESS) { | |
1412 ssl_FreeSocket(ns); | |
1413 SET_ERROR_CODE | |
1414 return NULL; | |
1415 } | |
1416 ns = ssl_FindSocket(fd); | |
1417 PORT_Assert(ns); | |
1418 if (ns) | |
1419 ns->TCPconnected = (PR_SUCCESS == ssl_DefGetpeername(ns, &addr)); | |
1420 return fd; | |
1421 } | |
1422 | |
1423 PRFileDesc * | |
1424 SSL_ImportFD(PRFileDesc *model, PRFileDesc *fd) | |
1425 { | |
1426 return ssl_ImportFD(model, fd, ssl_variant_stream); | |
1427 } | |
1428 | |
1429 PRFileDesc * | |
1430 DTLS_ImportFD(PRFileDesc *model, PRFileDesc *fd) | |
1431 { | |
1432 return ssl_ImportFD(model, fd, ssl_variant_datagram); | |
1433 } | |
1434 | |
1435 /* SSL_SetNextProtoCallback is used to select an application protocol | |
1436 * for ALPN and NPN. For ALPN, this runs on the server; for NPN it | |
1437 * runs on the client. */ | |
1438 /* Note: The ALPN version doesn't allow for the use of a default, setting a | |
1439 * status of SSL_NEXT_PROTO_NO_OVERLAP is treated as a failure. */ | |
1440 SECStatus | |
1441 SSL_SetNextProtoCallback(PRFileDesc *fd, SSLNextProtoCallback callback, | |
1442 void *arg) | |
1443 { | |
1444 sslSocket *ss = ssl_FindSocket(fd); | |
1445 | |
1446 if (!ss) { | |
1447 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetNextProtoCallback", SSL_GETP
ID(), | |
1448 fd)); | |
1449 return SECFailure; | |
1450 } | |
1451 | |
1452 ssl_GetSSL3HandshakeLock(ss); | |
1453 ss->nextProtoCallback = callback; | |
1454 ss->nextProtoArg = arg; | |
1455 ssl_ReleaseSSL3HandshakeLock(ss); | |
1456 | |
1457 return SECSuccess; | |
1458 } | |
1459 | |
1460 /* ssl_NextProtoNegoCallback is set as an ALPN/NPN callback when | |
1461 * SSL_SetNextProtoNego is used. | |
1462 */ | |
1463 static SECStatus | |
1464 ssl_NextProtoNegoCallback(void *arg, PRFileDesc *fd, | |
1465 const unsigned char *protos, unsigned int protos_len, | |
1466 unsigned char *protoOut, unsigned int *protoOutLen, | |
1467 unsigned int protoMaxLen) | |
1468 { | |
1469 unsigned int i, j; | |
1470 const unsigned char *result; | |
1471 sslSocket *ss = ssl_FindSocket(fd); | |
1472 | |
1473 if (!ss) { | |
1474 SSL_DBG(("%d: SSL[%d]: bad socket in ssl_NextProtoNegoCallback", | |
1475 SSL_GETPID(), fd)); | |
1476 return SECFailure; | |
1477 } | |
1478 | |
1479 /* For each protocol in server preference, see if we support it. */ | |
1480 for (i = 0; i < protos_len; ) { | |
1481 for (j = 0; j < ss->opt.nextProtoNego.len; ) { | |
1482 if (protos[i] == ss->opt.nextProtoNego.data[j] && | |
1483 PORT_Memcmp(&protos[i+1], &ss->opt.nextProtoNego.data[j+1], | |
1484 protos[i]) == 0) { | |
1485 /* We found a match. */ | |
1486 ss->ssl3.nextProtoState = SSL_NEXT_PROTO_NEGOTIATED; | |
1487 result = &protos[i]; | |
1488 goto found; | |
1489 } | |
1490 j += 1 + (unsigned int)ss->opt.nextProtoNego.data[j]; | |
1491 } | |
1492 i += 1 + (unsigned int)protos[i]; | |
1493 } | |
1494 | |
1495 /* The other side supports the extension, and either doesn't have any | |
1496 * protocols configured, or none of its options match ours. In this case we | |
1497 * request our favoured protocol. */ | |
1498 /* This will be treated as a failure for ALPN. */ | |
1499 ss->ssl3.nextProtoState = SSL_NEXT_PROTO_NO_OVERLAP; | |
1500 result = ss->opt.nextProtoNego.data; | |
1501 | |
1502 found: | |
1503 if (protoMaxLen < result[0]) { | |
1504 PORT_SetError(SEC_ERROR_OUTPUT_LEN); | |
1505 return SECFailure; | |
1506 } | |
1507 memcpy(protoOut, result + 1, result[0]); | |
1508 *protoOutLen = result[0]; | |
1509 return SECSuccess; | |
1510 } | |
1511 | |
1512 SECStatus | |
1513 SSL_SetNextProtoNego(PRFileDesc *fd, const unsigned char *data, | |
1514 unsigned int length) | |
1515 { | |
1516 sslSocket *ss; | |
1517 SECStatus rv; | |
1518 SECItem dataItem = { siBuffer, (unsigned char *) data, length }; | |
1519 | |
1520 ss = ssl_FindSocket(fd); | |
1521 if (!ss) { | |
1522 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetNextProtoNego", | |
1523 SSL_GETPID(), fd)); | |
1524 return SECFailure; | |
1525 } | |
1526 | |
1527 if (ssl3_ValidateNextProtoNego(data, length) != SECSuccess) | |
1528 return SECFailure; | |
1529 | |
1530 ssl_GetSSL3HandshakeLock(ss); | |
1531 SECITEM_FreeItem(&ss->opt.nextProtoNego, PR_FALSE); | |
1532 rv = SECITEM_CopyItem(NULL, &ss->opt.nextProtoNego, &dataItem); | |
1533 ssl_ReleaseSSL3HandshakeLock(ss); | |
1534 | |
1535 if (rv != SECSuccess) | |
1536 return rv; | |
1537 | |
1538 return SSL_SetNextProtoCallback(fd, ssl_NextProtoNegoCallback, NULL); | |
1539 } | |
1540 | |
1541 SECStatus | |
1542 SSL_GetNextProto(PRFileDesc *fd, SSLNextProtoState *state, unsigned char *buf, | |
1543 unsigned int *bufLen, unsigned int bufLenMax) | |
1544 { | |
1545 sslSocket *ss = ssl_FindSocket(fd); | |
1546 | |
1547 if (!ss) { | |
1548 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_GetNextProto", SSL_GETPID(), | |
1549 fd)); | |
1550 return SECFailure; | |
1551 } | |
1552 | |
1553 if (!state || !buf || !bufLen) { | |
1554 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1555 return SECFailure; | |
1556 } | |
1557 | |
1558 *state = ss->ssl3.nextProtoState; | |
1559 | |
1560 if (ss->ssl3.nextProtoState != SSL_NEXT_PROTO_NO_SUPPORT && | |
1561 ss->ssl3.nextProto.data) { | |
1562 if (ss->ssl3.nextProto.len > bufLenMax) { | |
1563 PORT_SetError(SEC_ERROR_OUTPUT_LEN); | |
1564 return SECFailure; | |
1565 } | |
1566 PORT_Memcpy(buf, ss->ssl3.nextProto.data, ss->ssl3.nextProto.len); | |
1567 *bufLen = ss->ssl3.nextProto.len; | |
1568 } else { | |
1569 *bufLen = 0; | |
1570 } | |
1571 | |
1572 return SECSuccess; | |
1573 } | |
1574 | |
1575 SECStatus SSL_SetSRTPCiphers(PRFileDesc *fd, | |
1576 const PRUint16 *ciphers, | |
1577 unsigned int numCiphers) | |
1578 { | |
1579 sslSocket *ss; | |
1580 unsigned int i; | |
1581 | |
1582 ss = ssl_FindSocket(fd); | |
1583 if (!ss || !IS_DTLS(ss)) { | |
1584 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetSRTPCiphers", | |
1585 SSL_GETPID(), fd)); | |
1586 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1587 return SECFailure; | |
1588 } | |
1589 | |
1590 if (numCiphers > MAX_DTLS_SRTP_CIPHER_SUITES) { | |
1591 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1592 return SECFailure; | |
1593 } | |
1594 | |
1595 ss->ssl3.dtlsSRTPCipherCount = 0; | |
1596 for (i = 0; i < numCiphers; i++) { | |
1597 const PRUint16 *srtpCipher = srtpCiphers; | |
1598 | |
1599 while (*srtpCipher) { | |
1600 if (ciphers[i] == *srtpCipher) | |
1601 break; | |
1602 srtpCipher++; | |
1603 } | |
1604 if (*srtpCipher) { | |
1605 ss->ssl3.dtlsSRTPCiphers[ss->ssl3.dtlsSRTPCipherCount++] = | |
1606 ciphers[i]; | |
1607 } else { | |
1608 SSL_DBG(("%d: SSL[%d]: invalid or unimplemented SRTP cipher " | |
1609 "suite specified: 0x%04hx", SSL_GETPID(), fd, | |
1610 ciphers[i])); | |
1611 } | |
1612 } | |
1613 | |
1614 if (ss->ssl3.dtlsSRTPCipherCount == 0) { | |
1615 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1616 return SECFailure; | |
1617 } | |
1618 | |
1619 return SECSuccess; | |
1620 } | |
1621 | |
1622 SECStatus | |
1623 SSL_GetSRTPCipher(PRFileDesc *fd, PRUint16 *cipher) | |
1624 { | |
1625 sslSocket * ss; | |
1626 | |
1627 ss = ssl_FindSocket(fd); | |
1628 if (!ss) { | |
1629 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_GetSRTPCipher", | |
1630 SSL_GETPID(), fd)); | |
1631 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1632 return SECFailure; | |
1633 } | |
1634 | |
1635 if (!ss->ssl3.dtlsSRTPCipherSuite) { | |
1636 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1637 return SECFailure; | |
1638 } | |
1639 | |
1640 *cipher = ss->ssl3.dtlsSRTPCipherSuite; | |
1641 return SECSuccess; | |
1642 } | |
1643 | |
1644 PRFileDesc * | |
1645 SSL_ReconfigFD(PRFileDesc *model, PRFileDesc *fd) | |
1646 { | |
1647 sslSocket * sm = NULL, *ss = NULL; | |
1648 int i; | |
1649 sslServerCerts * mc = NULL; | |
1650 sslServerCerts * sc = NULL; | |
1651 | |
1652 if (model == NULL) { | |
1653 PR_SetError(SEC_ERROR_INVALID_ARGS, 0); | |
1654 return NULL; | |
1655 } | |
1656 sm = ssl_FindSocket(model); | |
1657 if (sm == NULL) { | |
1658 SSL_DBG(("%d: SSL[%d]: bad model socket in ssl_ReconfigFD", | |
1659 SSL_GETPID(), model)); | |
1660 return NULL; | |
1661 } | |
1662 ss = ssl_FindSocket(fd); | |
1663 PORT_Assert(ss); | |
1664 if (ss == NULL) { | |
1665 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1666 return NULL; | |
1667 } | |
1668 | |
1669 ss->opt = sm->opt; | |
1670 ss->vrange = sm->vrange; | |
1671 PORT_Memcpy(ss->cipherSuites, sm->cipherSuites, sizeof sm->cipherSuites); | |
1672 PORT_Memcpy(ss->ssl3.dtlsSRTPCiphers, sm->ssl3.dtlsSRTPCiphers, | |
1673 sizeof(PRUint16) * sm->ssl3.dtlsSRTPCipherCount); | |
1674 ss->ssl3.dtlsSRTPCipherCount = sm->ssl3.dtlsSRTPCipherCount; | |
1675 | |
1676 if (!ss->opt.useSecurity) { | |
1677 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1678 return NULL; | |
1679 } | |
1680 /* This int should be SSLKEAType, but CC on Irix complains, | |
1681 * during the for loop. | |
1682 */ | |
1683 for (i=kt_null; i < kt_kea_size; i++) { | |
1684 mc = &(sm->serverCerts[i]); | |
1685 sc = &(ss->serverCerts[i]); | |
1686 if (mc->serverCert && mc->serverCertChain) { | |
1687 if (sc->serverCert) { | |
1688 CERT_DestroyCertificate(sc->serverCert); | |
1689 } | |
1690 sc->serverCert = CERT_DupCertificate(mc->serverCert); | |
1691 if (sc->serverCertChain) { | |
1692 CERT_DestroyCertificateList(sc->serverCertChain); | |
1693 } | |
1694 sc->serverCertChain = CERT_DupCertList(mc->serverCertChain); | |
1695 if (!sc->serverCertChain) | |
1696 goto loser; | |
1697 if (sm->certStatusArray[i]) { | |
1698 if (ss->certStatusArray[i]) { | |
1699 SECITEM_FreeArray(ss->certStatusArray[i], PR_TRUE); | |
1700 ss->certStatusArray[i] = NULL; | |
1701 } | |
1702 ss->certStatusArray[i] = SECITEM_DupArray(NULL, sm->certStatusAr
ray[i]); | |
1703 if (!ss->certStatusArray[i]) | |
1704 goto loser; | |
1705 } | |
1706 } | |
1707 if (mc->serverKeyPair) { | |
1708 if (sc->serverKeyPair) { | |
1709 ssl3_FreeKeyPair(sc->serverKeyPair); | |
1710 } | |
1711 sc->serverKeyPair = ssl3_GetKeyPairRef(mc->serverKeyPair); | |
1712 sc->serverKeyBits = mc->serverKeyBits; | |
1713 } | |
1714 } | |
1715 if (sm->stepDownKeyPair) { | |
1716 if (ss->stepDownKeyPair) { | |
1717 ssl3_FreeKeyPair(ss->stepDownKeyPair); | |
1718 } | |
1719 ss->stepDownKeyPair = ssl3_GetKeyPairRef(sm->stepDownKeyPair); | |
1720 } | |
1721 if (sm->ephemeralECDHKeyPair) { | |
1722 if (ss->ephemeralECDHKeyPair) { | |
1723 ssl3_FreeKeyPair(ss->ephemeralECDHKeyPair); | |
1724 } | |
1725 ss->ephemeralECDHKeyPair = | |
1726 ssl3_GetKeyPairRef(sm->ephemeralECDHKeyPair); | |
1727 } | |
1728 /* copy trust anchor names */ | |
1729 if (sm->ssl3.ca_list) { | |
1730 if (ss->ssl3.ca_list) { | |
1731 CERT_FreeDistNames(ss->ssl3.ca_list); | |
1732 } | |
1733 ss->ssl3.ca_list = CERT_DupDistNames(sm->ssl3.ca_list); | |
1734 if (!ss->ssl3.ca_list) { | |
1735 goto loser; | |
1736 } | |
1737 } | |
1738 | |
1739 if (sm->authCertificate) | |
1740 ss->authCertificate = sm->authCertificate; | |
1741 if (sm->authCertificateArg) | |
1742 ss->authCertificateArg = sm->authCertificateArg; | |
1743 if (sm->getClientAuthData) | |
1744 ss->getClientAuthData = sm->getClientAuthData; | |
1745 if (sm->getClientAuthDataArg) | |
1746 ss->getClientAuthDataArg = sm->getClientAuthDataArg; | |
1747 #ifdef NSS_PLATFORM_CLIENT_AUTH | |
1748 if (sm->getPlatformClientAuthData) | |
1749 ss->getPlatformClientAuthData = sm->getPlatformClientAuthData; | |
1750 if (sm->getPlatformClientAuthDataArg) | |
1751 ss->getPlatformClientAuthDataArg = sm->getPlatformClientAuthDataArg; | |
1752 #endif | |
1753 if (sm->sniSocketConfig) | |
1754 ss->sniSocketConfig = sm->sniSocketConfig; | |
1755 if (sm->sniSocketConfigArg) | |
1756 ss->sniSocketConfigArg = sm->sniSocketConfigArg; | |
1757 if (sm->handleBadCert) | |
1758 ss->handleBadCert = sm->handleBadCert; | |
1759 if (sm->badCertArg) | |
1760 ss->badCertArg = sm->badCertArg; | |
1761 if (sm->handshakeCallback) | |
1762 ss->handshakeCallback = sm->handshakeCallback; | |
1763 if (sm->handshakeCallbackData) | |
1764 ss->handshakeCallbackData = sm->handshakeCallbackData; | |
1765 if (sm->pkcs11PinArg) | |
1766 ss->pkcs11PinArg = sm->pkcs11PinArg; | |
1767 if (sm->getChannelID) | |
1768 ss->getChannelID = sm->getChannelID; | |
1769 if (sm->getChannelIDArg) | |
1770 ss->getChannelIDArg = sm->getChannelIDArg; | |
1771 return fd; | |
1772 loser: | |
1773 return NULL; | |
1774 } | |
1775 | |
1776 PRBool | |
1777 ssl3_VersionIsSupported(SSLProtocolVariant protocolVariant, | |
1778 SSL3ProtocolVersion version) | |
1779 { | |
1780 switch (protocolVariant) { | |
1781 case ssl_variant_stream: | |
1782 return (version >= SSL_LIBRARY_VERSION_3_0 && | |
1783 version <= SSL_LIBRARY_VERSION_MAX_SUPPORTED); | |
1784 case ssl_variant_datagram: | |
1785 return (version >= SSL_LIBRARY_VERSION_TLS_1_1 && | |
1786 version <= SSL_LIBRARY_VERSION_MAX_SUPPORTED); | |
1787 default: | |
1788 /* Can't get here */ | |
1789 PORT_Assert(PR_FALSE); | |
1790 return PR_FALSE; | |
1791 } | |
1792 } | |
1793 | |
1794 /* Returns PR_TRUE if the given version range is valid and | |
1795 ** fully supported; otherwise, returns PR_FALSE. | |
1796 */ | |
1797 static PRBool | |
1798 ssl3_VersionRangeIsValid(SSLProtocolVariant protocolVariant, | |
1799 const SSLVersionRange *vrange) | |
1800 { | |
1801 return vrange && | |
1802 vrange->min <= vrange->max && | |
1803 ssl3_VersionIsSupported(protocolVariant, vrange->min) && | |
1804 ssl3_VersionIsSupported(protocolVariant, vrange->max); | |
1805 } | |
1806 | |
1807 SECStatus | |
1808 SSL_VersionRangeGetSupported(SSLProtocolVariant protocolVariant, | |
1809 SSLVersionRange *vrange) | |
1810 { | |
1811 if (!vrange) { | |
1812 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1813 return SECFailure; | |
1814 } | |
1815 | |
1816 switch (protocolVariant) { | |
1817 case ssl_variant_stream: | |
1818 vrange->min = SSL_LIBRARY_VERSION_3_0; | |
1819 vrange->max = SSL_LIBRARY_VERSION_MAX_SUPPORTED; | |
1820 break; | |
1821 case ssl_variant_datagram: | |
1822 vrange->min = SSL_LIBRARY_VERSION_TLS_1_1; | |
1823 vrange->max = SSL_LIBRARY_VERSION_MAX_SUPPORTED; | |
1824 break; | |
1825 default: | |
1826 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1827 return SECFailure; | |
1828 } | |
1829 | |
1830 return SECSuccess; | |
1831 } | |
1832 | |
1833 SECStatus | |
1834 SSL_VersionRangeGetDefault(SSLProtocolVariant protocolVariant, | |
1835 SSLVersionRange *vrange) | |
1836 { | |
1837 if ((protocolVariant != ssl_variant_stream && | |
1838 protocolVariant != ssl_variant_datagram) || !vrange) { | |
1839 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1840 return SECFailure; | |
1841 } | |
1842 | |
1843 *vrange = *VERSIONS_DEFAULTS(protocolVariant); | |
1844 | |
1845 return SECSuccess; | |
1846 } | |
1847 | |
1848 SECStatus | |
1849 SSL_VersionRangeSetDefault(SSLProtocolVariant protocolVariant, | |
1850 const SSLVersionRange *vrange) | |
1851 { | |
1852 if (!ssl3_VersionRangeIsValid(protocolVariant, vrange)) { | |
1853 PORT_SetError(SSL_ERROR_INVALID_VERSION_RANGE); | |
1854 return SECFailure; | |
1855 } | |
1856 | |
1857 *VERSIONS_DEFAULTS(protocolVariant) = *vrange; | |
1858 | |
1859 return SECSuccess; | |
1860 } | |
1861 | |
1862 SECStatus | |
1863 SSL_VersionRangeGet(PRFileDesc *fd, SSLVersionRange *vrange) | |
1864 { | |
1865 sslSocket *ss = ssl_FindSocket(fd); | |
1866 | |
1867 if (!ss) { | |
1868 SSL_DBG(("%d: SSL[%d]: bad socket in SSL3_VersionRangeGet", | |
1869 SSL_GETPID(), fd)); | |
1870 return SECFailure; | |
1871 } | |
1872 | |
1873 if (!vrange) { | |
1874 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
1875 return SECFailure; | |
1876 } | |
1877 | |
1878 ssl_Get1stHandshakeLock(ss); | |
1879 ssl_GetSSL3HandshakeLock(ss); | |
1880 | |
1881 *vrange = ss->vrange; | |
1882 | |
1883 ssl_ReleaseSSL3HandshakeLock(ss); | |
1884 ssl_Release1stHandshakeLock(ss); | |
1885 | |
1886 return SECSuccess; | |
1887 } | |
1888 | |
1889 static PRCallOnceType checkTLS12TokenOnce; | |
1890 static PRBool tls12TokenExists; | |
1891 | |
1892 static PRStatus | |
1893 ssl_CheckTLS12Token(void) | |
1894 { | |
1895 tls12TokenExists = | |
1896 PK11_TokenExists(CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256); | |
1897 return PR_SUCCESS; | |
1898 } | |
1899 | |
1900 static PRBool | |
1901 ssl_TLS12TokenExists(void) | |
1902 { | |
1903 (void) PR_CallOnce(&checkTLS12TokenOnce, ssl_CheckTLS12Token); | |
1904 return tls12TokenExists; | |
1905 } | |
1906 | |
1907 SECStatus | |
1908 SSL_VersionRangeSet(PRFileDesc *fd, const SSLVersionRange *vrange) | |
1909 { | |
1910 sslSocket *ss = ssl_FindSocket(fd); | |
1911 | |
1912 if (!ss) { | |
1913 SSL_DBG(("%d: SSL[%d]: bad socket in SSL3_VersionRangeSet", | |
1914 SSL_GETPID(), fd)); | |
1915 return SECFailure; | |
1916 } | |
1917 | |
1918 if (!ssl3_VersionRangeIsValid(ss->protocolVariant, vrange)) { | |
1919 PORT_SetError(SSL_ERROR_INVALID_VERSION_RANGE); | |
1920 return SECFailure; | |
1921 } | |
1922 | |
1923 ssl_Get1stHandshakeLock(ss); | |
1924 ssl_GetSSL3HandshakeLock(ss); | |
1925 | |
1926 ss->vrange = *vrange; | |
1927 /* If we don't have a sufficiently up-to-date softoken then we cannot do | |
1928 * TLS 1.2. */ | |
1929 if (ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_2 && | |
1930 !ssl_TLS12TokenExists()) { | |
1931 /* If the user requested a minimum version of 1.2, then we don't | |
1932 * silently downgrade. */ | |
1933 if (ss->vrange.min >= SSL_LIBRARY_VERSION_TLS_1_2) { | |
1934 ssl_ReleaseSSL3HandshakeLock(ss); | |
1935 ssl_Release1stHandshakeLock(ss); | |
1936 PORT_SetError(SSL_ERROR_INVALID_VERSION_RANGE); | |
1937 return SECFailure; | |
1938 } | |
1939 ss->vrange.max = SSL_LIBRARY_VERSION_TLS_1_1; | |
1940 } | |
1941 | |
1942 ssl_ReleaseSSL3HandshakeLock(ss); | |
1943 ssl_Release1stHandshakeLock(ss); | |
1944 | |
1945 return SECSuccess; | |
1946 } | |
1947 | |
1948 const SECItemArray * | |
1949 SSL_PeerStapledOCSPResponses(PRFileDesc *fd) | |
1950 { | |
1951 sslSocket *ss = ssl_FindSocket(fd); | |
1952 | |
1953 if (!ss) { | |
1954 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_PeerStapledOCSPResponses", | |
1955 SSL_GETPID(), fd)); | |
1956 return NULL; | |
1957 } | |
1958 | |
1959 if (!ss->sec.ci.sid) { | |
1960 PORT_SetError(SEC_ERROR_NOT_INITIALIZED); | |
1961 return NULL; | |
1962 } | |
1963 | |
1964 return &ss->sec.ci.sid->peerCertStatus; | |
1965 } | |
1966 | |
1967 const SECItem * | |
1968 SSL_PeerSignedCertTimestamps(PRFileDesc *fd) | |
1969 { | |
1970 sslSocket *ss = ssl_FindSocket(fd); | |
1971 | |
1972 if (!ss) { | |
1973 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_PeerSignedCertTimestamps", | |
1974 SSL_GETPID(), fd)); | |
1975 return NULL; | |
1976 } | |
1977 | |
1978 if (!ss->sec.ci.sid) { | |
1979 PORT_SetError(SEC_ERROR_NOT_INITIALIZED); | |
1980 return NULL; | |
1981 } | |
1982 | |
1983 if (ss->sec.ci.sid->version < SSL_LIBRARY_VERSION_3_0) { | |
1984 PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_SSL2); | |
1985 return NULL; | |
1986 } | |
1987 return &ss->sec.ci.sid->u.ssl3.signedCertTimestamps; | |
1988 } | |
1989 | |
1990 SECStatus | |
1991 SSL_HandshakeResumedSession(PRFileDesc *fd, PRBool *handshake_resumed) { | |
1992 sslSocket *ss = ssl_FindSocket(fd); | |
1993 | |
1994 if (!ss) { | |
1995 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_HandshakeResumedSession", | |
1996 SSL_GETPID(), fd)); | |
1997 return SECFailure; | |
1998 } | |
1999 | |
2000 *handshake_resumed = ss->ssl3.hs.isResuming; | |
2001 return SECSuccess; | |
2002 } | |
2003 | |
2004 const SECItem * | |
2005 SSL_GetRequestedClientCertificateTypes(PRFileDesc *fd) | |
2006 { | |
2007 sslSocket *ss = ssl_FindSocket(fd); | |
2008 | |
2009 if (!ss) { | |
2010 SSL_DBG(("%d: SSL[%d]: bad socket in " | |
2011 "SSL_GetRequestedClientCertificateTypes", SSL_GETPID(), fd)); | |
2012 return NULL; | |
2013 } | |
2014 | |
2015 return ss->requestedCertTypes; | |
2016 } | |
2017 | |
2018 /************************************************************************/ | |
2019 /* The following functions are the TOP LEVEL SSL functions. | |
2020 ** They all get called through the NSPRIOMethods table below. | |
2021 */ | |
2022 | |
2023 static PRFileDesc * PR_CALLBACK | |
2024 ssl_Accept(PRFileDesc *fd, PRNetAddr *sockaddr, PRIntervalTime timeout) | |
2025 { | |
2026 sslSocket *ss; | |
2027 sslSocket *ns = NULL; | |
2028 PRFileDesc *newfd = NULL; | |
2029 PRFileDesc *osfd; | |
2030 PRStatus status; | |
2031 | |
2032 ss = ssl_GetPrivate(fd); | |
2033 if (!ss) { | |
2034 SSL_DBG(("%d: SSL[%d]: bad socket in accept", SSL_GETPID(), fd)); | |
2035 return NULL; | |
2036 } | |
2037 | |
2038 /* IF this is a listen socket, there shouldn't be any I/O going on */ | |
2039 SSL_LOCK_READER(ss); | |
2040 SSL_LOCK_WRITER(ss); | |
2041 ssl_Get1stHandshakeLock(ss); | |
2042 ssl_GetSSL3HandshakeLock(ss); | |
2043 | |
2044 ss->cTimeout = timeout; | |
2045 | |
2046 osfd = ss->fd->lower; | |
2047 | |
2048 /* First accept connection */ | |
2049 newfd = osfd->methods->accept(osfd, sockaddr, timeout); | |
2050 if (newfd == NULL) { | |
2051 SSL_DBG(("%d: SSL[%d]: accept failed, errno=%d", | |
2052 SSL_GETPID(), ss->fd, PORT_GetError())); | |
2053 } else { | |
2054 /* Create ssl module */ | |
2055 ns = ssl_DupSocket(ss); | |
2056 } | |
2057 | |
2058 ssl_ReleaseSSL3HandshakeLock(ss); | |
2059 ssl_Release1stHandshakeLock(ss); | |
2060 SSL_UNLOCK_WRITER(ss); | |
2061 SSL_UNLOCK_READER(ss); /* ss isn't used below here. */ | |
2062 | |
2063 if (ns == NULL) | |
2064 goto loser; | |
2065 | |
2066 /* push ssl module onto the new socket */ | |
2067 status = ssl_PushIOLayer(ns, newfd, PR_TOP_IO_LAYER); | |
2068 if (status != PR_SUCCESS) | |
2069 goto loser; | |
2070 | |
2071 /* Now start server connection handshake with client. | |
2072 ** Don't need locks here because nobody else has a reference to ns yet. | |
2073 */ | |
2074 if ( ns->opt.useSecurity ) { | |
2075 if ( ns->opt.handshakeAsClient ) { | |
2076 ns->handshake = ssl2_BeginClientHandshake; | |
2077 ss->handshaking = sslHandshakingAsClient; | |
2078 } else { | |
2079 ns->handshake = ssl2_BeginServerHandshake; | |
2080 ss->handshaking = sslHandshakingAsServer; | |
2081 } | |
2082 } | |
2083 ns->TCPconnected = 1; | |
2084 return newfd; | |
2085 | |
2086 loser: | |
2087 if (ns != NULL) | |
2088 ssl_FreeSocket(ns); | |
2089 if (newfd != NULL) | |
2090 PR_Close(newfd); | |
2091 return NULL; | |
2092 } | |
2093 | |
2094 static PRStatus PR_CALLBACK | |
2095 ssl_Connect(PRFileDesc *fd, const PRNetAddr *sockaddr, PRIntervalTime timeout) | |
2096 { | |
2097 sslSocket *ss; | |
2098 PRStatus rv; | |
2099 | |
2100 ss = ssl_GetPrivate(fd); | |
2101 if (!ss) { | |
2102 SSL_DBG(("%d: SSL[%d]: bad socket in connect", SSL_GETPID(), fd)); | |
2103 return PR_FAILURE; | |
2104 } | |
2105 | |
2106 /* IF this is a listen socket, there shouldn't be any I/O going on */ | |
2107 SSL_LOCK_READER(ss); | |
2108 SSL_LOCK_WRITER(ss); | |
2109 | |
2110 ss->cTimeout = timeout; | |
2111 rv = (PRStatus)(*ss->ops->connect)(ss, sockaddr); | |
2112 | |
2113 SSL_UNLOCK_WRITER(ss); | |
2114 SSL_UNLOCK_READER(ss); | |
2115 | |
2116 return rv; | |
2117 } | |
2118 | |
2119 static PRStatus PR_CALLBACK | |
2120 ssl_Bind(PRFileDesc *fd, const PRNetAddr *addr) | |
2121 { | |
2122 sslSocket * ss = ssl_GetPrivate(fd); | |
2123 PRStatus rv; | |
2124 | |
2125 if (!ss) { | |
2126 SSL_DBG(("%d: SSL[%d]: bad socket in bind", SSL_GETPID(), fd)); | |
2127 return PR_FAILURE; | |
2128 } | |
2129 SSL_LOCK_READER(ss); | |
2130 SSL_LOCK_WRITER(ss); | |
2131 | |
2132 rv = (PRStatus)(*ss->ops->bind)(ss, addr); | |
2133 | |
2134 SSL_UNLOCK_WRITER(ss); | |
2135 SSL_UNLOCK_READER(ss); | |
2136 return rv; | |
2137 } | |
2138 | |
2139 static PRStatus PR_CALLBACK | |
2140 ssl_Listen(PRFileDesc *fd, PRIntn backlog) | |
2141 { | |
2142 sslSocket * ss = ssl_GetPrivate(fd); | |
2143 PRStatus rv; | |
2144 | |
2145 if (!ss) { | |
2146 SSL_DBG(("%d: SSL[%d]: bad socket in listen", SSL_GETPID(), fd)); | |
2147 return PR_FAILURE; | |
2148 } | |
2149 SSL_LOCK_READER(ss); | |
2150 SSL_LOCK_WRITER(ss); | |
2151 | |
2152 rv = (PRStatus)(*ss->ops->listen)(ss, backlog); | |
2153 | |
2154 SSL_UNLOCK_WRITER(ss); | |
2155 SSL_UNLOCK_READER(ss); | |
2156 return rv; | |
2157 } | |
2158 | |
2159 static PRStatus PR_CALLBACK | |
2160 ssl_Shutdown(PRFileDesc *fd, PRIntn how) | |
2161 { | |
2162 sslSocket * ss = ssl_GetPrivate(fd); | |
2163 PRStatus rv; | |
2164 | |
2165 if (!ss) { | |
2166 SSL_DBG(("%d: SSL[%d]: bad socket in shutdown", SSL_GETPID(), fd)); | |
2167 return PR_FAILURE; | |
2168 } | |
2169 if (how == PR_SHUTDOWN_RCV || how == PR_SHUTDOWN_BOTH) { | |
2170 SSL_LOCK_READER(ss); | |
2171 } | |
2172 if (how == PR_SHUTDOWN_SEND || how == PR_SHUTDOWN_BOTH) { | |
2173 SSL_LOCK_WRITER(ss); | |
2174 } | |
2175 | |
2176 rv = (PRStatus)(*ss->ops->shutdown)(ss, how); | |
2177 | |
2178 if (how == PR_SHUTDOWN_SEND || how == PR_SHUTDOWN_BOTH) { | |
2179 SSL_UNLOCK_WRITER(ss); | |
2180 } | |
2181 if (how == PR_SHUTDOWN_RCV || how == PR_SHUTDOWN_BOTH) { | |
2182 SSL_UNLOCK_READER(ss); | |
2183 } | |
2184 return rv; | |
2185 } | |
2186 | |
2187 static PRStatus PR_CALLBACK | |
2188 ssl_Close(PRFileDesc *fd) | |
2189 { | |
2190 sslSocket *ss; | |
2191 PRStatus rv; | |
2192 | |
2193 ss = ssl_GetPrivate(fd); | |
2194 if (!ss) { | |
2195 SSL_DBG(("%d: SSL[%d]: bad socket in close", SSL_GETPID(), fd)); | |
2196 return PR_FAILURE; | |
2197 } | |
2198 | |
2199 /* There must not be any I/O going on */ | |
2200 SSL_LOCK_READER(ss); | |
2201 SSL_LOCK_WRITER(ss); | |
2202 | |
2203 /* By the time this function returns, | |
2204 ** ss is an invalid pointer, and the locks to which it points have | |
2205 ** been unlocked and freed. So, this is the ONE PLACE in all of SSL | |
2206 ** where the LOCK calls and the corresponding UNLOCK calls are not in | |
2207 ** the same function scope. The unlock calls are in ssl_FreeSocket(). | |
2208 */ | |
2209 rv = (PRStatus)(*ss->ops->close)(ss); | |
2210 | |
2211 return rv; | |
2212 } | |
2213 | |
2214 static int PR_CALLBACK | |
2215 ssl_Recv(PRFileDesc *fd, void *buf, PRInt32 len, PRIntn flags, | |
2216 PRIntervalTime timeout) | |
2217 { | |
2218 sslSocket *ss; | |
2219 int rv; | |
2220 | |
2221 ss = ssl_GetPrivate(fd); | |
2222 if (!ss) { | |
2223 SSL_DBG(("%d: SSL[%d]: bad socket in recv", SSL_GETPID(), fd)); | |
2224 return SECFailure; | |
2225 } | |
2226 SSL_LOCK_READER(ss); | |
2227 ss->rTimeout = timeout; | |
2228 if (!ss->opt.fdx) | |
2229 ss->wTimeout = timeout; | |
2230 rv = (*ss->ops->recv)(ss, (unsigned char*)buf, len, flags); | |
2231 SSL_UNLOCK_READER(ss); | |
2232 return rv; | |
2233 } | |
2234 | |
2235 static int PR_CALLBACK | |
2236 ssl_Send(PRFileDesc *fd, const void *buf, PRInt32 len, PRIntn flags, | |
2237 PRIntervalTime timeout) | |
2238 { | |
2239 sslSocket *ss; | |
2240 int rv; | |
2241 | |
2242 ss = ssl_GetPrivate(fd); | |
2243 if (!ss) { | |
2244 SSL_DBG(("%d: SSL[%d]: bad socket in send", SSL_GETPID(), fd)); | |
2245 return SECFailure; | |
2246 } | |
2247 SSL_LOCK_WRITER(ss); | |
2248 ss->wTimeout = timeout; | |
2249 if (!ss->opt.fdx) | |
2250 ss->rTimeout = timeout; | |
2251 rv = (*ss->ops->send)(ss, (const unsigned char*)buf, len, flags); | |
2252 SSL_UNLOCK_WRITER(ss); | |
2253 return rv; | |
2254 } | |
2255 | |
2256 static int PR_CALLBACK | |
2257 ssl_Read(PRFileDesc *fd, void *buf, PRInt32 len) | |
2258 { | |
2259 sslSocket *ss; | |
2260 int rv; | |
2261 | |
2262 ss = ssl_GetPrivate(fd); | |
2263 if (!ss) { | |
2264 SSL_DBG(("%d: SSL[%d]: bad socket in read", SSL_GETPID(), fd)); | |
2265 return SECFailure; | |
2266 } | |
2267 SSL_LOCK_READER(ss); | |
2268 ss->rTimeout = PR_INTERVAL_NO_TIMEOUT; | |
2269 if (!ss->opt.fdx) | |
2270 ss->wTimeout = PR_INTERVAL_NO_TIMEOUT; | |
2271 rv = (*ss->ops->read)(ss, (unsigned char*)buf, len); | |
2272 SSL_UNLOCK_READER(ss); | |
2273 return rv; | |
2274 } | |
2275 | |
2276 static int PR_CALLBACK | |
2277 ssl_Write(PRFileDesc *fd, const void *buf, PRInt32 len) | |
2278 { | |
2279 sslSocket *ss; | |
2280 int rv; | |
2281 | |
2282 ss = ssl_GetPrivate(fd); | |
2283 if (!ss) { | |
2284 SSL_DBG(("%d: SSL[%d]: bad socket in write", SSL_GETPID(), fd)); | |
2285 return SECFailure; | |
2286 } | |
2287 SSL_LOCK_WRITER(ss); | |
2288 ss->wTimeout = PR_INTERVAL_NO_TIMEOUT; | |
2289 if (!ss->opt.fdx) | |
2290 ss->rTimeout = PR_INTERVAL_NO_TIMEOUT; | |
2291 rv = (*ss->ops->write)(ss, (const unsigned char*)buf, len); | |
2292 SSL_UNLOCK_WRITER(ss); | |
2293 return rv; | |
2294 } | |
2295 | |
2296 static PRStatus PR_CALLBACK | |
2297 ssl_GetPeerName(PRFileDesc *fd, PRNetAddr *addr) | |
2298 { | |
2299 sslSocket *ss; | |
2300 | |
2301 ss = ssl_GetPrivate(fd); | |
2302 if (!ss) { | |
2303 SSL_DBG(("%d: SSL[%d]: bad socket in getpeername", SSL_GETPID(), fd)); | |
2304 return PR_FAILURE; | |
2305 } | |
2306 return (PRStatus)(*ss->ops->getpeername)(ss, addr); | |
2307 } | |
2308 | |
2309 /* | |
2310 */ | |
2311 SECStatus | |
2312 ssl_GetPeerInfo(sslSocket *ss) | |
2313 { | |
2314 PRFileDesc * osfd; | |
2315 int rv; | |
2316 PRNetAddr sin; | |
2317 | |
2318 osfd = ss->fd->lower; | |
2319 | |
2320 PORT_Memset(&sin, 0, sizeof(sin)); | |
2321 rv = osfd->methods->getpeername(osfd, &sin); | |
2322 if (rv < 0) { | |
2323 return SECFailure; | |
2324 } | |
2325 ss->TCPconnected = 1; | |
2326 if (sin.inet.family == PR_AF_INET) { | |
2327 PR_ConvertIPv4AddrToIPv6(sin.inet.ip, &ss->sec.ci.peer); | |
2328 ss->sec.ci.port = sin.inet.port; | |
2329 } else if (sin.ipv6.family == PR_AF_INET6) { | |
2330 ss->sec.ci.peer = sin.ipv6.ip; | |
2331 ss->sec.ci.port = sin.ipv6.port; | |
2332 } else { | |
2333 PORT_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR); | |
2334 return SECFailure; | |
2335 } | |
2336 return SECSuccess; | |
2337 } | |
2338 | |
2339 static PRStatus PR_CALLBACK | |
2340 ssl_GetSockName(PRFileDesc *fd, PRNetAddr *name) | |
2341 { | |
2342 sslSocket *ss; | |
2343 | |
2344 ss = ssl_GetPrivate(fd); | |
2345 if (!ss) { | |
2346 SSL_DBG(("%d: SSL[%d]: bad socket in getsockname", SSL_GETPID(), fd)); | |
2347 return PR_FAILURE; | |
2348 } | |
2349 return (PRStatus)(*ss->ops->getsockname)(ss, name); | |
2350 } | |
2351 | |
2352 SECStatus | |
2353 SSL_SetStapledOCSPResponses(PRFileDesc *fd, const SECItemArray *responses, | |
2354 SSLKEAType kea) | |
2355 { | |
2356 sslSocket *ss; | |
2357 | |
2358 ss = ssl_FindSocket(fd); | |
2359 if (!ss) { | |
2360 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetStapledOCSPResponses", | |
2361 SSL_GETPID(), fd)); | |
2362 return SECFailure; | |
2363 } | |
2364 | |
2365 if ( kea <= 0 || kea >= kt_kea_size) { | |
2366 SSL_DBG(("%d: SSL[%d]: invalid key in SSL_SetStapledOCSPResponses", | |
2367 SSL_GETPID(), fd)); | |
2368 return SECFailure; | |
2369 } | |
2370 | |
2371 if (ss->certStatusArray[kea]) { | |
2372 SECITEM_FreeArray(ss->certStatusArray[kea], PR_TRUE); | |
2373 ss->certStatusArray[kea] = NULL; | |
2374 } | |
2375 if (responses) { | |
2376 ss->certStatusArray[kea] = SECITEM_DupArray(NULL, responses); | |
2377 } | |
2378 return (ss->certStatusArray[kea] || !responses) ? SECSuccess : SECFailure; | |
2379 } | |
2380 | |
2381 SECStatus | |
2382 SSL_SetSockPeerID(PRFileDesc *fd, const char *peerID) | |
2383 { | |
2384 sslSocket *ss; | |
2385 | |
2386 ss = ssl_FindSocket(fd); | |
2387 if (!ss) { | |
2388 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetSockPeerID", | |
2389 SSL_GETPID(), fd)); | |
2390 return SECFailure; | |
2391 } | |
2392 | |
2393 if (ss->peerID) { | |
2394 PORT_Free(ss->peerID); | |
2395 ss->peerID = NULL; | |
2396 } | |
2397 if (peerID) | |
2398 ss->peerID = PORT_Strdup(peerID); | |
2399 return (ss->peerID || !peerID) ? SECSuccess : SECFailure; | |
2400 } | |
2401 | |
2402 #define PR_POLL_RW (PR_POLL_WRITE | PR_POLL_READ) | |
2403 | |
2404 static PRInt16 PR_CALLBACK | |
2405 ssl_Poll(PRFileDesc *fd, PRInt16 how_flags, PRInt16 *p_out_flags) | |
2406 { | |
2407 sslSocket *ss; | |
2408 PRInt16 new_flags = how_flags; /* should select on these flags. */ | |
2409 PRNetAddr addr; | |
2410 | |
2411 *p_out_flags = 0; | |
2412 ss = ssl_GetPrivate(fd); | |
2413 if (!ss) { | |
2414 SSL_DBG(("%d: SSL[%d]: bad socket in SSL_Poll", | |
2415 SSL_GETPID(), fd)); | |
2416 return 0; /* don't poll on this socket */ | |
2417 } | |
2418 | |
2419 if (ss->opt.useSecurity && | |
2420 ss->handshaking != sslHandshakingUndetermined && | |
2421 !ss->firstHsDone && | |
2422 (how_flags & PR_POLL_RW)) { | |
2423 if (!ss->TCPconnected) { | |
2424 ss->TCPconnected = (PR_SUCCESS == ssl_DefGetpeername(ss, &addr)); | |
2425 } | |
2426 /* If it's not connected, then presumably the application is polling | |
2427 ** on read or write appropriately, so don't change it. | |
2428 */ | |
2429 if (ss->TCPconnected) { | |
2430 if (!ss->handshakeBegun) { | |
2431 /* If the handshake has not begun, poll on read or write | |
2432 ** based on the local application's role in the handshake, | |
2433 ** not based on what the application requested. | |
2434 */ | |
2435 new_flags &= ~PR_POLL_RW; | |
2436 if (ss->handshaking == sslHandshakingAsClient) { | |
2437 new_flags |= PR_POLL_WRITE; | |
2438 } else { /* handshaking as server */ | |
2439 new_flags |= PR_POLL_READ; | |
2440 } | |
2441 } else | |
2442 /* First handshake is in progress */ | |
2443 if (ss->lastWriteBlocked) { | |
2444 if (new_flags & PR_POLL_READ) { | |
2445 /* The caller is waiting for data to be received, | |
2446 ** but the initial handshake is blocked on write, or the | |
2447 ** client's first handshake record has not been written. | |
2448 ** The code should select on write, not read. | |
2449 */ | |
2450 new_flags ^= PR_POLL_READ; /* don't select on read. */ | |
2451 new_flags |= PR_POLL_WRITE; /* do select on write. */ | |
2452 } | |
2453 } else if (new_flags & PR_POLL_WRITE) { | |
2454 /* The caller is trying to write, but the handshake is | |
2455 ** blocked waiting for data to read, and the first | |
2456 ** handshake has been sent. So do NOT to poll on write | |
2457 ** unless we did false start. | |
2458 */ | |
2459 if (!(ss->version >= SSL_LIBRARY_VERSION_3_0 && | |
2460 ss->ssl3.hs.canFalseStart)) { | |
2461 new_flags ^= PR_POLL_WRITE; /* don't select on write. */ | |
2462 } | |
2463 new_flags |= PR_POLL_READ; /* do select on read. */ | |
2464 } | |
2465 } | |
2466 } else if ((new_flags & PR_POLL_READ) && (SSL_DataPending(fd) > 0)) { | |
2467 *p_out_flags = PR_POLL_READ; /* it's ready already. */ | |
2468 return new_flags; | |
2469 } else if ((ss->lastWriteBlocked) && (how_flags & PR_POLL_READ) && | |
2470 (ss->pendingBuf.len != 0)) { /* write data waiting to be sent */ | |
2471 new_flags |= PR_POLL_WRITE; /* also select on write. */ | |
2472 } | |
2473 | |
2474 if (ss->version >= SSL_LIBRARY_VERSION_3_0 && | |
2475 ss->ssl3.hs.restartTarget != NULL) { | |
2476 /* Read and write will block until the asynchronous callback completes | |
2477 * (e.g. until SSL_AuthCertificateComplete is called), so don't tell | |
2478 * the caller to poll the socket unless there is pending write data. | |
2479 */ | |
2480 if (ss->lastWriteBlocked && ss->pendingBuf.len != 0) { | |
2481 /* Ignore any newly-received data on the socket, but do wait for | |
2482 * the socket to become writable again. Here, it is OK for an error | |
2483 * to be detected, because our logic for sending pending write data | |
2484 * will allow us to report the error to the caller without the risk | |
2485 * of the application spinning. | |
2486 */ | |
2487 new_flags &= (PR_POLL_WRITE | PR_POLL_EXCEPT); | |
2488 } else { | |
2489 /* Unfortunately, clearing new_flags will make it impossible for | |
2490 * the application to detect errors that it would otherwise be | |
2491 * able to detect with PR_POLL_EXCEPT, until the asynchronous | |
2492 * callback completes. However, we must clear all the flags to | |
2493 * prevent the application from spinning (alternating between | |
2494 * calling PR_Poll that would return PR_POLL_EXCEPT, and send/recv | |
2495 * which won't actually report the I/O error while we are waiting | |
2496 * for the asynchronous callback to complete). | |
2497 */ | |
2498 new_flags = 0; | |
2499 } | |
2500 } | |
2501 | |
2502 if (new_flags && (fd->lower->methods->poll != NULL)) { | |
2503 PRInt16 lower_out_flags = 0; | |
2504 PRInt16 lower_new_flags; | |
2505 lower_new_flags = fd->lower->methods->poll(fd->lower, new_flags, | |
2506 &lower_out_flags); | |
2507 if ((lower_new_flags & lower_out_flags) && (how_flags != new_flags)) { | |
2508 PRInt16 out_flags = lower_out_flags & ~PR_POLL_RW; | |
2509 if (lower_out_flags & PR_POLL_READ) | |
2510 out_flags |= PR_POLL_WRITE; | |
2511 if (lower_out_flags & PR_POLL_WRITE) | |
2512 out_flags |= PR_POLL_READ; | |
2513 *p_out_flags = out_flags; | |
2514 new_flags = how_flags; | |
2515 } else { | |
2516 *p_out_flags = lower_out_flags; | |
2517 new_flags = lower_new_flags; | |
2518 } | |
2519 } | |
2520 | |
2521 return new_flags; | |
2522 } | |
2523 | |
2524 static PRInt32 PR_CALLBACK | |
2525 ssl_TransmitFile(PRFileDesc *sd, PRFileDesc *fd, | |
2526 const void *headers, PRInt32 hlen, | |
2527 PRTransmitFileFlags flags, PRIntervalTime timeout) | |
2528 { | |
2529 PRSendFileData sfd; | |
2530 | |
2531 sfd.fd = fd; | |
2532 sfd.file_offset = 0; | |
2533 sfd.file_nbytes = 0; | |
2534 sfd.header = headers; | |
2535 sfd.hlen = hlen; | |
2536 sfd.trailer = NULL; | |
2537 sfd.tlen = 0; | |
2538 | |
2539 return sd->methods->sendfile(sd, &sfd, flags, timeout); | |
2540 } | |
2541 | |
2542 | |
2543 PRBool | |
2544 ssl_FdIsBlocking(PRFileDesc *fd) | |
2545 { | |
2546 PRSocketOptionData opt; | |
2547 PRStatus status; | |
2548 | |
2549 opt.option = PR_SockOpt_Nonblocking; | |
2550 opt.value.non_blocking = PR_FALSE; | |
2551 status = PR_GetSocketOption(fd, &opt); | |
2552 if (status != PR_SUCCESS) | |
2553 return PR_FALSE; | |
2554 return (PRBool)!opt.value.non_blocking; | |
2555 } | |
2556 | |
2557 PRBool | |
2558 ssl_SocketIsBlocking(sslSocket *ss) | |
2559 { | |
2560 return ssl_FdIsBlocking(ss->fd); | |
2561 } | |
2562 | |
2563 PRInt32 sslFirstBufSize = 8 * 1024; | |
2564 PRInt32 sslCopyLimit = 1024; | |
2565 | |
2566 static PRInt32 PR_CALLBACK | |
2567 ssl_WriteV(PRFileDesc *fd, const PRIOVec *iov, PRInt32 vectors, | |
2568 PRIntervalTime timeout) | |
2569 { | |
2570 PRInt32 bufLen; | |
2571 PRInt32 left; | |
2572 PRInt32 rv; | |
2573 PRInt32 sent = 0; | |
2574 const PRInt32 first_len = sslFirstBufSize; | |
2575 const PRInt32 limit = sslCopyLimit; | |
2576 PRBool blocking; | |
2577 PRIOVec myIov = { 0, 0 }; | |
2578 char buf[MAX_FRAGMENT_LENGTH]; | |
2579 | |
2580 if (vectors > PR_MAX_IOVECTOR_SIZE) { | |
2581 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR); | |
2582 return -1; | |
2583 } | |
2584 blocking = ssl_FdIsBlocking(fd); | |
2585 | |
2586 #define K16 sizeof(buf) | |
2587 #define KILL_VECTORS while (vectors && !iov->iov_len) { ++iov; --vectors; } | |
2588 #define GET_VECTOR do { myIov = *iov++; --vectors; KILL_VECTORS } while (0) | |
2589 #define HANDLE_ERR(rv, len) \ | |
2590 if (rv != len) { \ | |
2591 if (rv < 0) { \ | |
2592 if (!blocking \ | |
2593 && (PR_GetError() == PR_WOULD_BLOCK_ERROR) \ | |
2594 && (sent > 0)) { \ | |
2595 return sent; \ | |
2596 } else { \ | |
2597 return -1; \ | |
2598 } \ | |
2599 } \ | |
2600 /* Only a nonblocking socket can have partial sends */ \ | |
2601 PR_ASSERT(!blocking); \ | |
2602 return sent + rv; \ | |
2603 } | |
2604 #define SEND(bfr, len) \ | |
2605 do { \ | |
2606 rv = ssl_Send(fd, bfr, len, 0, timeout); \ | |
2607 HANDLE_ERR(rv, len) \ | |
2608 sent += len; \ | |
2609 } while (0) | |
2610 | |
2611 /* Make sure the first write is at least 8 KB, if possible. */ | |
2612 KILL_VECTORS | |
2613 if (!vectors) | |
2614 return ssl_Send(fd, 0, 0, 0, timeout); | |
2615 GET_VECTOR; | |
2616 if (!vectors) { | |
2617 return ssl_Send(fd, myIov.iov_base, myIov.iov_len, 0, timeout); | |
2618 } | |
2619 if (myIov.iov_len < first_len) { | |
2620 PORT_Memcpy(buf, myIov.iov_base, myIov.iov_len); | |
2621 bufLen = myIov.iov_len; | |
2622 left = first_len - bufLen; | |
2623 while (vectors && left) { | |
2624 int toCopy; | |
2625 GET_VECTOR; | |
2626 toCopy = PR_MIN(left, myIov.iov_len); | |
2627 PORT_Memcpy(buf + bufLen, myIov.iov_base, toCopy); | |
2628 bufLen += toCopy; | |
2629 left -= toCopy; | |
2630 myIov.iov_base += toCopy; | |
2631 myIov.iov_len -= toCopy; | |
2632 } | |
2633 SEND( buf, bufLen ); | |
2634 } | |
2635 | |
2636 while (vectors || myIov.iov_len) { | |
2637 PRInt32 addLen; | |
2638 if (!myIov.iov_len) { | |
2639 GET_VECTOR; | |
2640 } | |
2641 while (myIov.iov_len >= K16) { | |
2642 SEND(myIov.iov_base, K16); | |
2643 myIov.iov_base += K16; | |
2644 myIov.iov_len -= K16; | |
2645 } | |
2646 if (!myIov.iov_len) | |
2647 continue; | |
2648 | |
2649 if (!vectors || myIov.iov_len > limit) { | |
2650 addLen = 0; | |
2651 } else if ((addLen = iov->iov_len % K16) + myIov.iov_len <= limit) { | |
2652 /* Addlen is already computed. */; | |
2653 } else if (vectors > 1 && | |
2654 iov[1].iov_len % K16 + addLen + myIov.iov_len <= 2 * limit) { | |
2655 addLen = limit - myIov.iov_len; | |
2656 } else | |
2657 addLen = 0; | |
2658 | |
2659 if (!addLen) { | |
2660 SEND( myIov.iov_base, myIov.iov_len ); | |
2661 myIov.iov_len = 0; | |
2662 continue; | |
2663 } | |
2664 PORT_Memcpy(buf, myIov.iov_base, myIov.iov_len); | |
2665 bufLen = myIov.iov_len; | |
2666 do { | |
2667 GET_VECTOR; | |
2668 PORT_Memcpy(buf + bufLen, myIov.iov_base, addLen); | |
2669 myIov.iov_base += addLen; | |
2670 myIov.iov_len -= addLen; | |
2671 bufLen += addLen; | |
2672 | |
2673 left = PR_MIN( limit, K16 - bufLen); | |
2674 if (!vectors /* no more left */ | |
2675 || myIov.iov_len > 0 /* we didn't use that one all up */ | |
2676 || bufLen >= K16 /* it's full. */ | |
2677 ) { | |
2678 addLen = 0; | |
2679 } else if ((addLen = iov->iov_len % K16) <= left) { | |
2680 /* Addlen is already computed. */; | |
2681 } else if (vectors > 1 && | |
2682 iov[1].iov_len % K16 + addLen <= left + limit) { | |
2683 addLen = left; | |
2684 } else | |
2685 addLen = 0; | |
2686 | |
2687 } while (addLen); | |
2688 SEND( buf, bufLen ); | |
2689 } | |
2690 return sent; | |
2691 } | |
2692 | |
2693 /* | |
2694 * These functions aren't implemented. | |
2695 */ | |
2696 | |
2697 static PRInt32 PR_CALLBACK | |
2698 ssl_Available(PRFileDesc *fd) | |
2699 { | |
2700 PORT_Assert(0); | |
2701 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2702 return SECFailure; | |
2703 } | |
2704 | |
2705 static PRInt64 PR_CALLBACK | |
2706 ssl_Available64(PRFileDesc *fd) | |
2707 { | |
2708 PRInt64 res; | |
2709 | |
2710 PORT_Assert(0); | |
2711 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2712 LL_I2L(res, -1L); | |
2713 return res; | |
2714 } | |
2715 | |
2716 static PRStatus PR_CALLBACK | |
2717 ssl_FSync(PRFileDesc *fd) | |
2718 { | |
2719 PORT_Assert(0); | |
2720 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2721 return PR_FAILURE; | |
2722 } | |
2723 | |
2724 static PRInt32 PR_CALLBACK | |
2725 ssl_Seek(PRFileDesc *fd, PRInt32 offset, PRSeekWhence how) { | |
2726 PORT_Assert(0); | |
2727 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2728 return SECFailure; | |
2729 } | |
2730 | |
2731 static PRInt64 PR_CALLBACK | |
2732 ssl_Seek64(PRFileDesc *fd, PRInt64 offset, PRSeekWhence how) { | |
2733 PRInt64 res; | |
2734 | |
2735 PORT_Assert(0); | |
2736 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2737 LL_I2L(res, -1L); | |
2738 return res; | |
2739 } | |
2740 | |
2741 static PRStatus PR_CALLBACK | |
2742 ssl_FileInfo(PRFileDesc *fd, PRFileInfo *info) | |
2743 { | |
2744 PORT_Assert(0); | |
2745 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2746 return PR_FAILURE; | |
2747 } | |
2748 | |
2749 static PRStatus PR_CALLBACK | |
2750 ssl_FileInfo64(PRFileDesc *fd, PRFileInfo64 *info) | |
2751 { | |
2752 PORT_Assert(0); | |
2753 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2754 return PR_FAILURE; | |
2755 } | |
2756 | |
2757 static PRInt32 PR_CALLBACK | |
2758 ssl_RecvFrom(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, | |
2759 PRNetAddr *addr, PRIntervalTime timeout) | |
2760 { | |
2761 PORT_Assert(0); | |
2762 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2763 return SECFailure; | |
2764 } | |
2765 | |
2766 static PRInt32 PR_CALLBACK | |
2767 ssl_SendTo(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, | |
2768 const PRNetAddr *addr, PRIntervalTime timeout) | |
2769 { | |
2770 PORT_Assert(0); | |
2771 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
2772 return SECFailure; | |
2773 } | |
2774 | |
2775 static const PRIOMethods ssl_methods = { | |
2776 PR_DESC_LAYERED, | |
2777 ssl_Close, /* close */ | |
2778 ssl_Read, /* read */ | |
2779 ssl_Write, /* write */ | |
2780 ssl_Available, /* available */ | |
2781 ssl_Available64, /* available64 */ | |
2782 ssl_FSync, /* fsync */ | |
2783 ssl_Seek, /* seek */ | |
2784 ssl_Seek64, /* seek64 */ | |
2785 ssl_FileInfo, /* fileInfo */ | |
2786 ssl_FileInfo64, /* fileInfo64 */ | |
2787 ssl_WriteV, /* writev */ | |
2788 ssl_Connect, /* connect */ | |
2789 ssl_Accept, /* accept */ | |
2790 ssl_Bind, /* bind */ | |
2791 ssl_Listen, /* listen */ | |
2792 ssl_Shutdown, /* shutdown */ | |
2793 ssl_Recv, /* recv */ | |
2794 ssl_Send, /* send */ | |
2795 ssl_RecvFrom, /* recvfrom */ | |
2796 ssl_SendTo, /* sendto */ | |
2797 ssl_Poll, /* poll */ | |
2798 PR_EmulateAcceptRead, /* acceptread */ | |
2799 ssl_TransmitFile, /* transmitfile */ | |
2800 ssl_GetSockName, /* getsockname */ | |
2801 ssl_GetPeerName, /* getpeername */ | |
2802 NULL, /* getsockopt OBSOLETE */ | |
2803 NULL, /* setsockopt OBSOLETE */ | |
2804 NULL, /* getsocketoption */ | |
2805 NULL, /* setsocketoption */ | |
2806 PR_EmulateSendFile, /* Send a (partial) file with header/trailer*/ | |
2807 NULL, /* reserved for future use */ | |
2808 NULL, /* reserved for future use */ | |
2809 NULL, /* reserved for future use */ | |
2810 NULL, /* reserved for future use */ | |
2811 NULL /* reserved for future use */ | |
2812 }; | |
2813 | |
2814 | |
2815 static PRIOMethods combined_methods; | |
2816 | |
2817 static void | |
2818 ssl_SetupIOMethods(void) | |
2819 { | |
2820 PRIOMethods *new_methods = &combined_methods; | |
2821 const PRIOMethods *nspr_methods = PR_GetDefaultIOMethods(); | |
2822 const PRIOMethods *my_methods = &ssl_methods; | |
2823 | |
2824 *new_methods = *nspr_methods; | |
2825 | |
2826 new_methods->file_type = my_methods->file_type; | |
2827 new_methods->close = my_methods->close; | |
2828 new_methods->read = my_methods->read; | |
2829 new_methods->write = my_methods->write; | |
2830 new_methods->available = my_methods->available; | |
2831 new_methods->available64 = my_methods->available64; | |
2832 new_methods->fsync = my_methods->fsync; | |
2833 new_methods->seek = my_methods->seek; | |
2834 new_methods->seek64 = my_methods->seek64; | |
2835 new_methods->fileInfo = my_methods->fileInfo; | |
2836 new_methods->fileInfo64 = my_methods->fileInfo64; | |
2837 new_methods->writev = my_methods->writev; | |
2838 new_methods->connect = my_methods->connect; | |
2839 new_methods->accept = my_methods->accept; | |
2840 new_methods->bind = my_methods->bind; | |
2841 new_methods->listen = my_methods->listen; | |
2842 new_methods->shutdown = my_methods->shutdown; | |
2843 new_methods->recv = my_methods->recv; | |
2844 new_methods->send = my_methods->send; | |
2845 new_methods->recvfrom = my_methods->recvfrom; | |
2846 new_methods->sendto = my_methods->sendto; | |
2847 new_methods->poll = my_methods->poll; | |
2848 new_methods->acceptread = my_methods->acceptread; | |
2849 new_methods->transmitfile = my_methods->transmitfile; | |
2850 new_methods->getsockname = my_methods->getsockname; | |
2851 new_methods->getpeername = my_methods->getpeername; | |
2852 /* new_methods->getsocketoption = my_methods->getsocketoption; */ | |
2853 /* new_methods->setsocketoption = my_methods->setsocketoption; */ | |
2854 new_methods->sendfile = my_methods->sendfile; | |
2855 | |
2856 } | |
2857 | |
2858 static PRCallOnceType initIoLayerOnce; | |
2859 | |
2860 static PRStatus | |
2861 ssl_InitIOLayer(void) | |
2862 { | |
2863 ssl_layer_id = PR_GetUniqueIdentity("SSL"); | |
2864 ssl_SetupIOMethods(); | |
2865 ssl_inited = PR_TRUE; | |
2866 return PR_SUCCESS; | |
2867 } | |
2868 | |
2869 static PRStatus | |
2870 ssl_PushIOLayer(sslSocket *ns, PRFileDesc *stack, PRDescIdentity id) | |
2871 { | |
2872 PRFileDesc *layer = NULL; | |
2873 PRStatus status; | |
2874 | |
2875 if (!ssl_inited) { | |
2876 status = PR_CallOnce(&initIoLayerOnce, &ssl_InitIOLayer); | |
2877 if (status != PR_SUCCESS) | |
2878 goto loser; | |
2879 } | |
2880 | |
2881 if (ns == NULL) | |
2882 goto loser; | |
2883 | |
2884 layer = PR_CreateIOLayerStub(ssl_layer_id, &combined_methods); | |
2885 if (layer == NULL) | |
2886 goto loser; | |
2887 layer->secret = (PRFilePrivate *)ns; | |
2888 | |
2889 /* Here, "stack" points to the PRFileDesc on the top of the stack. | |
2890 ** "layer" points to a new FD that is to be inserted into the stack. | |
2891 ** If layer is being pushed onto the top of the stack, then | |
2892 ** PR_PushIOLayer switches the contents of stack and layer, and then | |
2893 ** puts stack on top of layer, so that after it is done, the top of | |
2894 ** stack is the same "stack" as it was before, and layer is now the | |
2895 ** FD for the former top of stack. | |
2896 ** After this call, stack always points to the top PRFD on the stack. | |
2897 ** If this function fails, the contents of stack and layer are as | |
2898 ** they were before the call. | |
2899 */ | |
2900 status = PR_PushIOLayer(stack, id, layer); | |
2901 if (status != PR_SUCCESS) | |
2902 goto loser; | |
2903 | |
2904 ns->fd = (id == PR_TOP_IO_LAYER) ? stack : layer; | |
2905 return PR_SUCCESS; | |
2906 | |
2907 loser: | |
2908 if (layer) { | |
2909 layer->dtor(layer); /* free layer */ | |
2910 } | |
2911 return PR_FAILURE; | |
2912 } | |
2913 | |
2914 /* if this fails, caller must destroy socket. */ | |
2915 static SECStatus | |
2916 ssl_MakeLocks(sslSocket *ss) | |
2917 { | |
2918 ss->firstHandshakeLock = PZ_NewMonitor(nssILockSSL); | |
2919 if (!ss->firstHandshakeLock) | |
2920 goto loser; | |
2921 ss->ssl3HandshakeLock = PZ_NewMonitor(nssILockSSL); | |
2922 if (!ss->ssl3HandshakeLock) | |
2923 goto loser; | |
2924 ss->specLock = NSSRWLock_New(SSL_LOCK_RANK_SPEC, NULL); | |
2925 if (!ss->specLock) | |
2926 goto loser; | |
2927 ss->recvBufLock = PZ_NewMonitor(nssILockSSL); | |
2928 if (!ss->recvBufLock) | |
2929 goto loser; | |
2930 ss->xmitBufLock = PZ_NewMonitor(nssILockSSL); | |
2931 if (!ss->xmitBufLock) | |
2932 goto loser; | |
2933 ss->writerThread = NULL; | |
2934 if (ssl_lock_readers) { | |
2935 ss->recvLock = PZ_NewLock(nssILockSSL); | |
2936 if (!ss->recvLock) | |
2937 goto loser; | |
2938 ss->sendLock = PZ_NewLock(nssILockSSL); | |
2939 if (!ss->sendLock) | |
2940 goto loser; | |
2941 } | |
2942 return SECSuccess; | |
2943 loser: | |
2944 ssl_DestroyLocks(ss); | |
2945 return SECFailure; | |
2946 } | |
2947 | |
2948 #if defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_BEOS) | |
2949 #define NSS_HAVE_GETENV 1 | |
2950 #endif | |
2951 | |
2952 #define LOWER(x) (x | 0x20) /* cheap ToLower function ignores LOCALE */ | |
2953 | |
2954 static void | |
2955 ssl_SetDefaultsFromEnvironment(void) | |
2956 { | |
2957 #if defined( NSS_HAVE_GETENV ) | |
2958 static int firsttime = 1; | |
2959 | |
2960 if (firsttime) { | |
2961 char * ev; | |
2962 firsttime = 0; | |
2963 #ifdef DEBUG | |
2964 ev = getenv("SSLDEBUGFILE"); | |
2965 if (ev && ev[0]) { | |
2966 ssl_trace_iob = fopen(ev, "w"); | |
2967 } | |
2968 if (!ssl_trace_iob) { | |
2969 ssl_trace_iob = stderr; | |
2970 } | |
2971 #ifdef TRACE | |
2972 ev = getenv("SSLTRACE"); | |
2973 if (ev && ev[0]) { | |
2974 ssl_trace = atoi(ev); | |
2975 SSL_TRACE(("SSL: tracing set to %d", ssl_trace)); | |
2976 } | |
2977 #endif /* TRACE */ | |
2978 ev = getenv("SSLDEBUG"); | |
2979 if (ev && ev[0]) { | |
2980 ssl_debug = atoi(ev); | |
2981 SSL_TRACE(("SSL: debugging set to %d", ssl_debug)); | |
2982 } | |
2983 #endif /* DEBUG */ | |
2984 ev = getenv("SSLKEYLOGFILE"); | |
2985 if (ev && ev[0]) { | |
2986 ssl_keylog_iob = fopen(ev, "a"); | |
2987 if (!ssl_keylog_iob) { | |
2988 SSL_TRACE(("SSL: failed to open key log file")); | |
2989 } else { | |
2990 if (ftell(ssl_keylog_iob) == 0) { | |
2991 fputs("# SSL/TLS secrets log file, generated by NSS\n", | |
2992 ssl_keylog_iob); | |
2993 } | |
2994 SSL_TRACE(("SSL: logging SSL/TLS secrets to %s", ev)); | |
2995 } | |
2996 } | |
2997 #ifndef NO_PKCS11_BYPASS | |
2998 ev = getenv("SSLBYPASS"); | |
2999 if (ev && ev[0]) { | |
3000 ssl_defaults.bypassPKCS11 = (ev[0] == '1'); | |
3001 SSL_TRACE(("SSL: bypass default set to %d", \ | |
3002 ssl_defaults.bypassPKCS11)); | |
3003 } | |
3004 #endif /* NO_PKCS11_BYPASS */ | |
3005 ev = getenv("SSLFORCELOCKS"); | |
3006 if (ev && ev[0] == '1') { | |
3007 ssl_force_locks = PR_TRUE; | |
3008 ssl_defaults.noLocks = 0; | |
3009 strcpy(lockStatus + LOCKSTATUS_OFFSET, "FORCED. "); | |
3010 SSL_TRACE(("SSL: force_locks set to %d", ssl_force_locks)); | |
3011 } | |
3012 ev = getenv("NSS_SSL_ENABLE_RENEGOTIATION"); | |
3013 if (ev) { | |
3014 if (ev[0] == '1' || LOWER(ev[0]) == 'u') | |
3015 ssl_defaults.enableRenegotiation = SSL_RENEGOTIATE_UNRESTRICTED; | |
3016 else if (ev[0] == '0' || LOWER(ev[0]) == 'n') | |
3017 ssl_defaults.enableRenegotiation = SSL_RENEGOTIATE_NEVER; | |
3018 else if (ev[0] == '2' || LOWER(ev[0]) == 'r') | |
3019 ssl_defaults.enableRenegotiation = SSL_RENEGOTIATE_REQUIRES_XTN; | |
3020 else if (ev[0] == '3' || LOWER(ev[0]) == 't') | |
3021 ssl_defaults.enableRenegotiation = SSL_RENEGOTIATE_TRANSITIONAL; | |
3022 SSL_TRACE(("SSL: enableRenegotiation set to %d", | |
3023 ssl_defaults.enableRenegotiation)); | |
3024 } | |
3025 ev = getenv("NSS_SSL_REQUIRE_SAFE_NEGOTIATION"); | |
3026 if (ev && ev[0] == '1') { | |
3027 ssl_defaults.requireSafeNegotiation = PR_TRUE; | |
3028 SSL_TRACE(("SSL: requireSafeNegotiation set to %d", | |
3029 PR_TRUE)); | |
3030 } | |
3031 ev = getenv("NSS_SSL_CBC_RANDOM_IV"); | |
3032 if (ev && ev[0] == '0') { | |
3033 ssl_defaults.cbcRandomIV = PR_FALSE; | |
3034 SSL_TRACE(("SSL: cbcRandomIV set to 0")); | |
3035 } | |
3036 } | |
3037 #endif /* NSS_HAVE_GETENV */ | |
3038 } | |
3039 | |
3040 /* | |
3041 ** Create a newsocket structure for a file descriptor. | |
3042 */ | |
3043 static sslSocket * | |
3044 ssl_NewSocket(PRBool makeLocks, SSLProtocolVariant protocolVariant) | |
3045 { | |
3046 sslSocket *ss; | |
3047 | |
3048 ssl_SetDefaultsFromEnvironment(); | |
3049 | |
3050 if (ssl_force_locks) | |
3051 makeLocks = PR_TRUE; | |
3052 | |
3053 /* Make a new socket and get it ready */ | |
3054 ss = (sslSocket*) PORT_ZAlloc(sizeof(sslSocket)); | |
3055 if (ss) { | |
3056 /* This should be of type SSLKEAType, but CC on IRIX | |
3057 * complains during the for loop. | |
3058 */ | |
3059 int i; | |
3060 SECStatus status; | |
3061 | |
3062 ss->opt = ssl_defaults; | |
3063 ss->opt.useSocks = PR_FALSE; | |
3064 ss->opt.noLocks = !makeLocks; | |
3065 ss->vrange = *VERSIONS_DEFAULTS(protocolVariant); | |
3066 ss->protocolVariant = protocolVariant; | |
3067 | |
3068 ss->peerID = NULL; | |
3069 ss->rTimeout = PR_INTERVAL_NO_TIMEOUT; | |
3070 ss->wTimeout = PR_INTERVAL_NO_TIMEOUT; | |
3071 ss->cTimeout = PR_INTERVAL_NO_TIMEOUT; | |
3072 ss->cipherSpecs = NULL; | |
3073 ss->sizeCipherSpecs = 0; /* produced lazily */ | |
3074 ss->preferredCipher = NULL; | |
3075 ss->url = NULL; | |
3076 | |
3077 for (i=kt_null; i < kt_kea_size; i++) { | |
3078 sslServerCerts * sc = ss->serverCerts + i; | |
3079 sc->serverCert = NULL; | |
3080 sc->serverCertChain = NULL; | |
3081 sc->serverKeyPair = NULL; | |
3082 sc->serverKeyBits = 0; | |
3083 ss->certStatusArray[i] = NULL; | |
3084 } | |
3085 ss->requestedCertTypes = NULL; | |
3086 ss->stepDownKeyPair = NULL; | |
3087 ss->dbHandle = CERT_GetDefaultCertDB(); | |
3088 | |
3089 /* Provide default implementation of hooks */ | |
3090 ss->authCertificate = SSL_AuthCertificate; | |
3091 ss->authCertificateArg = (void *)ss->dbHandle; | |
3092 ss->sniSocketConfig = NULL; | |
3093 ss->sniSocketConfigArg = NULL; | |
3094 ss->getClientAuthData = NULL; | |
3095 #ifdef NSS_PLATFORM_CLIENT_AUTH | |
3096 ss->getPlatformClientAuthData = NULL; | |
3097 ss->getPlatformClientAuthDataArg = NULL; | |
3098 #endif /* NSS_PLATFORM_CLIENT_AUTH */ | |
3099 ss->handleBadCert = NULL; | |
3100 ss->badCertArg = NULL; | |
3101 ss->pkcs11PinArg = NULL; | |
3102 ss->ephemeralECDHKeyPair = NULL; | |
3103 ss->getChannelID = NULL; | |
3104 ss->getChannelIDArg = NULL; | |
3105 | |
3106 ssl_ChooseOps(ss); | |
3107 ssl2_InitSocketPolicy(ss); | |
3108 ssl3_InitSocketPolicy(ss); | |
3109 PR_INIT_CLIST(&ss->ssl3.hs.lastMessageFlight); | |
3110 | |
3111 if (makeLocks) { | |
3112 status = ssl_MakeLocks(ss); | |
3113 if (status != SECSuccess) | |
3114 goto loser; | |
3115 } | |
3116 status = ssl_CreateSecurityInfo(ss); | |
3117 if (status != SECSuccess) | |
3118 goto loser; | |
3119 status = ssl_InitGather(&ss->gs); | |
3120 if (status != SECSuccess) { | |
3121 loser: | |
3122 ssl_DestroySocketContents(ss); | |
3123 ssl_DestroyLocks(ss); | |
3124 PORT_Free(ss); | |
3125 ss = NULL; | |
3126 } | |
3127 } | |
3128 return ss; | |
3129 } | |
3130 | |
OLD | NEW |