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

📄 cmtutils.c

📁 安全开发库。含客户端建立ssl连接、签名、证书验证、证书发布和撤销等。编译用到nss
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- 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_UNIX#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#else#ifdef XP_MAC#include "macsocket.h"#else /* Windows */#include <windows.h>#include <winsock.h>#endif#endif#include "cmtcmn.h"#include "cmtutils.h"#include "newproto.h"#include <string.h>/* Local defines */#if 0#define PSM_WAIT_BEFORE_SLEEP           (CM_TicksPerSecond() * 60)#define PSM_SPINTIME                    PSM_WAIT_BEFORE_SLEEP#define PSM_KEEP_CONNECTION_ALIVE       (PSM_WAIT_BEFORE_SLEEP * 900)#endif/* If you want to dump the messages sent between the plug-in and the PSM  * server, then remove the comment for the appropriate define. */#if 0#define PRINT_SEND_MESSAGES#define PRINT_RECEIVE_MESSAGES#endif#ifdef PRINT_SEND_MESSAGES#ifndef DEBUG_MESSAGES#define DEBUG_MESSAGES#endif /*DEBUG_MESSAGES*/#endif /*PRINT_SEND_MESSAGES*/#ifdef PRINT_RECEIVE_MESSAGES#ifndef DEBUG_MESSAGES#define DEBUG_MESSAGES#endif /*DEBUG_MESSAGES*/#endif /*PRINT_RECEIVE_MESSAGES*/#ifdef DEBUG_MESSAGES#define LOG(x) do { FILE *f; f=fopen("cmnav.log","a+"); if (f) { \   fprintf(f, x); fclose(f); } } while(0);#define LOG_S(x) do { FILE *f; f=fopen("cmnav.log","a+"); if (f) { \   fprintf(f, "%s", x); fclose(f); } } while(0);#define ASSERT(x) if (!(x)) { LOG("ASSERT:"); LOG(#x); LOG("\n"); exit(-1); }#else#define LOG(x)#define LOG_S(x)#define ASSERT(x)#endifCMUint32cmt_Strlen(char *str){    CMUint32 len = strlen(str);    return sizeof(CMInt32) + (((len + 3)/4)*4);}CMUint32cmt_Bloblen(CMTItem *blob){    return sizeof(CMInt32) + (((blob->len +3)/4)*4);}char *cmt_PackString(char *buf, char *str){    CMUint32 len = strlen(str);    CMUint32 networkLen = htonl(len);    CMUint32 padlen = ((len + 3)/4)*4;    memcpy(buf, &networkLen, sizeof(CMUint32));    memcpy(buf + sizeof(CMUint32), str, len);    memset(buf + sizeof(CMUint32) + len, 0, padlen - len);    return buf+sizeof(CMUint32)+padlen;}char *cmt_PackBlob(char *buf, CMTItem *blob){    CMUint32 len = blob->len;    CMUint32 networkLen = htonl(len);    CMUint32 padlen = (((blob->len + 3)/4)*4);    *((CMUint32*)buf) = networkLen;    memcpy(buf + sizeof(CMUint32), blob->data, len);    memset(buf + sizeof(CMUint32) + len, 0, padlen - len);    return buf + sizeof(CMUint32) + padlen;}char *cmt_UnpackString(char *buf, char **str){    char *p = NULL;    CMUint32 len, padlen;    /* Get the string length */    len = ntohl(*(CMUint32*)buf);    /* Get the padded length */    padlen = ((len + 3)/4)*4;    /* Allocate the string and copy the data */    p = (char *) malloc(len + 1);    if (!p) {        goto loser;    }    /* Copy the data and NULL terminate */    memcpy(p, buf+sizeof(CMUint32), len);    p[len] = 0;    *str = p;    return buf+sizeof(CMUint32)+padlen;loser:    *str = NULL;    if (p) {        free(p);    }    return buf+sizeof(CMUint32)+padlen;}char *cmt_UnpackBlob(char *buf, CMTItem **blob){    CMTItem *p = NULL;    CMUint32 len, padlen;    /* Get the blob length */    len = ntohl(*(CMUint32*)buf);        /* Get the padded length */    padlen = ((len + 3)/4)*4;    /* Allocate the CMTItem for the blob */    p = (CMTItem*)malloc(sizeof(CMTItem));    if (!p) {        goto loser;    }    p->len = len;    p->data = (unsigned char *) malloc(len);    if (!p->data) {        goto loser;    }    /* Copy that data across */    memcpy(p->data, buf+sizeof(CMUint32), len);    *blob = p;    return buf+sizeof(CMUint32)+padlen;loser:    *blob = NULL;    CMT_FreeMessage(p);    return buf+sizeof(CMUint32)+padlen;}#ifdef DEBUG_MESSAGESvoid prettyPrintMessage(CMTItem *msg){  int numLines = ((msg->len+7)/8);  char curBuffer[9], *cursor, string[2], hexVal[8];  char hexArray[25];  int i, j, numToCopy;  /*Try printing out 8 bytes at a time. */  LOG("\n**********************************************************\n");  LOG("About to pretty Print Message\n\n");  curBuffer[9] = '\0';  hexArray[24] = '\0';  hexVal[2] = '\0';  string[1] = '\0';  LOG("Header Info\n");  LOG("Message Type: ");  sprintf(hexArray, "%lx\n", msg->type);  LOG(hexArray);  LOG("Message Length: ");  sprintf (hexArray, "%ld\n\n", msg->len);  LOG(hexArray);  LOG("Body of Message\n");  for (i=0, cursor=msg->data; i<numLines; i++, cursor+=8) {    /* First copy over the buffer to our local array */    numToCopy = ((msg->len - (unsigned int)((unsigned long)cursor-(unsigned long)msg->data)) < 8) ?                msg->len - (unsigned int)((unsigned long)cursor-(unsigned long)msg->data) : 8;    memcpy(curBuffer, cursor, 8);    for (j=0;j<numToCopy;j++) {      string[0] = curBuffer[j];      if (isprint(curBuffer[j])) {	string[0] = curBuffer[j];      } else {	string[0] = ' ';      }      LOG(string);    }    string[0] = ' ';    for (;j<8;j++) {      LOG(string);    }    LOG("\t");    for (j=0; j<numToCopy; j++) {      sprintf (hexVal,"%.2x", 0x0ff & (unsigned short)curBuffer[j]);      LOG(hexVal);      LOG(" ");    }    LOG("\n");  }  LOG("Done Pretty Printing Message\n");  LOG("**********************************************************\n\n");}#endifCMTStatus CMT_SendMessage(PCMT_CONTROL control, CMTItem* message){	CMTStatus status;    CMUint32 msgCategory;    CMBool done = CM_FALSE;#ifdef PRINT_SEND_MESSAGES    LOG("About to print message sent to PSM\n");    prettyPrintMessage(message);#endif    /* Acquire lock on the control connection */    CMT_LOCK(control->mutex);    /* Try to send pending random data */    if (message->type != (SSM_REQUEST_MESSAGE | SSM_HELLO_MESSAGE))    {        /* If we've already said hello, then flush random data           just before sending the request. */        status = CMT_FlushPendingRandomData(control);        if (status != CMTSuccess)            goto loser;    }    	status = CMT_TransmitMessage(control, message);	if (status != CMTSuccess) {		goto loser;	}    /* We have to deal with other types of data on the socket and */    /* handle them accordingly */    while (!done) {    	status = CMT_ReceiveMessage(control, message);        if (status != CMTSuccess) {            goto loser;        }        msgCategory = (message->type & SSM_CATEGORY_MASK);        switch (msgCategory) {            case SSM_REPLY_OK_MESSAGE:                done = CM_TRUE;                break;            case SSM_REPLY_ERR_MESSAGE:                done = CM_TRUE;                break;            case SSM_EVENT_MESSAGE:                CMT_DispatchEvent(control, message);                break;            /* XXX FIX THIS!!! For the moment I'm ignoring all other types */            default:                break;        }    }    /* Release the control connection lock */    CMT_UNLOCK(control->mutex);	return CMTSuccess;loser:    /* Release the control connection lock */    CMT_UNLOCK(control->mutex);	return CMTFailure;}CMTStatus CMT_TransmitMessage(PCMT_CONTROL control, CMTItem * message){    CMTMessageHeader header;	CMUint32 sent, rv;    /* Set up the message header */    header.type = htonl(message->type);    header.len = htonl(message->len);    /* Obscure the message header */    rv = SSMObscure_Send(control->obscureObj, &header, sizeof(CMTMessageHeader));    if (rv != 0) {        goto loser;    }	/* Send the message header */	sent = CMT_WriteThisMany(control, control->sock,                              (void *)&header, sizeof(CMTMessageHeader));	if (sent != sizeof(CMTMessageHeader)) {

⌨️ 快捷键说明

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