selfserv.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,329 行 · 第 1/3 页
C
1,329 行
/* * 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. *//* -r flag is interepreted as follows: * 1 -r means request, not require, on initial handshake. * 2 -r's mean request and require, on initial handshake. * 3 -r's mean request, not require, on second handshake. * 4 -r's mean request and require, on second handshake. */#include <stdio.h>#include <string.h>#include "secutil.h"#if defined(XP_UNIX)#include <unistd.h>#endif#include <stdlib.h>#include <errno.h>#include <fcntl.h>#include <stdarg.h>#include "nspr.h"#include "prio.h"#include "prerror.h"#include "prnetdb.h"#include "plgetopt.h"#include "pk11func.h"#include "secitem.h"#include "nss.h"#include "ssl.h"#include "sslproto.h"#ifndef PORT_Sprintf#define PORT_Sprintf sprintf#endif#ifndef PORT_Strstr#define PORT_Strstr strstr#endif#ifndef PORT_Malloc#define PORT_Malloc PR_Malloc#endifint cipherSuites[] = { SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA, SSL_FORTEZZA_DMS_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_DES_CBC_SHA, SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5, SSL_FORTEZZA_DMS_WITH_NULL_SHA, SSL_RSA_WITH_NULL_MD5, 0};int ssl2CipherSuites[] = { SSL_EN_RC4_128_WITH_MD5, /* A */ SSL_EN_RC4_128_EXPORT40_WITH_MD5, /* B */ SSL_EN_RC2_128_CBC_WITH_MD5, /* C */ SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5, /* D */ SSL_EN_DES_64_CBC_WITH_MD5, /* E */ SSL_EN_DES_192_EDE3_CBC_WITH_MD5, /* F */ 0};int ssl3CipherSuites[] = { SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA, /* a */ SSL_FORTEZZA_DMS_WITH_RC4_128_SHA, /* b */ SSL_RSA_WITH_RC4_128_MD5, /* c */ SSL_RSA_WITH_3DES_EDE_CBC_SHA, /* d */ SSL_RSA_WITH_DES_CBC_SHA, /* e */ SSL_RSA_EXPORT_WITH_RC4_40_MD5, /* f */ SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5, /* g */ SSL_FORTEZZA_DMS_WITH_NULL_SHA, /* h */ SSL_RSA_WITH_NULL_MD5, /* i */ SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA, /* j */ SSL_RSA_FIPS_WITH_DES_CBC_SHA, /* k */ TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA, /* l */ TLS_RSA_EXPORT1024_WITH_RC4_56_SHA, /* m */ 0};int requestCert;int stopping;int verbose;SECItem bigBuf;/* Add custom password handler because SECU_GetModulePassword * makes automation of this program next to impossible. */char *ownPasswd(PK11SlotInfo *info, PRBool retry, void *arg){ char * passwd = NULL; if ( (!retry) && arg ) { passwd = PL_strdup((char *)arg); } return passwd;}#define PRINTF if (verbose) printf#define FPRINTF if (verbose) fprintf#define FLUSH if (verbose) { fflush(stdout); fflush(stderr); }static voidUsage(const char *progName){ fprintf(stderr, "Usage: %s -n rsa_nickname -p port [-3RTmrvx] [-w password]\n"" [-i pid_file] [-c ciphers] [-d dbdir] [-f fortezza_nickname] \n""-3 means disable SSL v3\n""-T means disable TLS\n""-R means disable detection of rollback from TLS to SSL3\n""-m means test the model-socket feature of SSL_ImportFD.\n""-r flag is interepreted as follows:\n"" 1 -r means request, not require, cert on initial handshake.\n"" 2 -r's mean request and require, cert on initial handshake.\n"" 3 -r's mean request, not require, cert on second handshake.\n"" 4 -r's mean request and require, cert on second handshake.\n""-v means verbose output\n""-x means use export policy.\n""-i pid_file file to write the process id of selfserve\n""-c ciphers Letter(s) chosen from the following list\n""A SSL2 RC4 128 WITH MD5\n""B SSL2 RC4 128 EXPORT40 WITH MD5\n""C SSL2 RC2 128 CBC WITH MD5\n""D SSL2 RC2 128 CBC EXPORT40 WITH MD5\n""E SSL2 DES 64 CBC WITH MD5\n""F SSL2 DES 192 EDE3 CBC WITH MD5\n""\n""a SSL3 FORTEZZA DMS WITH FORTEZZA CBC SHA\n""b SSL3 FORTEZZA DMS WITH RC4 128 SHA\n""c SSL3 RSA WITH RC4 128 MD5\n""d SSL3 RSA WITH 3DES EDE CBC SHA\n""e SSL3 RSA WITH DES CBC SHA\n""f SSL3 RSA EXPORT WITH RC4 40 MD5\n""g SSL3 RSA EXPORT WITH RC2 CBC 40 MD5\n""h SSL3 FORTEZZA DMS WITH NULL SHA\n""i SSL3 RSA WITH NULL MD5\n""j SSL3 RSA FIPS WITH 3DES EDE CBC SHA\n""k SSL3 RSA FIPS WITH DES CBC SHA\n""l SSL3 RSA EXPORT WITH DES CBC SHA\t(new)\n""m SSL3 RSA EXPORT WITH RC4 56 SHA\t(new)\n", progName); exit(1);}static voidnetworkStart(void){#if defined(XP_WIN) && !defined(NSPR20) WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(1, 1); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we couldn't find a useable winsock.dll. */ fputs("WSAStartup failed!\n", stderr); exit(1); }/* Confirm that the Windows Sockets DLL supports 1.1.*/ /* Note that if the DLL supports versions greater */ /* than 1.1 in addition to 1.1, it will still return */ /* 1.1 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { /* Tell the user that we couldn't find a useable winsock.dll. */ fputs("wrong winsock version\n", stderr); WSACleanup(); exit(1); } /* The Windows Sockets DLL is acceptable. Proceed. */ #endif}static voidnetworkEnd(void){#if defined(XP_WIN) && !defined(NSPR20) WSACleanup();#endif}static const char *errWarn(char * funcString){ PRErrorCode perr = PR_GetError(); const char * errString = SECU_Strerror(perr); fprintf(stderr, "selfserv: %s returned error %d:\n%s\n", funcString, perr, errString); return errString;}static voiderrExit(char * funcString){#if defined (XP_WIN) && !defined(NSPR20) int err; LPVOID lpMsgBuf; err = WSAGetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); /* Display the string. */ /*MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION ); */ fprintf(stderr, "%s\n", lpMsgBuf); /* Free the buffer. */ LocalFree( lpMsgBuf );#endif errWarn(funcString); exit(1);}voiddisableSSL2Ciphers(void){ int i; /* disable all the SSL2 cipher suites */ for (i = 0; ssl2CipherSuites[i] != 0; ++i) { SSL_EnableCipher(ssl2CipherSuites[i], SSL_NOT_ALLOWED); }}voiddisableSSL3Ciphers(void){ int i; /* disable all the SSL3 cipher suites */ for (i = 0; ssl3CipherSuites[i] != 0; ++i) { SSL_EnableCipher(ssl3CipherSuites[i], SSL_NOT_ALLOWED); }}static intmySSLAuthCertificate(void *arg, PRFileDesc *fd, PRBool checkSig, PRBool isServer){ SECStatus rv; CERTCertificate * peerCert; peerCert = SSL_PeerCertificate(fd); PRINTF("selfserv: Subject: %s\nselfserv: Issuer : %s\n", peerCert->subjectName, peerCert->issuerName); rv = SSL_AuthCertificate(arg, fd, checkSig, isServer); if (rv == SECSuccess) { fputs("selfserv: -- SSL3: Certificate Validated.\n", stderr); } else { int err = PR_GetError(); FPRINTF(stderr, "selfserv: -- SSL3: Certificate Invalid, err %d.\n%s\n", err, SECU_Strerror(err)); } FLUSH; return rv; }void printSecurityInfo(PRFileDesc *fd){ char * cp; /* bulk cipher name */ char * ip; /* cert issuer DN */ char * sp; /* cert subject DN */ int op; /* High, Low, Off */ int kp0; /* total key bits */ int kp1; /* secret key bits */ int result;/* statistics from ssl3_SendClientHello (sch) */extern long ssl3_sch_sid_cache_hits;extern long ssl3_sch_sid_cache_misses;extern long ssl3_sch_sid_cache_not_ok;/* statistics from ssl3_HandleServerHello (hsh) */extern long ssl3_hsh_sid_cache_hits;extern long ssl3_hsh_sid_cache_misses;extern long ssl3_hsh_sid_cache_not_ok;/* statistics from ssl3_HandleClientHello (hch) */extern long ssl3_hch_sid_cache_hits;extern long ssl3_hch_sid_cache_misses;extern long ssl3_hch_sid_cache_not_ok; PRINTF("selfserv: %ld cache hits; %ld cache misses, %ld cache not reusable\n", ssl3_hch_sid_cache_hits, ssl3_hch_sid_cache_misses, ssl3_hch_sid_cache_not_ok); result = SSL_SecurityStatus(fd, &op, &cp, &kp0, &kp1, &ip, &sp); if (result == SECSuccess) { PRINTF( "selfserv: bulk cipher %s, %d secret key bits, %d key bits, status: %d\n", cp, kp1, kp0, op); if (requestCert) { PRINTF("selfserv: subject DN: %s\n" "selfserv: issuer DN: %s\n", sp, ip); } PR_Free(cp); PR_Free(ip); PR_Free(sp); } FLUSH;}/**************************************************************************** Begin thread management routines and data.**************************************************************************/#define MAX_THREADS 32typedef int startFn(PRFileDesc *a, PRFileDesc *b, int c);PRLock * threadLock;PRCondVar * threadStartQ;PRCondVar * threadEndQ;int numUsed;int numRunning;typedef enum { rs_idle = 0, rs_running = 1, rs_zombie = 2 } runState;typedef struct perThreadStr { PRFileDesc *a; PRFileDesc *b; int c; int rv; startFn * startFunc; PRThread * prThread; PRBool inUse; runState running;} perThread;perThread threads[MAX_THREADS];voidthread_wrapper(void * arg){ perThread * slot = (perThread *)arg; /* wait for parent to finish launching us before proceeding. */ PR_Lock(threadLock); PR_Unlock(threadLock); slot->rv = (* slot->startFunc)(slot->a, slot->b, slot->c); PR_Lock(threadLock); slot->running = rs_zombie; /* notify the thread exit handler. */ PR_NotifyCondVar(threadEndQ); PR_Unlock(threadLock);}SECStatuslaunch_thread( startFn *startFunc, PRFileDesc *a, PRFileDesc *b, int c){ perThread * slot; int i; if (!threadStartQ) { threadLock = PR_NewLock(); threadStartQ = PR_NewCondVar(threadLock); threadEndQ = PR_NewCondVar(threadLock); } PR_Lock(threadLock); while (numRunning >= MAX_THREADS) { PR_WaitCondVar(threadStartQ, PR_INTERVAL_NO_TIMEOUT); } for (i = 0; i < numUsed; ++i) { slot = threads + i; if (slot->running == rs_idle) break; } if (i >= numUsed) { if (i >= MAX_THREADS) { /* something's really wrong here. */ PORT_Assert(i < MAX_THREADS); PR_Unlock(threadLock); return SECFailure;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?