if (cms_verbose) { fprintf(stderr, "Found certificate for %s\n", signOptions->nickname); }
signed_data(struct signOptionsStr signOptions){    NSSCMSMessage *cmsg = NULL;    NSSCMSContentInfo *cinfo;    NSSCMSSignedData *sigd;    NSSCMSSignerInfo *signerinfo;    CERTCertificate *cert, *ekpcert;#ifdef DEBUG    fprintf(stderr, "Input to signed_data:\n");    if (signOptions.options->password)	fprintf(stderr, "password [%s]\n", signOptions.options->password);    else	fprintf(stderr, "password [NULL]\n");    fprintf(stderr, "certUsage [%d]\n", signOptions.options->certUsage);    if (signOptions.options->certHandle)	fprintf(stderr, "certdb [%x]\n", signOptions.options->certHandle);    else	fprintf(stderr, "certdb [NULL]\n");    if (signOptions.nickname)	fprintf(stderr, "nickname [%s]\n", signOptions.nickname);    else	fprintf(stderr, "nickname [NULL]\n");#endif    if (signOptions.nickname == NULL) {	fprintf(stderr,         "ERROR: please indicate the nickname of a certificate to sign with.\n");	return NULL;    }    if ((cert = CERT_FindCertByNickname(signOptions.options->certHandle,                                         signOptions.nickname)) == NULL) {	SECU_PrintError(progName, 	                "the corresponding cert for key \"%s\" does not exist",	                signOptions.nickname);	return NULL;    }#ifdef DEBUG    fprintf(stderr, "Found certificate for %s\n", signOptions.nickname);#endif    /*     * create the message object     */    cmsg = NSS_CMSMessage_Create(NULL); /* create a message on its own pool */    if (cmsg == NULL) {	fprintf(stderr, "ERROR: cannot create CMS message.\n");	return NULL;    }    /*     * build chain of objects: message->signedData->data     */    if ((sigd = NSS_CMSSignedData_Create(cmsg)) == NULL) {	fprintf(stderr, "ERROR: cannot create CMS signedData object.\n");	goto loser;    }    cinfo = NSS_CMSMessage_GetContentInfo(cmsg);    if (NSS_CMSContentInfo_SetContent_SignedData(cmsg, cinfo, sigd)           != SECSuccess) {	fprintf(stderr, "ERROR: cannot attach CMS signedData object.\n");	goto loser;    }    cinfo = NSS_CMSSignedData_GetContentInfo(sigd);    /* we're always passing data in and detaching optionally */    if (NSS_CMSContentInfo_SetContent_Data(cmsg, cinfo, NULL,                                            signOptions.detached)           != SECSuccess) {	fprintf(stderr, "ERROR: cannot attach CMS data object.\n");	goto loser;    }    /*      * create & attach signer information     */    if ((signerinfo = NSS_CMSSignerInfo_Create(cmsg, cert, SEC_OID_SHA1))           == NULL) {	fprintf(stderr, "ERROR: cannot create CMS signerInfo object.\n");	goto loser;    }#ifdef DEBUG    fprintf(stderr, "Created CMS message, added signed data w/ signerinfo\n");#endif    /* we want the cert chain included for this one */    if (NSS_CMSSignerInfo_IncludeCerts(signerinfo, NSSCMSCM_CertChain,                                        signOptions.options->certUsage)           != SECSuccess) {	fprintf(stderr, "ERROR: cannot find cert chain.\n");	goto loser;    }#ifdef DEBUG    fprintf(stderr, "imported certificate\n");#endif    if (signOptions.signingTime) {	if (NSS_CMSSignerInfo_AddSigningTime(signerinfo, PR_Now()) 	      != SECSuccess) {	    fprintf(stderr, "ERROR: cannot add signingTime attribute.\n");	    goto loser;	}    }    if (signOptions.smimeProfile) {	if (NSS_CMSSignerInfo_AddSMIMECaps(signerinfo) != SECSuccess) {	    fprintf(stderr, "ERROR: cannot add SMIMECaps attribute.\n");	    goto loser;	}    }    if (signOptions.encryptionKeyPreferenceNick) {	/* get the cert, add it to the message */	if ((ekpcert = CERT_FindCertByNickname(signOptions.options->certHandle, 	                               signOptions.encryptionKeyPreferenceNick))	      == NULL) {	    SECU_PrintError(progName, 	                 "the corresponding cert for key \"%s\" does not exist",	                    signOptions.encryptionKeyPreferenceNick);	    goto loser;	}	if (NSS_CMSSignerInfo_AddSMIMEEncKeyPrefs(signerinfo, ekpcert, 	                                        signOptions.options->certHandle)	      != SECSuccess) {	    fprintf(stderr, "ERROR: cannot add SMIMEEncKeyPrefs attribute.\n");	    goto loser;	}	if (NSS_CMSSignedData_AddCertificate(sigd, ekpcert) != SECSuccess) {	    fprintf(stderr, "ERROR: cannot add encryption certificate.\n");	    goto loser;	}    } else {	/* check signing cert for fitness as encryption cert */	/* if yes, add signing cert as EncryptionKeyPreference */	if (NSS_CMSSignerInfo_AddSMIMEEncKeyPrefs(signerinfo, cert, 	                                        signOptions.options->certHandle)	      != SECSuccess) {	    fprintf(stderr, 	            "ERROR: cannot add default SMIMEEncKeyPrefs attribute.\n");	    goto loser;	}    }    if (NSS_CMSSignedData_AddSignerInfo(sigd, signerinfo) != SECSuccess) {	fprintf(stderr, "ERROR: cannot add CMS signerInfo object.\n");	goto loser;    }#ifdef DEBUG    fprintf(stderr, "created signed-date message\n");#endif    return cmsg;loser:    NSS_CMSMessage_Destroy(cmsg);    return NULL;}