main()
main(int argc, char **argv)
main(){    int                    length = 500;    int                    remaining = 500;    int                    n;    int                    fields = 3;    int                    i;    int                    serial;    int                    chainLen;    int                    which_key;    char                   *pos;#ifdef OFFLINE    char                   *form_output = "key=MIIBPTCBpzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA7SLqjWBL9Wl11Vlg%0AaMqZCvcQOL%2FnvSqYPPRP0XZy9SoAeyWzQnBOiCm2t8H5mK7r2jnKdAQOmfhjaJil%0A3hNVu3SekHOXF6Ze7bkWa6%2FSGVcY%2FojkydxFSgY43nd1iydzPQDp8WWLL%2BpVpt%2B%2B%0ATRhFtVXbF0fQI03j9h3BoTgP2lkCAwEAARYDZm9vMA0GCSqGSIb3DQEBBAUAA4GB%0AAJ8UfRKJ0GtG%2B%2BufCC6tAfTzKrq3CTBHnom55EyXcsAsv6WbDqI%2F0rLAPkn2Xo1r%0AnNhtMxIuj441blMt%2Fa3AGLOy5zmC7Qawt8IytvQikQ1XTpTBCXevytrmLjCmlURr%0ANJryTM48WaMQHiMiJpbXCqVJC1d%2FpEWBtqvALzZaOOIy&subject=CN%3D%22test%22%26serial-auto%3Dtrue%26serial_value%3D%26ver-1%3Dtrue%26ver-3%3Dfalse%26caChoiceradio-SignWithDefaultkey%3Dtrue%26caChoiceradio-SignWithRandomChain%3Dfalse%26autoCAs%3D%26caChoiceradio-SignWithSpecifiedChain%3Dfalse%26manCAs%3D%26%24";#else    char                   *form_output;#endif    char                   *issuerNameStr;    char                   *certName;    char                   *DBdir = DB_DIRECTORY;    char                   *prefixs[10] = {"CA#1-", "CA#2-", "CA#3-", 					   "CA#4-", "CA#5-", "CA#6-", 					   "CA#7-", "CA#8-", "CA#9-", ""};    Pair                   *form_data;    CERTCertificate        *cert;    CERTCertDBHandle       *handle;    CERTCertificateRequest *certReq = NULL;    int                    warpmonths = 0;    SECItem                *certDER;#ifdef FILEOUT    FILE                   *outfile;#endif    SECStatus              status = SECSuccess;    extern                 char prefix[PREFIX_LEN];    SEC_PKCS7ContentInfo   *certChain;    SECItem                *encodedCertChain;    PRBool                 UChain = PR_FALSE;#ifdef TEST    sleep(20);#endif    RNG_SystemInfoForRNG();    SECU_ConfigDirectory(DBdir);    PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);    PK11_SetPasswordFunc(return_dbpasswd);    NSS_InitReadWrite(DBdir);    handle = CERT_GetDefaultCertDB();    prefix[0]= '\0';#if !defined(OFFLINE)    form_output = (char*) PORT_Alloc(length);    if (form_output == NULL) {	error_allocate();    }    pos = form_output;    while (feof(stdin) == 0 ) {	if (remaining <= 1) {	    remaining += length;	    length = length * 2;	    form_output = PORT_Realloc(form_output, (length));	    if (form_output == NULL) {		error_allocate();	    }	    pos = form_output + length - remaining;	}	n = fread(pos, sizeof(char), (size_t) (remaining - 1), stdin);	pos += n;	remaining -= n;    }    *pos = '&';    pos++;    length = pos - form_output;#else    length = PORT_Strlen(form_output);#endif#ifdef FILEOUT    printf("Content-type: text/plain\n\n");    fwrite(form_output, sizeof(char), (size_t)length, stdout);    printf("\n");#endif#ifdef FILEOUT    fwrite(form_output, sizeof(char), (size_t)length, stdout);    printf("\n");    fflush(stdout);#endif    form_data = make_datastruct(form_output, length);    status = clean_input(form_data);#if !defined(OFFLINE)    PORT_Free(form_output);#endif#ifdef FILEOUT    i = 0;    while(return_name(form_data, i) != NULL) {        printf("%s",return_name(form_data,i));        printf("=\n");        printf("%s",return_data(form_data,i));        printf("\n");	i++;    }    printf("I got that done, woo hoo\n");    fflush(stdout);#endif    issuerNameStr = PORT_Alloc(35 * sizeof(char));    if (find_field_bool(form_data, "caChoiceradio-SignWithSpecifiedChain",			PR_FALSE)) {	UChain = PR_TRUE;	chainLen = atoi(find_field(form_data, "manCAs", PR_FALSE));	PORT_Strcpy(prefix, prefixs[0]);	issuerNameStr = PORT_Strcpy(issuerNameStr,			       "CN=Cert-O-Matic II, O=Cert-O-Matic II");	if (chainLen == 0) {	    UChain =  PR_FALSE;	}    } else {	if (find_field_bool(form_data, "caChoiceradio-SignWithRandomChain", 			    PR_FALSE)) {	    PORT_Strcpy(prefix,prefixs[9]);	    chainLen = atoi(find_field(form_data, "autoCAs", PR_FALSE));	    if (chainLen < 1 || chainLen > 18) {		issuerNameStr = PORT_Strcpy(issuerNameStr, 				       "CN=CA18, O=Cert-O-Matic II");	    }	    issuerNameStr = PORT_Strcpy(issuerNameStr, "CN=CA");	    issuerNameStr = PORT_Strcat(issuerNameStr, 				   find_field(form_data,"autoCAs", PR_FALSE));	    issuerNameStr = PORT_Strcat(issuerNameStr,", O=Cert-O-Matic II");	} else {	    issuerNameStr = PORT_Strcpy(issuerNameStr, 				   "CN=Cert-O-Matic II, O=Cert-O-Matic II");	}	chainLen = 0;    }    i = -1;    which_key = 0;    do {    	extern SECStatus cert_GetKeyID(CERTCertificate *cert);	i++;	if (i != 0 && UChain) {	    PORT_Strcpy(prefix, prefixs[i]);	}	/*        find_field(form_data,"subject", PR_TRUE); */	certReq = makeCertReq(form_data, which_key);#ifdef OFFLINE	serial = 900;#else	serial = get_serial_number(form_data);#endif	cert = MakeV1Cert(handle, certReq, issuerNameStr, PR_FALSE, 			  serial, warpmonths, form_data);	if (certReq != NULL) {	    CERT_DestroyCertificateRequest(certReq);	}	if (find_field_bool(form_data,"ver-3", PR_TRUE)) {	    status = add_extensions(cert, form_data, issuerNameStr, handle);	    if (status != SECSuccess) {		error_out("ERROR: Unable to add extensions");	    }	}	status = cert_GetKeyID(cert);	if (status == SECFailure) {	    error_out("ERROR: Unable to get Key ID.");	}	certDER = SignCert(cert, issuerNameStr, form_data, handle, which_key);	CERT_NewTempCertificate(handle, certDER, NULL, PR_FALSE, PR_TRUE);	issuerNameStr = find_field(form_data, "subject", PR_TRUE);	/*        SECITEM_FreeItem(certDER, PR_TRUE); */	CERT_DestroyCertificate(cert);	if (i == (chainLen - 1)) {	    i = 8;	}	++which_key;    } while (i < 9 && UChain);#ifdef FILEOUT    outfile = fopen("../certout", "wb");#endif    certName = find_field(form_data, "subject", PR_FALSE);    cert = CERT_FindCertByNameString(handle, certName);    certChain = SEC_PKCS7CreateCertsOnly (cert, PR_TRUE, handle);    if (certChain == NULL) {	error_out("ERROR: No certificates in cert chain");    }    encodedCertChain = SEC_PKCS7EncodeItem (NULL, NULL, certChain, NULL, NULL, 					    NULL);    if (encodedCertChain) {#if !defined(FILEOUT)	printf("Content-type: application/x-x509-user-cert\n\n");	fwrite (encodedCertChain->data, 1, encodedCertChain->len, stdout);#else	fwrite (encodedCertChain->data, 1, encodedCertChain->len, outfile);#endif    } else {	error_out("Error: Unable to DER encode certificate");    }#ifdef FILEOUT    printf("\nI got here!\n");    fflush(outfile);    fclose(outfile);#endif    fflush(stdout);    return;}