Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / ssl / sslimpl.h
blob2da1b57012b20a8d0ecc90e32780ec5b8cd4da94
1 /*
2 * This file is PRIVATE to SSL and should be the first thing included by
3 * any SSL implementation file.
5 * ***** BEGIN LICENSE BLOCK *****
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
16 * License.
18 * The Original Code is the Netscape security libraries.
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1994-2000
23 * the Initial Developer. All Rights Reserved.
25 * Contributor(s):
26 * Dr Stephen Henson <stephen.henson@gemplus.com>
27 * Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
29 * Alternatively, the contents of this file may be used under the terms of
30 * either the GNU General Public License Version 2 or later (the "GPL"), or
31 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 * in which case the provisions of the GPL or the LGPL are applicable instead
33 * of those above. If you wish to allow use of your version of this file only
34 * under the terms of either the GPL or the LGPL, and not to allow others to
35 * use your version of this file under the terms of the MPL, indicate your
36 * decision by deleting the provisions above and replace them with the notice
37 * and other provisions required by the GPL or the LGPL. If you do not delete
38 * the provisions above, a recipient may use your version of this file under
39 * the terms of any one of the MPL, the GPL or the LGPL.
41 * ***** END LICENSE BLOCK ***** */
42 /* $Id: sslimpl.h,v 1.56 2007/05/01 03:54:58 nelson%bolyard.com Exp $ */
44 #ifndef __sslimpl_h_
45 #define __sslimpl_h_
47 #ifdef DEBUG
48 #undef NDEBUG
49 #else
50 #undef NDEBUG
51 #define NDEBUG
52 #endif
53 #include "secport.h"
54 #include "secerr.h"
55 #include "sslerr.h"
56 #include "ssl3prot.h"
57 #include "hasht.h"
58 #include "nssilock.h"
59 #include "pkcs11t.h"
60 #if defined(XP_UNIX) || defined(XP_BEOS)
61 #include "unistd.h"
62 #endif
63 #include "nssrwlk.h"
64 #include "prthread.h"
66 #include "sslt.h" /* for some formerly private types, now public */
68 /* to make some of these old enums public without namespace pollution,
69 ** it was necessary to prepend ssl_ to the names.
70 ** These #defines preserve compatibility with the old code here in libssl.
72 typedef SSLKEAType SSL3KEAType;
73 typedef SSLMACAlgorithm SSL3MACAlgorithm;
74 typedef SSLSignType SSL3SignType;
76 #define sign_null ssl_sign_null
77 #define sign_rsa ssl_sign_rsa
78 #define sign_dsa ssl_sign_dsa
79 #define sign_ecdsa ssl_sign_ecdsa
81 #define calg_null ssl_calg_null
82 #define calg_rc4 ssl_calg_rc4
83 #define calg_rc2 ssl_calg_rc2
84 #define calg_des ssl_calg_des
85 #define calg_3des ssl_calg_3des
86 #define calg_idea ssl_calg_idea
87 #define calg_fortezza ssl_calg_fortezza /* deprecated, must preserve */
88 #define calg_aes ssl_calg_aes
89 #define calg_camellia ssl_calg_camellia
91 #define mac_null ssl_mac_null
92 #define mac_md5 ssl_mac_md5
93 #define mac_sha ssl_mac_sha
94 #define hmac_md5 ssl_hmac_md5
95 #define hmac_sha ssl_hmac_sha
97 #define SET_ERROR_CODE /* reminder */
98 #define SEND_ALERT /* reminder */
99 #define TEST_FOR_FAILURE /* reminder */
100 #define DEAL_WITH_FAILURE /* reminder */
102 #if defined(DEBUG) || defined(TRACE)
103 #ifdef __cplusplus
104 #define Debug 1
105 #else
106 extern int Debug;
107 #endif
108 #else
109 #undef Debug
110 #endif
112 #if defined(DEBUG) && !defined(TRACE) && !defined(NISCC_TEST)
113 #define TRACE
114 #endif
116 #ifdef TRACE
117 #define SSL_TRC(a,b) if (ssl_trace >= (a)) ssl_Trace b
118 #define PRINT_BUF(a,b) if (ssl_trace >= (a)) ssl_PrintBuf b
119 #define DUMP_MSG(a,b) if (ssl_trace >= (a)) ssl_DumpMsg b
120 #else
121 #define SSL_TRC(a,b)
122 #define PRINT_BUF(a,b)
123 #define DUMP_MSG(a,b)
124 #endif
126 #ifdef DEBUG
127 #define SSL_DBG(b) if (ssl_debug) ssl_Trace b
128 #else
129 #define SSL_DBG(b)
130 #endif
132 #if defined (DEBUG)
133 #ifdef macintosh
134 #include "pprthred.h"
135 #else
136 #include "private/pprthred.h" /* for PR_InMonitor() */
137 #endif
138 #define ssl_InMonitor(m) PZ_InMonitor(m)
139 #endif
141 #define LSB(x) ((unsigned char) (x & 0xff))
142 #define MSB(x) ((unsigned char) (((unsigned)(x)) >> 8))
144 /************************************************************************/
146 typedef enum { SSLAppOpRead = 0,
147 SSLAppOpWrite,
148 SSLAppOpRDWR,
149 SSLAppOpPost,
150 SSLAppOpHeader
151 } SSLAppOperation;
153 #define SSL_MIN_MASTER_KEY_BYTES 5
154 #define SSL_MAX_MASTER_KEY_BYTES 64
156 #define SSL2_SESSIONID_BYTES 16
157 #define SSL3_SESSIONID_BYTES 32
159 #define SSL_MIN_CHALLENGE_BYTES 16
160 #define SSL_MAX_CHALLENGE_BYTES 32
161 #define SSL_CHALLENGE_BYTES 16
163 #define SSL_CONNECTIONID_BYTES 16
165 #define SSL_MIN_CYPHER_ARG_BYTES 0
166 #define SSL_MAX_CYPHER_ARG_BYTES 32
168 #define SSL_MAX_MAC_BYTES 16
170 #define SSL3_RSA_PMS_LENGTH 48
171 #define SSL3_MASTER_SECRET_LENGTH 48
173 /* number of wrap mechanisms potentially used to wrap master secrets. */
174 #define SSL_NUM_WRAP_MECHS 15
176 /* This makes the cert cache entry exactly 4k. */
177 #define SSL_MAX_CACHED_CERT_LEN 4060
179 #define MAX_EXTENSION_SENDERS 3
181 #define NUM_MIXERS 9
183 /* Mask of the 25 named curves we support. */
184 #ifndef NSS_ECC_MORE_THAN_SUITE_B
185 #define SSL3_SUPPORTED_CURVES_MASK 0x3800000 /* only 3 curves, suite B*/
186 #else
187 #define SSL3_SUPPORTED_CURVES_MASK 0x3fffffe
188 #endif
190 #ifndef BPB
191 #define BPB 8 /* Bits Per Byte */
192 #endif
194 typedef struct sslBufferStr sslBuffer;
195 typedef struct sslConnectInfoStr sslConnectInfo;
196 typedef struct sslGatherStr sslGather;
197 typedef struct sslSecurityInfoStr sslSecurityInfo;
198 typedef struct sslSessionIDStr sslSessionID;
199 typedef struct sslSocketStr sslSocket;
200 typedef struct sslSocketOpsStr sslSocketOps;
202 typedef struct ssl3StateStr ssl3State;
203 typedef struct ssl3CertNodeStr ssl3CertNode;
204 typedef struct ssl3BulkCipherDefStr ssl3BulkCipherDef;
205 typedef struct ssl3MACDefStr ssl3MACDef;
206 typedef struct ssl3KeyPairStr ssl3KeyPair;
208 struct ssl3CertNodeStr {
209 struct ssl3CertNodeStr *next;
210 CERTCertificate * cert;
213 typedef SECStatus (*sslHandshakeFunc)(sslSocket *ss);
215 /* This type points to the low layer send func,
216 ** e.g. ssl2_SendStream or ssl3_SendPlainText.
217 ** These functions return the same values as PR_Send,
218 ** i.e. >= 0 means number of bytes sent, < 0 means error.
220 typedef PRInt32 (*sslSendFunc)(sslSocket *ss, const unsigned char *buf,
221 PRInt32 n, PRInt32 flags);
223 typedef void (*sslSessionIDCacheFunc) (sslSessionID *sid);
224 typedef void (*sslSessionIDUncacheFunc)(sslSessionID *sid);
225 typedef sslSessionID *(*sslSessionIDLookupFunc)(const PRIPv6Addr *addr,
226 unsigned char* sid,
227 unsigned int sidLen,
228 CERTCertDBHandle * dbHandle);
230 /* registerable callback function that either appends extension to buffer
231 * or returns length of data that it would have appended.
233 typedef PRInt32 (*ssl3HelloExtensionSenderFunc)(sslSocket *ss, PRBool append,
234 PRUint32 maxBytes);
236 /* registerable callback function that handles a received extension,
237 * of the given type.
239 typedef SECStatus (* ssl3HelloExtensionHandlerFunc)(sslSocket *ss,
240 PRUint16 ex_type,
241 SECItem * data);
243 /* row in a table of hello extension senders */
244 typedef struct {
245 PRInt32 ex_type;
246 ssl3HelloExtensionSenderFunc ex_sender;
247 } ssl3HelloExtensionSender;
249 /* row in a table of hello extension handlers */
250 typedef struct {
251 PRInt32 ex_type;
252 ssl3HelloExtensionHandlerFunc ex_handler;
253 } ssl3HelloExtensionHandler;
255 extern SECStatus
256 ssl3_RegisterServerHelloExtensionSender(sslSocket *ss, PRUint16 ex_type,
257 ssl3HelloExtensionSenderFunc cb);
259 extern PRInt32
260 ssl3_CallHelloExtensionSenders(sslSocket *ss, PRBool append, PRUint32 maxBytes,
261 const ssl3HelloExtensionSender *sender);
263 /* Socket ops */
264 struct sslSocketOpsStr {
265 int (*connect) (sslSocket *, const PRNetAddr *);
266 PRFileDesc *(*accept) (sslSocket *, PRNetAddr *);
267 int (*bind) (sslSocket *, const PRNetAddr *);
268 int (*listen) (sslSocket *, int);
269 int (*shutdown)(sslSocket *, int);
270 int (*close) (sslSocket *);
272 int (*recv) (sslSocket *, unsigned char *, int, int);
274 /* points to the higher-layer send func, e.g. ssl_SecureSend. */
275 int (*send) (sslSocket *, const unsigned char *, int, int);
276 int (*read) (sslSocket *, unsigned char *, int);
277 int (*write) (sslSocket *, const unsigned char *, int);
279 int (*getpeername)(sslSocket *, PRNetAddr *);
280 int (*getsockname)(sslSocket *, PRNetAddr *);
283 /* Flags interpreted by ssl send functions. */
284 #define ssl_SEND_FLAG_FORCE_INTO_BUFFER 0x40000000
285 #define ssl_SEND_FLAG_NO_BUFFER 0x20000000
286 #define ssl_SEND_FLAG_MASK 0x7f000000
289 ** A buffer object.
291 struct sslBufferStr {
292 unsigned char * buf;
293 unsigned int len;
294 unsigned int space;
298 ** SSL3 cipher suite policy and preference struct.
300 typedef struct {
301 #if !defined(_WIN32)
302 unsigned int cipher_suite : 16;
303 unsigned int policy : 8;
304 unsigned int enabled : 1;
305 unsigned int isPresent : 1;
306 #else
307 ssl3CipherSuite cipher_suite;
308 PRUint8 policy;
309 unsigned char enabled : 1;
310 unsigned char isPresent : 1;
311 #endif
312 } ssl3CipherSuiteCfg;
314 #ifdef NSS_ENABLE_ECC
315 #define ssl_V3_SUITES_IMPLEMENTED 49
316 #else
317 #define ssl_V3_SUITES_IMPLEMENTED 29
318 #endif /* NSS_ENABLE_ECC */
320 typedef struct sslOptionsStr {
321 unsigned int useSecurity : 1; /* 1 */
322 unsigned int useSocks : 1; /* 2 */
323 unsigned int requestCertificate : 1; /* 3 */
324 unsigned int requireCertificate : 2; /* 4-5 */
325 unsigned int handshakeAsClient : 1; /* 6 */
326 unsigned int handshakeAsServer : 1; /* 7 */
327 unsigned int enableSSL2 : 1; /* 8 */
328 unsigned int enableSSL3 : 1; /* 9 */
329 unsigned int enableTLS : 1; /* 10 */
330 unsigned int noCache : 1; /* 11 */
331 unsigned int fdx : 1; /* 12 */
332 unsigned int v2CompatibleHello : 1; /* 13 */
333 unsigned int detectRollBack : 1; /* 14 */
334 unsigned int noStepDown : 1; /* 15 */
335 unsigned int bypassPKCS11 : 1; /* 16 */
336 unsigned int noLocks : 1; /* 17 */
337 } sslOptions;
339 typedef enum { sslHandshakingUndetermined = 0,
340 sslHandshakingAsClient,
341 sslHandshakingAsServer
342 } sslHandshakingType;
344 typedef struct sslServerCertsStr {
345 /* Configuration state for server sockets */
346 CERTCertificate * serverCert;
347 CERTCertificateList * serverCertChain;
348 ssl3KeyPair * serverKeyPair;
349 unsigned int serverKeyBits;
350 } sslServerCerts;
352 #define SERVERKEY serverKeyPair->privKey
354 #define SSL_LOCK_RANK_SPEC 255
355 #define SSL_LOCK_RANK_GLOBAL NSS_RWLOCK_RANK_NONE
357 /* These are the valid values for shutdownHow.
358 ** These values are each 1 greater than the NSPR values, and the code
359 ** depends on that relation to efficiently convert PR_SHUTDOWN values
360 ** into ssl_SHUTDOWN values. These values use one bit for read, and
361 ** another bit for write, and can be used as bitmasks.
363 #define ssl_SHUTDOWN_NONE 0 /* NOT shutdown at all */
364 #define ssl_SHUTDOWN_RCV 1 /* PR_SHUTDOWN_RCV +1 */
365 #define ssl_SHUTDOWN_SEND 2 /* PR_SHUTDOWN_SEND +1 */
366 #define ssl_SHUTDOWN_BOTH 3 /* PR_SHUTDOWN_BOTH +1 */
369 ** A gather object. Used to read some data until a count has been
370 ** satisfied. Primarily for support of async sockets.
371 ** Everything in here is protected by the recvBufLock.
373 struct sslGatherStr {
374 int state; /* see GS_ values below. */ /* ssl 2 & 3 */
376 /* "buf" holds received plaintext SSL records, after decrypt and MAC check.
377 * SSL2: recv'd ciphertext records are put here, then decrypted in place.
378 * SSL3: recv'd ciphertext records are put in inbuf (see below), then
379 * decrypted into buf.
381 sslBuffer buf; /*recvBufLock*/ /* ssl 2 & 3 */
383 /* number of bytes previously read into hdr or buf(ssl2) or inbuf (ssl3).
384 ** (offset - writeOffset) is the number of ciphertext bytes read in but
385 ** not yet deciphered.
387 unsigned int offset; /* ssl 2 & 3 */
389 /* number of bytes to read in next call to ssl_DefRecv (recv) */
390 unsigned int remainder; /* ssl 2 & 3 */
392 /* Number of ciphertext bytes to read in after 2-byte SSL record header. */
393 unsigned int count; /* ssl2 only */
395 /* size of the final plaintext record.
396 ** == count - (recordPadding + MAC size)
398 unsigned int recordLen; /* ssl2 only */
400 /* number of bytes of padding to be removed after decrypting. */
401 /* This value is taken from the record's hdr[2], which means a too large
402 * value could crash us.
404 unsigned int recordPadding; /* ssl2 only */
406 /* plaintext DATA begins this many bytes into "buf". */
407 unsigned int recordOffset; /* ssl2 only */
409 int encrypted; /* SSL2 session is now encrypted. ssl2 only */
411 /* These next two values are used by SSL2 and SSL3.
412 ** DoRecv uses them to extract application data.
413 ** The difference between writeOffset and readOffset is the amount of
414 ** data available to the application. Note that the actual offset of
415 ** the data in "buf" is recordOffset (above), not readOffset.
416 ** In the current implementation, this is made available before the
417 ** MAC is checked!!
419 unsigned int readOffset; /* Spot where DATA reader (e.g. application
420 ** or handshake code) will read next.
421 ** Always zero for SSl3 application data.
423 /* offset in buf/inbuf/hdr into which new data will be read from socket. */
424 unsigned int writeOffset;
426 /* Buffer for ssl3 to read (encrypted) data from the socket */
427 sslBuffer inbuf; /*recvBufLock*/ /* ssl3 only */
429 /* The ssl[23]_GatherData functions read data into this buffer, rather
430 ** than into buf or inbuf, while in the GS_HEADER state.
431 ** The portion of the SSL record header put here always comes off the wire
432 ** as plaintext, never ciphertext.
433 ** For SSL2, the plaintext portion is two bytes long. For SSl3 it is 5.
435 unsigned char hdr[5]; /* ssl 2 & 3 */
438 /* sslGather.state */
439 #define GS_INIT 0
440 #define GS_HEADER 1
441 #define GS_MAC 2
442 #define GS_DATA 3
443 #define GS_PAD 4
445 typedef SECStatus (*SSLCipher)(void * context,
446 unsigned char * out,
447 int * outlen,
448 int maxout,
449 const unsigned char *in,
450 int inlen);
451 typedef SECStatus (*SSLDestroy)(void *context, PRBool freeit);
456 ** ssl3State and CipherSpec structs
459 /* The SSL bulk cipher definition */
460 typedef enum {
461 cipher_null,
462 cipher_rc4,
463 cipher_rc4_40,
464 cipher_rc4_56,
465 cipher_rc2,
466 cipher_rc2_40,
467 cipher_des,
468 cipher_3des,
469 cipher_des40,
470 cipher_idea,
471 cipher_aes_128,
472 cipher_aes_256,
473 cipher_camellia_128,
474 cipher_camellia_256,
475 cipher_missing /* reserved for no such supported cipher */
476 /* This enum must match ssl3_cipherName[] in ssl3con.c. */
477 } SSL3BulkCipher;
479 typedef enum { type_stream, type_block } CipherType;
481 #define MAX_IV_LENGTH 64
484 * Do not depend upon 64 bit arithmetic in the underlying machine.
486 typedef struct {
487 uint32 high;
488 uint32 low;
489 } SSL3SequenceNumber;
491 #define MAX_MAC_CONTEXT_BYTES 400
492 #define MAX_MAC_CONTEXT_LLONGS (MAX_MAC_CONTEXT_BYTES / 8)
494 #define MAX_CIPHER_CONTEXT_BYTES 2080
495 #define MAX_CIPHER_CONTEXT_LLONGS (MAX_CIPHER_CONTEXT_BYTES / 8)
497 typedef struct {
498 SSL3Opaque client_write_iv [24];
499 SSL3Opaque server_write_iv [24];
500 SSL3Opaque wrapped_master_secret [48];
501 PRUint16 wrapped_master_secret_len;
502 PRUint8 msIsWrapped;
503 PRUint8 resumable;
504 } ssl3SidKeys; /* 100 bytes */
506 typedef struct {
507 PK11SymKey *write_key;
508 PK11SymKey *write_mac_key;
509 PK11Context *write_mac_context;
510 SECItem write_key_item;
511 SECItem write_iv_item;
512 SECItem write_mac_key_item;
513 SSL3Opaque write_iv[MAX_IV_LENGTH];
514 PRUint64 cipher_context[MAX_CIPHER_CONTEXT_LLONGS];
515 } ssl3KeyMaterial;
518 ** These are the "specs" in the "ssl3" struct.
519 ** Access to the pointers to these specs, and all the specs' contents
520 ** (direct and indirect) is protected by the reader/writer lock ss->specLock.
522 typedef struct {
523 const ssl3BulkCipherDef *cipher_def;
524 const ssl3MACDef * mac_def;
525 int mac_size;
526 SSLCipher encode;
527 SSLCipher decode;
528 SSLDestroy destroy;
529 void * encodeContext;
530 void * decodeContext;
531 PRBool bypassCiphers; /* did double bypass (at least) */
532 PK11SymKey * master_secret;
533 SSL3SequenceNumber write_seq_num;
534 SSL3SequenceNumber read_seq_num;
535 SSL3ProtocolVersion version;
536 ssl3KeyMaterial client;
537 ssl3KeyMaterial server;
538 SECItem msItem;
539 unsigned char key_block[NUM_MIXERS * MD5_LENGTH];
540 unsigned char raw_master_secret[56];
541 } ssl3CipherSpec;
543 typedef enum { never_cached,
544 in_client_cache,
545 in_server_cache,
546 invalid_cache /* no longer in any cache. */
547 } Cached;
549 struct sslSessionIDStr {
550 sslSessionID * next; /* chain used for client sockets, only */
552 CERTCertificate * peerCert;
553 const char * peerID; /* client only */
554 const char * urlSvrName; /* client only */
555 CERTCertificate * localCert;
557 PRIPv6Addr addr;
558 PRUint16 port;
560 SSL3ProtocolVersion version;
562 PRUint32 creationTime; /* seconds since Jan 1, 1970 */
563 PRUint32 lastAccessTime; /* seconds since Jan 1, 1970 */
564 PRUint32 expirationTime; /* seconds since Jan 1, 1970 */
565 Cached cached;
566 int references;
568 SSLSignType authAlgorithm;
569 PRUint32 authKeyBits;
570 SSLKEAType keaType;
571 PRUint32 keaKeyBits;
573 union {
574 struct {
575 /* the V2 code depends upon the size of sessionID. */
576 unsigned char sessionID[SSL2_SESSIONID_BYTES];
578 /* Stuff used to recreate key and read/write cipher objects */
579 SECItem masterKey; /* never wrapped */
580 int cipherType;
581 SECItem cipherArg;
582 int keyBits;
583 int secretKeyBits;
584 } ssl2;
585 struct {
586 /* values that are copied into the server's on-disk SID cache. */
587 uint8 sessionIDLength;
588 SSL3Opaque sessionID[SSL3_SESSIONID_BYTES];
590 ssl3CipherSuite cipherSuite;
591 SSL3CompressionMethod compression;
592 int policy;
593 ssl3SidKeys keys;
594 CK_MECHANISM_TYPE masterWrapMech;
595 /* mechanism used to wrap master secret */
596 SSL3KEAType exchKeyType;
597 /* key type used in exchange algorithm,
598 * and to wrap the sym wrapping key. */
599 #ifdef NSS_ENABLE_ECC
600 PRUint32 negotiatedECCurves;
601 #endif /* NSS_ENABLE_ECC */
603 /* The following values are NOT restored from the server's on-disk
604 * session cache, but are restored from the client's cache.
606 PK11SymKey * clientWriteKey;
607 PK11SymKey * serverWriteKey;
609 /* The following values pertain to the slot that wrapped the
610 ** master secret. (used only in client)
612 SECMODModuleID masterModuleID;
613 /* what module wrapped the master secret */
614 CK_SLOT_ID masterSlotID;
615 PRUint16 masterWrapIndex;
616 /* what's the key index for the wrapping key */
617 PRUint16 masterWrapSeries;
618 /* keep track of the slot series, so we don't
619 * accidently try to use new keys after the
620 * card gets removed and replaced.*/
622 /* The following values pertain to the slot that did the signature
623 ** for client auth. (used only in client)
625 SECMODModuleID clAuthModuleID;
626 CK_SLOT_ID clAuthSlotID;
627 PRUint16 clAuthSeries;
629 char masterValid;
630 char clAuthValid;
632 } ssl3;
633 } u;
637 typedef struct ssl3CipherSuiteDefStr {
638 ssl3CipherSuite cipher_suite;
639 SSL3BulkCipher bulk_cipher_alg;
640 SSL3MACAlgorithm mac_alg;
641 SSL3KeyExchangeAlgorithm key_exchange_alg;
642 } ssl3CipherSuiteDef;
645 ** There are tables of these, all const.
647 typedef struct {
648 SSL3KeyExchangeAlgorithm kea;
649 SSL3KEAType exchKeyType;
650 SSL3SignType signKeyType;
651 PRBool is_limited;
652 int key_size_limit;
653 PRBool tls_keygen;
654 } ssl3KEADef;
656 typedef enum { kg_null, kg_strong, kg_export } SSL3KeyGenMode;
659 ** There are tables of these, all const.
661 struct ssl3BulkCipherDefStr {
662 SSL3BulkCipher cipher;
663 SSLCipherAlgorithm calg;
664 int key_size;
665 int secret_key_size;
666 CipherType type;
667 int iv_size;
668 int block_size;
669 SSL3KeyGenMode keygen_mode;
673 ** There are tables of these, all const.
675 struct ssl3MACDefStr {
676 SSL3MACAlgorithm mac;
677 CK_MECHANISM_TYPE mmech;
678 int pad_size;
679 int mac_size;
682 typedef enum {
683 wait_client_hello,
684 wait_client_cert,
685 wait_client_key,
686 wait_cert_verify,
687 wait_change_cipher,
688 wait_finished,
689 wait_server_hello,
690 wait_server_cert,
691 wait_server_key,
692 wait_cert_request,
693 wait_hello_done,
694 idle_handshake
695 } SSL3WaitState;
698 ** This is the "hs" member of the "ssl3" struct.
699 ** This entire struct is protected by ssl3HandshakeLock
701 typedef struct SSL3HandshakeStateStr {
702 SSL3Random server_random;
703 SSL3Random client_random;
704 SSL3WaitState ws;
705 PRUint64 md5_cx[MAX_MAC_CONTEXT_LLONGS];
706 PRUint64 sha_cx[MAX_MAC_CONTEXT_LLONGS];
707 PK11Context * md5; /* handshake running hashes */
708 PK11Context * sha;
709 const ssl3KEADef * kea_def;
710 ssl3CipherSuite cipher_suite;
711 const ssl3CipherSuiteDef *suite_def;
712 SSL3CompressionMethod compression;
713 sslBuffer msg_body; /* protected by recvBufLock */
714 /* partial handshake message from record layer */
715 unsigned int header_bytes;
716 /* number of bytes consumed from handshake */
717 /* message for message type and header length */
718 SSL3HandshakeType msg_type;
719 unsigned long msg_len;
720 SECItem ca_list; /* used only by client */
721 PRBool isResuming; /* are we resuming a session */
722 PRBool rehandshake; /* immediately start another handshake
723 * when this one finishes */
724 PRBool usedStepDownKey; /* we did a server key exchange. */
725 sslBuffer msgState; /* current state for handshake messages*/
726 /* protected by recvBufLock */
727 sslBuffer messages; /* Accumulated handshake messages */
728 #ifdef NSS_ENABLE_ECC
729 PRUint32 negotiatedECCurves; /* bit mask */
730 #endif /* NSS_ENABLE_ECC */
731 } SSL3HandshakeState;
736 ** This is the "ssl3" struct, as in "ss->ssl3".
737 ** note:
738 ** usually, crSpec == cwSpec and prSpec == pwSpec.
739 ** Sometimes, crSpec == pwSpec and prSpec == cwSpec.
740 ** But there are never more than 2 actual specs.
741 ** No spec must ever be modified if either "current" pointer points to it.
743 struct ssl3StateStr {
746 ** The following Specs and Spec pointers must be protected using the
747 ** Spec Lock.
749 ssl3CipherSpec * crSpec; /* current read spec. */
750 ssl3CipherSpec * prSpec; /* pending read spec. */
751 ssl3CipherSpec * cwSpec; /* current write spec. */
752 ssl3CipherSpec * pwSpec; /* pending write spec. */
754 CERTCertificate * clientCertificate; /* used by client */
755 SECKEYPrivateKey * clientPrivateKey; /* used by client */
756 CERTCertificateList *clientCertChain; /* used by client */
757 PRBool sendEmptyCert; /* used by client */
759 int policy;
760 /* This says what cipher suites we can do, and should
761 * be either SSL_ALLOWED or SSL_RESTRICTED
763 PRArenaPool * peerCertArena;
764 /* These are used to keep track of the peer CA */
765 void * peerCertChain;
766 /* chain while we are trying to validate it. */
767 CERTDistNames * ca_list;
768 /* used by server. trusted CAs for this socket. */
769 PRBool initialized;
770 SSL3HandshakeState hs;
771 ssl3CipherSpec specs[2]; /* one is current, one is pending. */
774 typedef struct {
775 SSL3ContentType type;
776 SSL3ProtocolVersion version;
777 sslBuffer * buf;
778 } SSL3Ciphertext;
780 struct ssl3KeyPairStr {
781 SECKEYPrivateKey * privKey;
782 SECKEYPublicKey * pubKey;
783 PRInt32 refCount; /* use PR_Atomic calls for this. */
786 typedef struct SSLWrappedSymWrappingKeyStr {
787 SSL3Opaque wrappedSymmetricWrappingkey[512];
788 SSL3Opaque wrapIV[24];
789 CK_MECHANISM_TYPE symWrapMechanism;
790 /* unwrapped symmetric wrapping key uses this mechanism */
791 CK_MECHANISM_TYPE asymWrapMechanism;
792 /* mechanism used to wrap the SymmetricWrappingKey using
793 * server's public and/or private keys. */
794 SSL3KEAType exchKeyType; /* type of keys used to wrap SymWrapKey*/
795 PRInt32 symWrapMechIndex;
796 PRUint16 wrappedSymKeyLen;
797 PRUint16 wrapIVLen;
798 } SSLWrappedSymWrappingKey;
810 * SSL2 buffers used in SSL3.
811 * writeBuf in the SecurityInfo maintained by sslsecur.c is used
812 * to hold the data just about to be passed to the kernel
813 * sendBuf in the ConnectInfo maintained by sslcon.c is used
814 * to hold handshake messages as they are accumulated
818 ** This is "ci", as in "ss->sec.ci".
820 ** Protection: All the variables in here are protected by
821 ** firstHandshakeLock AND (in ssl3) ssl3HandshakeLock
823 struct sslConnectInfoStr {
824 /* outgoing handshakes appended to this. */
825 sslBuffer sendBuf; /*xmitBufLock*/ /* ssl 2 & 3 */
827 PRIPv6Addr peer; /* ssl 2 & 3 */
828 unsigned short port; /* ssl 2 & 3 */
830 sslSessionID *sid; /* ssl 2 & 3 */
832 /* see CIS_HAVE defines below for the bit values in *elements. */
833 char elements; /* ssl2 only */
834 char requiredElements; /* ssl2 only */
835 char sentElements; /* ssl2 only */
837 char sentFinished; /* ssl2 only */
839 /* Length of server challenge. Used by client when saving challenge */
840 int serverChallengeLen; /* ssl2 only */
841 /* type of authentication requested by server */
842 unsigned char authType; /* ssl2 only */
844 /* Challenge sent by client to server in client-hello message */
845 /* SSL3 gets a copy of this. See ssl3_StartHandshakeHash(). */
846 unsigned char clientChallenge[SSL_MAX_CHALLENGE_BYTES]; /* ssl 2 & 3 */
848 /* Connection-id sent by server to client in server-hello message */
849 unsigned char connectionID[SSL_CONNECTIONID_BYTES]; /* ssl2 only */
851 /* Challenge sent by server to client in request-certificate message */
852 unsigned char serverChallenge[SSL_MAX_CHALLENGE_BYTES]; /* ssl2 only */
854 /* Information kept to handle a request-certificate message */
855 unsigned char readKey[SSL_MAX_MASTER_KEY_BYTES]; /* ssl2 only */
856 unsigned char writeKey[SSL_MAX_MASTER_KEY_BYTES]; /* ssl2 only */
857 unsigned keySize; /* ssl2 only */
860 /* bit values for ci->elements, ci->requiredElements, sentElements. */
861 #define CIS_HAVE_MASTER_KEY 0x01
862 #define CIS_HAVE_CERTIFICATE 0x02
863 #define CIS_HAVE_FINISHED 0x04
864 #define CIS_HAVE_VERIFY 0x08
866 /* Note: The entire content of this struct and whatever it points to gets
867 * blown away by SSL_ResetHandshake(). This is "sec" as in "ss->sec".
869 * Unless otherwise specified below, the contents of this struct are
870 * protected by firstHandshakeLock AND (in ssl3) ssl3HandshakeLock.
872 struct sslSecurityInfoStr {
873 sslSendFunc send; /*xmitBufLock*/ /* ssl 2 & 3 */
874 int isServer; /* Spec Lock?*/ /* ssl 2 & 3 */
875 sslBuffer writeBuf; /*xmitBufLock*/ /* ssl 2 & 3 */
877 int cipherType; /* ssl 2 & 3 */
878 int keyBits; /* ssl 2 & 3 */
879 int secretKeyBits; /* ssl 2 & 3 */
880 CERTCertificate *localCert; /* ssl 2 & 3 */
881 CERTCertificate *peerCert; /* ssl 2 & 3 */
882 SECKEYPublicKey *peerKey; /* ssl3 only */
884 SSLSignType authAlgorithm;
885 PRUint32 authKeyBits;
886 SSLKEAType keaType;
887 PRUint32 keaKeyBits;
890 ** Procs used for SID cache (nonce) management.
891 ** Different implementations exist for clients/servers
892 ** The lookup proc is only used for servers. Baloney!
894 sslSessionIDCacheFunc cache; /* ssl 2 & 3 */
895 sslSessionIDUncacheFunc uncache; /* ssl 2 & 3 */
898 ** everything below here is for ssl2 only.
899 ** This stuff is equivalent to SSL3's "spec", and is protected by the
900 ** same "Spec Lock" as used for SSL3's specs.
902 uint32 sendSequence; /*xmitBufLock*/ /* ssl2 only */
903 uint32 rcvSequence; /*recvBufLock*/ /* ssl2 only */
905 /* Hash information; used for one-way-hash functions (MD2, MD5, etc.) */
906 const SECHashObject *hash; /* Spec Lock */ /* ssl2 only */
907 void *hashcx; /* Spec Lock */ /* ssl2 only */
909 SECItem sendSecret; /* Spec Lock */ /* ssl2 only */
910 SECItem rcvSecret; /* Spec Lock */ /* ssl2 only */
912 /* Session cypher contexts; one for each direction */
913 void *readcx; /* Spec Lock */ /* ssl2 only */
914 void *writecx; /* Spec Lock */ /* ssl2 only */
915 SSLCipher enc; /* Spec Lock */ /* ssl2 only */
916 SSLCipher dec; /* Spec Lock */ /* ssl2 only */
917 void (*destroy)(void *, PRBool); /* Spec Lock */ /* ssl2 only */
919 /* Blocking information for the session cypher */
920 int blockShift; /* Spec Lock */ /* ssl2 only */
921 int blockSize; /* Spec Lock */ /* ssl2 only */
923 /* These are used during a connection handshake */
924 sslConnectInfo ci; /* ssl 2 & 3 */
930 ** SSL Socket struct
932 ** Protection: XXX
934 struct sslSocketStr {
935 PRFileDesc * fd;
937 /* Pointer to operations vector for this socket */
938 const sslSocketOps * ops;
940 /* SSL socket options */
941 sslOptions opt;
943 /* State flags */
944 unsigned long clientAuthRequested;
945 unsigned long delayDisabled; /* Nagle delay disabled */
946 unsigned long firstHsDone; /* first handshake is complete. */
947 unsigned long handshakeBegun;
948 unsigned long lastWriteBlocked;
949 unsigned long recvdCloseNotify; /* received SSL EOF. */
950 unsigned long TCPconnected;
951 unsigned long appDataBuffered;
953 /* version of the protocol to use */
954 SSL3ProtocolVersion version;
955 SSL3ProtocolVersion clientHelloVersion; /* version sent in client hello. */
957 sslSecurityInfo sec; /* not a pointer any more */
959 /* protected by firstHandshakeLock AND (in ssl3) ssl3HandshakeLock. */
960 const char *url; /* ssl 2 & 3 */
962 sslHandshakeFunc handshake; /*firstHandshakeLock*/
963 sslHandshakeFunc nextHandshake; /*firstHandshakeLock*/
964 sslHandshakeFunc securityHandshake; /*firstHandshakeLock*/
966 /* registered callbacks that send server hello extensions */
967 ssl3HelloExtensionSender serverExtensionSenders[MAX_EXTENSION_SENDERS];
969 /* the following variable is only used with socks or other proxies. */
970 char * peerID; /* String uniquely identifies target server. */
972 unsigned char * cipherSpecs;
973 unsigned int sizeCipherSpecs;
974 const unsigned char * preferredCipher;
976 ssl3KeyPair * stepDownKeyPair; /* RSA step down keys */
978 /* Callbacks */
979 SSLAuthCertificate authCertificate;
980 void *authCertificateArg;
981 SSLGetClientAuthData getClientAuthData;
982 void *getClientAuthDataArg;
983 SSLBadCertHandler handleBadCert;
984 void *badCertArg;
985 SSLHandshakeCallback handshakeCallback;
986 void *handshakeCallbackData;
987 void *pkcs11PinArg;
989 PRIntervalTime rTimeout; /* timeout for NSPR I/O */
990 PRIntervalTime wTimeout; /* timeout for NSPR I/O */
991 PRIntervalTime cTimeout; /* timeout for NSPR I/O */
993 PZLock * recvLock; /* lock against multiple reader threads. */
994 PZLock * sendLock; /* lock against multiple sender threads. */
996 PZMonitor * recvBufLock; /* locks low level recv buffers. */
997 PZMonitor * xmitBufLock; /* locks low level xmit buffers. */
999 /* Only one thread may operate on the socket until the initial handshake
1000 ** is complete. This Monitor ensures that. Since SSL2 handshake is
1001 ** only done once, this is also effectively the SSL2 handshake lock.
1003 PZMonitor * firstHandshakeLock;
1005 /* This monitor protects the ssl3 handshake state machine data.
1006 ** Only one thread (reader or writer) may be in the ssl3 handshake state
1007 ** machine at any time. */
1008 PZMonitor * ssl3HandshakeLock;
1010 /* reader/writer lock, protects the secret data needed to encrypt and MAC
1011 ** outgoing records, and to decrypt and MAC check incoming ciphertext
1012 ** records. */
1013 NSSRWLock * specLock;
1015 /* handle to perm cert db (and implicitly to the temp cert db) used
1016 ** with this socket.
1018 CERTCertDBHandle * dbHandle;
1020 PRThread * writerThread; /* thread holds SSL_LOCK_WRITER lock */
1022 PRUint16 shutdownHow; /* See ssl_SHUTDOWN defines below. */
1024 PRUint16 allowedByPolicy; /* copy of global policy bits. */
1025 PRUint16 maybeAllowedByPolicy; /* copy of global policy bits. */
1026 PRUint16 chosenPreference; /* SSL2 cipher preferences. */
1028 sslHandshakingType handshaking;
1030 /* Gather object used for gathering data */
1031 sslGather gs; /*recvBufLock*/
1033 sslBuffer saveBuf; /*xmitBufLock*/
1034 sslBuffer pendingBuf; /*xmitBufLock*/
1036 /* Configuration state for server sockets */
1037 /* server cert and key for each KEA type */
1038 sslServerCerts serverCerts[kt_kea_size];
1040 ssl3CipherSuiteCfg cipherSuites[ssl_V3_SUITES_IMPLEMENTED];
1041 ssl3KeyPair * ephemeralECDHKeyPair; /* for ECDHE-* handshake */
1043 /* SSL3 state info. Formerly was a pointer */
1044 ssl3State ssl3;
1049 /* All the global data items declared here should be protected using the
1050 ** ssl_global_data_lock, which is a reader/writer lock.
1052 extern NSSRWLock * ssl_global_data_lock;
1053 extern char ssl_debug;
1054 extern char ssl_trace;
1055 extern FILE * ssl_trace_iob;
1056 extern CERTDistNames * ssl3_server_ca_list;
1057 extern PRUint32 ssl_sid_timeout;
1058 extern PRUint32 ssl3_sid_timeout;
1059 extern PRBool ssl3_global_policy_some_restricted;
1061 extern const char * const ssl_cipherName[];
1062 extern const char * const ssl3_cipherName[];
1064 extern sslSessionIDLookupFunc ssl_sid_lookup;
1065 extern sslSessionIDCacheFunc ssl_sid_cache;
1066 extern sslSessionIDUncacheFunc ssl_sid_uncache;
1068 /************************************************************************/
1070 SEC_BEGIN_PROTOS
1072 /* Implementation of ops for default (non socks, non secure) case */
1073 extern int ssl_DefConnect(sslSocket *ss, const PRNetAddr *addr);
1074 extern PRFileDesc *ssl_DefAccept(sslSocket *ss, PRNetAddr *addr);
1075 extern int ssl_DefBind(sslSocket *ss, const PRNetAddr *addr);
1076 extern int ssl_DefListen(sslSocket *ss, int backlog);
1077 extern int ssl_DefShutdown(sslSocket *ss, int how);
1078 extern int ssl_DefClose(sslSocket *ss);
1079 extern int ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags);
1080 extern int ssl_DefSend(sslSocket *ss, const unsigned char *buf,
1081 int len, int flags);
1082 extern int ssl_DefRead(sslSocket *ss, unsigned char *buf, int len);
1083 extern int ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len);
1084 extern int ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name);
1085 extern int ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name);
1086 extern int ssl_DefGetsockopt(sslSocket *ss, PRSockOption optname,
1087 void *optval, PRInt32 *optlen);
1088 extern int ssl_DefSetsockopt(sslSocket *ss, PRSockOption optname,
1089 const void *optval, PRInt32 optlen);
1091 /* Implementation of ops for socks only case */
1092 extern int ssl_SocksConnect(sslSocket *ss, const PRNetAddr *addr);
1093 extern PRFileDesc *ssl_SocksAccept(sslSocket *ss, PRNetAddr *addr);
1094 extern int ssl_SocksBind(sslSocket *ss, const PRNetAddr *addr);
1095 extern int ssl_SocksListen(sslSocket *ss, int backlog);
1096 extern int ssl_SocksGetsockname(sslSocket *ss, PRNetAddr *name);
1097 extern int ssl_SocksRecv(sslSocket *ss, unsigned char *buf, int len, int flags);
1098 extern int ssl_SocksSend(sslSocket *ss, const unsigned char *buf,
1099 int len, int flags);
1100 extern int ssl_SocksRead(sslSocket *ss, unsigned char *buf, int len);
1101 extern int ssl_SocksWrite(sslSocket *ss, const unsigned char *buf, int len);
1103 /* Implementation of ops for secure only case */
1104 extern int ssl_SecureConnect(sslSocket *ss, const PRNetAddr *addr);
1105 extern PRFileDesc *ssl_SecureAccept(sslSocket *ss, PRNetAddr *addr);
1106 extern int ssl_SecureRecv(sslSocket *ss, unsigned char *buf,
1107 int len, int flags);
1108 extern int ssl_SecureSend(sslSocket *ss, const unsigned char *buf,
1109 int len, int flags);
1110 extern int ssl_SecureRead(sslSocket *ss, unsigned char *buf, int len);
1111 extern int ssl_SecureWrite(sslSocket *ss, const unsigned char *buf, int len);
1112 extern int ssl_SecureShutdown(sslSocket *ss, int how);
1113 extern int ssl_SecureClose(sslSocket *ss);
1115 /* Implementation of ops for secure socks case */
1116 extern int ssl_SecureSocksConnect(sslSocket *ss, const PRNetAddr *addr);
1117 extern PRFileDesc *ssl_SecureSocksAccept(sslSocket *ss, PRNetAddr *addr);
1118 extern PRFileDesc *ssl_FindTop(sslSocket *ss);
1120 /* Gather funcs. */
1121 extern sslGather * ssl_NewGather(void);
1122 extern SECStatus ssl_InitGather(sslGather *gs);
1123 extern void ssl_DestroyGather(sslGather *gs);
1124 extern int ssl2_GatherData(sslSocket *ss, sslGather *gs, int flags);
1125 extern int ssl2_GatherRecord(sslSocket *ss, int flags);
1126 extern SECStatus ssl_GatherRecord1stHandshake(sslSocket *ss);
1128 extern SECStatus ssl2_HandleClientHelloMessage(sslSocket *ss);
1129 extern SECStatus ssl2_HandleServerHelloMessage(sslSocket *ss);
1130 extern int ssl2_StartGatherBytes(sslSocket *ss, sslGather *gs,
1131 unsigned int count);
1133 extern SECStatus ssl_CreateSecurityInfo(sslSocket *ss);
1134 extern SECStatus ssl_CopySecurityInfo(sslSocket *ss, sslSocket *os);
1135 extern void ssl_ResetSecurityInfo(sslSecurityInfo *sec, PRBool doMemset);
1136 extern void ssl_DestroySecurityInfo(sslSecurityInfo *sec);
1138 extern sslSocket * ssl_DupSocket(sslSocket *old);
1140 extern void ssl_PrintBuf(sslSocket *ss, const char *msg, const void *cp, int len);
1141 extern void ssl_DumpMsg(sslSocket *ss, unsigned char *bp, unsigned len);
1143 extern int ssl_SendSavedWriteData(sslSocket *ss);
1144 extern SECStatus ssl_SaveWriteData(sslSocket *ss,
1145 const void* p, unsigned int l);
1146 extern SECStatus ssl2_BeginClientHandshake(sslSocket *ss);
1147 extern SECStatus ssl2_BeginServerHandshake(sslSocket *ss);
1148 extern int ssl_Do1stHandshake(sslSocket *ss);
1150 extern SECStatus sslBuffer_Grow(sslBuffer *b, unsigned int newLen);
1151 extern SECStatus sslBuffer_Append(sslBuffer *b, const void * data,
1152 unsigned int len);
1154 extern void ssl2_UseClearSendFunc(sslSocket *ss);
1155 extern void ssl_ChooseSessionIDProcs(sslSecurityInfo *sec);
1157 extern sslSessionID *ssl_LookupSID(const PRIPv6Addr *addr, PRUint16 port,
1158 const char *peerID, const char *urlSvrName);
1159 extern void ssl_FreeSID(sslSessionID *sid);
1161 extern int ssl3_SendApplicationData(sslSocket *ss, const PRUint8 *in,
1162 int len, int flags);
1164 extern PRBool ssl_FdIsBlocking(PRFileDesc *fd);
1166 extern SECStatus ssl_SetTimeout(PRFileDesc *fd, PRIntervalTime timeout);
1168 extern PRBool ssl_SocketIsBlocking(sslSocket *ss);
1170 extern void ssl_SetAlwaysBlock(sslSocket *ss);
1172 extern SECStatus ssl_EnableNagleDelay(sslSocket *ss, PRBool enabled);
1174 #define SSL_LOCK_READER(ss) if (ss->recvLock) PZ_Lock(ss->recvLock)
1175 #define SSL_UNLOCK_READER(ss) if (ss->recvLock) PZ_Unlock(ss->recvLock)
1176 #define SSL_LOCK_WRITER(ss) if (ss->sendLock) PZ_Lock(ss->sendLock)
1177 #define SSL_UNLOCK_WRITER(ss) if (ss->sendLock) PZ_Unlock(ss->sendLock)
1179 #define ssl_Get1stHandshakeLock(ss) \
1180 { if (!ss->opt.noLocks) PZ_EnterMonitor((ss)->firstHandshakeLock); }
1181 #define ssl_Release1stHandshakeLock(ss) \
1182 { if (!ss->opt.noLocks) PZ_ExitMonitor((ss)->firstHandshakeLock); }
1183 #define ssl_Have1stHandshakeLock(ss) \
1184 (PZ_InMonitor((ss)->firstHandshakeLock))
1186 #define ssl_GetSSL3HandshakeLock(ss) \
1187 { if (!ss->opt.noLocks) PZ_EnterMonitor((ss)->ssl3HandshakeLock); }
1188 #define ssl_ReleaseSSL3HandshakeLock(ss) \
1189 { if (!ss->opt.noLocks) PZ_ExitMonitor((ss)->ssl3HandshakeLock); }
1190 #define ssl_HaveSSL3HandshakeLock(ss) \
1191 (PZ_InMonitor((ss)->ssl3HandshakeLock))
1193 #define ssl_GetSpecReadLock(ss) \
1194 { if (!ss->opt.noLocks) NSSRWLock_LockRead((ss)->specLock); }
1195 #define ssl_ReleaseSpecReadLock(ss) \
1196 { if (!ss->opt.noLocks) NSSRWLock_UnlockRead((ss)->specLock); }
1198 #define ssl_GetSpecWriteLock(ss) \
1199 { if (!ss->opt.noLocks) NSSRWLock_LockWrite((ss)->specLock); }
1200 #define ssl_ReleaseSpecWriteLock(ss) \
1201 { if (!ss->opt.noLocks) NSSRWLock_UnlockWrite((ss)->specLock); }
1202 #define ssl_HaveSpecWriteLock(ss) \
1203 (NSSRWLock_HaveWriteLock((ss)->specLock))
1205 #define ssl_GetRecvBufLock(ss) \
1206 { if (!ss->opt.noLocks) PZ_EnterMonitor((ss)->recvBufLock); }
1207 #define ssl_ReleaseRecvBufLock(ss) \
1208 { if (!ss->opt.noLocks) PZ_ExitMonitor( (ss)->recvBufLock); }
1209 #define ssl_HaveRecvBufLock(ss) \
1210 (PZ_InMonitor((ss)->recvBufLock))
1212 #define ssl_GetXmitBufLock(ss) \
1213 { if (!ss->opt.noLocks) PZ_EnterMonitor((ss)->xmitBufLock); }
1214 #define ssl_ReleaseXmitBufLock(ss) \
1215 { if (!ss->opt.noLocks) PZ_ExitMonitor( (ss)->xmitBufLock); }
1216 #define ssl_HaveXmitBufLock(ss) \
1217 (PZ_InMonitor((ss)->xmitBufLock))
1220 extern SECStatus ssl3_KeyAndMacDeriveBypass(ssl3CipherSpec * pwSpec,
1221 const unsigned char * cr, const unsigned char * sr,
1222 PRBool isTLS, PRBool isExport);
1223 extern SECStatus ssl3_MasterKeyDeriveBypass( ssl3CipherSpec * pwSpec,
1224 const unsigned char * cr, const unsigned char * sr,
1225 const SECItem * pms, PRBool isTLS, PRBool isRSA);
1227 /* These functions are called from secnav, even though they're "private". */
1229 extern int ssl2_SendErrorMessage(struct sslSocketStr *ss, int error);
1230 extern int SSL_RestartHandshakeAfterServerCert(struct sslSocketStr *ss);
1231 extern int SSL_RestartHandshakeAfterCertReq(struct sslSocketStr *ss,
1232 CERTCertificate *cert,
1233 SECKEYPrivateKey *key,
1234 CERTCertificateList *certChain);
1235 extern sslSocket *ssl_FindSocket(PRFileDesc *fd);
1236 extern void ssl_FreeSocket(struct sslSocketStr *ssl);
1237 extern SECStatus SSL3_SendAlert(sslSocket *ss, SSL3AlertLevel level,
1238 SSL3AlertDescription desc);
1240 extern int ssl2_RestartHandshakeAfterCertReq(sslSocket * ss,
1241 CERTCertificate * cert,
1242 SECKEYPrivateKey * key);
1244 extern SECStatus ssl3_RestartHandshakeAfterCertReq(sslSocket * ss,
1245 CERTCertificate * cert,
1246 SECKEYPrivateKey * key,
1247 CERTCertificateList *certChain);
1249 extern int ssl2_RestartHandshakeAfterServerCert(sslSocket *ss);
1250 extern int ssl3_RestartHandshakeAfterServerCert(sslSocket *ss);
1253 * for dealing with SSL 3.0 clients sending SSL 2.0 format hellos
1255 extern SECStatus ssl3_HandleV2ClientHello(
1256 sslSocket *ss, unsigned char *buffer, int length);
1257 extern SECStatus ssl3_StartHandshakeHash(
1258 sslSocket *ss, unsigned char *buf, int length);
1261 * SSL3 specific routines
1263 SECStatus ssl3_SendClientHello(sslSocket *ss);
1266 * input into the SSL3 machinery from the actualy network reading code
1268 SECStatus ssl3_HandleRecord(
1269 sslSocket *ss, SSL3Ciphertext *cipher, sslBuffer *out);
1271 int ssl3_GatherAppDataRecord(sslSocket *ss, int flags);
1272 int ssl3_GatherCompleteHandshake(sslSocket *ss, int flags);
1274 * When talking to export clients or using export cipher suites, servers
1275 * with public RSA keys larger than 512 bits need to use a 512-bit public
1276 * key, signed by the larger key. The smaller key is a "step down" key.
1277 * Generate that key pair and keep it around.
1279 extern SECStatus ssl3_CreateRSAStepDownKeys(sslSocket *ss);
1281 #ifdef NSS_ENABLE_ECC
1282 extern void ssl3_FilterECCipherSuitesByServerCerts(sslSocket *ss);
1283 extern PRBool ssl3_IsECCEnabled(sslSocket *ss);
1284 extern SECStatus ssl3_DisableECCSuites(sslSocket * ss,
1285 const ssl3CipherSuite * suite);
1286 #endif /* NSS_ENABLE_ECC */
1288 extern SECStatus ssl3_CipherPrefSetDefault(ssl3CipherSuite which, PRBool on);
1289 extern SECStatus ssl3_CipherPrefGetDefault(ssl3CipherSuite which, PRBool *on);
1290 extern SECStatus ssl2_CipherPrefSetDefault(PRInt32 which, PRBool enabled);
1291 extern SECStatus ssl2_CipherPrefGetDefault(PRInt32 which, PRBool *enabled);
1293 extern SECStatus ssl3_CipherPrefSet(sslSocket *ss, ssl3CipherSuite which, PRBool on);
1294 extern SECStatus ssl3_CipherPrefGet(sslSocket *ss, ssl3CipherSuite which, PRBool *on);
1295 extern SECStatus ssl2_CipherPrefSet(sslSocket *ss, PRInt32 which, PRBool enabled);
1296 extern SECStatus ssl2_CipherPrefGet(sslSocket *ss, PRInt32 which, PRBool *enabled);
1298 extern SECStatus ssl3_SetPolicy(ssl3CipherSuite which, PRInt32 policy);
1299 extern SECStatus ssl3_GetPolicy(ssl3CipherSuite which, PRInt32 *policy);
1300 extern SECStatus ssl2_SetPolicy(PRInt32 which, PRInt32 policy);
1301 extern SECStatus ssl2_GetPolicy(PRInt32 which, PRInt32 *policy);
1303 extern void ssl2_InitSocketPolicy(sslSocket *ss);
1304 extern void ssl3_InitSocketPolicy(sslSocket *ss);
1306 extern SECStatus ssl3_ConstructV2CipherSpecsHack(sslSocket *ss,
1307 unsigned char *cs, int *size);
1309 extern SECStatus ssl3_RedoHandshake(sslSocket *ss, PRBool flushCache);
1311 extern void ssl3_DestroySSL3Info(sslSocket *ss);
1313 extern SECStatus ssl3_NegotiateVersion(sslSocket *ss,
1314 SSL3ProtocolVersion peerVersion);
1316 extern SECStatus ssl_GetPeerInfo(sslSocket *ss);
1318 #ifdef NSS_ENABLE_ECC
1319 /* ECDH functions */
1320 extern SECStatus ssl3_SendECDHClientKeyExchange(sslSocket * ss,
1321 SECKEYPublicKey * svrPubKey);
1322 extern SECStatus ssl3_HandleECDHServerKeyExchange(sslSocket *ss,
1323 SSL3Opaque *b, PRUint32 length);
1324 extern SECStatus ssl3_HandleECDHClientKeyExchange(sslSocket *ss,
1325 SSL3Opaque *b, PRUint32 length,
1326 SECKEYPublicKey *srvrPubKey,
1327 SECKEYPrivateKey *srvrPrivKey);
1328 extern SECStatus ssl3_SendECDHServerKeyExchange(sslSocket *ss);
1329 #endif
1331 extern SECStatus ssl3_ComputeCommonKeyHash(PRUint8 * hashBuf,
1332 unsigned int bufLen, SSL3Hashes *hashes,
1333 PRBool bypassPKCS11);
1334 extern SECStatus ssl3_InitPendingCipherSpec(sslSocket *ss, PK11SymKey *pms);
1335 extern SECStatus ssl3_AppendHandshake(sslSocket *ss, const void *void_src,
1336 PRInt32 bytes);
1337 extern SECStatus ssl3_AppendHandshakeHeader(sslSocket *ss,
1338 SSL3HandshakeType t, PRUint32 length);
1339 extern SECStatus ssl3_AppendHandshakeNumber(sslSocket *ss, PRInt32 num,
1340 PRInt32 lenSize);
1341 extern SECStatus ssl3_AppendHandshakeVariable( sslSocket *ss,
1342 const SSL3Opaque *src, PRInt32 bytes, PRInt32 lenSize);
1343 extern SECStatus ssl3_ConsumeHandshake(sslSocket *ss, void *v, PRInt32 bytes,
1344 SSL3Opaque **b, PRUint32 *length);
1345 extern PRInt32 ssl3_ConsumeHandshakeNumber(sslSocket *ss, PRInt32 bytes,
1346 SSL3Opaque **b, PRUint32 *length);
1347 extern SECStatus ssl3_ConsumeHandshakeVariable(sslSocket *ss, SECItem *i,
1348 PRInt32 bytes, SSL3Opaque **b, PRUint32 *length);
1349 extern SECStatus ssl3_SignHashes(SSL3Hashes *hash, SECKEYPrivateKey *key,
1350 SECItem *buf, PRBool isTLS);
1351 extern SECStatus ssl3_VerifySignedHashes(SSL3Hashes *hash,
1352 CERTCertificate *cert, SECItem *buf, PRBool isTLS,
1353 void *pwArg);
1355 /* functions that append extensions to hello messages. */
1356 extern PRInt32 ssl3_SendServerNameIndicationExtension( sslSocket * ss,
1357 PRBool append, PRUint32 maxBytes);
1359 /* call the registered extension handlers. */
1360 extern SECStatus ssl3_HandleClientHelloExtensions(sslSocket *ss,
1361 SSL3Opaque **b, PRUint32 *length);
1363 /* Construct a new NSPR socket for the app to use */
1364 extern PRFileDesc *ssl_NewPRSocket(sslSocket *ss, PRFileDesc *fd);
1365 extern void ssl_FreePRSocket(PRFileDesc *fd);
1367 /* Internal config function so SSL2 can initialize the present state of
1368 * various ciphers */
1369 extern int ssl3_config_match_init(sslSocket *);
1372 /* Create a new ref counted key pair object from two keys. */
1373 extern ssl3KeyPair * ssl3_NewKeyPair( SECKEYPrivateKey * privKey,
1374 SECKEYPublicKey * pubKey);
1376 /* get a new reference (bump ref count) to an ssl3KeyPair. */
1377 extern ssl3KeyPair * ssl3_GetKeyPairRef(ssl3KeyPair * keyPair);
1379 /* Decrement keypair's ref count and free if zero. */
1380 extern void ssl3_FreeKeyPair(ssl3KeyPair * keyPair);
1382 /* calls for accessing wrapping keys across processes. */
1383 extern PRBool
1384 ssl_GetWrappingKey( PRInt32 symWrapMechIndex,
1385 SSL3KEAType exchKeyType,
1386 SSLWrappedSymWrappingKey *wswk);
1388 /* The caller passes in the new value it wants
1389 * to set. This code tests the wrapped sym key entry in the file on disk.
1390 * If it is uninitialized, this function writes the caller's value into
1391 * the disk entry, and returns false.
1392 * Otherwise, it overwrites the caller's wswk with the value obtained from
1393 * the disk, and returns PR_TRUE.
1394 * This is all done while holding the locks/semaphores necessary to make
1395 * the operation atomic.
1397 extern PRBool
1398 ssl_SetWrappingKey(SSLWrappedSymWrappingKey *wswk);
1400 /* get rid of the symmetric wrapping key references. */
1401 extern SECStatus SSL3_ShutdownServerCache(void);
1403 extern void ssl_InitClientSessionCacheLock(void);
1405 extern void ssl_InitSymWrapKeysLock(void);
1407 /********************** misc calls *********************/
1409 extern int ssl_MapLowLevelError(int hiLevelError);
1411 extern PRUint32 ssl_Time(void);
1414 SECStatus SSL_DisableDefaultExportCipherSuites(void);
1415 SECStatus SSL_DisableExportCipherSuites(PRFileDesc * fd);
1416 PRBool SSL_IsExportCipherSuite(PRUint16 cipherSuite);
1419 #ifdef TRACE
1420 #define SSL_TRACE(msg) ssl_Trace msg
1421 #else
1422 #define SSL_TRACE(msg)
1423 #endif
1425 void ssl_Trace(const char *format, ...);
1427 SEC_END_PROTOS
1429 #ifdef XP_OS2_VACPP
1430 #include <process.h>
1431 #endif
1433 #if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
1434 #define SSL_GETPID getpid
1435 #elif defined(_WIN32_WCE)
1436 #define SSL_GETPID GetCurrentProcessId
1437 #elif defined(WIN32)
1438 extern int __cdecl _getpid(void);
1439 #define SSL_GETPID _getpid
1440 #else
1441 #define SSL_GETPID() 0
1442 #endif
1444 #endif /* __sslimpl_h_ */