Esempio n. 1
0
/*
 * Decrypt a feeCipherFile obtained via feeCFileNewFromDataRep().
 * recvPrivKey is required in all cases. If sendPubKey is present,
 * sendPubKey - rather than the embedded sender's public key - will be
 * used for signature validation.
 */
feeReturn decryptCipherFile(feeCipherFile cipherFile,
	feePubKey recvPrivKey,			// required
	feePubKey sendPubKey,			// optional, for signature
	unsigned char **plainText,		// malloc'd & RETURNED
	unsigned *plainTextLen,			// RETURNED
	feeSigStatus *sigStatus)		// RETURNED
{
	cipherFileEncrType 	encrType = feeCFileEncrType(cipherFile);
	feeReturn 		frtn;

	*plainText = NULL;
	*plainTextLen = 0;

	/*
	 * Dispatch to encrType-specific code.
	 */
	switch(encrType) {
	    case CFE_RandDES:
	    	frtn = decryptRandDES(cipherFile,
			recvPrivKey,
			sendPubKey,
			plainText,
			plainTextLen,
			sigStatus);
		break;
	    case CFE_PublicDES:
	    	frtn = decryptPubDES(cipherFile,
			recvPrivKey,
			sendPubKey,
			plainText,
			plainTextLen,
			sigStatus);
		break;
	    case CFE_FEED:
	    	frtn = decryptFEED(cipherFile,
			recvPrivKey,
			sendPubKey,
			plainText,
			plainTextLen,
			sigStatus);
		break;
	    case CFE_FEEDExp:
	    	frtn = decryptFEEDExp(cipherFile,
			recvPrivKey,
			sendPubKey,
			plainText,
			plainTextLen,
			sigStatus);
		break;
	    default:
	    	frtn = FR_Unimplemented;
		break;
	}
	return frtn;
}
Esempio n. 2
0
/*
 * Parse a cipherfile.
 *
 * sendPubKey only needed for cipherFileEncrType CFE_RandDES if signature
 * is present. If sendPubKey is present, it will be used for signature
 * validation rather than the embedded sender's public key.
 */
feeReturn parseCipherFile(feePubKey recvPrivKey,
	feePubKey sendPubKey,
	const unsigned char *cipherFileData,
	unsigned cipherFileDataLen,
	int doDec64,					// 1 ==> perform dec64
	cipherFileEncrType *encrType,	// RETURNED
	unsigned char **plainText,		// RETURNED
	unsigned *plainTextLen,			// RETURNED
	feeSigStatus *sigStatus,		// RETURNED
	unsigned *userData)				// RETURNED
{
	feeReturn 		frtn;
	unsigned char		*cipherData = NULL;
	unsigned		cipherDataLen;
	int			freeCipherData = 0;
	feeCipherFile		cipherFile = NULL;

	*plainText = NULL;
	*plainTextLen = 0;

	if(recvPrivKey == NULL) {	// always required
		frtn = FR_BadPubKey;
		goto out;
	}

	/*
	 * First, optional dec64()
	 */
	if(doDec64) {
		cipherData = dec64(cipherFileData,
			cipherFileDataLen,
			&cipherDataLen);
		if(cipherData == NULL) {
			frtn = FR_BadEnc64;
			goto out;
		}
		else {
			freeCipherData = 1;
		}
	}
	else {
		cipherData = (unsigned char *)cipherFileData;
		cipherDataLen = cipherFileDataLen;
	}

	/*
	 * Cons up a feeCipherFile object.
	 */
	frtn = feeCFileNewFromDataRep(cipherData,
		cipherDataLen,
		&cipherFile);
	if(frtn) {
		goto out;
	}
	*encrType = feeCFileEncrType(cipherFile);
	*userData = feeCFileUserData(cipherFile);
	frtn = decryptCipherFile(cipherFile,
		recvPrivKey,
		sendPubKey,
		plainText,
		plainTextLen,
		sigStatus);

out:
	/* free stuff */

	if(cipherData && freeCipherData) {
		ffree(cipherData);
	}
	if(cipherFile) {
		feeCFileFree(cipherFile);
	}
	return frtn;
}
feeReturn decryptFEEDExp(feeCipherFile cipherFile,
	feePubKey recvPrivKey,
	feePubKey sendPubKey,				// optional
	unsigned char **plainText,			// RETURNED
	unsigned *plainTextLen,				// RETURNED
	feeSigStatus *sigStatus)			// RETURNED
{
	feeReturn 	frtn = FR_Success;
	unsigned char	*cipherText = NULL;
	unsigned	cipherTextLen;
	feeFEEDExp	feed = NULL;
	unsigned char	*sigData = NULL;
	unsigned	sigDataLen;
	unsigned char	*sendPubKeyStr = NULL;
	unsigned	sendPubKeyStrLen = 0;
	feePubKey 	parsedSendPubKey = NULL;

	if(feeCFileEncrType(cipherFile) != CFE_FEEDExp) {
		frtn = FR_Internal;
		goto out;
	}

	/*
	 * Get ciphertext from cipherFile
	 */
	cipherText = feeCFileCipherText(cipherFile, &cipherTextLen);
	if(cipherText == NULL) {
		frtn = FR_BadCipherFile;
		goto out;
	}

	/*
	 * FEEDExp decrypt
	 */
	feed = feeFEEDExpNewWithPubKey(recvPrivKey, NULL, NULL);
	if(feed == NULL) {
		frtn = FR_BadPubKey;
		goto out;
	}
	frtn = feeFEEDExpDecrypt(feed,
		cipherText,
		cipherTextLen,
		plainText,
		plainTextLen);
	if(frtn) {
		goto out;
	}

	sigData = feeCFileSigData(cipherFile, &sigDataLen);
	if(sigData) {
		feeReturn sigFrtn;

		if(sendPubKey == NULL) {
			/*
			 * use embedded sender's public key
			 */
			sendPubKeyStr = feeCFileSendPubKeyData(cipherFile,
				&sendPubKeyStrLen);
			if(sendPubKeyStr == NULL) {
				frtn = FR_BadCipherFile;
				goto out;
			}
			parsedSendPubKey = feePubKeyAlloc();
			frtn = feePubKeyInitFromKeyString(parsedSendPubKey,
				(char *)sendPubKeyStr, sendPubKeyStrLen);
			if(frtn) {
				frtn = FR_BadCipherFile;
				goto out;
			}
			sendPubKey = parsedSendPubKey;
		}
		sigFrtn = feePubKeyVerifySignature(sendPubKey,
			cipherText,
			cipherTextLen,
			sigData,
			sigDataLen);
		switch(sigFrtn) {
		    case FR_Success:
		    	*sigStatus = SS_PresentValid;
			break;
		    default:
		    	*sigStatus = SS_PresentInvalid;
			break;
		}
	}
	else {
		*sigStatus = SS_NotPresent;
	}
out:
	if(cipherText) {
		ffree(cipherText);
	}
	if(feed) {
		feeFEEDExpFree(feed);
	}
	if(sigData) {
		ffree(sigData);
	}
	if(parsedSendPubKey) {
		feePubKeyFree(parsedSendPubKey);
	}
	if(sendPubKeyStr) {
		ffree(sendPubKeyStr);
	}
	return frtn;
}