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

📄 certutil.c

📁 支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509v3证书等安全协议或标准的开发库编译用到NSPR
💻 C
📖 第 1 页 / 共 5 页
字号:
	scanf ("%d", &intValue);	if (intValue >= 0 && intValue <8) {	    current->reasons.data = PORT_ArenaAlloc (arena, sizeof(char));	    if (current->reasons.data == NULL) {		GEN_BREAK (SECFailure);	    }	    *current->reasons.data = (char)(0x80 >> intValue);	    current->reasons.len = 1;	}	puts ("Enter value for the CRL Issuer name:\n");        current->crlIssuer = GetGeneralName (arena);	if (current->crlIssuer == NULL && (rv = PORT_GetError()) == SECFailure)	    break;	if (crlDistPoints == NULL) {	    crlDistPoints = PORT_ArenaZAlloc (arena, sizeof (*crlDistPoints));	    if (crlDistPoints == NULL) {		GEN_BREAK (SECFailure);	    }	}	    	crlDistPoints->distPoints = PORT_ArenaGrow (arena, 	     crlDistPoints->distPoints,	     sizeof (*crlDistPoints->distPoints) * count,	     sizeof (*crlDistPoints->distPoints) *(count + 1));	if (crlDistPoints->distPoints == NULL) {	    GEN_BREAK (SECFailure);	}		crlDistPoints->distPoints[count] = current;	++count;	if (GetYesNo ("Enter more value for the CRL distribution point extension [y/n]\n") == 0) {	    /* Add null to the end of the crlDistPoints to mark end of data */	    crlDistPoints->distPoints = PORT_ArenaGrow(arena, 		 crlDistPoints->distPoints,		 sizeof (*crlDistPoints->distPoints) * count,		 sizeof (*crlDistPoints->distPoints) *(count + 1));	    crlDistPoints->distPoints[count] = NULL;	    	    break;	}	    } while (1);        if (rv == SECSuccess) {	buffer[0] = 'n';	puts ("Is this a critical extension [y/n]? ");	gets (buffer);			rv = EncodeAndAddExtensionValue(arena, extHandle, crlDistPoints,	      (buffer[0] == 'Y' || buffer[0] == 'y') ? PR_TRUE : PR_FALSE,	      SEC_OID_X509_CRL_DIST_POINTS,	      (EXTEN_VALUE_ENCODER)  CERT_EncodeCRLDistributionPoints);    }    if (arena)	PORT_FreeArena (arena, PR_FALSE);    return (rv);}static SECStatusCreateCert(	CERTCertDBHandle *handle, 	char *  issuerNickName, 	PRFileDesc *inFile,	PRFileDesc *outFile, 	SECKEYPrivateKey *selfsignprivkey,	void 	*pwarg,	int     serialNumber, 	int     warpmonths,	int     validitylength,	PRBool  selfsign,	PRBool	keyUsage, 	PRBool  extKeyUsage,	PRBool  basicConstraint, 	PRBool  authKeyID,	PRBool  crlDistPoints, 	PRBool  nscpCertType){    void *	extHandle;    SECItem *	certDER;    PRArenaPool *arena			= NULL;    CERTCertificate *subjectCert 	= NULL;    /*CERTCertificate *issuerCert 	= NULL;*/    CERTCertificateRequest *certReq	= NULL;    SECStatus 	rv 			= SECSuccess;    SECItem 	reqDER;    reqDER.data = NULL;    do {	arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);	if (!arena) {	    GEN_BREAK (SEC_ERROR_NO_MEMORY);	}		/* Create a certrequest object from the input cert request der */	certReq = GetCertRequest(inFile);	if (certReq == NULL) {	    GEN_BREAK (SECFailure)	}	subjectCert = MakeV1Cert (handle, certReq, issuerNickName, selfsign,				  serialNumber, warpmonths, validitylength);	if (subjectCert == NULL) {	    GEN_BREAK (SECFailure)	}	extHandle = CERT_StartCertExtensions (subjectCert);	if (extHandle == NULL) {	    GEN_BREAK (SECFailure)	}	/* Add key usage extension */	if (keyUsage) {	    rv = AddKeyUsage(extHandle);	    if (rv)		break;	}	/* Add extended key usage extension */	if (extKeyUsage) {	    rv = AddExtKeyUsage(extHandle);	    if (rv)		break;	}	/* Add basic constraint extension */	if (basicConstraint) {	    rv = AddBasicConstraint(extHandle);	    if (rv)		break;	}	if (authKeyID) {	    rv = AddAuthKeyID (extHandle);	    if (rv)		break;	}    	if (crlDistPoints) {	    rv = AddCrlDistPoint (extHandle);	    if (rv)		break;	}		if (nscpCertType) {	    rv = AddNscpCertType(extHandle);	    if (rv)		break;	}       	CERT_FinishExtensions(extHandle);	certDER = SignCert (handle, subjectCert, selfsign, selfsignprivkey, issuerNickName,pwarg);	if (certDER)	   PR_Write(outFile, certDER->data, certDER->len);   	   /*fwrite (certDER->data, 1, certDER->len, outFile);*/    } while (0);    CERT_DestroyCertificateRequest (certReq);    CERT_DestroyCertificate (subjectCert);    PORT_FreeArena (arena, PR_FALSE);    if (rv != SECSuccess) {	PRErrorCode  perr = PR_GetError();        fprintf(stderr, "%s: unable to create cert (%s)\n", progName,               SECU_Strerror(perr));    }    return (rv);}/*  Certutil commands  */enum {    cmd_AddCert = 0,    cmd_CreateNewCert,    cmd_DeleteCert,    cmd_AddEmailCert,    cmd_DeleteKey,    cmd_GenKeyPair,    cmd_PrintHelp,    cmd_ListKeys,    cmd_ListCerts,    cmd_ModifyCertTrust,    cmd_NewDBs,    cmd_CertReq,    cmd_CreateAndAddCert,    cmd_ListModules,    cmd_CheckCertValidity,    cmd_ChangePassword};/*  Certutil options */enum {    opt_AddKeyUsageExt = 0,    opt_AddBasicConstraintExt,    opt_AddAuthorityKeyIDExt,    opt_AddCRLDistPtsExt,    opt_AddNSCertTypeExt,    opt_AddExtKeyUsageExt,    opt_ASCIIForIO,    opt_ValidityTime,    opt_IssuerName,    opt_CertDir,    opt_VerifySig,    opt_PasswordFile,    opt_KeySize,    opt_TokenName,    opt_InputFile,    opt_KeyIndex,    opt_KeyType,    opt_DetailedInfo,    opt_SerialNumber,    opt_Nickname,    opt_OutputFile,    opt_PhoneNumber,    opt_PQGFile,    opt_BinaryDER,    opt_Subject,    opt_Trust,    opt_Usage,    opt_Validity,    opt_OffsetMonths,    opt_SelfSign,    opt_Exponent,    opt_NoiseFile};static secuCommandFlag certutil_commands[] ={	{ /* cmd_AddCert             */  'A', PR_FALSE, 0, PR_FALSE },	{ /* cmd_CreateNewCert       */  'C', PR_FALSE, 0, PR_FALSE },	{ /* cmd_DeleteCert          */  'D', PR_FALSE, 0, PR_FALSE },	{ /* cmd_AddEmailCert        */  'E', PR_FALSE, 0, PR_FALSE },	{ /* cmd_DeleteKey           */  'F', PR_FALSE, 0, PR_FALSE },	{ /* cmd_GenKeyPair          */  'G', PR_FALSE, 0, PR_FALSE },	{ /* cmd_PrintHelp           */  'H', PR_FALSE, 0, PR_FALSE },	{ /* cmd_ListKeys            */  'K', PR_FALSE, 0, PR_FALSE },	{ /* cmd_ListCerts           */  'L', PR_FALSE, 0, PR_FALSE },	{ /* cmd_ModifyCertTrust     */  'M', PR_FALSE, 0, PR_FALSE },	{ /* cmd_NewDBs              */  'N', PR_FALSE, 0, PR_FALSE },	{ /* cmd_CertReq             */  'R', PR_FALSE, 0, PR_FALSE },	{ /* cmd_CreateAndAddCert    */  'S', PR_FALSE, 0, PR_FALSE },	{ /* cmd_ListModules         */  'U', PR_FALSE, 0, PR_FALSE },	{ /* cmd_CheckCertValidity   */  'V', PR_FALSE, 0, PR_FALSE },	{ /* cmd_ChangePassword      */  'W', PR_FALSE, 0, PR_FALSE }};static secuCommandFlag certutil_options[] ={	{ /* opt_AddKeyUsageExt      */  '1', PR_FALSE, 0, PR_FALSE },	{ /* opt_AddBasicConstraintExt*/ '2', PR_FALSE, 0, PR_FALSE },	{ /* opt_AddAuthorityKeyIDExt*/  '3', PR_FALSE, 0, PR_FALSE },	{ /* opt_AddCRLDistPtsExt    */  '4', PR_FALSE, 0, PR_FALSE },	{ /* opt_AddNSCertTypeExt    */  '5', PR_FALSE, 0, PR_FALSE },	{ /* opt_AddExtKeyUsageExt   */  '6', PR_FALSE, 0, PR_FALSE },	{ /* opt_ASCIIForIO          */  'a', PR_FALSE, 0, PR_FALSE },	{ /* opt_ValidityTime        */  'b', PR_TRUE,  0, PR_FALSE },	{ /* opt_IssuerName          */  'c', PR_TRUE,  0, PR_FALSE },	{ /* opt_CertDir             */  'd', PR_TRUE,  0, PR_FALSE },	{ /* opt_VerifySig           */  'e', PR_FALSE, 0, PR_FALSE },	{ /* opt_PasswordFile        */  'f', PR_TRUE,  0, PR_FALSE },	{ /* opt_KeySize             */  'g', PR_TRUE,  0, PR_FALSE },	{ /* opt_TokenName           */  'h', PR_TRUE,  0, PR_FALSE },	{ /* opt_InputFile           */  'i', PR_TRUE,  0, PR_FALSE },	{ /* opt_KeyIndex            */  'j', PR_TRUE,  0, PR_FALSE },	{ /* opt_KeyType             */  'k', PR_TRUE,  0, PR_FALSE },	{ /* opt_DetailedInfo        */  'l', PR_FALSE, 0, PR_FALSE },	{ /* opt_SerialNumber        */  'm', PR_TRUE,  0, PR_FALSE },	{ /* opt_Nickname            */  'n', PR_TRUE,  0, PR_FALSE },	{ /* opt_OutputFile          */  'o', PR_TRUE,  0, PR_FALSE },	{ /* opt_PhoneNumber         */  'p', PR_TRUE,  0, PR_FALSE },	{ /* opt_PQGFile             */  'q', PR_TRUE,  0, PR_FALSE },	{ /* opt_BinaryDER           */  'r', PR_FALSE, 0, PR_FALSE },	{ /* opt_Subject             */  's', PR_TRUE,  0, PR_FALSE },	{ /* opt_Trust               */  't', PR_TRUE,  0, PR_FALSE },	{ /* opt_Usage               */  'u', PR_TRUE,  0, PR_FALSE },	{ /* opt_Validity            */  'v', PR_TRUE,  0, PR_FALSE },	{ /* opt_OffsetMonths        */  'w', PR_TRUE,  0, PR_FALSE },	{ /* opt_SelfSign            */  'x', PR_FALSE, 0, PR_FALSE },	{ /* opt_Exponent            */  'y', PR_TRUE,  0, PR_FALSE },	{ /* opt_NoiseFile           */  'z', PR_TRUE,  0, PR_FALSE }};int main(int argc, char **argv){    CERTCertDBHandle *certHandle;    SECKEYKeyDBHandle *keyHandle;    PK11SlotInfo *slot = NULL;    CERTName *  subject         = 0;    PRFileDesc *inFile          = 0;    PRFileDesc *outFile         = 0;    char *      certfile        = "tempcert";    char *      certreqfile     = "tempcertreq";    char *      slotname        = "internal";    KeyType     keytype         = rsaKey;    /*char *	keyslot	        = NULL;*/    /*char *      keynickname     = NULL;*/    char *      name            = NULL;    int	        keysize	        = DEFAULT_KEY_BITS;    int         publicExponent  = 0x010001;    int         serialNumber    = 0;    int         warpmonths      = 0;    int         validitylength  = 0;    int         commandsEntered = 0;    char        commandToRun    = '\0';    secuPWData  pwdata          = { PW_NONE, 0 };    SECKEYPrivateKey *privkey;    SECKEYPublicKey *pubkey = NULL;    int i;    SECStatus rv;    secuCommand certutil;    certutil.numCommands = sizeof(certutil_commands) / sizeof(secuCommandFlag);    certutil.numOptions = sizeof(certutil_options) / sizeof(secuCommandFlag);    certutil.commands = certutil_commands;    certutil.options = certutil_options;    progName = strrchr(argv[0], '/');    progName = progName ? progName+1 : argv[0];    rv = SECU_ParseCommandLine(argc, argv, progName, &certutil);    if (rv != SECSuccess)	Usage(progName);    if (certutil.commands[cmd_PrintHelp].activated)	LongUsage(progName);    if (certutil.options[opt_PasswordFile].arg) {	pwdata.source = PW_FROMFILE;	pwdata.data = certutil.options[opt_PasswordFile].arg;    }    if (certutil.options[opt_CertDir].activated)	SECU_ConfigDirectory(certutil.options[opt_CertDir].arg);    if (certutil.options[opt_KeySize].activated) {	keysize = PORT_Atoi(certutil.options[opt_KeySize].arg);	if ((keysize < MIN_KEY_BITS) || (keysize > MAX_KEY_BITS)) {	    PR_fprintf(PR_STDERR,                        "%s -g:  Keysize must be between %d and %d.\n",	               MIN_KEY_BITS, MAX_KEY_BITS);	    return -1;	}    }    /*  -h specify token name  */    if (certutil.options[opt_TokenName].activated) {	if (PL_strcmp(certutil.options[opt_TokenName].arg, "all") == 0)	    slotname = NULL;	else	    slotname = PL_strdup(certutil.options[opt_TokenName].arg);    }    /*  -k key type  */    if (certutil.options[opt_KeyType].activated) {	if (PL_strcmp(certutil.options[opt_KeyType].arg, "rsa") == 0) {	    keytype = rsaKey;	} else if (PL_strcmp(certutil.options[opt_KeyType].arg, "dsa") == 0) {	    keytype = dsaKey;	} else if (PL_strcmp(certutil.options[opt_KeyType].arg, "all") == 0) {	    keytype = nullKey;	} else {	    PR_fprintf(PR_STDERR, "%s -k:  %s is not a recognized type.\n",	               progName, certutil.options[opt_KeyType].arg);	    return -1;	}    }    /*  -m serial number */    if (certutil.options[opt_SerialNumber].activated) {	serialNumber = PORT_Atoi(certutil.options[opt_SerialNumber].arg);	if (serialNumber < 0) {	    PR_fprintf(PR_STDERR, "%s -m:  %s is not a valid serial number.\n",	               progName, certutil.options[opt_SerialNumber].arg);	    return -1;	}    }    /*  -q PQG file  */    if (certutil.options[opt_PQGFile].activated) {	if (keytype != dsaKey) {	    PR_fprintf(PR_STDERR, "%s -q: PQG file is for DSA key (-k dsa).\n)",	               progName);	    return -1;	}    }    /*  -s subject name  */    if (certutil.options[opt_Subject].activated) {	subject = CERT_AsciiToName(certutil.options[opt_Subject].arg);	if (!subject) {	    PR_fprintf(PR_STDERR, "%s -s: improperly formatted name: \"%s\"\n",	               progName, certutil.options[opt_Subject].arg);	    return -1;	}    }    /*  -v validity period  */    if (certutil.options[opt_Validity].activated) {	validitylength = PORT_Atoi(certutil.options[opt_Validity].arg);	if (validitylength < 0) {	    PR_fprintf(PR_STDERR, "%s -v: incorrect validity period: \"%s\"\n",	               progName, certutil.options[opt_Validity].arg);	    return -1;	}    }    /*  -w warp months  */    if (certutil.options[opt_OffsetMonths].activated)	warpmonths = PORT_Atoi(certutil.options[opt_OffsetMonths].arg);    /*  -y public exponent (for RSA)  */    if (certutil.options[opt_Exponent].activated) {	publicExponent = PORT_Atoi(certutil.options[opt_Exponent].arg);	if ((publicExponent != 3) &&	    (publicExponent != 17) &&	    (publicExponent != 65537)) {	    PR_fprintf(PR_STDERR, "%s -y: incorrect public exponent %d.", 	                           progName, publicExponent);	    PR_fprintf(PR_STDERR, "Must be 3, 17, or 65537.\n");	    return -1;	}    }    /*  Check number of commands entered.  */    commandsEntered = 0;    for (i=0; i< certutil.numCommands; i++) {	if (certutil.commands[i].activated) {	    commandToRun = certutil.commands[i].flag;	    commandsEntered++;	}	if (commandsEntered > 1)	    break;    }    if (commandsEntered > 1) {	PR_fprintf(PR_STDERR, "%s: only one command at a time!\n", progName);	PR_fprintf(PR_STDERR, "You entered: ");	for (i=0; i< certutil.numCommands; i++) {	    if (certutil.commands[i].activated)		PR_fprintf(PR_STDERR, " -%c", certutil.commands[i].flag);	}	PR_fprintf(PR_STDERR, "\n");	return -1;    }    if (commandsEntered == 0) {	PR_fprintf(PR_STDERR, "%s: you must enter a command!\n", progName);	Usage(progName);    }    /*  -A, -D, -F, -M, -S, -V, and all require -n  */    if ((certutil.commands[cmd_AddCe

⌨️ 快捷键说明

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