dbck.c

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

C
1,870
字号
	    subjectEntry = (certDBEntrySubject *)&subjNode->entry;	    if (SECITEM_ItemsAreEqual(&derSubject, &subjectEntry->derSubject)) {		/*  Found matching subject name, create link.  */		map->pSubject = subjNode;		/*  Make sure subject entry has cert's key.  */		for (i=0; i<subjectEntry->ncerts; i++) {		    if (SECITEM_ItemsAreEqual(&certKey,		                              &subjectEntry->certKeys[i])) {			/*  Found matching cert key.  */			smap = (certDBSubjectEntryMap *)subjNode->appData;			smap->pCerts[i] = certNode;			break;		    }		}	    }	}    }    PORT_FreeArena(tmparena, PR_FALSE);    return SECSuccess;}SECStatusmapSubjectEntries(certDBArray *dbArray){    certDBEntrySubject *subjectEntry;    certDBEntryNickname *nicknameEntry;    certDBEntrySMime *smimeEntry;    certDBEntryListNode *subjNode, *nickNode, *smimeNode;    certDBSubjectEntryMap *subjMap;    certDBEntryMap *nickMap, *smimeMap;    PRCList *sElem, *nElem, *mElem;    for (sElem = PR_LIST_HEAD(&dbArray->subjects.link);         sElem != &dbArray->subjects.link; sElem = PR_NEXT_LINK(sElem)) {	/* Iterate over subject entries and map subjects to nickname	 * and smime entries.  The cert<->subject map will be handled	 * by a subsequent call to mapCertEntries.	 */	subjNode = LISTNODE_CAST(sElem);	subjectEntry = (certDBEntrySubject *)&subjNode->entry;	subjMap = (certDBSubjectEntryMap *)subjNode->appData;	/* need to alloc memory here for array of matching certs. */	subjMap->pCerts = PORT_ArenaAlloc(subjMap->arena, 	                                  subjectEntry->ncerts*sizeof(int));	subjMap->numCerts = subjectEntry->ncerts;	if (subjectEntry->nickname) {	    /* Subject should have a nickname entry, so create a link. */	    for (nElem = PR_LIST_HEAD(&dbArray->nicknames.link);	         nElem != &dbArray->nicknames.link; 	         nElem = PR_NEXT_LINK(nElem)) {		/*  Look for subject's nickname in nickname entries.  */		nickNode = LISTNODE_CAST(nElem);		nicknameEntry = (certDBEntryNickname *)&nickNode->entry;		nickMap = (certDBEntryMap *)nickNode->appData;		if (PL_strcmp(subjectEntry->nickname, 		              nicknameEntry->nickname) == 0) {		    /*  Found a nickname entry for subject's nickname.  */		    if (SECITEM_ItemsAreEqual(&subjectEntry->derSubject,		                              &nicknameEntry->subjectName)) {			/*  Nickname and subject match.  */			subjMap->pNickname = nickNode;			nickMap->pSubject = subjNode;		    } else {			/*  Nickname entry found is for diff. subject.  */			subjMap->pNickname = WrongEntry;		    }		}	    }	} else {	    subjMap->pNickname = NoNickname;	}	if (subjectEntry->emailAddr) {	    /* Subject should have an smime entry, so create a link. */	    for (mElem = PR_LIST_HEAD(&dbArray->smime.link);	         mElem != &dbArray->smime.link; mElem = PR_NEXT_LINK(mElem)) {		/*  Look for subject's email in S/MIME entries.  */		smimeNode = LISTNODE_CAST(mElem);		smimeEntry = (certDBEntrySMime *)&smimeNode->entry;		smimeMap = (certDBEntryMap *)smimeNode->appData;		if (PL_strcmp(subjectEntry->emailAddr, 		              smimeEntry->emailAddr) == 0) {		    /*  Found a S/MIME entry for subject's email.  */		    if (SECITEM_ItemsAreEqual(&subjectEntry->derSubject,		                              &smimeEntry->subjectName)) {			/*  S/MIME entry and subject match.  */			subjMap->pSMime = smimeNode;			smimeMap->pSubject = subjNode;		    } else {			/*  S/MIME entry found is for diff. subject.  */			subjMap->pSMime = WrongEntry;		    }		}	    }	} else {	    subjMap->pSMime = NoSMime;	}    }    return SECSuccess;}voidprintnode(dbDebugInfo *info, const char *str, int num){    if (!info->dograph)	return;    if (num < 0) {	PR_fprintf(info->graphfile, str);    } else {	PR_fprintf(info->graphfile, str, num);    }}PRBoolmap_handle_is_ok(dbDebugInfo *info, void *mapPtr, int indent){    if (mapPtr == NULL) {	if (indent > 0)	    printnode(info, "                ", -1);	if (indent >= 0)	    printnode(info, "******************* ", -1);	return PR_FALSE;    } else if (mapPtr == WrongEntry) {	if (indent > 0)	    printnode(info, "                  ", -1);	if (indent >= 0)	    printnode(info, "??????????????????? ", -1);	return PR_FALSE;    } else {	return PR_TRUE;    }}/* these call each other */void print_smime_graph(dbDebugInfo *info, certDBEntryMap *smimeMap,                        int direction);void print_nickname_graph(dbDebugInfo *info, certDBEntryMap *nickMap,                           int direction);void print_subject_graph(dbDebugInfo *info, certDBSubjectEntryMap *subjMap,                          int direction, int optindex, int opttype);void print_cert_graph(dbDebugInfo *info, certDBEntryMap *certMap,                       int direction);/* Given an smime entry, print its unique identifier.  If GOLEFT is  * specified, print the cert<-subject<-smime map, else just print * the smime entry. */voidprint_smime_graph(dbDebugInfo *info, certDBEntryMap *smimeMap, int direction){    certDBSubjectEntryMap *subjMap;    certDBEntryListNode *subjNode;    if (direction == GOLEFT) {	/* Need to output subject and cert first, see print_subject_graph */	subjNode = smimeMap->pSubject;	if (map_handle_is_ok(info, (void *)subjNode, 1)) {	    subjMap = (certDBSubjectEntryMap *)subjNode->appData; 	    print_subject_graph(info, subjMap, GOLEFT,	                        smimeMap->index, certDBEntryTypeSMimeProfile);	} else {	    printnode(info, "<---- S/MIME   %5d   ", smimeMap->index);	}    } else {	printnode(info, "S/MIME   %5d   ", smimeMap->index);    }}/* Given a nickname entry, print its unique identifier.  If GOLEFT is  * specified, print the cert<-subject<-nickname map, else just print * the nickname entry. */voidprint_nickname_graph(dbDebugInfo *info, certDBEntryMap *nickMap, int direction){    certDBSubjectEntryMap *subjMap;    certDBEntryListNode *subjNode;    if (direction == GOLEFT) {	/* Need to output subject and cert first, see print_subject_graph */	subjNode = nickMap->pSubject;	if (map_handle_is_ok(info, (void *)subjNode, 1)) {	    subjMap = (certDBSubjectEntryMap *)subjNode->appData;	    print_subject_graph(info, subjMap, GOLEFT,	                        nickMap->index, certDBEntryTypeNickname);	} else {	    printnode(info, "<---- Nickname %5d   ", nickMap->index);	}    } else {	printnode(info, "Nickname %5d   ", nickMap->index);    }}/* Given a subject entry, if going right print the graph of the nickname|smime * that it maps to (by its unique identifier); and if going left * print the list of certs that it points to. */voidprint_subject_graph(dbDebugInfo *info, certDBSubjectEntryMap *subjMap,                     int direction, int optindex, int opttype){    certDBEntryMap *map;    certDBEntryListNode *node;    int i;    /* The first line of output always contains the cert id, subject id,     * and nickname|smime id.  Subsequent lines may contain additional     * cert id's for the subject if going left or both directions.     * Ex. of printing the graph for a subject entry:     * Cert 3 <- Subject 5 -> Nickname 32     * Cert 8 /     * Cert 9 /     * means subject 5 has 3 certs, 3, 8, and 9, and corresponds     * to nickname entry 32.     * To accomplish the above, it is required to dump the entire first     * line left-to-right, regardless of the input direction, and then     * finish up any remaining cert entries.  Hence the code is uglier     * than one may expect.     */    if (direction == GOLEFT || direction == GOBOTH) {	/* In this case, nothing should be output until the first cert is	 * located and output (cert 3 in the above example).	 */	if (subjMap->numCerts == 0 || subjMap->pCerts == NULL)	    /* XXX uh-oh */	    return;	/* get the first cert and dump it. */	node = subjMap->pCerts[0];	if (map_handle_is_ok(info, (void *)node, 0)) {	    map = (certDBEntryMap *)node->appData;	    /* going left here stops. */	    print_cert_graph(info, map, GOLEFT); 	}	/* Now it is safe to output the subject id. */	if (direction == GOLEFT)	    printnode(info, "Subject  %5d <---- ", subjMap->index);	else /* direction == GOBOTH */	    printnode(info, "Subject  %5d ----> ", subjMap->index);    }    if (direction == GORIGHT || direction == GOBOTH) { 	/* Okay, now output the nickname|smime for this subject. */	if (direction != GOBOTH) /* handled above */	   printnode(info, "Subject  %5d ----> ", subjMap->index);	if (subjMap->pNickname) {	    node = subjMap->pNickname;	    if (map_handle_is_ok(info, (void *)node, 0)) {		map = (certDBEntryMap *)node->appData;		/* going right here stops. */		print_nickname_graph(info, map, GORIGHT);	    }	}	if (subjMap->pSMime) {	    node = subjMap->pSMime;	    if (map_handle_is_ok(info, (void *)node, 0)) {		map = (certDBEntryMap *)node->appData;		/* going right here stops. */		print_smime_graph(info, map, GORIGHT); 	    }	}	if (!subjMap->pNickname && !subjMap->pSMime) {	    printnode(info, "******************* ", -1);	}    }    if (direction != GORIGHT) { /* going right has only one cert */	if (opttype == certDBEntryTypeNickname)	    printnode(info, "Nickname %5d   ", optindex);	else if (opttype == certDBEntryTypeSMimeProfile)	    printnode(info, "S/MIME   %5d   ", optindex);	for (i=1 /* 1st one already done */; i<subjMap->numCerts; i++) {	    printnode(info, "\n", -1); /* start a new line */	    node = subjMap->pCerts[i];	    if (map_handle_is_ok(info, (void *)node, 0)) {		map = (certDBEntryMap *)node->appData;		/* going left here stops. */		print_cert_graph(info, map, GOLEFT); 		printnode(info, "/", -1);	    }	}    }}/* Given a cert entry, print its unique identifer.  If GORIGHT is specified, * print the cert->subject->nickname|smime map, else just print * the cert entry. */voidprint_cert_graph(dbDebugInfo *info, certDBEntryMap *certMap, int direction){    certDBSubjectEntryMap *subjMap;    certDBEntryListNode *subjNode;    if (direction == GOLEFT) {	printnode(info, "Cert     %5d <---- ", certMap->index);	/* only want cert entry, terminate here. */	return;    }    /* Keep going right then. */    printnode(info, "Cert     %5d ----> ", certMap->index);    subjNode = certMap->pSubject;    if (map_handle_is_ok(info, (void *)subjNode, 0)) {	subjMap = (certDBSubjectEntryMap *)subjNode->appData;	print_subject_graph(info, subjMap, GORIGHT, -1, -1);    }}SECStatuscomputeDBGraph(certDBArray *dbArray, dbDebugInfo *info){    PRCList *cElem, *sElem, *nElem, *mElem;    certDBEntryListNode *node;    certDBEntryMap *map;    certDBSubjectEntryMap *subjMap;    /* Graph is of this form:     *     * certs:     * cert ---> subject ---> (nickname|smime)     *     * subjects:     * cert <--- subject ---> (nickname|smime)     *     * nicknames and smime:     * cert <--- subject <--- (nickname|smime)     */    /* Print cert graph. */    for (cElem = PR_LIST_HEAD(&dbArray->certs.link);         cElem != &dbArray->certs.link; cElem = PR_NEXT_LINK(cElem)) {	/* Print graph of everything to right of cert entry. */	node = LISTNODE_CAST(cElem);	map = (certDBEntryMap *)node->appData;	print_cert_graph(info, map, GORIGHT);	printnode(info, "\n", -1);    }    printnode(info, "\n", -1);    /* Print subject graph. */    for (sElem = PR_LIST_HEAD(&dbArray->subjects.link);         sElem != &dbArray->subjects.link; sElem = PR_NEXT_LINK(sElem)) {	/* Print graph of everything to both sides of subject entry. */	node = LISTNODE_CAST(sElem);	subjMap = (certDBSubjectEntryMap *)node->appData;	print_subject_graph(info, subjMap, GOBOTH, -1, -1);	printnode(info, "\n", -1);    }    printnode(info, "\n", -1);    /* Print nickname graph. */    for (nElem = PR_LIST_HEAD(&dbArray->nicknames.link);         nElem != &dbArray->nicknames.link; nElem = PR_NEXT_LINK(nElem)) {	/* Print graph of everything to left of nickname entry. */	node = LISTNODE_CAST(nElem);	map = (certDBEntryMap *)node->appData;	print_nickname_graph(info, map, GOLEFT);	printnode(info, "\n", -1);    }    printnode(info, "\n", -1);    /* Print smime graph. */    for (mElem = PR_LIST_HEAD(&dbArray->smime.link);         mElem != &dbArray->smime.link; mElem = PR_NEXT_LINK(mElem)) {	/* Print graph of everything to left of smime entry. */	node = LISTNODE_CAST(mElem);	if (node == NULL) break;	map = (certDBEntryMap *)node->appData;	print_smime_graph(info, map, GOLEFT);	printnode(info, "\n", -1);    }    printnode(info, "\n", -1);    return SECSuccess;}/* * List the entries in the db, showing handles between entry types. */voidverboseOutput(certDBArray *dbArray, dbDebugInfo *info){

⌨️ 快捷键说明

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