swfutl.c

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

C
743
字号
{    unsigned char seed[20];    /* note: GetNoise doesn't always get 20 bytes, but adding more     * random data from the stack doesn't subtract entropy from the     * Random number generator, so just send it all.     */    RNG_GetNoise(seed,sizeof(seed));    RNG_RandomUpdate(seed,sizeof(seed));}/* * Get a random number */intfort_GenerateRandom(unsigned char *buf, int bytes){    SECStatus rv;    fort_AddNoise();    rv = RNG_GenerateGlobalRandomBytes(buf,bytes);    if (rv != SECSuccess) return CI_EXEC_FAIL;    return CI_OK;}/* * NOTE: that MAC is missing below. */#if defined (XP_UNIX) || defined (XP_OS2)#ifdef XP_UNIX#define NS_PATH_SEP ':'#define NS_DIR_SEP '/'#define NS_DEFAULT_PATH ".:/bin/netscape:/etc/netscape/:/etc"#endif#ifdef XP_OS2		/* for OS/2 */#define NS_PATH_SEP ';'#define NS_DIR_SEP '\\'#define NS_DEFAULT_PATH ".:\\bin\\netscape:\\etc\\netscape\\:\\etc"#endifPRInt32local_getFileInfo(const char *fn, PRFileInfo *info){    PRInt32 rv;    struct stat sb;    rv = stat(fn, &sb);    if (rv < 0)	return -1;    else if (NULL != info)    {        if (S_IFREG & sb.st_mode)            info->type = PR_FILE_FILE;        else if (S_IFDIR & sb.st_mode)            info->type = PR_FILE_DIRECTORY;        else            info->type = PR_FILE_OTHER;#if defined(OSF1)        if (0x7fffffffLL < sb.st_size)        {            return -1;        }#endif /* defined(OSF1) */        info->size = sb.st_size;    }    return rv;}#endif /* UNIX & OS/2 */#ifdef XP_WIN#define NS_PATH_SEP ';'#define NS_DIR_SEP '\\'#define NS_DEFAULT_PATH ".;c:\\program files\\netscape\\communicator\\program\\pkcs11\\netscape;c:\\netscape\\communicator\\program\\pkcs11\\netscape;c:\\windows\\system"/* * Since we're a pkcs #11 module, we may get  * loaded into lots of different binaries, each with different or no versions * of NSPR running... so we copy the one function we need. */#define _PR_IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')/* * IsRootDirectory -- * * Return PR_TRUE if the pathname 'fn' is a valid root directory, * else return PR_FALSE.  The char buffer pointed to by 'fn' must * be writable.  During the execution of this function, the contents * of the buffer pointed to by 'fn' may be modified, but on return * the original contents will be restored.  'buflen' is the size of * the buffer pointed to by 'fn'. * * Root directories come in three formats: * 1. / or \, meaning the root directory of the current drive. * 2. C:/ or C:\, where C is a drive letter. * 3. \\<server name>\<share point name>\ or *    \\<server name>\<share point name>, meaning the root directory *    of a UNC (Universal Naming Convention) name. */static PRBoolIsRootDirectory(char *fn, size_t buflen){    char *p;    PRBool slashAdded = PR_FALSE;    PRBool rv = PR_FALSE;    if (_PR_IS_SLASH(fn[0]) && fn[1] == '\0') {        return PR_TRUE;    }    if (isalpha(fn[0]) && fn[1] == ':' && _PR_IS_SLASH(fn[2])            && fn[3] == '\0') {        rv = GetDriveType(fn) > 1 ? PR_TRUE : PR_FALSE;        return rv;    }    /* The UNC root directory */    if (_PR_IS_SLASH(fn[0]) && _PR_IS_SLASH(fn[1])) {        /* The 'server' part should have at least one character. */        p = &fn[2];        if (*p == '\0' || _PR_IS_SLASH(*p)) {            return PR_FALSE;        }        /* look for the next slash */        do {            p++;        } while (*p != '\0' && !_PR_IS_SLASH(*p));        if (*p == '\0') {            return PR_FALSE;        }        /* The 'share' part should have at least one character. */        p++;        if (*p == '\0' || _PR_IS_SLASH(*p)) {            return PR_FALSE;        }        /* look for the final slash */        do {            p++;        } while (*p != '\0' && !_PR_IS_SLASH(*p));        if (_PR_IS_SLASH(*p) && p[1] != '\0') {            return PR_FALSE;        }        if (*p == '\0') {            /*             * GetDriveType() doesn't work correctly if the             * path is of the form \\server\share, so we add             * a final slash temporarily.             */            if ((p + 1) < (fn + buflen)) {                *p++ = '\\';                *p = '\0';                slashAdded = PR_TRUE;            } else {                return PR_FALSE; /* name too long */            }        }        rv = GetDriveType(fn) > 1 ? PR_TRUE : PR_FALSE;        /* restore the 'fn' buffer */        if (slashAdded) {            *--p = '\0';        }    }    return rv;}PRInt32local_getFileInfo(const char *fn, PRFileInfo *info){    HANDLE hFindFile;    WIN32_FIND_DATA findFileData;    char pathbuf[MAX_PATH + 1];        if (NULL == fn || '\0' == *fn) {        return -1;    }    /*     * FindFirstFile() expands wildcard characters.  So     * we make sure the pathname contains no wildcard.     */    if (NULL != strpbrk(fn, "?*")) {        return -1;    }    hFindFile = FindFirstFile(fn, &findFileData);    if (INVALID_HANDLE_VALUE == hFindFile) {        DWORD len;        char *filePart;        /*         * FindFirstFile() does not work correctly on root directories.         * It also doesn't work correctly on a pathname that ends in a         * slash.  So we first check to see if the pathname specifies a         * root directory.  If not, and if the pathname ends in a slash,         * we remove the final slash and try again.         */        /*         * If the pathname does not contain ., \, and /, it cannot be         * a root directory or a pathname that ends in a slash.         */        if (NULL == strpbrk(fn, ".\\/")) {            return -1;        }         len = GetFullPathName(fn, sizeof(pathbuf), pathbuf,                &filePart);        if (len > sizeof(pathbuf)) {            return -1;        }        if (IsRootDirectory(pathbuf, sizeof(pathbuf))) {            info->type = PR_FILE_DIRECTORY;            info->size = 0;            /*             * These timestamps don't make sense for root directories.             */            info->modifyTime = 0;            info->creationTime = 0;            return 0;        }        if (!((pathbuf[len - 1] == '/') || (pathbuf[len-1] == '\\'))) {            return -1;        } else {            pathbuf[len - 1] = '\0';            hFindFile = FindFirstFile(pathbuf, &findFileData);            if (INVALID_HANDLE_VALUE == hFindFile) {                return -1;            }        }    }    FindClose(hFindFile);    if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {        info->type = PR_FILE_DIRECTORY;    } else {        info->type = PR_FILE_FILE;    }    info->size = findFileData.nFileSizeLow;    return 0;}#endif /* XP_WIN */#ifdef XP_MAC#error Need to write fort_FindFileInPath for Mac#define NS_PATH_SEP ','#define NS_DIR_SEP ':'#define NS_DEFAULT_PATH ",System Folder,System Folder:Netscape f:pkcs11:netscape"#endif#define NETSCAPE_INIT_FILE "nsswft.swf"/* * OK, We're deep in the bottom of MACI and PKCS #11... We need to * find our fortezza key file. We have no clue of where the our binary lives * or where our key file lives. This function lets us search manual paths  * to find our key file. */char *fort_FindFileInPath(char *path, char *fn){    char *next;    char *holdData;    char *ret = NULL;    int len = 0;    int fn_len = PORT_Strlen(fn)+1; /* include the NULL */    PRFileInfo info;    char dirSep = NS_DIR_SEP;    holdData = PORT_Alloc(strlen(path)+1+fn_len);    while ((next = local_index(path,NS_PATH_SEP)) != NULL) {	len = next - path;	PORT_Memcpy(holdData,path,len);	if ((len != 0) && (holdData[len-1] != dirSep)) {	  PORT_Memcpy(&holdData[len],&dirSep,1);	  len++;	}	PORT_Memcpy(&holdData[len],fn,fn_len);		if ((local_getFileInfo(holdData,&info) == 0) &&		(info.type == PR_FILE_FILE) && (info.size != 0)) {	    ret = PORT_Strdup(holdData);	    PORT_Free(holdData);	    return ret;	}	path = next+1;    }    len = strlen(path);    PORT_Memcpy(holdData,path,len);    if ((len != 0) && (holdData[len-1] != dirSep)) {	  PORT_Memcpy(&holdData[len],&dirSep,1);	  len++;    }    PORT_Memcpy(&holdData[len],fn,fn_len);	    if ((local_getFileInfo(holdData,&info) == 0) &&		(info.type == PR_FILE_FILE) && (info.size != 0)) {	    ret = PORT_Strdup(holdData);    }    PORT_Free(holdData);    return ret;}static char *path_table[] = {   "PATH","LD_LIBRARY_PATH","LIBPATH"};static int path_table_size = sizeof(path_table)/sizeof(path_table[0]);char *fort_LookupFORTEZZAInitFile(void){    char *fname = NULL;    char *home = NULL;#ifdef XP_UNIX    char unix_home[512];#endif    int i;    /* first try to get it from the environment */    fname = getenv("SW_FORTEZZA_FILE");    if (fname != NULL) {	return PORT_Strdup(fname);    }#ifdef XP_UNIX    home = getenv("HOME");    if (home) {	strncpy(unix_home,home, sizeof(unix_home)-sizeof("/.netscape"));	strcat(unix_home,"/.netscape");	fname = fort_FindFileInPath(unix_home,NETSCAPE_INIT_FILE);	if (fname) return fname;    }#endif#ifdef XP_WIN    home = getenv("windir");    if (home) {	fname = fort_FindFileInPath(home,NETSCAPE_INIT_FILE);	if (fname) return fname;    }#endif	    fname = fort_FindFileInPath(NS_DEFAULT_PATH,NETSCAPE_INIT_FILE);    if (fname) return fname;    /* now search the system paths */    for (i=0; i < path_table_size; i++) {    	char *path = getenv(path_table[i]);	if (path != NULL) {	    fname = fort_FindFileInPath(path,NETSCAPE_INIT_FILE);	    if (fname) return fname;	}    }    return NULL;}

⌨️ 快捷键说明

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