advisor.c

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

C
2,020
字号
/* -*- 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. */#ifdef XP_MAC#include "platform.h"#endif#include "advisor.h"#include "nlsutil.h"#include "minihttp.h"#include "p12res.h"#include "textgen.h"#include "sslskst.h"#include "certlist.h"#include "ocsp.h"#include "secoid.h"#include "prefs.h"#include "messages.h"#include "secerr.h"#include "sslerr.h"#include "base64.h"/* * This is the structure used to gather all of the CA's that can be used * for OCSP responders. */typedef struct SSMDefaultOCSPRespondersListStr{    SSMSortedList *respondersWithAIA;    SSMSortedList *respondersWithoutAIA;    SSMTextGenContext *cx;    char *wrapper, *defaultSigner;} SSMDefaultOCSPRespondersList;SSMStatus SSM_SetSelectedItemInfo(SSMSecurityAdvisorContext* cx);#define SSMRESOURCE(object) (&object->super)#define SSM_NO_INFO						"sa_no_info"#define SSM_NAVIGATOR_NO_SEC            "sa_navigator_no_sec"#define SSM_NAVIGATOR_SSL               "sa_navigator_ssl"#define SSM_NAVIGATOR_BAD_SSL			"sa_navigator_bad_ssl"#define SSM_MESSAGE						"sa_message"#define SSM_MESSAGE_NOT_SIGNED			"sa_message_not_signed"#define SSM_MESSAGE_NOT_ENCRYPTED		"sa_message_not_encrypted"#define SSM_MESSAGE_SIGNED				"sa_message_signed"#define SSM_MESSAGE_ENCRYPTED			"sa_message_encrypted"#define SSM_MESSAGE_BAD_SIGNED			"sa_message_bad_signed"#define SSM_MESSAGE_BAD_ENCRYPTED		"sa_message_bad_encrypted"/* A list of User agent strings that we know can do S/MIME * and want the Java tab as well. */#define COMMON_TO_SMIME_AND_JAVA "Mozilla/4.7"const char *kSMimeApps[]  = {COMMON_TO_SMIME_AND_JAVA, NULL};const char *kJavaJSApps[] = {COMMON_TO_SMIME_AND_JAVA, NULL};char * SSM_ConvertStringToHTMLString(char * string);char * SSMUI_GetPKCS12Error(PRIntn error, PRBool isBackup);PRBoolSSM_IsCRLPresent(SSMControlConnection *ctrl){    SECStatus rv = SECFailure;    CERTCrlHeadNode *head = NULL;    PRBool retVal = PR_FALSE;        rv = SEC_LookupCrls(ctrl->m_certdb, &head, -1);    if (rv != SECSuccess) {        goto done;    }    if (head == NULL) {        goto done;    }    retVal = (head->first == NULL) ? PR_FALSE : PR_TRUE;    PORT_FreeArena(head->arena, PR_FALSE); done:    return retVal;}SSMStatus SSMSecurityAdvisorContext_Create(SSMControlConnection *ctrl,                                  InfoSecAdvisor *info,                                  SSMResource **res){    SSMStatus rv = PR_SUCCESS;    SSMSecurityAdvisorContext *ct;	int i;    *res = NULL; /* in case we fail */        ct = (SSMSecurityAdvisorContext *)         PR_CALLOC(sizeof(SSMSecurityAdvisorContext));    if (!ct)         goto loser;    rv = SSMResource_Init(ctrl, &ct->super, SSM_RESTYPE_SECADVISOR_CONTEXT);    if (rv != PR_SUCCESS)         goto loser;        /* this hash will contail list of formatted certs nickname to display */    ct->m_certhash = NULL;    ct->m_certsIncluded = 0;        /* register us with ControlConection */    if (!ctrl->m_secAdvisorList) {        ctrl->m_secAdvisorList = (SECItem *) PR_Malloc(sizeof(SECItem));        ctrl->m_secAdvisorList->len = 0;        ctrl->m_secAdvisorList->data = NULL;    }     ctrl->m_secAdvisorList->len++;    ctrl->m_secAdvisorList->data = (unsigned char *) PR_REALLOC(ctrl->m_secAdvisorList->data,                                                ctrl->m_secAdvisorList->len);    ctrl->m_secAdvisorList->data[ ctrl->m_secAdvisorList->len - 1 ] =         ((SSMResource *)ct)->m_id;    if (info) {        ct->infoContext = info->infoContext;        ct->resID = info->resID;        ct->hostname = info->hostname ? PL_strdup(info->hostname) : NULL;		ct->senderAddr = info->senderAddr ? PL_strdup(info->senderAddr) : NULL;		ct->encryptedP7CInfo = info->encryptedP7CInfo;		ct->signedP7CInfo = info->signedP7CInfo;		ct->decodeError = info->decodeError;		ct->verifyError = info->verifyError;		ct->encryptthis = info->encryptthis;		ct->signthis = info->signthis;		ct->numRecipients = info->numRecipients;	    if (info->numRecipients > 0) {			ct->recipients = (char **) PR_CALLOC(sizeof(char*)*(info->numRecipients));			if (!ct->recipients) {				goto loser;			}	        for (i=0;i<info->numRecipients;i++) {		        ct->recipients[i] = PL_strdup(info->recipients[i]);			}		}        SSM_SetSelectedItemInfo(ct);    }    /* Create a URL for the security advisor window. */    rv = (SSMStatus) SSM_GenerateURL(ctrl, "get", "secadvisor",                          &ct->super, NULL,                          &ct->m_width, &ct->m_height,                          &ct->m_url);    if (rv != SSM_SUCCESS)        goto loser;    SSMSecurityAdvisorContext_Invariant(ct);    *res = &ct->super;    return PR_SUCCESS; loser:    if (rv == PR_SUCCESS) rv = PR_FAILURE;    if (ct)     {        ct->super.m_refCount = 1; /* force destroy */        SSM_FreeResource(&ct->super);    }            return rv;}char * SSMUI_GetPKCS12Error(PRIntn error, PRBool isBackup){    char * responseKey;        switch (error) {     case SSM_ERR_NO_PASSWORD:        responseKey = "pkcs12_bad_portable_password_restore";        break;    case SSM_ERR_BAD_DB_PASSWORD:        responseKey = "pkcs12_bad_db_password";        break;    case SSM_ERR_BAD_FILENAME:        responseKey = "pkcs12_bad_filepath";        break;    case SSM_ERR_NEED_USER_INIT_DB:        responseKey = "pkcs12_need_db_init";        break;    case SSM_ERR_CANNOT_DECODE:        responseKey="pkcs12_cannot_decode";        break;    case SSM_PKCS12_CERT_ALREADY_EXISTS:        responseKey="pkcs12_cert_already_exists";        break;    case SSM_ERR_BAD_REQUEST:    default:        responseKey = (isBackup) ? "pkcs12_backup_failure" :                                    "pkcs12_restore_failure";    }    return responseKey;}SSMStatus SSMSecurityAdvisorContext_Destroy(SSMResource *res, PRBool doFree){    SSMSecurityAdvisorContext *ct = (SSMSecurityAdvisorContext *) res;    PRIntn i = 0, others = 0;    if (ct)    {        PR_ASSERT(SSM_IsAKindOf(res, SSM_RESTYPE_SECADVISOR_CONTEXT));        SSMResource_Destroy(res, PR_FALSE);                /* Dereference the security info object */        if (ct->m_infoSource)        {            SSM_FreeResource(ct->m_infoSource);            ct->m_infoSource = NULL;        }        /* Free the URL */        PR_FREEIF(ct->m_url);        if (ct->m_certhash)            SSMSortedList_Destroy(ct->m_certhash);                /* deregister with control connection */        while (i < res->m_connection->m_secAdvisorList->len) {            if (res->m_connection->m_secAdvisorList->data[i] == res->m_id)                res->m_connection->m_secAdvisorList->data[i] = 0;            if (res->m_connection->m_secAdvisorList->data[i])                others ++;            i++;        }        if (!others) {            SECITEM_ZfreeItem(res->m_connection->m_secAdvisorList, PR_TRUE);            res->m_connection->m_secAdvisorList = NULL;        }        if (ct->socketStatus) {            SSM_FreeResource(&ct->socketStatus->super);        }        PR_FREEIF(ct->hostname);        PR_FREEIF(ct->senderAddr);        if (ct->recipients) {            for (i=0; i<ct->numRecipients; i++) {                PR_FREEIF(ct->recipients[i]);            }            PR_FREEIF(ct->recipients);        }        /* Free if asked */        if (doFree)            PR_Free(ct);    }    return PR_SUCCESS; /* no way to fail, really */}void SSMSecurityAdvisorContext_Invariant(SSMSecurityAdvisorContext *ct){    /* Check superclass. */    SSMResource_Invariant(&ct->super);    /* Make sure we always have a URL. */    PR_ASSERT(ct->m_url != NULL);}SSMStatus SSMSecurityAdvisorContext_GetAttrIDs(SSMResource *res,                                     SSMAttributeID **ids,                                     PRIntn *count){    SSMStatus rv;    rv = SSMResource_GetAttrIDs(res, ids, count);    if (rv != PR_SUCCESS)        goto loser;    *ids = (SSMAttributeID *) PR_REALLOC(*ids, (*count + 4) * sizeof(SSMAttributeID));    if (! *ids) goto loser;    (*ids)[*count++] = SSM_FID_SECADVISOR_URL;    (*ids)[*count++] = SSM_FID_SECADVISOR_WIDTH;    (*ids)[*count++] = SSM_FID_SECADVISOR_HEIGHT;    (*ids)[*count++] = SSM_FID_CLIENT_CONTEXT;    goto done; loser:    if (rv == PR_SUCCESS) rv = PR_FAILURE; done:    return rv;}SSMStatus SSMSecurityAdvisorContext_GetAttr(SSMResource *res,                                  SSMAttributeID attrID,                                  SSMResourceAttrType attrType,                                  SSMAttributeValue *value){    SSMStatus rv = PR_SUCCESS;    SSMSecurityAdvisorContext *ct = (SSMSecurityAdvisorContext *) res;    SSMSecurityAdvisorContext_Invariant(ct);    switch(attrID)    {    case SSM_FID_SECADVISOR_URL:        /* Duplicate and return the string. */        value->type = SSM_STRING_ATTRIBUTE;        value->u.string.len = PL_strlen(ct->m_url);        value->u.string.data = (unsigned char *) PL_strdup(ct->m_url);        break;    case SSM_FID_SECADVISOR_WIDTH:    case SSM_FID_SECADVISOR_HEIGHT:        value->type = SSM_NUMERIC_ATTRIBUTE;        value->u.numeric = (attrID == SSM_FID_SECADVISOR_WIDTH) ?            ct->m_width : ct->m_height;        break;    case SSM_FID_CLIENT_CONTEXT:      SSM_DEBUG("Getting security advisor client context");      value->type = SSM_STRING_ATTRIBUTE;      if (!(value->u.string.data = (unsigned char *) PR_Malloc(res->m_clientContext.len))) {          goto loser;      }      memcpy(value->u.string.data, res->m_clientContext.data, res->m_clientContext.len);      value->u.string.len = res->m_clientContext.len;      break;    default:        rv = SSMResource_GetAttr(res,attrID,attrType,value);        if (rv != PR_SUCCESS)            goto loser;    }    goto done; loser:    value->type = SSM_NO_ATTRIBUTE;    if (rv == PR_SUCCESS)        rv = PR_FAILURE; done:    return rv;}SSMStatus SSMSecurityAdvisorContext_SetAttr(SSMResource *res,                                  SSMAttributeID attrID,                                  SSMAttributeValue *value){    switch(attrID) {    case SSM_FID_CLIENT_CONTEXT:      SSM_DEBUG("Setting security advisor client context\n");      if (value->type != SSM_STRING_ATTRIBUTE) {          goto loser;      }      if (!(res->m_clientContext.data = (unsigned char *) PR_Malloc(value->u.string.len))) {          goto loser;      }      memcpy(res->m_clientContext.data, value->u.string.data, value->u.string.len);      res->m_clientContext.len = value->u.string.len;      break;    default:      SSM_DEBUG("Got unknown security advisor Set Attribute Request %d\n", attrID);      goto loser;      break;    }    return PR_SUCCESS;loser:    return PR_FAILURE;}/* Preference keys used in Security Advisor JavaScript. * They are used to cache temporary changes the user has made. */#define SSL2_SPK "enable_ssl2"#define SSL3_SPK "enable_ssl3"#define CLIENT_AUTH_SPK "client_auth_auto_select"#define EMAIL_CERT_SPK "default_email_cert"#define WARN_ENTER_SECURE_SPK "warn_entering_secure"

⌨️ 快捷键说明

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