Пример #1
0
LPSTR __cdecl _pgp_decrypt_key(LPCSTR szEncMsg, LPCSTR pgpKey)
{
	#if defined(_WIN64)
		return 0;
	#else
		LPSTR szPlainMsg = 0;
		PGPSize dwPlainMsgLen;

		PGPUInt32 dwKeys;
		PGPKeyDBRef PrivateKeyDB;
		if (CheckPGPError(_pgp_import_key(&PrivateKeyDB,pgpKey)))
			return 0;
		PGPCountKeysInKeyDB(PrivateKeyDB, &dwKeys);

		if(dwKeys==0) {
			PGPFreeKeyDB(PrivateKeyDB);
			return 0;
		}

		int iTry = 0;

		do {
			if (!pszPassphrase &&
				PGPPassphraseDialog(pgpContext,
				PGPOUIOutputPassphrase(pgpContext, &pszPassphrase),
				PGPOLastOption(pgpContext)) == kPGPError_UserAbort) {
					iTry = 3;
					break;
			}

			PGPError err = PGPDecode(pgpContext,
				PGPOInputBuffer(pgpContext, szEncMsg, lstrlen(szEncMsg)),
				PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szPlainMsg, 16384, &dwPlainMsgLen),
				PGPOKeyDBRef(pgpContext, PrivateKeyDB),
				PGPOPassphrase(pgpContext, pszPassphrase),
				PGPOLastOption(pgpContext));

			if (CheckPGPError(err))
				iTry = 3;
			else
				if (!dwPlainMsgLen) {
					PGPFreeData(pszPassphrase);
					pszPassphrase = 0;
					iTry++;
				}

		} while(!dwPlainMsgLen && iTry<3);

		PGPFreeKeyDB(PrivateKeyDB);

		if(iTry == 3) return 0;

		LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwPlainMsgLen+1);
		_pgp_memcpy(szMsg, szPlainMsg, dwPlainMsgLen);
		szMsg[dwPlainMsgLen] = 0;
		PGPFreeData((LPVOID)szPlainMsg);

		return szMsg;
	#endif
}
Пример #2
0
LPSTR __cdecl _pgp_encrypt_keydb(LPCSTR szPlainMsg, PVOID pgpKeyID)
{
	#if defined(_WIN64)
		return 0;
	#else
		PGPKeyID *RemoteKeyID = (PGPKeyID *) pgpKeyID;
		LPSTR szEncMsg = 0;
		PGPSize dwEncMsgLen;

		ClearPGPError();
		if (!pgpKeyDB)
			return 0;

		PGPKeyDBObjRef PublicKey;
		PGPFindKeyByKeyID(pgpKeyDB, RemoteKeyID, &PublicKey);

		PGPError err = PGPEncode(pgpContext,
			PGPOInputBuffer(pgpContext, szPlainMsg, lstrlen(szPlainMsg)),
			PGPOArmorOutput(pgpContext, TRUE),
			PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szEncMsg, 16384, &dwEncMsgLen),
			PGPOEncryptToKeyDBObj(pgpContext, PublicKey),
			PGPOVersionString(pgpContext, szVersionStr),
			PGPOLastOption(pgpContext));

		if (CheckPGPError(err))
			return 0;

		LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwEncMsgLen+1);
		_pgp_memcpy(szMsg, szEncMsg, dwEncMsgLen);
		szMsg[dwEncMsgLen] = 0;
		PGPFreeData((LPVOID)szEncMsg);

		return szMsg;
	#endif
}
Пример #3
0
LPSTR __cdecl _pgp_decrypt_keydb(LPCSTR szEncMsg)
{
    LPSTR szPlainMsg = 0;
    DWORD dwPlainMsgLen;

	ClearPGPError();
	if(!pgpKeyDB)
		return 0;

    int iTry = 0;

    do {
		if (!pszPassphrase && 
			PGPPassphraseDialog(pgpContext,
								PGPOUIOutputPassphrase(pgpContext, &pszPassphrase),
								PGPOLastOption(pgpContext)) == kPGPError_UserAbort) {
	    	iTry = 3;
	    	break;
		}

	    PGPError err = PGPDecode(pgpContext,
	       PGPOInputBuffer(pgpContext, szEncMsg, lstrlen(szEncMsg)),
	       PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szPlainMsg, 16384, (PGPUInt32 *)&dwPlainMsgLen),
#if (PGP_WIN32 < 0x700)
		   PGPOKeySetRef(pgpContext, pgpKeyDB),
#else
	       PGPOKeyDBRef(pgpContext, pgpKeyDB),
#endif
	       PGPOPassphrase(pgpContext, pszPassphrase),
	       PGPOLastOption(pgpContext));

	    if (CheckPGPError(err))
			iTry = 3;
		else
	    if (!dwPlainMsgLen) {
			PGPFreeData(pszPassphrase);
			pszPassphrase = 0;
			iTry++;
	    }

	} while(!dwPlainMsgLen && iTry<3);

	if(iTry == 3) return 0;

    LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwPlainMsgLen+1);
    _pgp_memcpy(szMsg, szPlainMsg, dwPlainMsgLen);
    szMsg[dwPlainMsgLen] = 0;
    PGPFreeData((LPVOID)szPlainMsg);

    return szMsg;
}
Пример #4
0
LPSTR __cdecl _pgp_encrypt_key(LPCSTR szPlainMsg, LPCSTR pgpKey)
{
	#if defined(_WIN64)
		return 0;
	#else
		LPSTR szEncMsg = 0;
		PGPSize dwEncMsgLen;

		PGPUInt32 dwKeys;
		PGPKeyDBRef PublicKeyDB;
		if (CheckPGPError(_pgp_import_key(&PublicKeyDB,pgpKey)))
			return 0;

		PGPKeyIterRef KeyIterRef;
		PGPNewKeyIterFromKeyDB(PublicKeyDB, &KeyIterRef);

		PGPKeyDBObjRef PublicKey;
		PGPKeyIterNextKeyDBObj(KeyIterRef, kPGPKeyDBObjType_Key, &PublicKey);

		PGPCountKeysInKeyDB(PublicKeyDB, &dwKeys);

		if(dwKeys==0) {
			PGPFreeKeyIter(KeyIterRef);
			PGPFreeKeyDB(PublicKeyDB);
			return 0;
		}

		PGPError err = PGPEncode(pgpContext,
			PGPOInputBuffer(pgpContext, szPlainMsg, lstrlen(szPlainMsg)),
			PGPOArmorOutput(pgpContext, TRUE),
			PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szEncMsg, 16384, &dwEncMsgLen),
			PGPOEncryptToKeyDBObj(pgpContext, PublicKey),
			PGPOVersionString(pgpContext, szVersionStr),
			PGPOLastOption(pgpContext));

		PGPFreeKeyIter(KeyIterRef);
		PGPFreeKeyDB(PublicKeyDB);

		if (CheckPGPError(err))
			return 0;

		LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwEncMsgLen+1);
		_pgp_memcpy(szMsg, szEncMsg, dwEncMsgLen);
		szMsg[dwEncMsgLen] = 0;
		PGPFreeData((LPVOID)szEncMsg);

		return szMsg;
	#endif
}
Пример #5
0
LPSTR __cdecl _pgp_encrypt_keydb(LPCSTR szPlainMsg, PVOID pgpKeyID)
{
   	PGPKeyID *RemoteKeyID = (PGPKeyID *) pgpKeyID;
    LPSTR szEncMsg = 0;
    DWORD dwEncMsgLen;

	ClearPGPError();
	if(!pgpKeyDB)
		return 0;

#if (PGP_WIN32 < 0x700)
    PGPFilterRef IDFilter;
    PGPNewKeyIDFilter(pgpContext, RemoteKeyID, &IDFilter);

    PGPKeySetRef PublicKey;
    PGPFilterKeySet(pgpKeyDB, IDFilter, &PublicKey);
#else
    PGPKeyDBObjRef PublicKey;
    PGPFindKeyByKeyID(pgpKeyDB, RemoteKeyID, &PublicKey);
#endif

    PGPError err = PGPEncode(pgpContext,
      PGPOInputBuffer(pgpContext, szPlainMsg, lstrlen(szPlainMsg)),
      PGPOArmorOutput(pgpContext, TRUE),
      PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szEncMsg, 16384, (PGPUInt32 *)&dwEncMsgLen),
#if (PGP_WIN32 < 0x700)
      PGPOEncryptToKeySet(pgpContext, PublicKey),
#else
      PGPOEncryptToKeyDBObj(pgpContext, PublicKey),
#endif
      PGPOVersionString(pgpContext, szVersionStr),
      PGPOLastOption(pgpContext));

#if (PGP_WIN32 < 0x700)
	PGPFreeKeySet(PublicKey);
	PGPFreeFilter(IDFilter);
#endif

    if (CheckPGPError(err))
       return 0;

    LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwEncMsgLen+1);
    _pgp_memcpy(szMsg, szEncMsg, dwEncMsgLen);
    szMsg[dwEncMsgLen] = 0;
    PGPFreeData((LPVOID)szEncMsg);

    return szMsg;
}
Пример #6
0
PGPUInt32
CHTTPXcertServer::DigestKey(
    PGPKeyRef	inKey,
    char *		inOutputBuffer)
{
    StPGPKeySetRef		singleKeySet;
    StPGPHashContextRef	hashContext;
    StPGPDataRef		buffer;
    PGPSize				bufSize;
    StPGPDataRef		encodedBuffer;
    PGPError			pgpErr;

    pgpErr = PGPNewSingletonKeySet(inKey, &singleKeySet);
    ThrowIfPGPError_(pgpErr);

    pgpErr = PGPExportKeySet(	singleKeySet,
                                PGPOExportFormat(mContext, kPGPExportFormat_X509Cert),
                                PGPOAllocatedOutputBuffer(	mContext,
                                        (void **) &buffer,
                                        MAX_PGPSize,
                                        &bufSize),
                                PGPOLastOption(mContext));
    ThrowIfPGPError_(pgpErr);
    singleKeySet.Free();
    encodedBuffer = static_cast<PGPByte *>(
                        PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                    GetMaxBase64EncodedBufferSize(bufSize),
                                    kPGPMemoryMgrFlags_None));
    if (encodedBuffer == 0) {
        ThrowPGPError_(kPGPError_OutOfMemory);
    };
    bufSize = Base64Encode(	static_cast<const PGPByte *>(buffer),
                            bufSize,
                            encodedBuffer);
    buffer.Free();
    pgpErr = PGPNewHashContext(	PGPGetContextMemoryMgr(mContext),
                                kPGPHashAlgorithm_MD5,
                                &hashContext);
    ThrowIfPGPError_(pgpErr);

    // We have to skip the CRs in order to match XCert's hash
    char *	cur = encodedBuffer;
    char *	next;

    while ((next = strchr(cur, '\r')) != 0) {
        pgpErr = PGPContinueHash(hashContext, cur, next - cur);
        ThrowIfPGPError_(pgpErr);
        cur = next + 1;
    }
    pgpErr = PGPContinueHash(hashContext, cur, strlen(cur));
    ThrowIfPGPError_(pgpErr);
    encodedBuffer.Free();
    pgpErr = PGPGetHashSize(hashContext, &bufSize);
    buffer = static_cast<PGPByte *>(PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                    bufSize,
                                    kPGPMemoryMgrFlags_None));
    if (buffer == 0) {
        ThrowPGPError_(kPGPError_OutOfMemory);
    };
    pgpErr = PGPFinalizeHash(hashContext, static_cast<char *>(buffer));
    ThrowIfPGPError_(pgpErr);
    return HexEncode(buffer, bufSize, inOutputBuffer);
}
Пример #7
0
LPSTR __cdecl _pgp_decrypt_key(LPCSTR szEncMsg, LPCSTR pgpKey)
{
    LPSTR szPlainMsg = 0;
    DWORD dwPlainMsgLen;

    PGPUInt32 dwKeys;
#if (PGP_WIN32 < 0x700)
	PGPKeySetRef PrivateKeyDB;
    if (CheckPGPError(_pgp_import_key(&PrivateKeyDB,pgpKey)))
       return 0;
	PGPCountKeys(PrivateKeyDB, &dwKeys);
#else
	PGPKeyDBRef PrivateKeyDB;
    if (CheckPGPError(_pgp_import_key(&PrivateKeyDB,pgpKey)))
       return 0;
	PGPCountKeysInKeyDB(PrivateKeyDB, &dwKeys);
#endif
	if(dwKeys==0) {
#if (PGP_WIN32 < 0x700)
		PGPFreeKeySet(PrivateKeyDB);
#else
		PGPFreeKeyDB(PrivateKeyDB);
#endif
		return 0;
	}

	int iTry = 0;

    do {
   	   	if (!pszPassphrase &&
			PGPPassphraseDialog(pgpContext,
								PGPOUIOutputPassphrase(pgpContext, &pszPassphrase),
								PGPOLastOption(pgpContext)) == kPGPError_UserAbort) {
			iTry = 3;
			break;
    	}

/*
#if (PGP_WIN32 < 0x700)
	    PGPKeyListRef PrivateKeyList;
	    PGPOrderKeySet(PrivateKeyDB, kPGPKeyIDOrdering, &PrivateKeyList);

	    PGPKeyIterRef KeyIterRef;
	    PGPNewKeyIter(PrivateKeyList, &KeyIterRef);

		PGPKeyRef PrivateKey;
	    for(int i=0;i<dwKeys;i++) {
			PGPKeyIterNext(KeyIterRef, &PrivateKey);
			PGPOPassphraseIsValid(PrivateKey,
							      PGPOPassphrase(pgpContext, pszPassphrase),
							      PGPOLastOption(pgpContext));
		}

	    PGPFreeKeyList(PrivateKeyList);
	    PGPFreeKeyIter(KeyIterRef);
#else
	    PGPKeyIterRef KeyIterRef;
	    PGPNewKeyIterFromKeyDB(PrivateKeyDB, &KeyIterRef);

		PGPKeyDBObjRef KeyDBObjRef;
	    for(int i=0;i<dwKeys;i++) {
			PGPKeyIterNextKeyDBObj(KeyIterRef, kPGPKeyDBObjType_Key, &KeyDBObjRef);
			PGPOPassphraseIsValid(PrivateKey,
							      PGPOPassphrase(pgpContext, pszPassphrase),
							      PGPOLastOption(pgpContext));
		}

	    PGPFreeKeyIter(KeyIterRef);
#endif
*/
		PGPError err = PGPDecode(pgpContext,
	       PGPOInputBuffer(pgpContext, szEncMsg, lstrlen(szEncMsg)),
	       PGPOAllocatedOutputBuffer(pgpContext, (LPVOID *)&szPlainMsg, 16384, (PGPUInt32 *)&dwPlainMsgLen),
#if (PGP_WIN32 < 0x700)
		   PGPOKeySetRef(pgpContext, PrivateKeyDB),
#else
	       PGPOKeyDBRef(pgpContext, PrivateKeyDB),
#endif
	       PGPOPassphrase(pgpContext, pszPassphrase),
	       PGPOLastOption(pgpContext));

	    if (CheckPGPError(err))
			iTry = 3;
		else
	    if (!dwPlainMsgLen) {
			PGPFreeData(pszPassphrase);
			pszPassphrase = 0;
			iTry++;
		}

    } while(!dwPlainMsgLen && iTry<3);
    
#if (PGP_WIN32 < 0x700)
	PGPFreeKeySet(PrivateKeyDB);
#else
	PGPFreeKeyDB(PrivateKeyDB);
#endif

	if(iTry == 3) return 0;

    LPSTR szMsg = (LPSTR) LocalAlloc(LPTR,dwPlainMsgLen+1);
    _pgp_memcpy(szMsg, szPlainMsg, dwPlainMsgLen);
    szMsg[dwPlainMsgLen] = 0;
    PGPFreeData((LPVOID)szPlainMsg);

    return szMsg;
}
	void
CCRSVerisignServer::PackageAndSendCRSMessage(
	PGPExportFormat	inExportFormat,
	PGPOutputFormat	inOutputFormat,
	PGPKeyRef		inKey,
	PGPKeyRef		inSigningKey,
	PGPByte *		inPassphrase,
	PGPSize			inPassphraseLength,
	PGPBoolean		inIsPassphrase,
	void **			outBuffer,
	PGPSize *		outBufferSize)
{
	PGPError		pgpErr;
	StPGPKeySetRef	singleKeySet;
	StPGPDataRef	buffer;
	PGPSize			bufSize;
	StPGPDataRef	crsMessage;
	PGPSize			crsMessageSize;
	
	InitOperation();
	if (! PGPKeyRefIsValid(inKey)) {
		ThrowPGPError_(kPGPError_OptionNotFound);
	}
	if (! PGPKeyRefIsValid(inSigningKey)) {
		ThrowPGPError_(kPGPError_OptionNotFound);
	}
	if (NULL==(int)(inPassphrase)) {
		ThrowPGPError_(kPGPError_OptionNotFound);
	}
	
	pgpErr = PGPNewSingletonKeySet(inKey, &singleKeySet);
	ThrowIfPGPError_(pgpErr);
	pgpErr = PGPExportKeySet(	singleKeySet,
								PGPOExportFormat(	mContext,
													inExportFormat),
								PGPOAllocatedOutputBuffer(	mContext,
															(void **) &buffer,
															MAX_PGPSize,
															&bufSize),
								PGPOLastOption(mContext));
	ThrowIfPGPError_(pgpErr);
	singleKeySet.Free();
	pgpErr = PGPEncode(	mContext,
						PGPOSignWithKey(	mContext,
											inSigningKey,
											(inIsPassphrase) ?
												PGPOPassphraseBuffer(mContext,
													inPassphrase, inPassphraseLength) :
												PGPOPasskeyBuffer(mContext,
													inPassphrase, inPassphraseLength),
											PGPOLastOption(mContext)),
						PGPOInputBuffer(mContext, static_cast<PGPByte *>(buffer), bufSize),
						PGPOAllocatedOutputBuffer(	mContext,
													(void **) &crsMessage,
													MAX_PGPSize,
													&crsMessageSize),
						PGPOOutputFormat(	mContext,
											inOutputFormat),
						PGPOLastOption(mContext));
	ThrowIfPGPError_(pgpErr);
	buffer.Free();
	*outBufferSize = PostCRSMessage(kPGPKeyServerState_Querying,
									(mPath == 0) ? kDefaultPath : mPath,
									reinterpret_cast<PGPByte **>(outBuffer),
									crsMessageSize,
									static_cast<const PGPByte *>(crsMessage));
}