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 }
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; }
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; }
int addToKeyring(struct pgpmainBones *mainbPtr, char* keyfile, PGPFileSpecRef ringFileSpec, char *origRingFileName ) { PGPContextRef context = mainbPtr->pgpContext; struct pgpfileBones *filebPtr = mainbPtr->filebPtr; PGPFileSpecRef keyFileSpec = kPGPInvalidRef; PGPKeySetRef ringSet = kPGPInvalidRef; #ifdef NOTPREFERREDMETHOD PGPKeySetRef keySet = kPGPInvalidRef; #endif /* NOTPREFERREDMETHOD */ PGPKeySetRef emptyKeySet = kPGPInvalidRef; PGPError err,er2; char *ringfile = NULL; PGPEnv *env = mainbPtr->envbPtr->m_env; PGPInt32 pri; PGPBoolean compatible = mainbPtr->envbPtr->compatible; err = PGPGetFullPathFromFileSpec( ringFileSpec, &ringfile ); pgpAssertNoErr(err); /* Manually open the input file, gives us an early out if it is missing */ err = PGPNewFileSpecFromFullPath(context, keyfile, &keyFileSpec); if(err) { pgpShowError( filebPtr, err, __FILE__,__LINE__); goto done; } /* add the keys to a in memory key set, then do one add operation at end to the target key set */ err = PGPNewKeySet(context, &emptyKeySet); if(IsPGPError(err)) goto done; mainbPtr->workingRingSet = emptyKeySet; /* Get or create target keySet. Must be both public and secret keyrings, in case the input contains new secret keys. */ err = pgpOpenKeyringsFromPubringSpec( mainbPtr, ringFileSpec, &ringSet, kPGPKeyRingOpenFlags_Mutable); if(err) { /* XXX The error msg context here may differ from 262 */ fprintf(filebPtr->pgpout, ringFileSpec ? LANG( "\nKey ring file '%s' cannot be created.\n") : LANG("Default ring file cannot be created"), origRingFileName); goto done; } /* * mark key set so that we know what was added */ err = pgpMarkKeySet(ringSet); pgpAssertNoErr(err); /* Allow eventhandler in dodecode.c to do the remaining work */ fprintf(filebPtr->pgpout,LANG("\nLooking for new keys...\n")); err = PGPDecode( context, PGPOInputFile( context, keyFileSpec ), PGPOKeySetRef( context, ringSet ), PGPOSendEventIfKeyFound( context, TRUE ), PGPOEventHandler( context, addHandler, (PGPUserValue) mainbPtr ), PGPOLastOption( context ) ); if(err) { pgpShowError( filebPtr, err,__FILE__,__LINE__ ); goto done; } else { /* count number of keys before add */ PGPUInt32 countBefore = 0; err = PGPCountKeys(ringSet, &countBefore); pgpAssertNoErr(err); err = PGPAddKeys(mainbPtr->workingRingSet, ringSet); if(IsntPGPError(err) && PGPKeySetNeedsCommit(ringSet)) { /* * show what was added */ PGPUInt32 keys = 0; PGPSize dummy,uids,sigs,rvks; err = PGPCountKeys(ringSet, &keys); pgpAssertNoErr(err); keys -= countBefore; fprintf(filebPtr->pgpout, LANG("\n\nSummary of changes : \n")); err = pgpShowKeySetUnmarked( filebPtr, ringSet, &dummy, &uids, &sigs, &rvks ); pgpAssertNoErr(err); if(keys || uids || sigs || rvks) { fprintf(filebPtr->pgpout, LANG("\nAdded :\n")); if (keys) fprintf(filebPtr->pgpout, LANG("%4d new key(s)\n"), keys); if (sigs) fprintf(filebPtr->pgpout, LANG("%4d new signatures(s)\n"), sigs); if (uids) fprintf(filebPtr->pgpout, LANG("%4d new user ID(s)\n"), uids); if (rvks) fprintf(filebPtr->pgpout, LANG("%4d new revocation(s)\n"), rvks); } else { fprintf(filebPtr->pgpout, LANG("No new keys found.")); } PGPCommitKeyRingChanges(ringSet); } } #ifdef NOTPREFERREDMETHOD /* Rather than calling this here, we call it from the callback Copy all keys from keySet which aren't in ringSet already */ err = addToWorkingRingSet( mainbPtr, keySet ); #endif /* NOTPREFERREDMETHOD */ done: if(emptyKeySet != kPGPInvalidRef) { er2 = PGPFreeKeySet(emptyKeySet); pgpAssertNoErr(er2); } #ifdef NOTPREFERREDMETHOD if( keySet != kPGPInvalidRef ) { er2 = PGPFreeKeySet( keySet ); pgpAssertNoErr(er2); } #endif /* NOTPREFERREDMETHOD */ if( ringSet != kPGPInvalidRef ) { er2 = PGPFreeKeySet( ringSet ); pgpAssertNoErr(er2); mainbPtr->workingRingSet = NULL; } if( keyFileSpec != kPGPInvalidRef ) { er2 = PGPFreeFileSpec(keyFileSpec); pgpAssertNoErr(er2); } if(!compatible && IsPGPError(err) && pgpenvGetInt( env, PGPENV_VERBOSE, &pri, &er2 ) ) { pgpShowError(filebPtr, err,__FILE__,__LINE__); } if(ringfile) PGPFreeData(ringfile); return err ? -1 : 0; }