sslimpl.h

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C头文件 代码 · 共 1,251 行 · 第 1/3 页

H
1,251
字号
/* * This file is PRIVATE to SSL and should be the first thing included by * any SSL implementation file. * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. * * $Id: sslimpl.h,v 1.4 2000/09/12 20:15:43 jgmyers%netscape.com Exp $ */#ifndef __sslimpl_h_#define __sslimpl_h_#ifdef DEBUG#undef NDEBUG#else#undef NDEBUG#define NDEBUG#endif#include "secport.h"#include "secerr.h"#include "sslerr.h"#include "ssl3prot.h"#include "hasht.h"#include "prlock.h"#include "pkcs11t.h"#ifdef XP_UNIX#include "unistd.h"#endif#include "nssrwlk.h"#if defined(DEBUG) || defined(TRACE)#ifdef __cplusplus#define Debug 1#elseextern int Debug;#endif#else#undef Debug#endif#if defined(DEBUG)#define TRACE#endif#ifdef TRACE#define SSL_TRC(a,b) if (ssl_trace >= (a)) ssl_Trace b#define PRINT_BUF(a,b) if (ssl_trace >= (a)) ssl_PrintBuf b#define DUMP_MSG(a,b) if (ssl_trace >= (a)) ssl_DumpMsg b#else#define SSL_TRC(a,b)#define PRINT_BUF(a,b)#define DUMP_MSG(a,b)#endif#ifdef DEBUG#define SSL_DBG(b) if (ssl_debug) ssl_Trace b#else#define SSL_DBG(b)#endif#if defined (DEBUG)#ifdef macintosh#include "pprthred.h"#else#include "private/pprthred.h"	/* for PR_InMonitor() */#endif#define ssl_InMonitor(m) PR_InMonitor(m)#endif#define LSB(x) ((unsigned char) (x & 0xff))#define MSB(x) ((unsigned char) (((unsigned)(x)) >> 8))/************************************************************************/typedef enum { SSLAppOpRead = 0,	       SSLAppOpWrite,	       SSLAppOpRDWR,	       SSLAppOpPost,	       SSLAppOpHeader} SSLAppOperation;#define SSL_MIN_MASTER_KEY_BYTES	5#define SSL_MAX_MASTER_KEY_BYTES	64#define SSL_SESSIONID_BYTES		16#define SSL3_SESSIONID_BYTES		32#define SSL_MIN_CHALLENGE_BYTES		16#define SSL_MAX_CHALLENGE_BYTES		32#define SSL_CHALLENGE_BYTES		16#define SSL_CONNECTIONID_BYTES		16#define SSL_MIN_CYPHER_ARG_BYTES	0#define SSL_MAX_CYPHER_ARG_BYTES	32#define SSL_MAX_MAC_BYTES		16/* number of wrap mechanisms potentially used to wrap master secrets. */#define SSL_NUM_WRAP_MECHS              13/* This makes the cert cache entry exactly 4k. */#define SSL_MAX_CACHED_CERT_LEN		4060typedef struct sslBufferStr             sslBuffer;typedef struct sslConnectInfoStr        sslConnectInfo;typedef struct sslGatherStr             sslGather;typedef struct sslSecurityInfoStr       sslSecurityInfo;typedef struct sslSessionIDStr          sslSessionID;typedef struct sslSocketStr             sslSocket;typedef struct sslSocketOpsStr          sslSocketOps;typedef struct sslSocksInfoStr          sslSocksInfo;typedef struct ssl3StateStr             ssl3State;typedef struct ssl3CertNodeStr          ssl3CertNode;typedef struct ssl3BulkCipherDefStr     ssl3BulkCipherDef;typedef struct ssl3MACDefStr            ssl3MACDef;typedef struct ssl3KeyPairStr		ssl3KeyPair;struct ssl3CertNodeStr {    struct ssl3CertNodeStr *next;    CERTCertificate *       cert;};typedef SECStatus (*sslHandshakeFunc)(sslSocket *ss);/* This type points to the low layer send func, ** e.g. ssl2_SendStream or ssl3_SendPlainText.** These functions return the same values as PR_Send, ** i.e.  >= 0 means number of bytes sent, < 0 means error.*/typedef PRInt32       (*sslSendFunc)(sslSocket *ss, const unsigned char *buf,			             PRInt32 n, PRInt32 flags);typedef void          (*sslSessionIDCacheFunc)  (sslSessionID *sid);typedef void          (*sslSessionIDUncacheFunc)(sslSessionID *sid);typedef sslSessionID *(*sslSessionIDLookupFunc)(const PRIPv6Addr    *addr,						unsigned char* sid,						unsigned int   sidLen,                                                CERTCertDBHandle * dbHandle);/* Socket ops */struct sslSocketOpsStr {    int         (*connect) (sslSocket *, const PRNetAddr *);    PRFileDesc *(*accept)  (sslSocket *, PRNetAddr *);    int         (*bind)    (sslSocket *, const PRNetAddr *);    int         (*listen)  (sslSocket *, int);    int         (*shutdown)(sslSocket *, int);    int         (*close)   (sslSocket *);    int         (*recv)    (sslSocket *, unsigned char *, int, int);    /* points to the higher-layer send func, e.g. ssl_SecureSend. */    int         (*send)    (sslSocket *, const unsigned char *, int, int);    int         (*read)    (sslSocket *, unsigned char *, int);    int         (*write)   (sslSocket *, const unsigned char *, int);    int         (*getpeername)(sslSocket *, PRNetAddr *);    int         (*getsockname)(sslSocket *, PRNetAddr *);};/* Flags interpreted by ssl send functions. */#define ssl_SEND_FLAG_FORCE_INTO_BUFFER	0x40000000#define ssl_SEND_FLAG_NO_BUFFER		0x20000000#define ssl_SEND_FLAG_MASK		0x7f000000/*** A buffer object.*/struct sslBufferStr {    unsigned char *	buf;    unsigned int 	len;    unsigned int 	space;};/*** SSL3 cipher suite policy and preference struct.*/typedef struct {#ifdef AIX    unsigned int    cipher_suite : 16;    unsigned int    policy       :  8;    unsigned int    enabled      :  1;    unsigned int    isPresent    :  1;#else    ssl3CipherSuite cipher_suite;    PRUint8         policy;    unsigned char   enabled   : 1;    unsigned char   isPresent : 1;#endif} ssl3CipherSuiteCfg;#define ssl_V3_SUITES_IMPLEMENTED 13typedef struct sslOptionsStr {    unsigned int useSecurity		: 1;  /*  1 */    unsigned int useSocks		: 1;  /*  2 */    unsigned int requestCertificate	: 1;  /*  3 */    unsigned int requireCertificate	: 2;  /*  4-5 */    unsigned int handshakeAsClient	: 1;  /*  6 */    unsigned int handshakeAsServer	: 1;  /*  7 */    unsigned int enableSSL2		: 1;  /*  8 */    unsigned int enableSSL3		: 1;  /*  9 */    unsigned int enableTLS		: 1;  /* 10 */    unsigned int noCache		: 1;  /* 11 */    unsigned int fdx			: 1;  /* 12 */    unsigned int v2CompatibleHello	: 1;  /* 13 */    unsigned int detectRollBack  	: 1;  /* 14 */} sslOptions;/*** SSL Socket struct**** Protection:  XXX*/struct sslSocketStr {    PRFileDesc *	fd;    /* Pointer to operations vector for this socket */    sslSocketOps *	ops;    /* State flags */    unsigned int     useSocks		: 1;    unsigned int     useSecurity	: 1;    unsigned int     requestCertificate	: 1;    unsigned int     requireCertificate	: 2;    unsigned int     handshakeAsClient	: 1;    unsigned int     handshakeAsServer	: 1;    unsigned int     enableSSL2		: 1;    unsigned int     enableSSL3		: 1;    unsigned int     enableTLS		: 1;    unsigned int     clientAuthRequested: 1;    unsigned int     noCache		: 1;    unsigned int     fdx		: 1; /* simultaneous read/write threads */    unsigned int     v2CompatibleHello	: 1; /* Send v3+ client hello in v2 format */    unsigned int     detectRollBack   	: 1; /* Detect rollback to SSL v3 */    unsigned int     connected		: 1; /* initial handshake is complete. */    unsigned int     recvdCloseNotify	: 1; /* received SSL EOF. */    /* version of the protocol to use */    SSL3ProtocolVersion version;    SSL3ProtocolVersion clientHelloVersion; /* version sent in client hello. */    /* Non-zero if socks is enabled */    sslSocksInfo *   socks;    /* Non-zero if security is enabled */    sslSecurityInfo *sec;    /* protected by firstHandshakeLock AND (in ssl3) ssl3HandshakeLock. */    const char      *url;				/* ssl 2 & 3 */    /* Gather object used for gathering data */    sslGather *      gather;				/*recvBufLock*/    sslHandshakeFunc handshake;				/*firstHandshakeLock*/    sslHandshakeFunc nextHandshake;			/*firstHandshakeLock*/    sslHandshakeFunc securityHandshake;			/*firstHandshakeLock*/    sslBuffer        saveBuf;				/*xmitBufLock*/    sslBuffer        pendingBuf;			/*xmitBufLock*/    /* the following 3 variables are only used with socks or other proxies. */    PRIPv6Addr       peer;	/* Target server IP address */    int              port;	/* Target server port number. */    char *           peerID;	/* String uniquely identifies target server. */    /* End of socks variables. */    ssl3State *      ssl3;    unsigned char *  cipherSpecs;    unsigned int     sizeCipherSpecs;const unsigned char *  preferredCipher;    /* Configuration state for server sockets */    CERTCertificate *     serverCert[kt_kea_size];    CERTCertificateList * serverCertChain[kt_kea_size];    SECKEYPrivateKey *    serverKey[kt_kea_size];    ssl3KeyPair *         stepDownKeyPair;	/* RSA step down keys */    /* Callbacks */    SSLAuthCertificate        authCertificate;    void                     *authCertificateArg;    SSLGetClientAuthData      getClientAuthData;    void                     *getClientAuthDataArg;    SSLBadCertHandler         handleBadCert;    void                     *badCertArg;    SSLHandshakeCallback      handshakeCallback;    void                     *handshakeCallbackData;    void                     *pkcs11PinArg;    PRIntervalTime            rTimeout; /* timeout for NSPR I/O */    PRIntervalTime            wTimeout; /* timeout for NSPR I/O */    PRIntervalTime            cTimeout; /* timeout for NSPR I/O */    PRLock *      recvLock;	/* lock against multiple reader threads. */    PRLock *      sendLock;	/* lock against multiple sender threads. */    PRMonitor *   recvBufLock;	/* locks low level recv buffers. */    PRMonitor *   xmitBufLock;	/* locks low level xmit buffers. */    /* Only one thread may operate on the socket until the initial handshake    ** is complete.  This Monitor ensures that.  Since SSL2 handshake is    ** only done once, this is also effectively the SSL2 handshake lock.    */    PRMonitor *   firstHandshakeLock;     /* This monitor protects the ssl3 handshake state machine data.    ** Only one thread (reader or writer) may be in the ssl3 handshake state    ** machine at any time.  */    PRMonitor *   ssl3HandshakeLock;    /* reader/writer lock, protects the secret data needed to encrypt and MAC    ** outgoing records, and to decrypt and MAC check incoming ciphertext     ** records.  */    NSSRWLock *   specLock;    /* handle to perm cert db (and implicitly to the temp cert db) used     ** with this socket.     */    CERTCertDBHandle * dbHandle;    PRUint16	shutdownHow; 	/* See ssl_SHUTDOWN defines below. */    PRUint16	allowedByPolicy;          /* copy of global policy bits. */    PRUint16	maybeAllowedByPolicy;     /* copy of global policy bits. */    PRUint16	chosenPreference;         /* SSL2 cipher preferences. */    ssl3CipherSuiteCfg cipherSuites[ssl_V3_SUITES_IMPLEMENTED];};#define SSL_LOCK_RANK_SPEC 	255#define SSL_LOCK_RANK_GLOBAL 	NSS_RWLOCK_RANK_NONE/* These are the valid values for shutdownHow. ** These values are each 1 greater than the NSPR values, and the code** depends on that relation to efficiently convert PR_SHUTDOWN values ** into ssl_SHUTDOWN values.  These values use one bit for read, and ** another bit for write, and can be used as bitmasks.*/#define ssl_SHUTDOWN_NONE	0	/* NOT shutdown at all */#define ssl_SHUTDOWN_RCV	1	/* PR_SHUTDOWN_RCV  +1 */#define ssl_SHUTDOWN_SEND	2	/* PR_SHUTDOWN_SEND +1 */#define ssl_SHUTDOWN_BOTH	3	/* PR_SHUTDOWN_BOTH +1 *//*** A gather object. Used to read some data until a count has been** satisfied. Primarily for support of async sockets.** Everything in here is protected by the recvBufLock.*/struct sslGatherStr {    int           state;	/* see GS_ values below. */     /* ssl 2 & 3 */    /* "buf" holds received plaintext SSL records, after decrypt and MAC check.     * SSL2: recv'd ciphertext records are put here, then decrypted in place.     * SSL3: recv'd ciphertext records are put in inbuf (see below), then      *       decrypted into buf.     */    sslBuffer     buf;				/*recvBufLock*/	/* ssl 2 & 3 */    /* number of bytes previously read into hdr or buf(ssl2) or inbuf (ssl3).     ** (offset - writeOffset) is the number of ciphertext bytes read in but     **     not yet deciphered.    */    unsigned int  offset;                                       /* ssl 2 & 3 */    /* number of bytes to read in next call to ssl_DefRecv (recv) */    unsigned int  remainder;                                    /* ssl 2 & 3 */    /* Number of ciphertext bytes to read in after 2-byte SSL record header. */    unsigned int  count;					/* ssl2 only */    /* size of the final plaintext record.     ** == count - (recordPadding + MAC size)    */    unsigned int  recordLen;					/* ssl2 only */    /* number of bytes of padding to be removed after decrypting. */    /* This value is taken from the record's hdr[2], which means a too large     * value could crash us.     */    unsigned int  recordPadding;				/* ssl2 only */    /* plaintext DATA begins this many bytes into "buf".  */    unsigned int  recordOffset;					/* ssl2 only */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?