main.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 659 行 · 第 1/2 页

C
659
字号
        SSMTextGen_DestroyContext(cx);    }    PK11_ConfigurePKCS11(manufacturerID, libraryDescription, tokenDescription,                         privateTokenDescription, slotDescription,                          privateSlotDescription, fipsSlotDescription,                          fipsPrivateSlotDescription, 0, 0);    return SECSuccess; loser:    PR_FREEIF(manufacturerID);    PR_FREEIF(libraryDescription);    PR_FREEIF(tokenDescription);    PR_FREEIF(privateTokenDescription);    PR_FREEIF(slotDescription);    PR_FREEIF(privateSlotDescription);    if (cx != NULL) {        SSMTextGen_DestroyContext(cx);    }    return SECFailure;}#ifdef XP_UNIX#define CATCH_SIGNAL_DEFAULT(SIGNAL) \    if (((int)signal(SIGNAL, psm_signal_handler_default)) == ((int)SIG_ERR)) \        goto loser;#define CATCH_SIGNAL_IGNORE(SIGNAL) \    if (((int)signal(SIGNAL, psm_signal_handler_ignore)) == ((int)SIG_ERR)) \        goto loser;static voidpsm_signal_handler_default(int sig){#ifdef DEBUG  printf ("Trapping the signal %d\n", sig);#endif  SSM_ReleaseLockFile();  kill(getpid(),SIGKILL);}static voidpsm_signal_handler_ignore(int sig){#ifdef DEBUG  printf ("Ignoring the signal %d\n", sig);#endif  signal(sig,psm_signal_handler_ignore);}static SSMStatuspsm_catch_signals(void){  CATCH_SIGNAL_IGNORE(SIGHUP);  CATCH_SIGNAL_DEFAULT(SIGINT);  CATCH_SIGNAL_DEFAULT(SIGQUIT);  CATCH_SIGNAL_DEFAULT(SIGILL);  CATCH_SIGNAL_DEFAULT(SIGTRAP);  CATCH_SIGNAL_DEFAULT(SIGABRT);  CATCH_SIGNAL_DEFAULT(SIGIOT);#ifdef SIGEMT  CATCH_SIGNAL_DEFAULT(SIGEMT);#endif  CATCH_SIGNAL_DEFAULT(SIGFPE);  CATCH_SIGNAL_DEFAULT(SIGBUS);  CATCH_SIGNAL_DEFAULT(SIGSEGV);#ifdef SIGSYS  CATCH_SIGNAL_DEFAULT(SIGSYS);#endif  CATCH_SIGNAL_IGNORE(SIGPIPE);  CATCH_SIGNAL_DEFAULT(SIGTERM);#ifndef LINUX  CATCH_SIGNAL_DEFAULT(SIGUSR1);#endif  CATCH_SIGNAL_DEFAULT(SIGUSR2);#ifdef SIGXCPU  CATCH_SIGNAL_DEFAULT(SIGXCPU);#endif#ifdef SIGDANGER  CATCH_SIGNAL_DEFAULT(SIGDANGER);#endif  return SSM_SUCCESS; loser:  return SSM_FAILURE;}#endif#if defined(XP_PC) && !defined(DEBUG)int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,                   LPSTR lpszLine, int nShow)#elif defined(XP_MAC)/*	We run RunMacPSM in a separate thread off the main thread. 	This is because we can't do any blocking I/O routines on the main	thread, because Mac NSPR doesn't own the original thread used to	run the app.*/static void* glue_component = NULL;void RunMacPSM(void *arg)#elseint main(int argc, char ** argv)#endif{#if (defined(XP_PC) && !defined(DEBUG)) || (defined(XP_MAC))    /* substitute argc and argv for NSPR */    int argc = 0;    char *argv[] = {"", NULL};#endif    PRIntn result = 0;#ifdef XP_MAC    glue_component = arg;#endif#ifdef DEBUG    PR_STDIO_INIT();#endif#ifdef DEBUG    /* Initialize logging. */    SSM_InitLogging();#endif#ifdef TIMEBOMB    SSM_CheckTimebomb();#endif#ifdef XP_UNIX    if (psm_catch_signals() != SSM_SUCCESS) {      SSM_DEBUG("Couldn't set signal handlers.  Quitting\n");      exit(1);    }#endif    SSM_SetPolicy();    enable_SMIME_cipher_prefs();    if (SSM_GetPolicy() == ssmDomestic) {      SSM_EnableHighGradeKeyGen();    }	/* Initialize NLS layer */	if (nlsInit() != PR_TRUE) {		SSM_DEBUG("Failed to initialize the NLS layer\n");		exit(1);	}    /* Initialize global list of control connections. */    connections = SSM_NewCollection();    if (connections == NULL)    {        SSM_DEBUG("Can't initialize! (%ld)\n", (long) result);        exit(1);    }    /* Initialize global list of tokens */    result = SSM_HashCreate(&tokenList);    if (result != PR_SUCCESS || !tokenList) {       SSM_DEBUG("Can't initialize - tokenList \n");       exit(result);    }    tokenLock = PR_NewMonitor();    if (!tokenLock) {        SSM_DEBUG("Can't initialize - tokenLock\n");	exit(1);    }    /* Initialize hash table of control connections */    result = SSM_HashCreate(&ctrlConnections);    if (result != PR_SUCCESS || !ctrlConnections) {        SSM_DEBUG("Can't initialize global table for control connections \n");        exit(result);    }    /* Initialize resource table */    SSM_ResourceInit();    if (SSM_InitPolicyHandler() != PR_SUCCESS) {        SSM_DEBUG("Couldn't initialize the Policy Handler.\n");        exit (1);    }    /* initialize random number generator */    SSM_DEBUG("Initializing random number generator.\n");    RNG_RNGInit();    RNG_SystemInfoForRNG();       /*     * All the ciphers except SSL_RSA_WITH_NULL_MD5 are on by default.     * Enable encryption, enable NULL cipher.      */#ifdef XP_MAC	result = mainLoop(argc, argv);#else    result = PR_Initialize(mainLoop, argc, argv, 0);#endif#ifdef DEBUG    printf("main: Finishing (%ld)\n", (long) result);#endif#ifdef XP_UNIX    SSM_ReleaseLockFile();#endif#ifndef XP_MAC    return result;#endif}#ifdef XP_MACvoid *psm_malloc(unsigned long numbytes){	return NewPtrClear(numbytes);}voidpsm_free(void *ptr){	DisposePtr((char *) ptr);}#endifPRIntn mainLoop(PRIntn argc, char ** argv){    PRFileDesc              *socket, *respsocket;    PRNetAddr               clientaddr;    SSMControlConnection    *curconnect;    SSMResourceID           ctrlID;    SSMStatus                status = PR_FAILURE;    PRBool                  alive = PR_TRUE;    /* Register ourselves so that logs, etc can identify us */    SSM_RegisterThread("main", NULL);    /* Open NLS stuff */    SSM_DEBUG("Initializing NLS.\n");#ifdef XP_MAC	SSM_InitNLS(":ui:");#else    SSM_InitNLS("ui");#endif    ssm_InitializePKCS11Strings();    /* Initialize the protocol */#ifdef XP_MAC	CMT_Init(malloc, free);#else    CMT_Init(PR_Malloc, PR_Free);#endif    /* Open the HTTP listener */    SSM_DEBUG("Opening HTTP thread.\n");	status = SSM_InitHTTP();    if (status != SSM_SUCCESS)     {        SSM_DEBUG("Couldn't open web port. Exiting.\n");        goto loser;    }    /* open a port for control connections, with well-known port# */    socket = SSM_OpenControlPort();    if (!socket)     {        SSM_DEBUG("Couldn't open control port. Exiting.\n");        goto loser;    }#ifdef XP_MAC    if (PR_CEnterMonitor(glue_component) != NULL) {        PR_CNotify(glue_component);        PR_CExitMonitor(glue_component);    }#endif        while (alive)     {        /* wait until there is incoming request */          respsocket = PR_Accept(socket, &clientaddr, PR_INTERVAL_NO_TIMEOUT);         /*      while (respsocket == NULL)                 {                 if (PR_GetError() != PR_WOULD_BLOCK_ERROR)                 goto loser;                PR_Sleep(CARTMAN_SPINTIME);#ifdef DEBUG                printf("master: Still ready for client connection on port %d\n", CARTMAN_PORT); fflush(stdout);#endif                respsocket = PR_Accept(socket, &clientaddr,                 PR_SecondsToInterval(2));                } */        if (!respsocket) {            /* accept failed: abort */            status = PR_GetError();            SSM_DEBUG("Error %d accepting control connection.  Exiting.\n",                      status);            goto loser;        }        if (SSM_SocketPeerCheck(respsocket, PR_TRUE)) {            SSM_DEBUG("creating control connection.\n");            status = SSM_CreateResource(SSM_RESTYPE_CONTROL_CONNECTION,                                        respsocket,                                        NULL,                                        &ctrlID,                                        (SSMResource **) &curconnect);            if (status != PR_SUCCESS)                break;            PR_ASSERT(RESOURCE_CLASS(curconnect) == SSM_RESTYPE_CONTROL_CONNECTION);        }        else {            /* connection did not come from localhost: shut down the              * connection and continue to loop             */            SSM_DEBUG("Connection attempt from a non-local host!\n");            status = PR_Shutdown(respsocket, PR_SHUTDOWN_BOTH);            respsocket = NULL;        }    } /* end while(true) */ loser:    /* Shut down the HTTP thread. */    if (httpListenThread != NULL){      PR_Interrupt(httpListenThread);    }    exit(1);    /* ### mwelch - should we return meaningful error code? */    return 0;}

⌨️ 快捷键说明

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