2 * SSL v2 handshake functions, and functions common to SSL2 and SSL3.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12 #include "cryptohi.h" /* for SGN_ funcs */
13 #include "keyhi.h" /* for SECKEY_ high level functions. */
21 #include "prtime.h" /* for PR_Now() */
23 static PRBool policyWasSet
;
25 /* This ordered list is indexed by (SSL_CK_xx * 3) */
26 /* Second and third bytes are MSB and LSB of master key length. */
27 static const PRUint8 allCipherSuites
[] = {
29 SSL_CK_RC4_128_WITH_MD5
, 0x00, 0x80,
30 SSL_CK_RC4_128_EXPORT40_WITH_MD5
, 0x00, 0x80,
31 SSL_CK_RC2_128_CBC_WITH_MD5
, 0x00, 0x80,
32 SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
, 0x00, 0x80,
33 SSL_CK_IDEA_128_CBC_WITH_MD5
, 0x00, 0x80,
34 SSL_CK_DES_64_CBC_WITH_MD5
, 0x00, 0x40,
35 SSL_CK_DES_192_EDE3_CBC_WITH_MD5
, 0x00, 0xC0,
39 #define ssl2_NUM_SUITES_IMPLEMENTED 6
41 /* This list is sent back to the client when the client-hello message
42 * contains no overlapping ciphers, so the client can report what ciphers
43 * are supported by the server. Unlike allCipherSuites (above), this list
44 * is sorted by descending preference, not by cipherSuite number.
46 static const PRUint8 implementedCipherSuites
[ssl2_NUM_SUITES_IMPLEMENTED
* 3] = {
47 SSL_CK_RC4_128_WITH_MD5
, 0x00, 0x80,
48 SSL_CK_RC2_128_CBC_WITH_MD5
, 0x00, 0x80,
49 SSL_CK_DES_192_EDE3_CBC_WITH_MD5
, 0x00, 0xC0,
50 SSL_CK_DES_64_CBC_WITH_MD5
, 0x00, 0x40,
51 SSL_CK_RC4_128_EXPORT40_WITH_MD5
, 0x00, 0x80,
52 SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
, 0x00, 0x80
55 typedef struct ssl2SpecsStr
{
56 PRUint8 nkm
; /* do this many hashes to generate key material. */
57 PRUint8 nkd
; /* size of readKey and writeKey in bytes. */
60 CK_MECHANISM_TYPE mechanism
;
61 PRUint8 keyLen
; /* cipher symkey size in bytes. */
62 PRUint8 pubLen
; /* publicly reveal this many bytes of key. */
63 PRUint8 ivLen
; /* length of IV data at *ca. */
66 static const ssl2Specs ssl_Specs
[] = {
69 /* SSL_CK_RC4_128_WITH_MD5 */
70 { 2, 16, 1, 0, CKM_RC4
, 16, 0, 0, },
71 /* SSL_CK_RC4_128_EXPORT40_WITH_MD5 */
72 { 2, 16, 1, 0, CKM_RC4
, 16, 11, 0, },
73 /* SSL_CK_RC2_128_CBC_WITH_MD5 */
74 { 2, 16, 8, 3, CKM_RC2_CBC
, 16, 0, 8, },
75 /* SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5 */
76 { 2, 16, 8, 3, CKM_RC2_CBC
, 16, 11, 8, },
77 /* SSL_CK_IDEA_128_CBC_WITH_MD5 */
79 /* SSL_CK_DES_64_CBC_WITH_MD5 */
80 { 1, 8, 8, 3, CKM_DES_CBC
, 8, 0, 8, },
81 /* SSL_CK_DES_192_EDE3_CBC_WITH_MD5 */
82 { 3, 24, 8, 3, CKM_DES3_CBC
, 24, 0, 8, },
85 #define SET_ERROR_CODE /* reminder */
86 #define TEST_FOR_FAILURE /* reminder */
89 ** Put a string tag in the library so that we can examine an executable
90 ** and see what kind of security it supports.
92 const char *ssl_version
= "SECURITY_VERSION:"
103 const char * const ssl_cipherName
[] = {
113 "unknown", /* was fortezza, NO LONGER USED */
117 /* bit-masks, showing which SSLv2 suites are allowed.
118 * lsb corresponds to first cipher suite in allCipherSuites[].
120 static PRUint16 allowedByPolicy
; /* all off by default */
121 static PRUint16 maybeAllowedByPolicy
; /* all off by default */
122 static PRUint16 chosenPreference
= 0xff; /* all on by default */
124 /* bit values for the above two bit masks */
125 #define SSL_CB_RC4_128_WITH_MD5 (1 << SSL_CK_RC4_128_WITH_MD5)
126 #define SSL_CB_RC4_128_EXPORT40_WITH_MD5 (1 << SSL_CK_RC4_128_EXPORT40_WITH_MD5)
127 #define SSL_CB_RC2_128_CBC_WITH_MD5 (1 << SSL_CK_RC2_128_CBC_WITH_MD5)
128 #define SSL_CB_RC2_128_CBC_EXPORT40_WITH_MD5 (1 << SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5)
129 #define SSL_CB_IDEA_128_CBC_WITH_MD5 (1 << SSL_CK_IDEA_128_CBC_WITH_MD5)
130 #define SSL_CB_DES_64_CBC_WITH_MD5 (1 << SSL_CK_DES_64_CBC_WITH_MD5)
131 #define SSL_CB_DES_192_EDE3_CBC_WITH_MD5 (1 << SSL_CK_DES_192_EDE3_CBC_WITH_MD5)
132 #define SSL_CB_IMPLEMENTED \
133 (SSL_CB_RC4_128_WITH_MD5 | \
134 SSL_CB_RC4_128_EXPORT40_WITH_MD5 | \
135 SSL_CB_RC2_128_CBC_WITH_MD5 | \
136 SSL_CB_RC2_128_CBC_EXPORT40_WITH_MD5 | \
137 SSL_CB_DES_64_CBC_WITH_MD5 | \
138 SSL_CB_DES_192_EDE3_CBC_WITH_MD5)
141 /* Construct a socket's list of cipher specs from the global default values.
144 ssl2_ConstructCipherSpecs(sslSocket
*ss
)
147 unsigned int allowed
;
154 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
157 PORT_Assert(ss
!= 0);
158 allowed
= !ss
->opt
.enableSSL2
? 0 :
159 (ss
->allowedByPolicy
& ss
->chosenPreference
& SSL_CB_IMPLEMENTED
);
166 /* Call ssl3_config_match_init() once here,
167 * instead of inside ssl3_ConstructV2CipherSpecsHack(),
168 * because the latter gets called twice below,
169 * and then again in ssl2_BeginClientHandshake().
171 ssl3_config_match_init(ss
);
173 /* ask SSL3 how many cipher suites it has. */
174 rv
= ssl3_ConstructV2CipherSpecsHack(ss
, NULL
, &ssl3_count
);
179 /* Allocate memory to hold cipher specs */
181 cs
= (PRUint8
*) PORT_Alloc(count
* 3);
183 PORT_SetError(SSL_ERROR_SSL_DISABLED
);
187 if (ss
->cipherSpecs
!= NULL
) {
188 PORT_Free(ss
->cipherSpecs
);
190 ss
->cipherSpecs
= cs
;
191 ss
->sizeCipherSpecs
= count
* 3;
193 /* fill in cipher specs for SSL2 cipher suites */
194 allowed
= !ss
->opt
.enableSSL2
? 0 :
195 (ss
->allowedByPolicy
& ss
->chosenPreference
& SSL_CB_IMPLEMENTED
);
196 for (i
= 0; i
< ssl2_NUM_SUITES_IMPLEMENTED
* 3; i
+= 3) {
197 const PRUint8
* hs
= implementedCipherSuites
+ i
;
198 int ok
= allowed
& (1U << hs
[0]);
207 /* now have SSL3 add its suites onto the end */
208 rv
= ssl3_ConstructV2CipherSpecsHack(ss
, cs
, &final_count
);
210 /* adjust for any difference between first pass and second pass */
211 ss
->sizeCipherSpecs
-= (ssl3_count
- final_count
) * 3;
216 /* This function is called immediately after ssl2_ConstructCipherSpecs()
217 ** at the beginning of a handshake. It detects cases where a protocol
218 ** (e.g. SSL2 or SSL3) is logically enabled, but all its cipher suites
219 ** for that protocol have been disabled. If such cases, it clears the
220 ** enable bit for the protocol. If no protocols remain enabled, or
221 ** if no cipher suites are found, it sets the error code and returns
222 ** SECFailure, otherwise it returns SECSuccess.
225 ssl2_CheckConfigSanity(sslSocket
*ss
)
227 unsigned int allowed
;
228 int ssl3CipherCount
= 0;
231 /* count the SSL2 and SSL3 enabled ciphers.
232 * if either is zero, clear the socket's enable for that protocol.
234 if (!ss
->cipherSpecs
)
237 allowed
= ss
->allowedByPolicy
& ss
->chosenPreference
;
239 ss
->opt
.enableSSL2
= PR_FALSE
; /* not really enabled if no ciphers */
241 /* ssl3_config_match_init was called in ssl2_ConstructCipherSpecs(). */
242 /* Ask how many ssl3 CipherSuites were enabled. */
243 rv
= ssl3_ConstructV2CipherSpecsHack(ss
, NULL
, &ssl3CipherCount
);
244 if (rv
!= SECSuccess
|| ssl3CipherCount
<= 0) {
245 /* SSL3/TLS not really enabled if no ciphers */
246 ss
->vrange
.min
= SSL_LIBRARY_VERSION_NONE
;
247 ss
->vrange
.max
= SSL_LIBRARY_VERSION_NONE
;
250 if (!ss
->opt
.enableSSL2
&& SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
)) {
251 SSL_DBG(("%d: SSL[%d]: Can't handshake! all versions disabled.",
252 SSL_GETPID(), ss
->fd
));
254 PORT_SetError(SSL_ERROR_SSL_DISABLED
);
261 * Since this is a global (not per-socket) setting, we cannot use the
262 * HandshakeLock to protect this. Probably want a global lock.
265 ssl2_SetPolicy(PRInt32 which
, PRInt32 policy
)
268 SECStatus rv
= SECSuccess
;
271 bitMask
= 1 << which
;
273 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
274 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
278 if (policy
== SSL_ALLOWED
) {
279 allowedByPolicy
|= bitMask
;
280 maybeAllowedByPolicy
|= bitMask
;
281 } else if (policy
== SSL_RESTRICTED
) {
282 allowedByPolicy
&= ~bitMask
;
283 maybeAllowedByPolicy
|= bitMask
;
285 allowedByPolicy
&= ~bitMask
;
286 maybeAllowedByPolicy
&= ~bitMask
;
288 allowedByPolicy
&= SSL_CB_IMPLEMENTED
;
289 maybeAllowedByPolicy
&= SSL_CB_IMPLEMENTED
;
291 policyWasSet
= PR_TRUE
;
296 ssl2_GetPolicy(PRInt32 which
, PRInt32
*oPolicy
)
302 bitMask
= 1 << which
;
304 /* Caller assures oPolicy is not null. */
305 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
306 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
307 *oPolicy
= SSL_NOT_ALLOWED
;
311 if (maybeAllowedByPolicy
& bitMask
) {
312 policy
= (allowedByPolicy
& bitMask
) ? SSL_ALLOWED
: SSL_RESTRICTED
;
314 policy
= SSL_NOT_ALLOWED
;
322 * Since this is a global (not per-socket) setting, we cannot use the
323 * HandshakeLock to protect this. Probably want a global lock.
324 * Called from SSL_CipherPrefSetDefault in sslsock.c
325 * These changes have no effect on any sslSockets already created.
328 ssl2_CipherPrefSetDefault(PRInt32 which
, PRBool enabled
)
333 bitMask
= 1 << which
;
335 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
336 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
341 chosenPreference
|= bitMask
;
343 chosenPreference
&= ~bitMask
;
344 chosenPreference
&= SSL_CB_IMPLEMENTED
;
350 ssl2_CipherPrefGetDefault(PRInt32 which
, PRBool
*enabled
)
352 PRBool rv
= PR_FALSE
;
356 bitMask
= 1 << which
;
358 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
359 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
364 rv
= (PRBool
)((chosenPreference
& bitMask
) != 0);
370 ssl2_CipherPrefSet(sslSocket
*ss
, PRInt32 which
, PRBool enabled
)
375 bitMask
= 1 << which
;
377 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
378 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
383 ss
->chosenPreference
|= bitMask
;
385 ss
->chosenPreference
&= ~bitMask
;
386 ss
->chosenPreference
&= SSL_CB_IMPLEMENTED
;
392 ssl2_CipherPrefGet(sslSocket
*ss
, PRInt32 which
, PRBool
*enabled
)
394 PRBool rv
= PR_FALSE
;
398 bitMask
= 1 << which
;
400 if (!(bitMask
& SSL_CB_IMPLEMENTED
)) {
401 PORT_SetError(SSL_ERROR_UNKNOWN_CIPHER_SUITE
);
406 rv
= (PRBool
)((ss
->chosenPreference
& bitMask
) != 0);
412 /* copy global default policy into socket. */
414 ssl2_InitSocketPolicy(sslSocket
*ss
)
416 ss
->allowedByPolicy
= allowedByPolicy
;
417 ss
->maybeAllowedByPolicy
= maybeAllowedByPolicy
;
418 ss
->chosenPreference
= chosenPreference
;
422 /************************************************************************/
424 /* Called from ssl2_CreateSessionCypher(), which already holds handshake lock.
427 ssl2_CreateMAC(sslSecurityInfo
*sec
, SECItem
*readKey
, SECItem
*writeKey
,
430 switch (cipherChoice
) {
432 case SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
:
433 case SSL_CK_RC2_128_CBC_WITH_MD5
:
434 case SSL_CK_RC4_128_EXPORT40_WITH_MD5
:
435 case SSL_CK_RC4_128_WITH_MD5
:
436 case SSL_CK_DES_64_CBC_WITH_MD5
:
437 case SSL_CK_DES_192_EDE3_CBC_WITH_MD5
:
438 sec
->hash
= HASH_GetHashObject(HASH_AlgMD5
);
439 SECITEM_CopyItem(0, &sec
->sendSecret
, writeKey
);
440 SECITEM_CopyItem(0, &sec
->rcvSecret
, readKey
);
444 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP
);
447 sec
->hashcx
= (*sec
->hash
->create
)();
448 if (sec
->hashcx
== NULL
)
453 /************************************************************************
454 * All the Send functions below must acquire and release the socket's
458 /* Called from all the Send* functions below. */
460 ssl2_GetSendBuffer(sslSocket
*ss
, unsigned int len
)
462 SECStatus rv
= SECSuccess
;
464 PORT_Assert(ss
->opt
.noLocks
|| ssl_HaveXmitBufLock(ss
));
469 if (len
> ss
->sec
.ci
.sendBuf
.space
) {
470 rv
= sslBuffer_Grow(&ss
->sec
.ci
.sendBuf
, len
);
471 if (rv
!= SECSuccess
) {
472 SSL_DBG(("%d: SSL[%d]: ssl2_GetSendBuffer failed, tried to get %d bytes",
473 SSL_GETPID(), ss
->fd
, len
));
481 * ssl2_ClientSetupSessionCypher() <- ssl2_HandleServerHelloMessage()
482 * ssl2_HandleRequestCertificate() <- ssl2_HandleMessage() <-
484 * ssl2_HandleMessage() <- ssl_Do1stHandshake()
485 * ssl2_HandleServerHelloMessage() <- ssl_Do1stHandshake()
486 after ssl2_BeginClientHandshake()
487 * ssl2_HandleClientHelloMessage() <- ssl_Do1stHandshake()
488 after ssl2_BeginServerHandshake()
490 * Acquires and releases the socket's xmitBufLock.
493 ssl2_SendErrorMessage(sslSocket
*ss
, int error
)
496 PRUint8 msg
[SSL_HL_ERROR_HBYTES
];
498 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
500 msg
[0] = SSL_MT_ERROR
;
504 ssl_GetXmitBufLock(ss
); /***************************************/
506 SSL_TRC(3, ("%d: SSL[%d]: sending error %d", SSL_GETPID(), ss
->fd
, error
));
508 ss
->handshakeBegun
= 1;
509 rv
= (*ss
->sec
.send
)(ss
, msg
, sizeof(msg
), 0);
513 ssl_ReleaseXmitBufLock(ss
); /***************************************/
517 /* Called from ssl2_TryToFinish().
518 * Acquires and releases the socket's xmitBufLock.
521 ssl2_SendClientFinishedMessage(sslSocket
*ss
)
523 SECStatus rv
= SECSuccess
;
525 PRUint8 msg
[1 + SSL_CONNECTIONID_BYTES
];
527 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
529 ssl_GetXmitBufLock(ss
); /***************************************/
531 if (ss
->sec
.ci
.sentFinished
== 0) {
532 ss
->sec
.ci
.sentFinished
= 1;
534 SSL_TRC(3, ("%d: SSL[%d]: sending client-finished",
535 SSL_GETPID(), ss
->fd
));
537 msg
[0] = SSL_MT_CLIENT_FINISHED
;
538 PORT_Memcpy(msg
+1, ss
->sec
.ci
.connectionID
,
539 sizeof(ss
->sec
.ci
.connectionID
));
541 DUMP_MSG(29, (ss
, msg
, 1 + sizeof(ss
->sec
.ci
.connectionID
)));
542 sent
= (*ss
->sec
.send
)(ss
, msg
, 1 + sizeof(ss
->sec
.ci
.connectionID
), 0);
543 rv
= (sent
>= 0) ? SECSuccess
: (SECStatus
)sent
;
545 ssl_ReleaseXmitBufLock(ss
); /***************************************/
550 * ssl2_HandleClientSessionKeyMessage() <- ssl2_HandleClientHelloMessage()
551 * ssl2_HandleClientHelloMessage() <- ssl_Do1stHandshake()
552 after ssl2_BeginServerHandshake()
553 * Acquires and releases the socket's xmitBufLock.
556 ssl2_SendServerVerifyMessage(sslSocket
*ss
)
563 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
565 ssl_GetXmitBufLock(ss
); /***************************************/
567 sendLen
= 1 + SSL_CHALLENGE_BYTES
;
568 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
569 if (rv
!= SECSuccess
) {
573 msg
= ss
->sec
.ci
.sendBuf
.buf
;
574 msg
[0] = SSL_MT_SERVER_VERIFY
;
575 PORT_Memcpy(msg
+1, ss
->sec
.ci
.clientChallenge
, SSL_CHALLENGE_BYTES
);
577 DUMP_MSG(29, (ss
, msg
, sendLen
));
578 sent
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
580 rv
= (sent
>= 0) ? SECSuccess
: (SECStatus
)sent
;
583 ssl_ReleaseXmitBufLock(ss
); /***************************************/
587 /* Called from ssl2_TryToFinish().
588 * Acquires and releases the socket's xmitBufLock.
591 ssl2_SendServerFinishedMessage(sslSocket
*ss
)
596 SECStatus rv
= SECSuccess
;
598 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
600 ssl_GetXmitBufLock(ss
); /***************************************/
602 if (ss
->sec
.ci
.sentFinished
== 0) {
603 ss
->sec
.ci
.sentFinished
= 1;
604 PORT_Assert(ss
->sec
.ci
.sid
!= 0);
605 sid
= ss
->sec
.ci
.sid
;
607 SSL_TRC(3, ("%d: SSL[%d]: sending server-finished",
608 SSL_GETPID(), ss
->fd
));
610 sendLen
= 1 + sizeof(sid
->u
.ssl2
.sessionID
);
611 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
612 if (rv
!= SECSuccess
) {
616 msg
= ss
->sec
.ci
.sendBuf
.buf
;
617 msg
[0] = SSL_MT_SERVER_FINISHED
;
618 PORT_Memcpy(msg
+1, sid
->u
.ssl2
.sessionID
,
619 sizeof(sid
->u
.ssl2
.sessionID
));
621 DUMP_MSG(29, (ss
, msg
, sendLen
));
622 sent
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
625 /* If send failed, it is now a bogus session-id */
627 (*ss
->sec
.uncache
)(sid
);
628 rv
= (SECStatus
)sent
;
629 } else if (!ss
->opt
.noCache
) {
630 if (sid
->cached
== never_cached
) {
631 (*ss
->sec
.cache
)(sid
);
639 ssl_ReleaseXmitBufLock(ss
); /***************************************/
643 /* Called from ssl2_ClientSetupSessionCypher() <-
644 * ssl2_HandleServerHelloMessage()
645 * after ssl2_BeginClientHandshake()
646 * Acquires and releases the socket's xmitBufLock.
649 ssl2_SendSessionKeyMessage(sslSocket
*ss
, int cipher
, int keySize
,
650 PRUint8
*ca
, int caLen
,
651 PRUint8
*ck
, int ckLen
,
652 PRUint8
*ek
, int ekLen
)
659 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
661 ssl_GetXmitBufLock(ss
); /***************************************/
663 sendLen
= SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ ckLen
+ ekLen
+ caLen
;
664 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
665 if (rv
!= SECSuccess
)
668 SSL_TRC(3, ("%d: SSL[%d]: sending client-session-key",
669 SSL_GETPID(), ss
->fd
));
671 msg
= ss
->sec
.ci
.sendBuf
.buf
;
672 msg
[0] = SSL_MT_CLIENT_MASTER_KEY
;
674 msg
[2] = MSB(keySize
);
675 msg
[3] = LSB(keySize
);
682 PORT_Memcpy(msg
+SSL_HL_CLIENT_MASTER_KEY_HBYTES
, ck
, ckLen
);
683 PORT_Memcpy(msg
+SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ckLen
, ek
, ekLen
);
684 PORT_Memcpy(msg
+SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ckLen
+ekLen
, ca
, caLen
);
686 DUMP_MSG(29, (ss
, msg
, sendLen
));
687 sent
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
688 rv
= (sent
>= 0) ? SECSuccess
: (SECStatus
)sent
;
690 ssl_ReleaseXmitBufLock(ss
); /***************************************/
694 /* Called from ssl2_TriggerNextMessage() <- ssl2_HandleMessage()
695 * Acquires and releases the socket's xmitBufLock.
698 ssl2_SendCertificateRequestMessage(sslSocket
*ss
)
705 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
707 ssl_GetXmitBufLock(ss
); /***************************************/
709 sendLen
= SSL_HL_REQUEST_CERTIFICATE_HBYTES
+ SSL_CHALLENGE_BYTES
;
710 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
711 if (rv
!= SECSuccess
)
714 SSL_TRC(3, ("%d: SSL[%d]: sending certificate request",
715 SSL_GETPID(), ss
->fd
));
717 /* Generate random challenge for client to encrypt */
718 PK11_GenerateRandom(ss
->sec
.ci
.serverChallenge
, SSL_CHALLENGE_BYTES
);
720 msg
= ss
->sec
.ci
.sendBuf
.buf
;
721 msg
[0] = SSL_MT_REQUEST_CERTIFICATE
;
722 msg
[1] = SSL_AT_MD5_WITH_RSA_ENCRYPTION
;
723 PORT_Memcpy(msg
+ SSL_HL_REQUEST_CERTIFICATE_HBYTES
,
724 ss
->sec
.ci
.serverChallenge
, SSL_CHALLENGE_BYTES
);
726 DUMP_MSG(29, (ss
, msg
, sendLen
));
727 sent
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
728 rv
= (sent
>= 0) ? SECSuccess
: (SECStatus
)sent
;
730 ssl_ReleaseXmitBufLock(ss
); /***************************************/
734 /* Called from ssl2_HandleRequestCertificate() <- ssl2_HandleMessage()
735 * Acquires and releases the socket's xmitBufLock.
738 ssl2_SendCertificateResponseMessage(sslSocket
*ss
, SECItem
*cert
,
744 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
746 ssl_GetXmitBufLock(ss
); /***************************************/
748 sendLen
= SSL_HL_CLIENT_CERTIFICATE_HBYTES
+ encCode
->len
+ cert
->len
;
749 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
753 SSL_TRC(3, ("%d: SSL[%d]: sending certificate response",
754 SSL_GETPID(), ss
->fd
));
756 msg
= ss
->sec
.ci
.sendBuf
.buf
;
757 msg
[0] = SSL_MT_CLIENT_CERTIFICATE
;
758 msg
[1] = SSL_CT_X509_CERTIFICATE
;
759 msg
[2] = MSB(cert
->len
);
760 msg
[3] = LSB(cert
->len
);
761 msg
[4] = MSB(encCode
->len
);
762 msg
[5] = LSB(encCode
->len
);
763 PORT_Memcpy(msg
+ SSL_HL_CLIENT_CERTIFICATE_HBYTES
, cert
->data
, cert
->len
);
764 PORT_Memcpy(msg
+ SSL_HL_CLIENT_CERTIFICATE_HBYTES
+ cert
->len
,
765 encCode
->data
, encCode
->len
);
767 DUMP_MSG(29, (ss
, msg
, sendLen
));
768 rv
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
773 ssl_ReleaseXmitBufLock(ss
); /***************************************/
777 /********************************************************************
778 ** Send functions above this line must aquire & release the socket's
780 ** All the ssl2_Send functions below this line are called vis ss->sec.send
781 ** and require that the caller hold the xmitBufLock.
785 ** Called from ssl2_SendStream, ssl2_SendBlock, but not from ssl2_SendClear.
788 ssl2_CalcMAC(PRUint8
* result
,
789 sslSecurityInfo
* sec
,
790 const PRUint8
* data
,
791 unsigned int dataLen
,
792 unsigned int paddingLen
)
794 const PRUint8
* secret
= sec
->sendSecret
.data
;
795 unsigned int secretLen
= sec
->sendSecret
.len
;
796 unsigned long sequenceNumber
= sec
->sendSequence
;
799 PRUint8 padding
[32];/* XXX max blocksize? */
801 if (!sec
->hash
|| !sec
->hash
->length
)
806 /* Reset hash function */
807 (*sec
->hash
->begin
)(sec
->hashcx
);
809 /* Feed hash the data */
810 (*sec
->hash
->update
)(sec
->hashcx
, secret
, secretLen
);
811 (*sec
->hash
->update
)(sec
->hashcx
, data
, dataLen
);
812 PORT_Memset(padding
, paddingLen
, paddingLen
);
813 (*sec
->hash
->update
)(sec
->hashcx
, padding
, paddingLen
);
815 seq
[0] = (PRUint8
) (sequenceNumber
>> 24);
816 seq
[1] = (PRUint8
) (sequenceNumber
>> 16);
817 seq
[2] = (PRUint8
) (sequenceNumber
>> 8);
818 seq
[3] = (PRUint8
) (sequenceNumber
);
820 PRINT_BUF(60, (0, "calc-mac secret:", secret
, secretLen
));
821 PRINT_BUF(60, (0, "calc-mac data:", data
, dataLen
));
822 PRINT_BUF(60, (0, "calc-mac padding:", padding
, paddingLen
));
823 PRINT_BUF(60, (0, "calc-mac seq:", seq
, 4));
825 (*sec
->hash
->update
)(sec
->hashcx
, seq
, 4);
828 (*sec
->hash
->end
)(sec
->hashcx
, result
, &nout
, sec
->hash
->length
);
834 ** Maximum transmission amounts. These are tiny bit smaller than they
835 ** need to be (they account for the MAC length plus some padding),
836 ** assuming the MAC is 16 bytes long and the padding is a max of 7 bytes
837 ** long. This gives an additional 9 bytes of slop to work within.
839 #define MAX_STREAM_CYPHER_LEN 0x7fe0
840 #define MAX_BLOCK_CYPHER_LEN 0x3fe0
843 ** Send some data in the clear.
844 ** Package up data with the length header and send it.
846 ** Return count of bytes successfully written, or negative number (failure).
849 ssl2_SendClear(sslSocket
*ss
, const PRUint8
*in
, PRInt32 len
, PRInt32 flags
)
856 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveXmitBufLock(ss
) );
858 SSL_TRC(10, ("%d: SSL[%d]: sending %d bytes in the clear",
859 SSL_GETPID(), ss
->fd
, len
));
860 PRINT_BUF(50, (ss
, "clear data:", (PRUint8
*) in
, len
));
863 amount
= PR_MIN( len
, MAX_STREAM_CYPHER_LEN
);
864 if (amount
+ 2 > ss
->sec
.writeBuf
.space
) {
865 rv
= sslBuffer_Grow(&ss
->sec
.writeBuf
, amount
+ 2);
866 if (rv
!= SECSuccess
) {
871 out
= ss
->sec
.writeBuf
.buf
;
874 ** Construct message.
876 out
[0] = 0x80 | MSB(amount
);
877 out
[1] = LSB(amount
);
878 PORT_Memcpy(&out
[2], in
, amount
);
880 /* Now send the data */
881 rv
= ssl_DefSend(ss
, out
, amount
+ 2, flags
& ~ssl_SEND_FLAG_MASK
);
883 if (PORT_GetError() == PR_WOULD_BLOCK_ERROR
) {
886 /* Return short write if some data already went out... */
893 if ((unsigned)rv
< (amount
+ 2)) {
894 /* Short write. Save the data and return. */
895 if (ssl_SaveWriteData(ss
, out
+ rv
, amount
+ 2 - rv
)
900 ss
->sec
.sendSequence
++;
905 ss
->sec
.sendSequence
++;
915 ** Send some data, when using a stream cipher. Stream ciphers have a
916 ** block size of 1. Package up the data with the length header
920 ssl2_SendStream(sslSocket
*ss
, const PRUint8
*in
, PRInt32 len
, PRInt32 flags
)
931 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveXmitBufLock(ss
) );
933 SSL_TRC(10, ("%d: SSL[%d]: sending %d bytes using stream cipher",
934 SSL_GETPID(), ss
->fd
, len
));
935 PRINT_BUF(50, (ss
, "clear data:", (PRUint8
*) in
, len
));
938 ssl_GetSpecReadLock(ss
); /*************************************/
940 macLen
= ss
->sec
.hash
->length
;
941 amount
= PR_MIN( len
, MAX_STREAM_CYPHER_LEN
);
942 buflen
= amount
+ 2 + macLen
;
943 if (buflen
> ss
->sec
.writeBuf
.space
) {
944 rv
= sslBuffer_Grow(&ss
->sec
.writeBuf
, buflen
);
945 if (rv
!= SECSuccess
) {
949 out
= ss
->sec
.writeBuf
.buf
;
950 nout
= amount
+ macLen
;
951 out
[0] = 0x80 | MSB(nout
);
955 rv
= ssl2_CalcMAC(out
+2, /* put MAC here */
957 in
, amount
, /* input addr & length */
959 if (rv
!= SECSuccess
)
963 rv
= (*ss
->sec
.enc
)(ss
->sec
.writecx
, out
+2, &nout
, macLen
, out
+2, macLen
);
966 /* Encrypt data from caller */
967 rv
= (*ss
->sec
.enc
)(ss
->sec
.writecx
, out
+2+macLen
, &nout
, amount
, in
, amount
);
970 ssl_ReleaseSpecReadLock(ss
); /*************************************/
972 PRINT_BUF(50, (ss
, "encrypted data:", out
, buflen
));
974 rv
= ssl_DefSend(ss
, out
, buflen
, flags
& ~ssl_SEND_FLAG_MASK
);
976 if (PORT_GetError() == PR_WOULD_BLOCK_ERROR
) {
977 SSL_TRC(50, ("%d: SSL[%d]: send stream would block, "
978 "saving data", SSL_GETPID(), ss
->fd
));
981 SSL_TRC(10, ("%d: SSL[%d]: send stream error %d",
982 SSL_GETPID(), ss
->fd
, PORT_GetError()));
983 /* Return short write if some data already went out... */
990 if ((unsigned)rv
< buflen
) {
991 /* Short write. Save the data and return. */
992 if (ssl_SaveWriteData(ss
, out
+ rv
, buflen
- rv
) == SECFailure
) {
996 ss
->sec
.sendSequence
++;
1001 ss
->sec
.sendSequence
++;
1011 ssl_ReleaseSpecReadLock(ss
);
1016 ** Send some data, when using a block cipher. Package up the data with
1017 ** the length header and send it.
1019 /* XXX assumes blocksize is > 7 */
1021 ssl2_SendBlock(sslSocket
*ss
, const PRUint8
*in
, PRInt32 len
, PRInt32 flags
)
1023 PRUint8
* out
; /* begining of output buffer. */
1024 PRUint8
* op
; /* next output byte goes here. */
1025 int rv
; /* value from funcs we called. */
1026 int count
= 0; /* this function's return value. */
1028 unsigned int hlen
; /* output record hdr len, 2 or 3 */
1029 unsigned int macLen
; /* MAC is this many bytes long. */
1030 int amount
; /* of plaintext to go in record. */
1031 unsigned int padding
; /* add this many padding byte. */
1032 int nout
; /* ciphertext size after header. */
1033 int buflen
; /* size of generated record. */
1035 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveXmitBufLock(ss
) );
1037 SSL_TRC(10, ("%d: SSL[%d]: sending %d bytes using block cipher",
1038 SSL_GETPID(), ss
->fd
, len
));
1039 PRINT_BUF(50, (ss
, "clear data:", in
, len
));
1042 ssl_GetSpecReadLock(ss
); /*************************************/
1044 macLen
= ss
->sec
.hash
->length
;
1045 /* Figure out how much to send, including mac and padding */
1046 amount
= PR_MIN( len
, MAX_BLOCK_CYPHER_LEN
);
1047 nout
= amount
+ macLen
;
1048 padding
= nout
& (ss
->sec
.blockSize
- 1);
1051 padding
= ss
->sec
.blockSize
- padding
;
1056 buflen
= hlen
+ nout
;
1057 if (buflen
> ss
->sec
.writeBuf
.space
) {
1058 rv
= sslBuffer_Grow(&ss
->sec
.writeBuf
, buflen
);
1059 if (rv
!= SECSuccess
) {
1063 out
= ss
->sec
.writeBuf
.buf
;
1065 /* Construct header */
1072 *op
++ = 0x80 | MSB(nout
);
1077 rv
= ssl2_CalcMAC(op
, /* MAC goes here. */
1079 in
, amount
, /* intput addr, len */
1081 if (rv
!= SECSuccess
)
1085 /* Copy in the input data */
1086 /* XXX could eliminate the copy by folding it into the encryption */
1087 PORT_Memcpy(op
, in
, amount
);
1090 PORT_Memset(op
, padding
, padding
);
1094 /* Encrypt result */
1095 rv
= (*ss
->sec
.enc
)(ss
->sec
.writecx
, out
+hlen
, &nout
, buflen
-hlen
,
1096 out
+hlen
, op
- (out
+ hlen
));
1100 ssl_ReleaseSpecReadLock(ss
); /*************************************/
1102 PRINT_BUF(50, (ss
, "final xmit data:", out
, op
- out
));
1104 rv
= ssl_DefSend(ss
, out
, op
- out
, flags
& ~ssl_SEND_FLAG_MASK
);
1106 if (PORT_GetError() == PR_WOULD_BLOCK_ERROR
) {
1109 SSL_TRC(10, ("%d: SSL[%d]: send block error %d",
1110 SSL_GETPID(), ss
->fd
, PORT_GetError()));
1111 /* Return short write if some data already went out... */
1118 if (rv
< (op
- out
)) {
1119 /* Short write. Save the data and return. */
1120 if (ssl_SaveWriteData(ss
, out
+ rv
, op
- out
- rv
) == SECFailure
) {
1124 ss
->sec
.sendSequence
++;
1129 ss
->sec
.sendSequence
++;
1139 ssl_ReleaseSpecReadLock(ss
);
1144 ** Called from: ssl2_HandleServerHelloMessage,
1145 ** ssl2_HandleClientSessionKeyMessage,
1146 ** ssl2_HandleClientHelloMessage,
1150 ssl2_UseEncryptedSendFunc(sslSocket
*ss
)
1152 ssl_GetXmitBufLock(ss
);
1153 PORT_Assert(ss
->sec
.hashcx
!= 0);
1155 ss
->gs
.encrypted
= 1;
1156 ss
->sec
.send
= (ss
->sec
.blockSize
> 1) ? ssl2_SendBlock
: ssl2_SendStream
;
1157 ssl_ReleaseXmitBufLock(ss
);
1160 /* Called while initializing socket in ssl_CreateSecurityInfo().
1161 ** This function allows us to keep the name of ssl2_SendClear static.
1164 ssl2_UseClearSendFunc(sslSocket
*ss
)
1166 ss
->sec
.send
= ssl2_SendClear
;
1169 /************************************************************************
1170 ** END of Send functions. *
1171 *************************************************************************/
1173 /***********************************************************************
1174 * For SSL3, this gathers in and handles records/messages until either
1175 * the handshake is complete or application data is available.
1177 * For SSL2, this gathers in only the next SSLV2 record.
1179 * Called from ssl_Do1stHandshake() via function pointer ss->handshake.
1180 * Caller must hold handshake lock.
1181 * This function acquires and releases the RecvBufLock.
1183 * returns SECSuccess for success.
1184 * returns SECWouldBlock when that value is returned by ssl2_GatherRecord() or
1185 * ssl3_GatherCompleteHandshake().
1186 * returns SECFailure on all other errors.
1188 * The gather functions called by ssl_GatherRecord1stHandshake are expected
1189 * to return values interpreted as follows:
1190 * 1 : the function completed without error.
1191 * 0 : the function read EOF.
1192 * -1 : read error, or PR_WOULD_BLOCK_ERROR, or handleRecord error.
1193 * -2 : the function wants ssl_GatherRecord1stHandshake to be called again
1194 * immediately, by ssl_Do1stHandshake.
1196 * This code is similar to, and easily confused with, DoRecv() in sslsecur.c
1198 * This function is called from ssl_Do1stHandshake().
1199 * The following functions put ssl_GatherRecord1stHandshake into ss->handshake:
1200 * ssl2_HandleMessage
1201 * ssl2_HandleVerifyMessage
1202 * ssl2_HandleServerHelloMessage
1203 * ssl2_BeginClientHandshake
1204 * ssl2_HandleClientSessionKeyMessage
1205 * ssl3_RestartHandshakeAfterCertReq
1206 * ssl3_RestartHandshakeAfterServerCert
1207 * ssl2_HandleClientHelloMessage
1208 * ssl2_BeginServerHandshake
1211 ssl_GatherRecord1stHandshake(sslSocket
*ss
)
1215 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1217 ssl_GetRecvBufLock(ss
);
1219 /* The special case DTLS logic is needed here because the SSL/TLS
1220 * version wants to auto-detect SSL2 vs. SSL3 on the initial handshake
1221 * (ss->version == 0) but with DTLS it gets confused, so we force the
1224 if ((ss
->version
>= SSL_LIBRARY_VERSION_3_0
) || IS_DTLS(ss
)) {
1225 /* Wait for handshake to complete, or application data to arrive. */
1226 rv
= ssl3_GatherCompleteHandshake(ss
, 0);
1228 /* See if we have a complete record */
1229 rv
= ssl2_GatherRecord(ss
, 0);
1231 SSL_TRC(10, ("%d: SSL[%d]: handshake gathering, rv=%d",
1232 SSL_GETPID(), ss
->fd
, rv
));
1234 ssl_ReleaseRecvBufLock(ss
);
1237 if (rv
== SECWouldBlock
) {
1238 /* Progress is blocked waiting for callback completion. */
1239 SSL_TRC(10, ("%d: SSL[%d]: handshake blocked (need %d)",
1240 SSL_GETPID(), ss
->fd
, ss
->gs
.remainder
));
1241 return SECWouldBlock
;
1245 PORT_SetError(PR_END_OF_FILE_ERROR
);
1247 return SECFailure
; /* rv is < 0 here. */
1250 SSL_TRC(10, ("%d: SSL[%d]: got handshake record of %d bytes",
1251 SSL_GETPID(), ss
->fd
, ss
->gs
.recordLen
));
1253 ss
->handshake
= 0; /* makes ssl_Do1stHandshake call ss->nextHandshake.*/
1257 /************************************************************************/
1259 /* Called from ssl2_ServerSetupSessionCypher()
1260 * ssl2_ClientSetupSessionCypher()
1263 ssl2_FillInSID(sslSessionID
* sid
,
1271 SSLSignType authAlgorithm
,
1272 PRUint32 authKeyBits
,
1274 PRUint32 keaKeyBits
)
1276 PORT_Assert(sid
->references
== 1);
1277 PORT_Assert(sid
->cached
== never_cached
);
1278 PORT_Assert(sid
->u
.ssl2
.masterKey
.data
== 0);
1279 PORT_Assert(sid
->u
.ssl2
.cipherArg
.data
== 0);
1281 sid
->version
= SSL_LIBRARY_VERSION_2
;
1283 sid
->u
.ssl2
.cipherType
= cipher
;
1284 sid
->u
.ssl2
.masterKey
.data
= (PRUint8
*) PORT_Alloc(keyLen
);
1285 if (!sid
->u
.ssl2
.masterKey
.data
) {
1288 PORT_Memcpy(sid
->u
.ssl2
.masterKey
.data
, keyData
, keyLen
);
1289 sid
->u
.ssl2
.masterKey
.len
= keyLen
;
1290 sid
->u
.ssl2
.keyBits
= keyBits
;
1291 sid
->u
.ssl2
.secretKeyBits
= secretKeyBits
;
1292 sid
->authAlgorithm
= authAlgorithm
;
1293 sid
->authKeyBits
= authKeyBits
;
1294 sid
->keaType
= keaType
;
1295 sid
->keaKeyBits
= keaKeyBits
;
1296 sid
->lastAccessTime
= sid
->creationTime
= ssl_Time();
1297 sid
->expirationTime
= sid
->creationTime
+ ssl_sid_timeout
;
1300 sid
->u
.ssl2
.cipherArg
.data
= (PRUint8
*) PORT_Alloc(caLen
);
1301 if (!sid
->u
.ssl2
.cipherArg
.data
) {
1304 sid
->u
.ssl2
.cipherArg
.len
= caLen
;
1305 PORT_Memcpy(sid
->u
.ssl2
.cipherArg
.data
, ca
, caLen
);
1311 ** Construct session keys given the masterKey (tied to the session-id),
1312 ** the client's challenge and the server's nonce.
1314 ** Called from ssl2_CreateSessionCypher() <-
1317 ssl2_ProduceKeys(sslSocket
* ss
,
1320 SECItem
* masterKey
,
1321 PRUint8
* challenge
,
1325 PK11Context
* cx
= 0;
1326 unsigned nkm
= 0; /* number of hashes to generate key mat. */
1327 unsigned nkd
= 0; /* size of readKey and writeKey. */
1333 PRUint8 km
[3*16]; /* buffer for key material. */
1338 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1341 cx
= PK11_CreateDigestContext(SEC_OID_MD5
);
1343 ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE
);
1347 nkm
= ssl_Specs
[cipherType
].nkm
;
1348 nkd
= ssl_Specs
[cipherType
].nkd
;
1350 readKey
->data
= (PRUint8
*) PORT_Alloc(nkd
);
1355 writeKey
->data
= (PRUint8
*) PORT_Alloc(nkd
);
1356 if (!writeKey
->data
)
1358 writeKey
->len
= nkd
;
1360 /* Produce key material */
1362 for (i
= 0, off
= 0; i
< nkm
; i
++, off
+= 16) {
1363 rv
= PK11_DigestBegin(cx
);
1364 rv
|= PK11_DigestOp(cx
, masterKey
->data
, masterKey
->len
);
1365 rv
|= PK11_DigestOp(cx
, &countChar
, 1);
1366 rv
|= PK11_DigestOp(cx
, challenge
, SSL_CHALLENGE_BYTES
);
1367 rv
|= PK11_DigestOp(cx
, nonce
, SSL_CONNECTIONID_BYTES
);
1368 rv
|= PK11_DigestFinal(cx
, km
+off
, &part
, MD5_LENGTH
);
1369 if (rv
!= SECSuccess
) {
1370 ssl_MapLowLevelError(SSL_ERROR_MD5_DIGEST_FAILURE
);
1378 PORT_Memcpy(readKey
->data
, km
, nkd
);
1379 PORT_Memcpy(writeKey
->data
, km
+ nkd
, nkd
);
1382 PK11_DestroyContext(cx
, PR_TRUE
);
1386 /* Called from ssl2_ServerSetupSessionCypher()
1387 ** <- ssl2_HandleClientSessionKeyMessage()
1388 ** <- ssl2_HandleClientHelloMessage()
1389 ** and from ssl2_ClientSetupSessionCypher()
1390 ** <- ssl2_HandleServerHelloMessage()
1393 ssl2_CreateSessionCypher(sslSocket
*ss
, sslSessionID
*sid
, PRBool isClient
)
1395 SECItem
* rk
= NULL
;
1396 SECItem
* wk
= NULL
;
1399 int cipherType
= sid
->u
.ssl2
.cipherType
;
1400 PK11SlotInfo
* slot
= NULL
;
1401 CK_MECHANISM_TYPE mechanism
;
1410 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1411 if (ss
->sec
.ci
.sid
== 0)
1412 goto sec_loser
; /* don't crash if asserts are off */
1414 /* Trying to cut down on all these switch statements that should be tables.
1415 * So, test cipherType once, here, and then use tables below.
1417 switch (cipherType
) {
1418 case SSL_CK_RC4_128_EXPORT40_WITH_MD5
:
1419 case SSL_CK_RC4_128_WITH_MD5
:
1420 case SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
:
1421 case SSL_CK_RC2_128_CBC_WITH_MD5
:
1422 case SSL_CK_DES_64_CBC_WITH_MD5
:
1423 case SSL_CK_DES_192_EDE3_CBC_WITH_MD5
:
1427 SSL_DBG(("%d: SSL[%d]: ssl2_CreateSessionCypher: unknown cipher=%d",
1428 SSL_GETPID(), ss
->fd
, cipherType
));
1429 PORT_SetError(isClient
? SSL_ERROR_BAD_SERVER
: SSL_ERROR_BAD_CLIENT
);
1433 rk
= isClient
? &readKey
: &writeKey
;
1434 wk
= isClient
? &writeKey
: &readKey
;
1436 /* Produce the keys for this session */
1437 rv
= ssl2_ProduceKeys(ss
, &readKey
, &writeKey
, &sid
->u
.ssl2
.masterKey
,
1438 ss
->sec
.ci
.clientChallenge
, ss
->sec
.ci
.connectionID
,
1440 if (rv
!= SECSuccess
)
1442 PRINT_BUF(7, (ss
, "Session read-key: ", rk
->data
, rk
->len
));
1443 PRINT_BUF(7, (ss
, "Session write-key: ", wk
->data
, wk
->len
));
1445 PORT_Memcpy(ss
->sec
.ci
.readKey
, readKey
.data
, readKey
.len
);
1446 PORT_Memcpy(ss
->sec
.ci
.writeKey
, writeKey
.data
, writeKey
.len
);
1447 ss
->sec
.ci
.keySize
= readKey
.len
;
1450 rv
= ssl2_CreateMAC(&ss
->sec
, rk
, wk
, cipherType
);
1451 if (rv
!= SECSuccess
)
1454 /* First create the session key object */
1455 SSL_TRC(3, ("%d: SSL[%d]: using %s", SSL_GETPID(), ss
->fd
,
1456 ssl_cipherName
[cipherType
]));
1459 mechanism
= ssl_Specs
[cipherType
].mechanism
;
1461 /* set destructer before we call loser... */
1462 ss
->sec
.destroy
= (void (*)(void*, PRBool
)) PK11_DestroyContext
;
1463 slot
= PK11_GetBestSlot(mechanism
, ss
->pkcs11PinArg
);
1467 param
= PK11_ParamFromIV(mechanism
, &sid
->u
.ssl2
.cipherArg
);
1470 readcx
= PK11_CreateContextByRawKey(slot
, mechanism
, PK11_OriginUnwrap
,
1471 CKA_DECRYPT
, rk
, param
,
1473 SECITEM_FreeItem(param
, PR_TRUE
);
1477 /* build the client context */
1478 param
= PK11_ParamFromIV(mechanism
, &sid
->u
.ssl2
.cipherArg
);
1481 writecx
= PK11_CreateContextByRawKey(slot
, mechanism
, PK11_OriginUnwrap
,
1482 CKA_ENCRYPT
, wk
, param
,
1484 SECITEM_FreeItem(param
,PR_TRUE
);
1485 if (writecx
== NULL
)
1487 PK11_FreeSlot(slot
);
1490 ss
->sec
.enc
= (SSLCipher
) PK11_CipherOp
;
1491 ss
->sec
.dec
= (SSLCipher
) PK11_CipherOp
;
1492 ss
->sec
.readcx
= (void *) readcx
;
1493 ss
->sec
.writecx
= (void *) writecx
;
1494 ss
->sec
.blockSize
= ssl_Specs
[cipherType
].blockSize
;
1495 ss
->sec
.blockShift
= ssl_Specs
[cipherType
].blockShift
;
1496 ss
->sec
.cipherType
= sid
->u
.ssl2
.cipherType
;
1497 ss
->sec
.keyBits
= sid
->u
.ssl2
.keyBits
;
1498 ss
->sec
.secretKeyBits
= sid
->u
.ssl2
.secretKeyBits
;
1502 if (ss
->sec
.destroy
) {
1503 if (readcx
) (*ss
->sec
.destroy
)(readcx
, PR_TRUE
);
1504 if (writecx
) (*ss
->sec
.destroy
)(writecx
, PR_TRUE
);
1506 ss
->sec
.destroy
= NULL
;
1507 if (slot
) PK11_FreeSlot(slot
);
1514 SECITEM_ZfreeItem(rk
, PR_FALSE
);
1517 SECITEM_ZfreeItem(wk
, PR_FALSE
);
1523 ** Setup the server ciphers given information from a CLIENT-MASTER-KEY
1525 ** "ss" pointer to the ssl-socket object
1526 ** "cipher" the cipher type to use
1527 ** "keyBits" the size of the final cipher key
1528 ** "ck" the clear-key data
1529 ** "ckLen" the number of bytes of clear-key data
1530 ** "ek" the encrypted-key data
1531 ** "ekLen" the number of bytes of encrypted-key data
1532 ** "ca" the cipher-arg data
1533 ** "caLen" the number of bytes of cipher-arg data
1535 ** The MASTER-KEY is constructed by first decrypting the encrypted-key
1536 ** data. This produces the SECRET-KEY-DATA. The MASTER-KEY is composed by
1537 ** concatenating the clear-key data with the SECRET-KEY-DATA. This code
1538 ** checks to make sure that the client didn't send us an improper amount
1539 ** of SECRET-KEY-DATA (it restricts the length of that data to match the
1542 ** Called from ssl2_HandleClientSessionKeyMessage().
1545 ssl2_ServerSetupSessionCypher(sslSocket
*ss
, int cipher
, unsigned int keyBits
,
1546 PRUint8
*ck
, unsigned int ckLen
,
1547 PRUint8
*ek
, unsigned int ekLen
,
1548 PRUint8
*ca
, unsigned int caLen
)
1550 PRUint8
* dk
= NULL
; /* decrypted master key */
1552 sslServerCerts
* sc
= ss
->serverCerts
+ kt_rsa
;
1553 PRUint8
* kbuf
= 0; /* buffer for RSA decrypted data. */
1554 unsigned int ddLen
; /* length of RSA decrypted data in kbuf */
1555 unsigned int keySize
;
1556 unsigned int dkLen
; /* decrypted key length in bytes */
1559 PRUint16 allowed
; /* cipher kinds enabled and allowed by policy */
1560 PRUint8 mkbuf
[SSL_MAX_MASTER_KEY_BYTES
];
1562 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1563 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveRecvBufLock(ss
) );
1564 PORT_Assert((sc
->SERVERKEY
!= 0));
1565 PORT_Assert((ss
->sec
.ci
.sid
!= 0));
1566 sid
= ss
->sec
.ci
.sid
;
1568 /* Trying to cut down on all these switch statements that should be tables.
1569 * So, test cipherType once, here, and then use tables below.
1572 case SSL_CK_RC4_128_EXPORT40_WITH_MD5
:
1573 case SSL_CK_RC4_128_WITH_MD5
:
1574 case SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
:
1575 case SSL_CK_RC2_128_CBC_WITH_MD5
:
1576 case SSL_CK_DES_64_CBC_WITH_MD5
:
1577 case SSL_CK_DES_192_EDE3_CBC_WITH_MD5
:
1581 SSL_DBG(("%d: SSL[%d]: ssl2_ServerSetupSessionCypher: unknown cipher=%d",
1582 SSL_GETPID(), ss
->fd
, cipher
));
1583 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1587 allowed
= ss
->allowedByPolicy
& ss
->chosenPreference
& SSL_CB_IMPLEMENTED
;
1588 if (!(allowed
& (1 << cipher
))) {
1589 /* client chose a kind we don't allow! */
1590 SSL_DBG(("%d: SSL[%d]: disallowed cipher=%d",
1591 SSL_GETPID(), ss
->fd
, cipher
));
1592 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1596 keySize
= ssl_Specs
[cipher
].keyLen
;
1597 if (keyBits
!= keySize
* BPB
) {
1598 SSL_DBG(("%d: SSL[%d]: invalid master secret key length=%d (bits)!",
1599 SSL_GETPID(), ss
->fd
, keyBits
));
1600 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1604 if (ckLen
!= ssl_Specs
[cipher
].pubLen
) {
1605 SSL_DBG(("%d: SSL[%d]: invalid clear key length, ckLen=%d (bytes)!",
1606 SSL_GETPID(), ss
->fd
, ckLen
));
1607 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1611 if (caLen
!= ssl_Specs
[cipher
].ivLen
) {
1612 SSL_DBG(("%d: SSL[%d]: invalid key args length, caLen=%d (bytes)!",
1613 SSL_GETPID(), ss
->fd
, caLen
));
1614 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1618 modulusLen
= PK11_GetPrivateModulusLen(sc
->SERVERKEY
);
1619 if (modulusLen
== -1) {
1620 /* XXX If the key is bad, then PK11_PubDecryptRaw will fail below. */
1623 if (ekLen
> modulusLen
|| ekLen
+ ckLen
< keySize
) {
1624 SSL_DBG(("%d: SSL[%d]: invalid encrypted key length, ekLen=%d (bytes)!",
1625 SSL_GETPID(), ss
->fd
, ekLen
));
1626 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1630 /* allocate the buffer to hold the decrypted portion of the key. */
1631 kbuf
= (PRUint8
*)PORT_Alloc(modulusLen
);
1635 dkLen
= keySize
- ckLen
;
1636 dk
= kbuf
+ modulusLen
- dkLen
;
1638 /* Decrypt encrypted half of the key.
1639 ** NOTE: PK11_PubDecryptRaw will barf on a non-RSA key. This is
1640 ** desired behavior here.
1642 rv
= PK11_PubDecryptRaw(sc
->SERVERKEY
, kbuf
, &ddLen
, modulusLen
, ek
, ekLen
);
1643 if (rv
!= SECSuccess
)
1646 /* Is the length of the decrypted data (ddLen) the expected value? */
1647 if (modulusLen
!= ddLen
)
1650 /* Cheaply verify that PKCS#1 was used to format the encryption block */
1651 if ((kbuf
[0] != 0x00) || (kbuf
[1] != 0x02) || (dk
[-1] != 0x00)) {
1652 SSL_DBG(("%d: SSL[%d]: strange encryption block",
1653 SSL_GETPID(), ss
->fd
));
1654 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1658 /* Make sure we're not subject to a version rollback attack. */
1659 if (!SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
)) {
1660 static const PRUint8 threes
[8] = { 0x03, 0x03, 0x03, 0x03,
1661 0x03, 0x03, 0x03, 0x03 };
1663 if (PORT_Memcmp(dk
- 8 - 1, threes
, 8) == 0) {
1664 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
1670 /* Defense against the Bleichenbacher attack.
1671 * Provide the client with NO CLUES that the decrypted master key
1672 * was erroneous. Don't send any error messages.
1673 * Instead, Generate a completely bogus master key .
1675 PK11_GenerateRandom(dk
, dkLen
);
1679 ** Construct master key out of the pieces.
1682 PORT_Memcpy(mkbuf
, ck
, ckLen
);
1684 PORT_Memcpy(mkbuf
+ ckLen
, dk
, dkLen
);
1686 /* Fill in session-id */
1687 rv
= ssl2_FillInSID(sid
, cipher
, mkbuf
, keySize
, ca
, caLen
,
1688 keyBits
, keyBits
- (ckLen
<<3),
1689 ss
->sec
.authAlgorithm
, ss
->sec
.authKeyBits
,
1690 ss
->sec
.keaType
, ss
->sec
.keaKeyBits
);
1691 if (rv
!= SECSuccess
) {
1695 /* Create session ciphers */
1696 rv
= ssl2_CreateSessionCypher(ss
, sid
, PR_FALSE
);
1697 if (rv
!= SECSuccess
) {
1701 SSL_TRC(1, ("%d: SSL[%d]: server, using %s cipher, clear=%d total=%d",
1702 SSL_GETPID(), ss
->fd
, ssl_cipherName
[cipher
],
1703 ckLen
<<3, keySize
<<3));
1715 /************************************************************************/
1718 ** Rewrite the incoming cipher specs, comparing to list of specs we support,
1719 ** (ss->cipherSpecs) and eliminating anything we don't support
1721 * Note: Our list may contain SSL v3 ciphers.
1722 * We MUST NOT match on any of those.
1723 * Fortunately, this is easy to detect because SSLv3 ciphers have zero
1724 * in the first byte, and none of the SSLv2 ciphers do.
1726 * Called from ssl2_HandleClientHelloMessage().
1727 * Returns the number of bytes of "qualified cipher specs",
1728 * which is typically a multiple of 3, but will be zero if there are none.
1731 ssl2_QualifyCypherSpecs(sslSocket
*ss
,
1732 PRUint8
* cs
, /* cipher specs in client hello msg. */
1740 PRUint8 qualifiedSpecs
[ssl2_NUM_SUITES_IMPLEMENTED
* 3];
1742 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1743 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveRecvBufLock(ss
) );
1745 if (!ss
->cipherSpecs
) {
1746 SECStatus rv
= ssl2_ConstructCipherSpecs(ss
);
1747 if (rv
!= SECSuccess
|| !ss
->cipherSpecs
)
1751 PRINT_BUF(10, (ss
, "specs from client:", cs
, csLen
));
1752 qs
= qualifiedSpecs
;
1753 ms
= ss
->cipherSpecs
;
1754 for (mc
= ss
->sizeCipherSpecs
; mc
> 0; mc
-= 3, ms
+= 3) {
1757 for (hs
= cs
, hc
= csLen
; hc
> 0; hs
+= 3, hc
-= 3) {
1758 if ((hs
[0] == ms
[0]) &&
1761 /* Copy this cipher spec into the "keep" section */
1770 hc
= qs
- qualifiedSpecs
;
1771 PRINT_BUF(10, (ss
, "qualified specs from client:", qualifiedSpecs
, hc
));
1772 PORT_Memcpy(cs
, qualifiedSpecs
, hc
);
1777 ** Pick the best cipher we can find, given the array of server cipher
1778 ** specs. Returns cipher number (e.g. SSL_CK_*), or -1 for no overlap.
1779 ** If successful, stores the master key size (bytes) in *pKeyLen.
1781 ** This is correct only for the client side, but presently
1782 ** this function is only called from
1783 ** ssl2_ClientSetupSessionCypher() <- ssl2_HandleServerHelloMessage()
1785 ** Note that most servers only return a single cipher suite in their
1786 ** ServerHello messages. So, the code below for finding the "best" cipher
1787 ** suite usually has only one choice. The client and server should send
1788 ** their cipher suite lists sorted in descending order by preference.
1791 ssl2_ChooseSessionCypher(sslSocket
*ss
,
1792 int hc
, /* number of cs's in hs. */
1793 PRUint8
* hs
, /* server hello's cipher suites. */
1794 int * pKeyLen
) /* out: sym key size in bytes. */
1799 int bestRealKeySize
;
1804 const PRUint8
* preferred
;
1805 static const PRUint8 noneSuch
[3] = { 0, 0, 0 };
1807 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
1808 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveRecvBufLock(ss
) );
1810 if (!ss
->cipherSpecs
) {
1811 SECStatus rv
= ssl2_ConstructCipherSpecs(ss
);
1812 if (rv
!= SECSuccess
|| !ss
->cipherSpecs
)
1816 if (!ss
->preferredCipher
) {
1817 unsigned int allowed
= ss
->allowedByPolicy
& ss
->chosenPreference
&
1820 preferred
= implementedCipherSuites
;
1821 for (i
= ssl2_NUM_SUITES_IMPLEMENTED
; i
> 0; --i
) {
1822 if (0 != (allowed
& (1U << preferred
[0]))) {
1823 ss
->preferredCipher
= preferred
;
1830 preferred
= ss
->preferredCipher
? ss
->preferredCipher
: noneSuch
;
1832 ** Scan list of ciphers received from peer and look for a match in
1834 * Note: Our list may contain SSL v3 ciphers.
1835 * We MUST NOT match on any of those.
1836 * Fortunately, this is easy to detect because SSLv3 ciphers have zero
1837 * in the first byte, and none of the SSLv2 ciphers do.
1839 bestKeySize
= bestRealKeySize
= 0;
1842 for (i
= 0, ms
= ss
->cipherSpecs
; i
< ss
->sizeCipherSpecs
; i
+= 3, ms
+= 3) {
1843 if ((hs
[0] == preferred
[0]) &&
1844 (hs
[1] == preferred
[1]) &&
1845 (hs
[2] == preferred
[2]) &&
1847 /* Pick this cipher immediately! */
1848 *pKeyLen
= (((hs
[1] << 8) | hs
[2]) + 7) >> 3;
1851 if ((hs
[0] == ms
[0]) && (hs
[1] == ms
[1]) && (hs
[2] == ms
[2]) &&
1855 /* Use secret keySize to determine which cipher is best */
1856 realKeySize
= (hs
[1] << 8) | hs
[2];
1858 case SSL_CK_RC4_128_EXPORT40_WITH_MD5
:
1859 case SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
:
1863 keySize
= realKeySize
;
1866 if (keySize
> bestKeySize
) {
1868 bestKeySize
= keySize
;
1869 bestRealKeySize
= realKeySize
;
1875 if (bestCypher
< 0) {
1877 ** No overlap between server and client. Re-examine server list
1878 ** to see what kind of ciphers it does support so that we can set
1879 ** the error code appropriately.
1881 if ((ohs
[0] == SSL_CK_RC4_128_WITH_MD5
) ||
1882 (ohs
[0] == SSL_CK_RC2_128_CBC_WITH_MD5
)) {
1883 PORT_SetError(SSL_ERROR_US_ONLY_SERVER
);
1884 } else if ((ohs
[0] == SSL_CK_RC4_128_EXPORT40_WITH_MD5
) ||
1885 (ohs
[0] == SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5
)) {
1886 PORT_SetError(SSL_ERROR_EXPORT_ONLY_SERVER
);
1888 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP
);
1890 SSL_DBG(("%d: SSL[%d]: no cipher overlap", SSL_GETPID(), ss
->fd
));
1893 *pKeyLen
= (bestRealKeySize
+ 7) >> 3;
1901 ssl2_ClientHandleServerCert(sslSocket
*ss
, PRUint8
*certData
, int certLen
)
1903 CERTCertificate
*cert
= NULL
;
1906 certItem
.data
= certData
;
1907 certItem
.len
= certLen
;
1909 /* decode the certificate */
1910 cert
= CERT_NewTempCertificate(ss
->dbHandle
, &certItem
, NULL
,
1914 SSL_DBG(("%d: SSL[%d]: decode of server certificate fails",
1915 SSL_GETPID(), ss
->fd
));
1916 PORT_SetError(SSL_ERROR_BAD_CERTIFICATE
);
1922 if (ssl_trace
>= 1) {
1925 issuer
= CERT_NameToAscii(&cert
->issuer
);
1926 subject
= CERT_NameToAscii(&cert
->subject
);
1927 SSL_TRC(1,("%d: server certificate issuer: '%s'",
1928 SSL_GETPID(), issuer
? issuer
: "OOPS"));
1929 SSL_TRC(1,("%d: server name: '%s'",
1930 SSL_GETPID(), subject
? subject
: "OOPS"));
1937 ss
->sec
.peerCert
= cert
;
1943 * Format one block of data for public/private key encryption using
1944 * the rules defined in PKCS #1. SSL2 does this itself to handle the
1945 * rollback detection.
1947 #define RSA_BLOCK_MIN_PAD_LEN 8
1948 #define RSA_BLOCK_FIRST_OCTET 0x00
1949 #define RSA_BLOCK_AFTER_PAD_OCTET 0x00
1950 #define RSA_BLOCK_PUBLIC_OCTET 0x02
1952 ssl_FormatSSL2Block(unsigned modulusLen
, SECItem
*data
)
1954 unsigned char *block
;
1960 if (modulusLen
< data
->len
+ (3 + RSA_BLOCK_MIN_PAD_LEN
)) {
1961 PORT_SetError(SEC_ERROR_BAD_KEY
);
1964 block
= (unsigned char *) PORT_Alloc(modulusLen
);
1971 * All RSA blocks start with two octets:
1974 *bp
++ = RSA_BLOCK_FIRST_OCTET
;
1975 *bp
++ = RSA_BLOCK_PUBLIC_OCTET
;
1978 * 0x00 || BT || Pad || 0x00 || ActualData
1979 * 1 1 padLen 1 data->len
1980 * Pad is all non-zero random bytes.
1982 padLen
= modulusLen
- data
->len
- 3;
1983 PORT_Assert (padLen
>= RSA_BLOCK_MIN_PAD_LEN
);
1984 rv
= PK11_GenerateRandom(bp
, padLen
);
1985 if (rv
== SECFailure
) goto loser
;
1986 /* replace all the 'zero' bytes */
1987 for (i
= 0; i
< padLen
; i
++) {
1988 while (bp
[i
] == RSA_BLOCK_AFTER_PAD_OCTET
) {
1989 rv
= PK11_GenerateRandom(bp
+i
, 1);
1990 if (rv
== SECFailure
) goto loser
;
1994 *bp
++ = RSA_BLOCK_AFTER_PAD_OCTET
;
1995 PORT_Memcpy (bp
, data
->data
, data
->len
);
1999 if (block
) PORT_Free(block
);
2004 ** Given the server's public key and cipher specs, generate a session key
2005 ** that is ready to use for encrypting/decrypting the byte stream. At
2006 ** the same time, generate the SSL_MT_CLIENT_MASTER_KEY message and
2007 ** send it to the server.
2009 ** Called from ssl2_HandleServerHelloMessage()
2012 ssl2_ClientSetupSessionCypher(sslSocket
*ss
, PRUint8
*cs
, int csLen
)
2015 PRUint8
* ca
; /* points to iv data, or NULL if none. */
2016 PRUint8
* ekbuf
= 0;
2017 CERTCertificate
* cert
= 0;
2018 SECKEYPublicKey
* serverKey
= 0;
2019 unsigned modulusLen
= 0;
2022 int keyLen
; /* cipher symkey size in bytes. */
2023 int ckLen
; /* publicly reveal this many bytes of key. */
2024 int caLen
; /* length of IV data at *ca. */
2027 unsigned char *eblock
; /* holds unencrypted PKCS#1 formatted key. */
2028 SECItem rek
; /* holds portion of symkey to be encrypted. */
2030 PRUint8 keyData
[SSL_MAX_MASTER_KEY_BYTES
];
2033 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2037 sid
= ss
->sec
.ci
.sid
;
2038 PORT_Assert(sid
!= 0);
2040 cert
= ss
->sec
.peerCert
;
2042 serverKey
= CERT_ExtractPublicKey(cert
);
2044 SSL_DBG(("%d: SSL[%d]: extract public key failed: error=%d",
2045 SSL_GETPID(), ss
->fd
, PORT_GetError()));
2046 PORT_SetError(SSL_ERROR_BAD_CERTIFICATE
);
2051 ss
->sec
.authAlgorithm
= ssl_sign_rsa
;
2052 ss
->sec
.keaType
= ssl_kea_rsa
;
2053 ss
->sec
.keaKeyBits
= \
2054 ss
->sec
.authKeyBits
= SECKEY_PublicKeyStrengthInBits(serverKey
);
2056 /* Choose a compatible cipher with the server */
2058 cipher
= ssl2_ChooseSessionCypher(ss
, nc
, cs
, &keyLen
);
2060 /* ssl2_ChooseSessionCypher has set error code. */
2061 ssl2_SendErrorMessage(ss
, SSL_PE_NO_CYPHERS
);
2065 /* Generate the random keys */
2066 PK11_GenerateRandom(keyData
, sizeof(keyData
));
2069 ** Next, carve up the keys into clear and encrypted portions. The
2070 ** clear data is taken from the start of keyData and the encrypted
2071 ** portion from the remainder. Note that each of these portions is
2072 ** carved in half, one half for the read-key and one for the
2077 /* We know that cipher is a legit value here, because
2078 * ssl2_ChooseSessionCypher doesn't return bogus values.
2080 ckLen
= ssl_Specs
[cipher
].pubLen
; /* cleartext key length. */
2081 caLen
= ssl_Specs
[cipher
].ivLen
; /* IV length. */
2083 PORT_Assert(sizeof iv
>= caLen
);
2084 PK11_GenerateRandom(iv
, caLen
);
2088 /* Fill in session-id */
2089 rv
= ssl2_FillInSID(sid
, cipher
, keyData
, keyLen
,
2090 ca
, caLen
, keyLen
<< 3, (keyLen
- ckLen
) << 3,
2091 ss
->sec
.authAlgorithm
, ss
->sec
.authKeyBits
,
2092 ss
->sec
.keaType
, ss
->sec
.keaKeyBits
);
2093 if (rv
!= SECSuccess
) {
2097 SSL_TRC(1, ("%d: SSL[%d]: client, using %s cipher, clear=%d total=%d",
2098 SSL_GETPID(), ss
->fd
, ssl_cipherName
[cipher
],
2099 ckLen
<<3, keyLen
<<3));
2101 /* Now setup read and write ciphers */
2102 rv
= ssl2_CreateSessionCypher(ss
, sid
, PR_TRUE
);
2103 if (rv
!= SECSuccess
) {
2108 ** Fill in the encryption buffer with some random bytes. Then
2109 ** copy in the portion of the session key we are encrypting.
2111 modulusLen
= SECKEY_PublicKeyStrength(serverKey
);
2112 rek
.data
= keyData
+ ckLen
;
2113 rek
.len
= keyLen
- ckLen
;
2114 eblock
= ssl_FormatSSL2Block(modulusLen
, &rek
);
2118 /* Set up the padding for version 2 rollback detection. */
2119 /* XXX We should really use defines here */
2120 if (!SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
)) {
2121 PORT_Assert((modulusLen
- rek
.len
) > 12);
2122 PORT_Memset(eblock
+ modulusLen
- rek
.len
- 8 - 1, 0x03, 8);
2124 ekbuf
= (PRUint8
*) PORT_Alloc(modulusLen
);
2127 PRINT_BUF(10, (ss
, "master key encryption block:",
2128 eblock
, modulusLen
));
2130 /* Encrypt ekitem */
2131 rv
= PK11_PubEncryptRaw(serverKey
, ekbuf
, eblock
, modulusLen
,
2136 /* Now we have everything ready to send */
2137 rv
= ssl2_SendSessionKeyMessage(ss
, cipher
, keyLen
<< 3, ca
, caLen
,
2138 keyData
, ckLen
, ekbuf
, modulusLen
);
2139 if (rv
!= SECSuccess
) {
2150 PORT_Memset(keyData
, 0, sizeof(keyData
));
2151 PORT_ZFree(ekbuf
, modulusLen
);
2152 PORT_ZFree(eblock
, modulusLen
);
2153 SECKEY_DestroyPublicKey(serverKey
);
2157 /************************************************************************/
2160 * Called from ssl2_HandleMessage in response to SSL_MT_SERVER_FINISHED message.
2161 * Caller holds recvBufLock and handshakeLock
2164 ssl2_ClientRegSessionID(sslSocket
*ss
, PRUint8
*s
)
2166 sslSessionID
*sid
= ss
->sec
.ci
.sid
;
2168 /* Record entry in nonce cache */
2169 if (sid
->peerCert
== NULL
) {
2170 PORT_Memcpy(sid
->u
.ssl2
.sessionID
, s
, sizeof(sid
->u
.ssl2
.sessionID
));
2171 sid
->peerCert
= CERT_DupCertificate(ss
->sec
.peerCert
);
2174 if (!ss
->opt
.noCache
&& sid
->cached
== never_cached
)
2175 (*ss
->sec
.cache
)(sid
);
2178 /* Called from ssl2_HandleMessage() */
2180 ssl2_TriggerNextMessage(sslSocket
*ss
)
2184 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2186 if ((ss
->sec
.ci
.requiredElements
& CIS_HAVE_CERTIFICATE
) &&
2187 !(ss
->sec
.ci
.sentElements
& CIS_HAVE_CERTIFICATE
)) {
2188 ss
->sec
.ci
.sentElements
|= CIS_HAVE_CERTIFICATE
;
2189 rv
= ssl2_SendCertificateRequestMessage(ss
);
2195 /* See if it's time to send our finished message, or if the handshakes are
2196 ** complete. Send finished message if appropriate.
2197 ** Returns SECSuccess unless anything goes wrong.
2199 ** Called from ssl2_HandleMessage,
2200 ** ssl2_HandleVerifyMessage
2201 ** ssl2_HandleServerHelloMessage
2202 ** ssl2_HandleClientSessionKeyMessage
2205 ssl2_TryToFinish(sslSocket
*ss
)
2210 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2212 e
= ss
->sec
.ci
.elements
;
2213 ef
= e
| CIS_HAVE_FINISHED
;
2214 if ((ef
& ss
->sec
.ci
.requiredElements
) == ss
->sec
.ci
.requiredElements
) {
2215 if (ss
->sec
.isServer
) {
2216 /* Send server finished message if we already didn't */
2217 rv
= ssl2_SendServerFinishedMessage(ss
);
2219 /* Send client finished message if we already didn't */
2220 rv
= ssl2_SendClientFinishedMessage(ss
);
2222 if (rv
!= SECSuccess
) {
2225 if ((e
& ss
->sec
.ci
.requiredElements
) == ss
->sec
.ci
.requiredElements
) {
2226 /* Totally finished */
2235 ** Called from ssl2_HandleRequestCertificate
2238 ssl2_SignResponse(sslSocket
*ss
,
2239 SECKEYPrivateKey
*key
,
2242 SGNContext
* sgn
= NULL
;
2243 PRUint8
* challenge
;
2245 SECStatus rv
= SECFailure
;
2247 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2249 challenge
= ss
->sec
.ci
.serverChallenge
;
2250 len
= ss
->sec
.ci
.serverChallengeLen
;
2252 /* Sign the expected data... */
2253 sgn
= SGN_NewContext(SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION
,key
);
2256 rv
= SGN_Begin(sgn
);
2257 if (rv
!= SECSuccess
)
2259 rv
= SGN_Update(sgn
, ss
->sec
.ci
.readKey
, ss
->sec
.ci
.keySize
);
2260 if (rv
!= SECSuccess
)
2262 rv
= SGN_Update(sgn
, ss
->sec
.ci
.writeKey
, ss
->sec
.ci
.keySize
);
2263 if (rv
!= SECSuccess
)
2265 rv
= SGN_Update(sgn
, challenge
, len
);
2266 if (rv
!= SECSuccess
)
2268 rv
= SGN_Update(sgn
, ss
->sec
.peerCert
->derCert
.data
,
2269 ss
->sec
.peerCert
->derCert
.len
);
2270 if (rv
!= SECSuccess
)
2272 rv
= SGN_End(sgn
, response
);
2273 if (rv
!= SECSuccess
)
2277 SGN_DestroyContext(sgn
, PR_TRUE
);
2278 return rv
== SECSuccess
? SECSuccess
: SECFailure
;
2282 ** Try to handle a request-certificate message. Get client's certificate
2283 ** and private key and sign a message for the server to see.
2284 ** Caller must hold handshakeLock
2286 ** Called from ssl2_HandleMessage().
2289 ssl2_HandleRequestCertificate(sslSocket
*ss
)
2291 CERTCertificate
* cert
= NULL
; /* app-selected client cert. */
2292 SECKEYPrivateKey
*key
= NULL
; /* priv key for cert. */
2300 * These things all need to be initialized before we can "goto loser".
2302 response
.data
= NULL
;
2304 /* get challenge info from connectionInfo */
2305 authType
= ss
->sec
.ci
.authType
;
2307 if (authType
!= SSL_AT_MD5_WITH_RSA_ENCRYPTION
) {
2308 SSL_TRC(7, ("%d: SSL[%d]: unsupported auth type 0x%x", SSL_GETPID(),
2313 /* Get certificate and private-key from client */
2314 if (!ss
->getClientAuthData
) {
2315 SSL_TRC(7, ("%d: SSL[%d]: client doesn't support client-auth",
2316 SSL_GETPID(), ss
->fd
));
2319 ret
= (*ss
->getClientAuthData
)(ss
->getClientAuthDataArg
, ss
->fd
,
2321 if ( ret
== SECWouldBlock
) {
2322 PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_SSL2
);
2331 /* check what the callback function returned */
2332 if ((!cert
) || (!key
)) {
2333 /* we are missing either the key or cert */
2335 /* got a cert, but no key - free it */
2336 CERT_DestroyCertificate(cert
);
2340 /* got a key, but no cert - free it */
2341 SECKEY_DestroyPrivateKey(key
);
2347 rv
= ssl2_SignResponse(ss
, key
, &response
);
2348 if ( rv
!= SECSuccess
) {
2353 /* Send response message */
2354 ret
= ssl2_SendCertificateResponseMessage(ss
, &cert
->derCert
, &response
);
2356 /* Now, remember the cert we sent. But first, forget any previous one. */
2357 if (ss
->sec
.localCert
) {
2358 CERT_DestroyCertificate(ss
->sec
.localCert
);
2360 ss
->sec
.localCert
= CERT_DupCertificate(cert
);
2361 PORT_Assert(!ss
->sec
.ci
.sid
->localCert
);
2362 if (ss
->sec
.ci
.sid
->localCert
) {
2363 CERT_DestroyCertificate(ss
->sec
.ci
.sid
->localCert
);
2365 ss
->sec
.ci
.sid
->localCert
= cert
;
2371 SSL_TRC(7, ("%d: SSL[%d]: no certificate (ret=%d)", SSL_GETPID(),
2373 ret
= ssl2_SendErrorMessage(ss
, SSL_PE_NO_CERTIFICATE
);
2378 CERT_DestroyCertificate(cert
);
2381 SECKEY_DestroyPrivateKey(key
);
2383 if ( response
.data
) {
2384 PORT_Free(response
.data
);
2391 ** Called from ssl2_HandleMessage for SSL_MT_CLIENT_CERTIFICATE message.
2392 ** Caller must hold HandshakeLock and RecvBufLock, since cd and response
2393 ** are contained in the gathered input data.
2396 ssl2_HandleClientCertificate(sslSocket
* ss
,
2397 PRUint8 certType
, /* XXX unused */
2401 unsigned int responseLen
)
2403 CERTCertificate
*cert
= NULL
;
2404 SECKEYPublicKey
*pubKey
= NULL
;
2405 VFYContext
* vfy
= NULL
;
2407 SECStatus rv
= SECFailure
;
2411 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2412 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveRecvBufLock(ss
) );
2414 /* Extract the certificate */
2416 certItem
.len
= cdLen
;
2418 cert
= CERT_NewTempCertificate(ss
->dbHandle
, &certItem
, NULL
,
2424 /* save the certificate, since the auth routine will need it */
2425 ss
->sec
.peerCert
= cert
;
2427 /* Extract the public key */
2428 pubKey
= CERT_ExtractPublicKey(cert
);
2432 /* Verify the response data... */
2433 rep
.data
= response
;
2434 rep
.len
= responseLen
;
2435 /* SSL 2.0 only supports RSA certs, so we don't have to worry about
2437 vfy
= VFY_CreateContext(pubKey
, &rep
, SEC_OID_PKCS1_RSA_ENCRYPTION
,
2441 rv
= VFY_Begin(vfy
);
2445 rv
= VFY_Update(vfy
, ss
->sec
.ci
.readKey
, ss
->sec
.ci
.keySize
);
2448 rv
= VFY_Update(vfy
, ss
->sec
.ci
.writeKey
, ss
->sec
.ci
.keySize
);
2451 rv
= VFY_Update(vfy
, ss
->sec
.ci
.serverChallenge
, SSL_CHALLENGE_BYTES
);
2455 derCert
= &ss
->serverCerts
[kt_rsa
].serverCert
->derCert
;
2456 rv
= VFY_Update(vfy
, derCert
->data
, derCert
->len
);
2463 /* Now ask the server application if it likes the certificate... */
2464 rv
= (SECStatus
) (*ss
->authCertificate
)(ss
->authCertificateArg
,
2465 ss
->fd
, PR_TRUE
, PR_TRUE
);
2466 /* Hey, it liked it. */
2467 if (SECSuccess
== rv
)
2471 ss
->sec
.peerCert
= NULL
;
2472 CERT_DestroyCertificate(cert
);
2475 VFY_DestroyContext(vfy
, PR_TRUE
);
2476 SECKEY_DestroyPublicKey(pubKey
);
2481 ** Handle remaining messages between client/server. Process finished
2482 ** messages from either side and any authentication requests.
2483 ** This should only be called for SSLv2 handshake messages,
2484 ** not for application data records.
2485 ** Caller must hold handshake lock.
2487 ** Called from ssl_Do1stHandshake().
2491 ssl2_HandleMessage(sslSocket
*ss
)
2495 unsigned len
, certType
, certLen
, responseLen
;
2499 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2501 ssl_GetRecvBufLock(ss
);
2503 data
= ss
->gs
.buf
.buf
+ ss
->gs
.recordOffset
;
2505 if (ss
->gs
.recordLen
< 1) {
2508 SSL_TRC(3, ("%d: SSL[%d]: received %d message",
2509 SSL_GETPID(), ss
->fd
, data
[0]));
2510 DUMP_MSG(29, (ss
, data
, ss
->gs
.recordLen
));
2513 case SSL_MT_CLIENT_FINISHED
:
2514 if (ss
->sec
.ci
.elements
& CIS_HAVE_FINISHED
) {
2515 SSL_DBG(("%d: SSL[%d]: dup client-finished message",
2516 SSL_GETPID(), ss
->fd
));
2520 /* See if nonce matches */
2521 len
= ss
->gs
.recordLen
- 1;
2523 if ((len
!= sizeof(ss
->sec
.ci
.connectionID
)) ||
2524 (PORT_Memcmp(ss
->sec
.ci
.connectionID
, cid
, len
) != 0)) {
2525 SSL_DBG(("%d: SSL[%d]: bad connection-id", SSL_GETPID(), ss
->fd
));
2526 PRINT_BUF(5, (ss
, "sent connection-id",
2527 ss
->sec
.ci
.connectionID
,
2528 sizeof(ss
->sec
.ci
.connectionID
)));
2529 PRINT_BUF(5, (ss
, "rcvd connection-id", cid
, len
));
2533 SSL_TRC(5, ("%d: SSL[%d]: got client finished, waiting for 0x%d",
2534 SSL_GETPID(), ss
->fd
,
2535 ss
->sec
.ci
.requiredElements
^ ss
->sec
.ci
.elements
));
2536 ss
->sec
.ci
.elements
|= CIS_HAVE_FINISHED
;
2539 case SSL_MT_SERVER_FINISHED
:
2540 if (ss
->sec
.ci
.elements
& CIS_HAVE_FINISHED
) {
2541 SSL_DBG(("%d: SSL[%d]: dup server-finished message",
2542 SSL_GETPID(), ss
->fd
));
2546 if (ss
->gs
.recordLen
- 1 != SSL2_SESSIONID_BYTES
) {
2547 SSL_DBG(("%d: SSL[%d]: bad server-finished message, len=%d",
2548 SSL_GETPID(), ss
->fd
, ss
->gs
.recordLen
));
2551 ssl2_ClientRegSessionID(ss
, data
+1);
2552 SSL_TRC(5, ("%d: SSL[%d]: got server finished, waiting for 0x%d",
2553 SSL_GETPID(), ss
->fd
,
2554 ss
->sec
.ci
.requiredElements
^ ss
->sec
.ci
.elements
));
2555 ss
->sec
.ci
.elements
|= CIS_HAVE_FINISHED
;
2558 case SSL_MT_REQUEST_CERTIFICATE
:
2559 len
= ss
->gs
.recordLen
- 2;
2560 if ((len
< SSL_MIN_CHALLENGE_BYTES
) ||
2561 (len
> SSL_MAX_CHALLENGE_BYTES
)) {
2563 SSL_DBG(("%d: SSL[%d]: bad cert request message: code len=%d",
2564 SSL_GETPID(), ss
->fd
, len
));
2568 /* save auth request info */
2569 ss
->sec
.ci
.authType
= data
[1];
2570 ss
->sec
.ci
.serverChallengeLen
= len
;
2571 PORT_Memcpy(ss
->sec
.ci
.serverChallenge
, data
+ 2, len
);
2573 rv
= ssl2_HandleRequestCertificate(ss
);
2574 if (rv
== SECWouldBlock
) {
2575 SSL_TRC(3, ("%d: SSL[%d]: async cert request",
2576 SSL_GETPID(), ss
->fd
));
2577 /* someone is handling this asynchronously */
2578 ssl_ReleaseRecvBufLock(ss
);
2579 return SECWouldBlock
;
2587 case SSL_MT_CLIENT_CERTIFICATE
:
2588 if (!ss
->authCertificate
) {
2589 /* Server asked for authentication and can't handle it */
2590 PORT_SetError(SSL_ERROR_BAD_SERVER
);
2593 if (ss
->gs
.recordLen
< SSL_HL_CLIENT_CERTIFICATE_HBYTES
) {
2598 certLen
= (data
[2] << 8) | data
[3];
2599 responseLen
= (data
[4] << 8) | data
[5];
2600 if (certType
!= SSL_CT_X509_CERTIFICATE
) {
2601 PORT_SetError(SSL_ERROR_UNSUPPORTED_CERTIFICATE_TYPE
);
2604 if (certLen
+ responseLen
+ SSL_HL_CLIENT_CERTIFICATE_HBYTES
2605 > ss
->gs
.recordLen
) {
2606 /* prevent overflow crash. */
2609 rv
= ssl2_HandleClientCertificate(ss
, data
[1],
2610 data
+ SSL_HL_CLIENT_CERTIFICATE_HBYTES
,
2612 data
+ SSL_HL_CLIENT_CERTIFICATE_HBYTES
+ certLen
,
2615 rv2
= ssl2_SendErrorMessage(ss
, SSL_PE_BAD_CERTIFICATE
);
2619 ss
->sec
.ci
.elements
|= CIS_HAVE_CERTIFICATE
;
2623 rv
= (data
[1] << 8) | data
[2];
2624 SSL_TRC(2, ("%d: SSL[%d]: got error message, error=0x%x",
2625 SSL_GETPID(), ss
->fd
, rv
));
2627 /* Convert protocol error number into API error number */
2629 case SSL_PE_NO_CYPHERS
:
2630 rv
= SSL_ERROR_NO_CYPHER_OVERLAP
;
2632 case SSL_PE_NO_CERTIFICATE
:
2633 rv
= SSL_ERROR_NO_CERTIFICATE
;
2635 case SSL_PE_BAD_CERTIFICATE
:
2636 rv
= SSL_ERROR_BAD_CERTIFICATE
;
2638 case SSL_PE_UNSUPPORTED_CERTIFICATE_TYPE
:
2639 rv
= SSL_ERROR_UNSUPPORTED_CERTIFICATE_TYPE
;
2644 /* XXX make certificate-request optionally fail... */
2649 SSL_DBG(("%d: SSL[%d]: unknown message %d",
2650 SSL_GETPID(), ss
->fd
, data
[0]));
2654 SSL_TRC(3, ("%d: SSL[%d]: handled %d message, required=0x%x got=0x%x",
2655 SSL_GETPID(), ss
->fd
, data
[0],
2656 ss
->sec
.ci
.requiredElements
, ss
->sec
.ci
.elements
));
2658 rv
= ssl2_TryToFinish(ss
);
2659 if (rv
!= SECSuccess
)
2662 ss
->gs
.recordLen
= 0;
2663 ssl_ReleaseRecvBufLock(ss
);
2665 if (ss
->handshake
== 0) {
2669 ss
->handshake
= ssl_GatherRecord1stHandshake
;
2670 ss
->nextHandshake
= ssl2_HandleMessage
;
2671 return ssl2_TriggerNextMessage(ss
);
2674 PORT_SetError(ss
->sec
.isServer
? SSL_ERROR_BAD_CLIENT
: SSL_ERROR_BAD_SERVER
);
2678 ssl_ReleaseRecvBufLock(ss
);
2682 /************************************************************************/
2684 /* Called from ssl_Do1stHandshake, after ssl2_HandleServerHelloMessage.
2687 ssl2_HandleVerifyMessage(sslSocket
*ss
)
2692 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2693 ssl_GetRecvBufLock(ss
);
2695 data
= ss
->gs
.buf
.buf
+ ss
->gs
.recordOffset
;
2696 DUMP_MSG(29, (ss
, data
, ss
->gs
.recordLen
));
2697 if ((ss
->gs
.recordLen
!= 1 + SSL_CHALLENGE_BYTES
) ||
2698 (data
[0] != SSL_MT_SERVER_VERIFY
) ||
2699 NSS_SecureMemcmp(data
+1, ss
->sec
.ci
.clientChallenge
,
2700 SSL_CHALLENGE_BYTES
)) {
2702 PORT_SetError(SSL_ERROR_BAD_SERVER
);
2705 ss
->sec
.ci
.elements
|= CIS_HAVE_VERIFY
;
2707 SSL_TRC(5, ("%d: SSL[%d]: got server-verify, required=0x%d got=0x%x",
2708 SSL_GETPID(), ss
->fd
, ss
->sec
.ci
.requiredElements
,
2709 ss
->sec
.ci
.elements
));
2711 rv
= ssl2_TryToFinish(ss
);
2715 ss
->gs
.recordLen
= 0;
2716 ssl_ReleaseRecvBufLock(ss
);
2718 if (ss
->handshake
== 0) {
2721 ss
->handshake
= ssl_GatherRecord1stHandshake
;
2722 ss
->nextHandshake
= ssl2_HandleMessage
;
2727 ssl_ReleaseRecvBufLock(ss
);
2731 /* Not static because ssl2_GatherData() tests ss->nextHandshake for this value.
2733 * Called from ssl_Do1stHandshake after ssl2_BeginClientHandshake()
2736 ssl2_HandleServerHelloMessage(sslSocket
*ss
)
2743 int needed
, sidHit
, certLen
, csLen
, cidLen
, certType
, err
;
2745 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2747 if (!ss
->opt
.enableSSL2
) {
2748 PORT_SetError(SSL_ERROR_SSL2_DISABLED
);
2752 ssl_GetRecvBufLock(ss
);
2754 PORT_Assert(ss
->sec
.ci
.sid
!= 0);
2755 sid
= ss
->sec
.ci
.sid
;
2757 data
= ss
->gs
.buf
.buf
+ ss
->gs
.recordOffset
;
2758 DUMP_MSG(29, (ss
, data
, ss
->gs
.recordLen
));
2760 /* Make sure first message has some data and is the server hello message */
2761 if ((ss
->gs
.recordLen
< SSL_HL_SERVER_HELLO_HBYTES
)
2762 || (data
[0] != SSL_MT_SERVER_HELLO
)) {
2763 if ((data
[0] == SSL_MT_ERROR
) && (ss
->gs
.recordLen
== 3)) {
2764 err
= (data
[1] << 8) | data
[2];
2765 if (err
== SSL_PE_NO_CYPHERS
) {
2766 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP
);
2775 ss
->version
= (data
[3] << 8) | data
[4];
2776 certLen
= (data
[5] << 8) | data
[6];
2777 csLen
= (data
[7] << 8) | data
[8];
2778 cidLen
= (data
[9] << 8) | data
[10];
2779 cert
= data
+ SSL_HL_SERVER_HELLO_HBYTES
;
2780 cs
= cert
+ certLen
;
2783 ("%d: SSL[%d]: server-hello, hit=%d vers=%x certLen=%d csLen=%d cidLen=%d",
2784 SSL_GETPID(), ss
->fd
, sidHit
, ss
->version
, certLen
,
2786 if (ss
->version
!= SSL_LIBRARY_VERSION_2
) {
2787 if (ss
->version
< SSL_LIBRARY_VERSION_2
) {
2788 SSL_TRC(3, ("%d: SSL[%d]: demoting self (%x) to server version (%x)",
2789 SSL_GETPID(), ss
->fd
, SSL_LIBRARY_VERSION_2
,
2792 SSL_TRC(1, ("%d: SSL[%d]: server version is %x (we are %x)",
2793 SSL_GETPID(), ss
->fd
, ss
->version
, SSL_LIBRARY_VERSION_2
));
2794 /* server claims to be newer but does not follow protocol */
2795 PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION
);
2800 if ((SSL_HL_SERVER_HELLO_HBYTES
+ certLen
+ csLen
+ cidLen
2803 /* || cidLen < SSL_CONNECTIONID_BYTES || cidLen > 32 */
2808 /* Save connection-id.
2809 ** This code only saves the first 16 byte of the connectionID.
2810 ** If the connectionID is shorter than 16 bytes, it is zero-padded.
2812 if (cidLen
< sizeof ss
->sec
.ci
.connectionID
)
2813 memset(ss
->sec
.ci
.connectionID
, 0, sizeof ss
->sec
.ci
.connectionID
);
2814 cidLen
= PR_MIN(cidLen
, sizeof ss
->sec
.ci
.connectionID
);
2815 PORT_Memcpy(ss
->sec
.ci
.connectionID
, cs
+ csLen
, cidLen
);
2817 /* See if session-id hit */
2818 needed
= CIS_HAVE_MASTER_KEY
| CIS_HAVE_FINISHED
| CIS_HAVE_VERIFY
;
2820 if (certLen
|| csLen
) {
2821 /* Uh oh - bogus server */
2822 SSL_DBG(("%d: SSL[%d]: client, huh? hit=%d certLen=%d csLen=%d",
2823 SSL_GETPID(), ss
->fd
, sidHit
, certLen
, csLen
));
2828 SSL_TRC(1, ("%d: SSL[%d]: client, using nonce for peer=0x%08x "
2830 SSL_GETPID(), ss
->fd
, ss
->sec
.ci
.peer
, ss
->sec
.ci
.port
));
2831 ss
->sec
.peerCert
= CERT_DupCertificate(sid
->peerCert
);
2832 ss
->sec
.authAlgorithm
= sid
->authAlgorithm
;
2833 ss
->sec
.authKeyBits
= sid
->authKeyBits
;
2834 ss
->sec
.keaType
= sid
->keaType
;
2835 ss
->sec
.keaKeyBits
= sid
->keaKeyBits
;
2836 rv
= ssl2_CreateSessionCypher(ss
, sid
, PR_TRUE
);
2837 if (rv
!= SECSuccess
) {
2841 if (certType
!= SSL_CT_X509_CERTIFICATE
) {
2842 PORT_SetError(SSL_ERROR_UNSUPPORTED_CERTIFICATE_TYPE
);
2846 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP
);
2847 SSL_DBG(("%d: SSL[%d]: no cipher overlap",
2848 SSL_GETPID(), ss
->fd
));
2852 SSL_DBG(("%d: SSL[%d]: client, huh? certLen=%d csLen=%d",
2853 SSL_GETPID(), ss
->fd
, certLen
, csLen
));
2857 if (sid
->cached
!= never_cached
) {
2858 /* Forget our session-id - server didn't like it */
2859 SSL_TRC(7, ("%d: SSL[%d]: server forgot me, uncaching session-id",
2860 SSL_GETPID(), ss
->fd
));
2861 if (ss
->sec
.uncache
)
2862 (*ss
->sec
.uncache
)(sid
);
2864 ss
->sec
.ci
.sid
= sid
= PORT_ZNew(sslSessionID
);
2868 sid
->references
= 1;
2869 sid
->addr
= ss
->sec
.ci
.peer
;
2870 sid
->port
= ss
->sec
.ci
.port
;
2873 /* decode the server's certificate */
2874 rv
= ssl2_ClientHandleServerCert(ss
, cert
, certLen
);
2875 if (rv
!= SECSuccess
) {
2876 if (PORT_GetError() == SSL_ERROR_BAD_CERTIFICATE
) {
2877 (void) ssl2_SendErrorMessage(ss
, SSL_PE_BAD_CERTIFICATE
);
2882 /* Setup new session cipher */
2883 rv
= ssl2_ClientSetupSessionCypher(ss
, cs
, csLen
);
2884 if (rv
!= SECSuccess
) {
2885 if (PORT_GetError() == SSL_ERROR_BAD_CERTIFICATE
) {
2886 (void) ssl2_SendErrorMessage(ss
, SSL_PE_BAD_CERTIFICATE
);
2892 /* Build up final list of required elements */
2893 ss
->sec
.ci
.elements
= CIS_HAVE_MASTER_KEY
;
2894 ss
->sec
.ci
.requiredElements
= needed
;
2897 /* verify the server's certificate. if sidHit, don't check signatures */
2898 rv
= (* ss
->authCertificate
)(ss
->authCertificateArg
, ss
->fd
,
2899 (PRBool
)(!sidHit
), PR_FALSE
);
2901 if (ss
->handleBadCert
) {
2902 rv
= (*ss
->handleBadCert
)(ss
->badCertArg
, ss
->fd
);
2904 if ( rv
== SECWouldBlock
) {
2905 SSL_DBG(("%d: SSL[%d]: SSL2 bad cert handler returned "
2906 "SECWouldBlock", SSL_GETPID(), ss
->fd
));
2907 PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_SSL2
);
2911 SSL_DBG(("%d: SSL[%d]: server certificate is no good: error=%d",
2912 SSL_GETPID(), ss
->fd
, PORT_GetError()));
2918 SSL_DBG(("%d: SSL[%d]: server certificate is no good: error=%d",
2919 SSL_GETPID(), ss
->fd
, PORT_GetError()));
2925 ** At this point we have a completed session key and our session
2926 ** cipher is setup and ready to go. Switch to encrypted write routine
2927 ** as all future message data is to be encrypted.
2929 ssl2_UseEncryptedSendFunc(ss
);
2931 rv
= ssl2_TryToFinish(ss
);
2932 if (rv
!= SECSuccess
)
2935 ss
->gs
.recordLen
= 0;
2937 ssl_ReleaseRecvBufLock(ss
);
2939 if (ss
->handshake
== 0) {
2943 SSL_TRC(5, ("%d: SSL[%d]: got server-hello, required=0x%d got=0x%x",
2944 SSL_GETPID(), ss
->fd
, ss
->sec
.ci
.requiredElements
,
2945 ss
->sec
.ci
.elements
));
2946 ss
->handshake
= ssl_GatherRecord1stHandshake
;
2947 ss
->nextHandshake
= ssl2_HandleVerifyMessage
;
2951 PORT_SetError(SSL_ERROR_BAD_SERVER
);
2955 ssl_ReleaseRecvBufLock(ss
);
2959 /* Sends out the initial client Hello message on the connection.
2960 * Acquires and releases the socket's xmitBufLock.
2963 ssl2_BeginClientHandshake(sslSocket
*ss
)
2968 PRUint8
*localCipherSpecs
= NULL
;
2969 unsigned int localCipherSize
;
2971 int sendLen
, sidLen
= 0;
2973 TLSExtensionData
*xtnData
;
2975 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
2977 ss
->sec
.isServer
= 0;
2978 ss
->sec
.sendSequence
= 0;
2979 ss
->sec
.rcvSequence
= 0;
2980 ssl_ChooseSessionIDProcs(&ss
->sec
);
2982 if (!ss
->cipherSpecs
) {
2983 rv
= ssl2_ConstructCipherSpecs(ss
);
2984 if (rv
!= SECSuccess
)
2988 /* count the SSL2 and SSL3 enabled ciphers.
2989 * if either is zero, clear the socket's enable for that protocol.
2991 rv
= ssl2_CheckConfigSanity(ss
);
2992 if (rv
!= SECSuccess
)
2995 /* Get peer name of server */
2996 rv
= ssl_GetPeerInfo(ss
);
3000 * On some HP-UX B.11.00 systems, getpeername() occasionally
3001 * fails with ENOTCONN after a successful completion of
3002 * non-blocking connect. I found that if we do a write()
3003 * and then retry getpeername(), it will work.
3005 if (PR_GetError() == PR_NOT_CONNECTED_ERROR
) {
3007 (void) PR_Write(ss
->fd
->lower
, &dummy
, 0);
3008 rv
= ssl_GetPeerInfo(ss
);
3018 SSL_TRC(3, ("%d: SSL[%d]: sending client-hello", SSL_GETPID(), ss
->fd
));
3020 /* Try to find server in our session-id cache */
3021 if (ss
->opt
.noCache
) {
3024 sid
= ssl_LookupSID(&ss
->sec
.ci
.peer
, ss
->sec
.ci
.port
, ss
->peerID
,
3027 while (sid
) { /* this isn't really a loop */
3028 PRBool sidVersionEnabled
=
3029 (!SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
) &&
3030 sid
->version
>= ss
->vrange
.min
&&
3031 sid
->version
<= ss
->vrange
.max
) ||
3032 (sid
->version
< SSL_LIBRARY_VERSION_3_0
&& ss
->opt
.enableSSL2
);
3034 /* if we're not doing this SID's protocol any more, drop it. */
3035 if (!sidVersionEnabled
) {
3036 if (ss
->sec
.uncache
)
3037 ss
->sec
.uncache(sid
);
3042 if (sid
->version
< SSL_LIBRARY_VERSION_3_0
) {
3043 /* If the cipher in this sid is not enabled, drop it. */
3044 for (i
= 0; i
< ss
->sizeCipherSpecs
; i
+= 3) {
3045 if (ss
->cipherSpecs
[i
] == sid
->u
.ssl2
.cipherType
)
3048 if (i
>= ss
->sizeCipherSpecs
) {
3049 if (ss
->sec
.uncache
)
3050 ss
->sec
.uncache(sid
);
3056 sidLen
= sizeof(sid
->u
.ssl2
.sessionID
);
3057 PRINT_BUF(4, (ss
, "client, found session-id:", sid
->u
.ssl2
.sessionID
,
3059 ss
->version
= sid
->version
;
3060 PORT_Assert(!ss
->sec
.localCert
);
3061 if (ss
->sec
.localCert
) {
3062 CERT_DestroyCertificate(ss
->sec
.localCert
);
3064 ss
->sec
.localCert
= CERT_DupCertificate(sid
->localCert
);
3065 break; /* this isn't really a loop */
3069 sid
= PORT_ZNew(sslSessionID
);
3073 sid
->references
= 1;
3074 sid
->cached
= never_cached
;
3075 sid
->addr
= ss
->sec
.ci
.peer
;
3076 sid
->port
= ss
->sec
.ci
.port
;
3077 if (ss
->peerID
!= NULL
) {
3078 sid
->peerID
= PORT_Strdup(ss
->peerID
);
3080 if (ss
->url
!= NULL
) {
3081 sid
->urlSvrName
= PORT_Strdup(ss
->url
);
3084 ss
->sec
.ci
.sid
= sid
;
3086 PORT_Assert(sid
!= NULL
);
3088 if ((sid
->version
>= SSL_LIBRARY_VERSION_3_0
|| !ss
->opt
.v2CompatibleHello
) &&
3089 !SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
)) {
3090 ss
->gs
.state
= GS_INIT
;
3091 ss
->handshake
= ssl_GatherRecord1stHandshake
;
3093 /* ssl3_SendClientHello will override this if it succeeds. */
3094 ss
->version
= SSL_LIBRARY_VERSION_3_0
;
3096 ssl_GetSSL3HandshakeLock(ss
);
3097 ssl_GetXmitBufLock(ss
);
3098 rv
= ssl3_SendClientHello(ss
, PR_FALSE
);
3099 ssl_ReleaseXmitBufLock(ss
);
3100 ssl_ReleaseSSL3HandshakeLock(ss
);
3104 #if defined(NSS_ENABLE_ECC)
3105 /* ensure we don't neogtiate ECC cipher suites with SSL2 hello */
3106 ssl3_DisableECCSuites(ss
, NULL
); /* disable all ECC suites */
3107 if (ss
->cipherSpecs
!= NULL
) {
3108 PORT_Free(ss
->cipherSpecs
);
3109 ss
->cipherSpecs
= NULL
;
3110 ss
->sizeCipherSpecs
= 0;
3114 if (!ss
->cipherSpecs
) {
3115 rv
= ssl2_ConstructCipherSpecs(ss
);
3120 localCipherSpecs
= ss
->cipherSpecs
;
3121 localCipherSize
= ss
->sizeCipherSpecs
;
3123 /* Add 3 for SCSV */
3124 sendLen
= SSL_HL_CLIENT_HELLO_HBYTES
+ localCipherSize
+ 3 + sidLen
+
3125 SSL_CHALLENGE_BYTES
;
3127 /* Generate challenge bytes for server */
3128 PK11_GenerateRandom(ss
->sec
.ci
.clientChallenge
, SSL_CHALLENGE_BYTES
);
3130 ssl_GetXmitBufLock(ss
); /***************************************/
3132 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
3136 /* Construct client-hello message */
3137 cp
= msg
= ss
->sec
.ci
.sendBuf
.buf
;
3138 msg
[0] = SSL_MT_CLIENT_HELLO
;
3139 ss
->clientHelloVersion
= SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
) ?
3140 SSL_LIBRARY_VERSION_2
: ss
->vrange
.max
;
3142 msg
[1] = MSB(ss
->clientHelloVersion
);
3143 msg
[2] = LSB(ss
->clientHelloVersion
);
3144 /* Add 3 for SCSV */
3145 msg
[3] = MSB(localCipherSize
+ 3);
3146 msg
[4] = LSB(localCipherSize
+ 3);
3147 msg
[5] = MSB(sidLen
);
3148 msg
[6] = LSB(sidLen
);
3149 msg
[7] = MSB(SSL_CHALLENGE_BYTES
);
3150 msg
[8] = LSB(SSL_CHALLENGE_BYTES
);
3151 cp
+= SSL_HL_CLIENT_HELLO_HBYTES
;
3152 PORT_Memcpy(cp
, localCipherSpecs
, localCipherSize
);
3153 cp
+= localCipherSize
;
3155 * Add SCSV. SSL 2.0 cipher suites are listed before SSL 3.0 cipher
3156 * suites in localCipherSpecs for compatibility with SSL 2.0 servers.
3157 * Since SCSV looks like an SSL 3.0 cipher suite, we can't add it at
3165 PORT_Memcpy(cp
, sid
->u
.ssl2
.sessionID
, sidLen
);
3168 PORT_Memcpy(cp
, ss
->sec
.ci
.clientChallenge
, SSL_CHALLENGE_BYTES
);
3170 /* Send it to the server */
3171 DUMP_MSG(29, (ss
, msg
, sendLen
));
3172 ss
->handshakeBegun
= 1;
3173 rv
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
3175 ssl_ReleaseXmitBufLock(ss
); /***************************************/
3181 rv
= ssl3_StartHandshakeHash(ss
, msg
, sendLen
);
3187 * Since we sent the SCSV, pretend we sent empty RI extension. We need
3188 * to record the extension has been advertised after ssl3_InitState has
3189 * been called, which ssl3_StartHandshakeHash took care for us above.
3191 xtnData
= &ss
->xtnData
;
3192 xtnData
->advertised
[xtnData
->numAdvertised
++] = ssl_renegotiation_info_xtn
;
3194 /* Setup to receive servers hello message */
3195 ssl_GetRecvBufLock(ss
);
3196 ss
->gs
.recordLen
= 0;
3197 ssl_ReleaseRecvBufLock(ss
);
3199 ss
->handshake
= ssl_GatherRecord1stHandshake
;
3200 ss
->nextHandshake
= ssl2_HandleServerHelloMessage
;
3204 ssl_ReleaseXmitBufLock(ss
);
3209 /************************************************************************/
3211 /* Handle the CLIENT-MASTER-KEY message.
3212 ** Acquires and releases RecvBufLock.
3213 ** Called from ssl2_HandleClientHelloMessage().
3216 ssl2_HandleClientSessionKeyMessage(sslSocket
*ss
)
3222 unsigned int keyBits
;
3227 ssl_GetRecvBufLock(ss
);
3229 data
= ss
->gs
.buf
.buf
+ ss
->gs
.recordOffset
;
3230 DUMP_MSG(29, (ss
, data
, ss
->gs
.recordLen
));
3232 if ((ss
->gs
.recordLen
< SSL_HL_CLIENT_MASTER_KEY_HBYTES
)
3233 || (data
[0] != SSL_MT_CLIENT_MASTER_KEY
)) {
3237 keyBits
= (data
[2] << 8) | data
[3];
3238 ckLen
= (data
[4] << 8) | data
[5];
3239 ekLen
= (data
[6] << 8) | data
[7];
3240 caLen
= (data
[8] << 8) | data
[9];
3242 SSL_TRC(5, ("%d: SSL[%d]: session-key, cipher=%d keyBits=%d ckLen=%d ekLen=%d caLen=%d",
3243 SSL_GETPID(), ss
->fd
, cipher
, keyBits
, ckLen
, ekLen
, caLen
));
3245 if (ss
->gs
.recordLen
<
3246 SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ ckLen
+ ekLen
+ caLen
) {
3247 SSL_DBG(("%d: SSL[%d]: protocol size mismatch dataLen=%d",
3248 SSL_GETPID(), ss
->fd
, ss
->gs
.recordLen
));
3252 /* Use info from client to setup session key */
3253 rv
= ssl2_ServerSetupSessionCypher(ss
, cipher
, keyBits
,
3254 data
+ SSL_HL_CLIENT_MASTER_KEY_HBYTES
, ckLen
,
3255 data
+ SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ ckLen
, ekLen
,
3256 data
+ SSL_HL_CLIENT_MASTER_KEY_HBYTES
+ ckLen
+ ekLen
, caLen
);
3257 ss
->gs
.recordLen
= 0; /* we're done with this record. */
3259 ssl_ReleaseRecvBufLock(ss
);
3261 if (rv
!= SECSuccess
) {
3264 ss
->sec
.ci
.elements
|= CIS_HAVE_MASTER_KEY
;
3265 ssl2_UseEncryptedSendFunc(ss
);
3267 /* Send server verify message now that keys are established */
3268 rv
= ssl2_SendServerVerifyMessage(ss
);
3269 if (rv
!= SECSuccess
)
3272 rv
= ssl2_TryToFinish(ss
);
3273 if (rv
!= SECSuccess
)
3275 if (ss
->handshake
== 0) {
3279 SSL_TRC(5, ("%d: SSL[%d]: server: waiting for elements=0x%d",
3280 SSL_GETPID(), ss
->fd
,
3281 ss
->sec
.ci
.requiredElements
^ ss
->sec
.ci
.elements
));
3282 ss
->handshake
= ssl_GatherRecord1stHandshake
;
3283 ss
->nextHandshake
= ssl2_HandleMessage
;
3285 return ssl2_TriggerNextMessage(ss
);
3288 ssl_ReleaseRecvBufLock(ss
);
3289 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
3297 ** Handle the initial hello message from the client
3299 ** not static because ssl2_GatherData() tests ss->nextHandshake for this value.
3302 ssl2_HandleClientHelloMessage(sslSocket
*ss
)
3305 sslServerCerts
* sc
;
3306 CERTCertificate
*serverCert
;
3311 PRUint8
*cert
= NULL
;
3313 unsigned int challengeLen
;
3321 int gotXmitBufLock
= 0;
3322 #if defined(SOLARIS) && defined(i386)
3323 volatile PRUint8 hit
;
3327 PRUint8 csImpl
[sizeof implementedCipherSuites
];
3329 PORT_Assert( ss
->opt
.noLocks
|| ssl_Have1stHandshakeLock(ss
) );
3331 sc
= ss
->serverCerts
+ kt_rsa
;
3332 serverCert
= sc
->serverCert
;
3334 ssl_GetRecvBufLock(ss
);
3337 data
= ss
->gs
.buf
.buf
+ ss
->gs
.recordOffset
;
3338 DUMP_MSG(29, (ss
, data
, ss
->gs
.recordLen
));
3340 /* Make sure first message has some data and is the client hello message */
3341 if ((ss
->gs
.recordLen
< SSL_HL_CLIENT_HELLO_HBYTES
)
3342 || (data
[0] != SSL_MT_CLIENT_HELLO
)) {
3346 /* Get peer name of client */
3347 rv
= ssl_GetPeerInfo(ss
);
3348 if (rv
!= SECSuccess
) {
3352 /* Examine version information */
3354 * See if this might be a V2 client hello asking to use the V3 protocol
3356 if ((data
[0] == SSL_MT_CLIENT_HELLO
) &&
3357 (data
[1] >= MSB(SSL_LIBRARY_VERSION_3_0
)) &&
3358 !SSL3_ALL_VERSIONS_DISABLED(&ss
->vrange
)) {
3359 rv
= ssl3_HandleV2ClientHello(ss
, data
, ss
->gs
.recordLen
);
3360 if (rv
!= SECFailure
) { /* Success */
3361 ss
->handshake
= NULL
;
3362 ss
->nextHandshake
= ssl_GatherRecord1stHandshake
;
3363 ss
->securityHandshake
= NULL
;
3364 ss
->gs
.state
= GS_INIT
;
3366 /* ssl3_HandleV3ClientHello has set ss->version,
3367 ** and has gotten us a brand new sid.
3369 ss
->sec
.ci
.sid
->version
= ss
->version
;
3371 ssl_ReleaseRecvBufLock(ss
);
3374 /* Previously, there was a test here to see if SSL2 was enabled.
3375 ** If not, an error code was set, and SECFailure was returned,
3376 ** without sending any error code to the other end of the connection.
3377 ** That test has been removed. If SSL2 has been disabled, there
3378 ** should be no SSL2 ciphers enabled, and consequently, the code
3379 ** below should send the ssl2 error message SSL_PE_NO_CYPHERS.
3380 ** We now believe this is the correct thing to do, even when SSL2
3381 ** has been explicitly disabled by the application.
3384 /* Extract info from message */
3385 ss
->version
= (data
[1] << 8) | data
[2];
3387 /* If some client thinks ssl v2 is 2.0 instead of 0.2, we'll allow it. */
3388 if (ss
->version
>= SSL_LIBRARY_VERSION_3_0
) {
3389 ss
->version
= SSL_LIBRARY_VERSION_2
;
3392 csLen
= (data
[3] << 8) | data
[4];
3393 sdLen
= (data
[5] << 8) | data
[6];
3394 challengeLen
= (data
[7] << 8) | data
[8];
3395 cs
= data
+ SSL_HL_CLIENT_HELLO_HBYTES
;
3397 challenge
= sd
+ sdLen
;
3398 PRINT_BUF(7, (ss
, "server, client session-id value:", sd
, sdLen
));
3400 if (!csLen
|| (csLen
% 3) != 0 ||
3401 (sdLen
!= 0 && sdLen
!= SSL2_SESSIONID_BYTES
) ||
3402 challengeLen
< SSL_MIN_CHALLENGE_BYTES
||
3403 challengeLen
> SSL_MAX_CHALLENGE_BYTES
||
3404 (unsigned)ss
->gs
.recordLen
!=
3405 SSL_HL_CLIENT_HELLO_HBYTES
+ csLen
+ sdLen
+ challengeLen
) {
3406 SSL_DBG(("%d: SSL[%d]: bad client hello message, len=%d should=%d",
3407 SSL_GETPID(), ss
->fd
, ss
->gs
.recordLen
,
3408 SSL_HL_CLIENT_HELLO_HBYTES
+csLen
+sdLen
+challengeLen
));
3412 SSL_TRC(3, ("%d: SSL[%d]: client version is %x",
3413 SSL_GETPID(), ss
->fd
, ss
->version
));
3414 if (ss
->version
!= SSL_LIBRARY_VERSION_2
) {
3415 if (ss
->version
> SSL_LIBRARY_VERSION_2
) {
3417 ** Newer client than us. Things are ok because new clients
3418 ** are required to be backwards compatible with old servers.
3419 ** Change version number to our version number so that client
3422 ss
->version
= SSL_LIBRARY_VERSION_2
;
3424 SSL_TRC(1, ("%d: SSL[%d]: client version is %x (we are %x)",
3425 SSL_GETPID(), ss
->fd
, ss
->version
, SSL_LIBRARY_VERSION_2
));
3426 PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION
);
3431 /* Qualify cipher specs before returning them to client */
3432 csLen
= ssl2_QualifyCypherSpecs(ss
, cs
, csLen
);
3434 /* no overlap, send client our list of supported SSL v2 ciphers. */
3436 csLen
= sizeof implementedCipherSuites
;
3437 PORT_Memcpy(cs
, implementedCipherSuites
, csLen
);
3438 csLen
= ssl2_QualifyCypherSpecs(ss
, cs
, csLen
);
3440 /* We don't support any SSL v2 ciphers! */
3441 ssl2_SendErrorMessage(ss
, SSL_PE_NO_CYPHERS
);
3442 PORT_SetError(SSL_ERROR_NO_CYPHER_OVERLAP
);
3445 /* Since this handhsake is going to fail, don't cache it. */
3446 ss
->opt
.noCache
= 1;
3449 /* Squirrel away the challenge for later */
3450 PORT_Memcpy(ss
->sec
.ci
.clientChallenge
, challenge
, challengeLen
);
3452 /* Examine message and see if session-id is good */
3453 ss
->sec
.ci
.elements
= 0;
3454 if (sdLen
> 0 && !ss
->opt
.noCache
) {
3455 SSL_TRC(7, ("%d: SSL[%d]: server, lookup client session-id for 0x%08x%08x%08x%08x",
3456 SSL_GETPID(), ss
->fd
, ss
->sec
.ci
.peer
.pr_s6_addr32
[0],
3457 ss
->sec
.ci
.peer
.pr_s6_addr32
[1],
3458 ss
->sec
.ci
.peer
.pr_s6_addr32
[2],
3459 ss
->sec
.ci
.peer
.pr_s6_addr32
[3]));
3460 sid
= (*ssl_sid_lookup
)(&ss
->sec
.ci
.peer
, sd
, sdLen
, ss
->dbHandle
);
3465 /* Got a good session-id. Short cut! */
3466 SSL_TRC(1, ("%d: SSL[%d]: server, using session-id for 0x%08x (age=%d)",
3467 SSL_GETPID(), ss
->fd
, ss
->sec
.ci
.peer
,
3468 ssl_Time() - sid
->creationTime
));
3469 PRINT_BUF(1, (ss
, "session-id value:", sd
, sdLen
));
3470 ss
->sec
.ci
.sid
= sid
;
3471 ss
->sec
.ci
.elements
= CIS_HAVE_MASTER_KEY
;
3476 ss
->sec
.authAlgorithm
= sid
->authAlgorithm
;
3477 ss
->sec
.authKeyBits
= sid
->authKeyBits
;
3478 ss
->sec
.keaType
= sid
->keaType
;
3479 ss
->sec
.keaKeyBits
= sid
->keaKeyBits
;
3481 rv
= ssl2_CreateSessionCypher(ss
, sid
, PR_FALSE
);
3482 if (rv
!= SECSuccess
) {
3486 SECItem
* derCert
= &serverCert
->derCert
;
3488 SSL_TRC(7, ("%d: SSL[%d]: server, lookup nonce missed",
3489 SSL_GETPID(), ss
->fd
));
3495 sid
= PORT_ZNew(sslSessionID
);
3499 sid
->references
= 1;
3500 sid
->addr
= ss
->sec
.ci
.peer
;
3501 sid
->port
= ss
->sec
.ci
.port
;
3503 /* Invent a session-id */
3504 ss
->sec
.ci
.sid
= sid
;
3505 PK11_GenerateRandom(sid
->u
.ssl2
.sessionID
+2, SSL2_SESSIONID_BYTES
-2);
3508 sid
->u
.ssl2
.sessionID
[0] = MSB(pid
);
3509 sid
->u
.ssl2
.sessionID
[1] = LSB(pid
);
3510 cert
= derCert
->data
;
3511 certLen
= derCert
->len
;
3513 /* pretend that server sids remember the local cert. */
3514 PORT_Assert(!sid
->localCert
);
3515 if (sid
->localCert
) {
3516 CERT_DestroyCertificate(sid
->localCert
);
3518 sid
->localCert
= CERT_DupCertificate(serverCert
);
3520 ss
->sec
.authAlgorithm
= ssl_sign_rsa
;
3521 ss
->sec
.keaType
= ssl_kea_rsa
;
3522 ss
->sec
.keaKeyBits
= \
3523 ss
->sec
.authKeyBits
= ss
->serverCerts
[kt_rsa
].serverKeyBits
;
3526 /* server sids don't remember the local cert, so whether we found
3527 ** a sid or not, just "remember" we used the rsa server cert.
3529 if (ss
->sec
.localCert
) {
3530 CERT_DestroyCertificate(ss
->sec
.localCert
);
3532 ss
->sec
.localCert
= CERT_DupCertificate(serverCert
);
3534 /* Build up final list of required elements */
3535 ss
->sec
.ci
.requiredElements
= CIS_HAVE_MASTER_KEY
| CIS_HAVE_FINISHED
;
3536 if (ss
->opt
.requestCertificate
) {
3537 ss
->sec
.ci
.requiredElements
|= CIS_HAVE_CERTIFICATE
;
3539 ss
->sec
.ci
.sentElements
= 0;
3541 /* Send hello message back to client */
3542 sendLen
= SSL_HL_SERVER_HELLO_HBYTES
+ certLen
+ csLen
3543 + SSL_CONNECTIONID_BYTES
;
3545 ssl_GetXmitBufLock(ss
); gotXmitBufLock
= 1;
3546 rv
= ssl2_GetSendBuffer(ss
, sendLen
);
3547 if (rv
!= SECSuccess
) {
3551 SSL_TRC(3, ("%d: SSL[%d]: sending server-hello (%d)",
3552 SSL_GETPID(), ss
->fd
, sendLen
));
3554 msg
= ss
->sec
.ci
.sendBuf
.buf
;
3555 msg
[0] = SSL_MT_SERVER_HELLO
;
3557 msg
[2] = SSL_CT_X509_CERTIFICATE
;
3558 msg
[3] = MSB(ss
->version
);
3559 msg
[4] = LSB(ss
->version
);
3560 msg
[5] = MSB(certLen
);
3561 msg
[6] = LSB(certLen
);
3562 msg
[7] = MSB(csLen
);
3563 msg
[8] = LSB(csLen
);
3564 msg
[9] = MSB(SSL_CONNECTIONID_BYTES
);
3565 msg
[10] = LSB(SSL_CONNECTIONID_BYTES
);
3567 PORT_Memcpy(msg
+SSL_HL_SERVER_HELLO_HBYTES
, cert
, certLen
);
3570 PORT_Memcpy(msg
+SSL_HL_SERVER_HELLO_HBYTES
+certLen
, cs
, csLen
);
3572 PORT_Memcpy(msg
+SSL_HL_SERVER_HELLO_HBYTES
+certLen
+csLen
,
3573 ss
->sec
.ci
.connectionID
, SSL_CONNECTIONID_BYTES
);
3575 DUMP_MSG(29, (ss
, msg
, sendLen
));
3577 ss
->handshakeBegun
= 1;
3578 sent
= (*ss
->sec
.send
)(ss
, msg
, sendLen
, 0);
3582 ssl_ReleaseXmitBufLock(ss
); gotXmitBufLock
= 0;
3584 ss
->gs
.recordLen
= 0;
3585 ss
->handshake
= ssl_GatherRecord1stHandshake
;
3587 /* Old SID Session key is good. Go encrypted */
3588 ssl2_UseEncryptedSendFunc(ss
);
3590 /* Send server verify message now that keys are established */
3591 rv
= ssl2_SendServerVerifyMessage(ss
);
3592 if (rv
!= SECSuccess
)
3595 ss
->nextHandshake
= ssl2_HandleMessage
;
3596 ssl_ReleaseRecvBufLock(ss
);
3597 rv
= ssl2_TriggerNextMessage(ss
);
3600 ss
->nextHandshake
= ssl2_HandleClientSessionKeyMessage
;
3601 ssl_ReleaseRecvBufLock(ss
);
3605 PORT_SetError(SSL_ERROR_BAD_CLIENT
);
3609 if (gotXmitBufLock
) {
3610 ssl_ReleaseXmitBufLock(ss
); gotXmitBufLock
= 0;
3612 SSL_TRC(10, ("%d: SSL[%d]: server, wait for client-hello lossage",
3613 SSL_GETPID(), ss
->fd
));
3614 ssl_ReleaseRecvBufLock(ss
);
3619 ssl2_BeginServerHandshake(sslSocket
*ss
)
3622 sslServerCerts
* rsaAuth
= ss
->serverCerts
+ kt_rsa
;
3624 ss
->sec
.isServer
= 1;
3625 ssl_ChooseSessionIDProcs(&ss
->sec
);
3626 ss
->sec
.sendSequence
= 0;
3627 ss
->sec
.rcvSequence
= 0;
3629 /* don't turn on SSL2 if we don't have an RSA key and cert */
3630 if (!rsaAuth
->serverKeyPair
|| !rsaAuth
->SERVERKEY
||
3631 !rsaAuth
->serverCert
) {
3632 ss
->opt
.enableSSL2
= PR_FALSE
;
3635 if (!ss
->cipherSpecs
) {
3636 rv
= ssl2_ConstructCipherSpecs(ss
);
3637 if (rv
!= SECSuccess
)
3641 /* count the SSL2 and SSL3 enabled ciphers.
3642 * if either is zero, clear the socket's enable for that protocol.
3644 rv
= ssl2_CheckConfigSanity(ss
);
3645 if (rv
!= SECSuccess
)
3649 ** Generate connection-id. Always do this, even if things fail
3650 ** immediately. This way the random number generator is always
3651 ** rolling around, every time we get a connection.
3653 PK11_GenerateRandom(ss
->sec
.ci
.connectionID
,
3654 sizeof(ss
->sec
.ci
.connectionID
));
3656 ss
->gs
.recordLen
= 0;
3657 ss
->handshake
= ssl_GatherRecord1stHandshake
;
3658 ss
->nextHandshake
= ssl2_HandleClientHelloMessage
;
3665 /* This function doesn't really belong in this file.
3666 ** It's here to keep AIX compilers from optimizing it away,
3667 ** and not including it in the DSO.
3671 extern const char __nss_ssl_rcsid
[];
3672 extern const char __nss_ssl_sccsid
[];
3675 NSSSSL_VersionCheck(const char *importedVersion
)
3678 * This is the secret handshake algorithm.
3680 * This release has a simple version compatibility
3681 * check algorithm. This release is not backward
3682 * compatible with previous major releases. It is
3683 * not compatible with future major, minor, or
3686 volatile char c
; /* force a reference that won't get optimized away */
3688 c
= __nss_ssl_rcsid
[0] + __nss_ssl_sccsid
[0];
3689 return NSS_VersionCheck(importedVersion
);
3693 NSSSSL_GetVersion(void)