⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kgenctxt.c

📁 安全开发库。含客户端建立ssl连接、签名、证书验证、证书发布和撤销等。编译用到nss
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//*  * 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. */#include "kgenctxt.h"#include "ssmerrs.h"#include "minihttp.h"#include "textgen.h"#include "crypto.h"#include "base64.h"#include "newproto.h"#include "messages.h"#include "advisor.h"#include "oldfunc.h"#define SSMRESOURCE(ct) (&(ct)->super)#define DEFAULT_KEY_GEN_ALLOC 5void SSMKeyGenContext_ServiceThread(void *arg);/* Initialize an SSMKeyGenContext object. */SSMStatusSSMKeyGenContext_Init(SSMKeyGenContext *ct,                       SSMKeyGenContextCreateArg *arg,                       SSMResourceType type){    SSMStatus rv;    SSMControlConnection * parent = arg->parent;    rv = SSMResource_Init(parent, SSMRESOURCE(ct), type);    if (rv != PR_SUCCESS) goto loser;    ct->m_parent = parent;    ct->m_ctxtype = arg->type;    SSM_GetResourceReference(SSMRESOURCE(ct)); /* get ref for new thread */    if (arg->param) {        ct->super.m_clientContext.data = arg->param->data;        ct->super.m_clientContext.len  = arg->param->len;    }    /*     *  Create full keyGenContext for CRMF request and      *  keyGenContext-light for old-style keygen.     *  Differentiate on arg->type.     */    switch (arg->type) {       case SSM_CRMF_KEYGEN:        /* Create message queue and key collection. */        ct->m_incomingQ = SSM_NewCollection();        if (!ct->m_incomingQ) goto loser;        ct->m_keys = SSM_NewCollection();        if (!ct->m_keys) goto loser;        ct->m_serviceThread = SSM_CreateThread(SSMRESOURCE(ct),                                               SSMKeyGenContext_ServiceThread);        if (!ct->m_serviceThread)          goto loser;        break;      case SSM_OLD_STYLE_KEYGEN:        /* don't need to do anything */        ct->m_serviceThread = NULL;        SSM_DEBUG("Creating a keygen context for old-style keygen.\n");        break;      default:        goto loser;        break;    }          /* this is temporary fix, need to send this event in any case */#if 0     if (arg->type == SSM_CRMF_KEYGEN) {#endif    rv = SSMControlConnection_SendUIEvent(ct->m_parent,                                          "get",                                          "keygen_window",                                          SSMRESOURCE(ct),                                          NULL,                                          &SSMRESOURCE(ct)->m_clientContext);    if (rv != SSM_SUCCESS)        goto loser;#if 0    }#endif    if (arg->type == SSM_CRMF_KEYGEN) {      rv = SSM_SendQMessage(ct->m_incomingQ, SSM_PRIORITY_SHUTDOWN,                          SSM_KEYGEN_CXT_MESSAGE_DATA_PROVIDER_OPEN,                          0, NULL, PR_FALSE);      if (rv != PR_SUCCESS)        goto loser;      ct->m_keyGens = SSM_ZNEW_ARRAY(SSMKeyGenParams*, DEFAULT_KEY_GEN_ALLOC);      if (ct->m_keyGens == NULL) {        goto loser;      }    }    ct->m_numKeyGens = 0;    ct->m_allocKeyGens = DEFAULT_KEY_GEN_ALLOC;    ct->m_eaCert = NULL;    ct->m_userCancel = PR_FALSE;    goto done; loser:    if (rv == PR_SUCCESS) rv = PR_FAILURE;    ct->m_serviceThread = PR_GetCurrentThread(); done:	/* 		Prevent the control thread from responding if we successfully		created this object. This is because we want to make sure that		the Create Resource response is sent before the UI event due		to weirdness in the browser. 			See SSMKeyGenContext_ServiceThread below; at the top of that		routine, the Create Resource request is sent just before the		UI event.	*/	if (rv == PR_SUCCESS && arg->type == SSM_CRMF_KEYGEN)           rv = SSM_ERR_DEFER_RESPONSE;    return rv;}SSMStatusSSMKeyGenContext_Shutdown(SSMResource *res, SSMStatus status){    SSMKeyGenContext *ct = (SSMKeyGenContext *) res;    SSMStatus rv = PR_SUCCESS, trv;    SSMKeyGenContext_Invariant(ct);    SSM_LockResource(res);    if (res->m_resourceShutdown) {        rv = SSM_ERR_ALREADY_SHUT_DOWN;    } else {        /* If this is the service thread, deregister it. */        rv = SSMResource_Shutdown(res, status);                if (rv == PR_SUCCESS)        {            /* Post shutdown msgs to all applicable queues. */                        if (status != PR_SUCCESS && ct->m_incomingQ)                trv = SSM_SendQMessage(ct->m_incomingQ,                                       SSM_PRIORITY_SHUTDOWN,                                       SSM_DATA_PROVIDER_SHUTDOWN,                                       0, NULL, PR_TRUE);                        /* Wake up the keygen thread */            if (ct->m_serviceThread)                PR_Interrupt(ct->m_serviceThread);        }    }    SSM_NotifyResource(res);    res->m_resourceShutdown = PR_TRUE;    SSM_UnlockResource(res);    return rv;}SSMStatusSSMKeyGenContext_Destroy(SSMResource *res, PRBool doFree){    SSMKeyGenContext *ct = (SSMKeyGenContext *) res;    SSMStatus rv = PR_SUCCESS;    int i;        PR_ASSERT(SSM_IsAKindOf(res, SSM_RESTYPE_KEYGEN_CONTEXT));    PR_ASSERT(res->m_threadCount == 0);    SSM_LockResource(res);    if (ct->slot)         PK11_FreeSlot(ct->slot);        ct->slot = NULL;     if (ct->m_incomingQ)        ssm_DrainAndDestroyQueue(&ct->m_incomingQ);    if (ct->m_keys)    {        SSMKeyPair *kp;                /* Have to drain the collection before destroying it. */        while((rv = SSM_Dequeue(ct->m_keys, SSM_PRIORITY_ANY,                                (void **) &kp, PR_FALSE)) == PR_SUCCESS)        {            if ((res->m_status != PR_SUCCESS) && (res->m_status != SSM_ERR_CLIENT_DESTROY))                /* Bad status, so free keys as we remove them. */                if (kp == NULL)                   break;                SSM_FreeResource(&kp->super);        }        SSM_DestroyCollection(ct->m_keys);    }    if (ct->m_eaCert != NULL) {        CERT_DestroyCertificate(ct->m_eaCert);    }    ct->m_eaCert = NULL;    if (ct->m_keyGens) {        for (i=0; i<ct->m_numKeyGens; i++) {            SSM_FreeResource(SSMRESOURCE(ct->m_keyGens[i]->kp));            PR_Free(ct->m_keyGens[i]);            ct->m_keyGens[i] = NULL;        }        PR_Free(ct->m_keyGens);        ct->m_keyGens = NULL;    }        SSM_UnlockResource(res); /* we're done, stand aside for superclass */    SSMResource_Destroy(res, PR_FALSE); /* destroy superclass fields */    if (doFree)        PR_DELETE(res);    return PR_SUCCESS;}/*    SSMKeyGenContext_Create creates an SSMKeyGenContext object.    The value argument is ignored.*/SSMStatusSSMKeyGenContext_Create(void *arg, SSMControlConnection * connection,                         SSMResource **res){    SSMStatus rv = PR_SUCCESS;    SSMKeyGenContext *ct;    SSMKeyGenContextCreateArg *carg = (SSMKeyGenContextCreateArg *) arg;    *res = NULL; /* in case we fail */    if (!arg)    {        rv = PR_INVALID_ARGUMENT_ERROR;        goto loser;    }    ct = (SSMKeyGenContext *) PR_CALLOC(sizeof(SSMKeyGenContext));    if (!ct)     {        rv = PR_OUT_OF_MEMORY_ERROR;        goto loser;    }    SSMRESOURCE(ct)->m_connection = connection;    rv = SSMKeyGenContext_Init(ct, carg, SSM_RESTYPE_KEYGEN_CONTEXT);    if ((rv != PR_SUCCESS) && (rv != SSM_ERR_DEFER_RESPONSE)) goto loser;    SSMKeyGenContext_Invariant(ct);        *res = &ct->super;    return rv; loser:    if (rv == PR_SUCCESS) rv = PR_FAILURE;    if (ct)     {        SSM_ShutdownResource(SSMRESOURCE(ct), rv); /* force destroy */        SSM_FreeResource(&ct->super);    }    return rv;}SSMStatusSSMKeyGenContext_FormSubmitHandler(SSMResource * res, HTTPRequest * req){   SSMStatus rv = SSM_FAILURE;   char * tmpStr = NULL;	  /* make sure you got the right baseRef */  rv = SSM_HTTPParamValue(req, "baseRef", &tmpStr);  if (rv != SSM_SUCCESS ||      PL_strcmp(tmpStr, "windowclose_doclose_js") != 0) {    goto loser;  }  if (!res->m_formName)    goto loser;  if (PL_strcmp(res->m_formName, "set_db_password") == 0)    return rv = SSM_SetDBPasswordHandler(req);  else     goto loser;loser:  return SSM_HTTPDefaultCommandHandler(req);}SSMStatus SSMKeyGenContext_Print(SSMResource   *res,                       char *fmt,                       PRIntn numParams,                       char **value,                       char **resultStr){    SSMKeyGenContext  *ct              = (SSMKeyGenContext*)res;    char              *escrowCAName    = NULL;    SSMTextGenContext *textGenCxt      = NULL;    SSMStatus          rv = PR_FAILURE;    char              mechStr[48];    PR_ASSERT(resultStr != NULL);    if (resultStr == NULL) {        return PR_FAILURE;    }    if (!SSM_IsAKindOf(res, SSM_RESTYPE_KEYGEN_CONTEXT)) {        goto loser;    }    /*      * this function thinks it's always called for key escrow dialog.     * not so, it is also called for set password dialog, and maybe called for     * other things too.      * Assume, if no param values are supplied through the arguments, it is     * indeed key escrow case, otherwise use arguments.     */    if (!numParams) {        if (ct->m_eaCert != NULL) {            escrowCAName = CERT_GetCommonName(&ct->m_eaCert->subject);        }        if(escrowCAName == NULL) {            if (SSMTextGen_NewTopLevelContext(NULL, &textGenCxt) !=                 SSM_SUCCESS)                 goto loser;                        rv = SSM_FindUTF8StringInBundles(textGenCxt,                                              "certificate_authority_text",                                             &escrowCAName);             if (rv != PR_SUCCESS)                goto loser;        }        /* The escrow name should be in a format we can print already. */        PR_snprintf(mechStr, 48, "%d",                     SSMKeyGenContext_GenMechToAlgMech(ct->mech));        *resultStr = PR_smprintf(fmt, res->m_id, mechStr, escrowCAName);    } else 

⌨️ 快捷键说明

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