Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
PGPError printNextSignatureChainLink(struct pgpmainBones *mainbPtr,
        PGPKeySetRef bothRingsSet,
        PGPKeyRef key,
        PGPBoolean visited,
        PGPUInt32 currentDepth)
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    PGPUserValue traceValue;
    PGPUInt32 keyDepth;
    PGPKeyListRef kidsList;
    PGPKeySetRef kidsSet;
    PGPFilterRef kidsFilter;
    PGPFilterRef singletonFilter;
    PGPFilterRef notParentFilter;
    PGPKeyIterRef keyIter;
    char useridstr[kPGPMaxUserIDSize];
    PGPError err = kPGPError_NoErr;
    PGPKeyID keyID;

    /* For first time at node? Create keyset containing its children. */

    if (!visited)  {

        err = PGPGetKeyIDFromKey( key, &keyID);
        pgpAssertNoErr(err);

        err = PGPNewSigKeyIDFilter(context, &keyID, &kidsFilter);
        pgpAssertNoErr(err);

        err = PGPNewKeyIDFilter( context, &keyID, &singletonFilter );
        pgpAssertNoErr(err);

        err = PGPNegateFilter( singletonFilter, &notParentFilter);
        pgpAssertNoErr(err);

        err = PGPIntersectFilters( notParentFilter, kidsFilter,
                &kidsFilter );

        pgpAssertNoErr(err);


        if (kidsFilter == NULL) return 0;           /* childless node */

        err = PGPFilterKeySet(bothRingsSet, kidsFilter, &kidsSet);
        pgpAssertNoErr(err);

        err = PGPOrderKeySet( kidsSet, kPGPAnyOrdering, &kidsList);
        pgpAssertNoErr(err);

        err = PGPNewKeyIter(kidsList, &keyIter);
        pgpAssertNoErr(err);

        err = PGPKeyIterRewind( keyIter);
        pgpAssertNoErr(err);

        err = PGPKeyIterNext( keyIter, &key);

        if (IsPGPError(err)) {  /* childless */

            PGPFreeKeyIter( keyIter);
            return 0;
        }
    }

    while (key != NULL) {

        err = PGPGetKeyUserVal(key, &traceValue);
        pgpAssertNoErr(err);

        visited = (PGPUInt32)traceValue & VISITED_MASK;
        keyDepth = (PGPUInt32)traceValue & DEPTH_MASK;

        err = pgpGetUserIDStringFromKey(key, useridstr);
        pgpAssertNoErr(err);

        fprintf(filebPtr->pgpout, LANG("%*s"), (2 * currentDepth), " ");
        fprintf(filebPtr->pgpout, LANG("> %s\n"), useridstr);

        if (visited || (keyDepth < currentDepth) )  return 0;

        traceValue = (PGPUserValue)( VISITED_MASK | keyDepth);
        err = PGPSetKeyUserVal(key, traceValue);
        pgpAssertNoErr(err);

        err = PGPGetKeyIDFromKey( key, &keyID);
        pgpAssertNoErr(err);

        err = printNextSignatureChainLink(mainbPtr, bothRingsSet, key,
                visited, currentDepth+1);

        err = PGPKeyIterNext( keyIter, &key );

        if ( IsPGPError(err) ) {    /* traversed all children of this node */

            PGPFreeKeyIter( keyIter);
            return 0;
        }
    }
    return err;
} /* printNextSignatureChainLink */
Ejemplo n.º 3
0
PGPError findUltimatelyTrustedKeys(struct pgpmainBones *mainbPtr,
        PGPKeySetRef secRingSet,
        PGPKeySetRef bothRingsSet,
        PGPFilterRef *depthFilter)
{

    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    PGPKeyIterRef keyiter;
    PGPKeyRef skey;
    PGPKeyListRef secKeylist;
    PGPKeyListRef bothKeylist;
    PGPError err;
    PGPUInt32 trust;
    PGPBoolean compatible = mainbPtr->envbPtr->compatible;
    char kstr[kPGPMaxKeyIDStringSize];
    char useridstr[ kPGPMaxUserIDSize ];
    PGPContextRef context = mainbPtr->pgpContext;
    PGPKeyID kid;
    PGPBoolean firstUltimatelyTrustedKey = TRUE;
    PGPFilterRef singletonFilter = NULL;

    /* Pass 1: locate ultimately-trusted keys. */

    fprintf(filebPtr->pgpout,
        LANG("\nPass 1: Looking for the \"ultimately-trusted\" keys...\n"));

    err = PGPOrderKeySet( secRingSet, kPGPAnyOrdering, &secKeylist );
    if( IsPGPError(err) )
        return -1;

    err = PGPOrderKeySet( bothRingsSet, kPGPAnyOrdering, &bothKeylist );
    if( IsPGPError(err) )
        return -1;

    err = PGPNewKeyIter( secKeylist, &keyiter );
    pgpAssertNoErr(err);
    err = PGPKeyIterRewind( keyiter );
    pgpAssertNoErr(err);

    err = PGPKeyIterNext( keyiter, &skey);
    /*if error, no keys found.*/

    if (err) {
        fprintf(filebPtr->pgpout, LANG("No ultimately-trusted keys.") );
        return err;
    }

    while( skey != NULL )
    {

        err = PGPGetKeyNumber( skey, kPGPKeyPropTrust, &trust);
        pgpAssertNoErr(err);

        if (trust == kPGPKeyTrust_Ultimate) {

            err = pgpGetKeyIDStringCompatFromKey( skey, TRUE, compatible,
                    kstr );

            pgpAssertNoErr(err);
            err =  pgpGetUserIDStringFromKey( skey, useridstr );
            pgpAssertNoErr(err);

            /*  '*' means axiomatically trusted.  */

            fprintf(filebPtr->pgpout, LANG("* %s %s\n") ,kstr,useridstr);

            /* Make filter for depth 0 (=ultimately-trusted) keys. */

            err = PGPGetKeyIDFromKey  (skey, &kid);
            pgpAssertNoErr(err);

            if (firstUltimatelyTrustedKey) {

                err = PGPNewKeyIDFilter( context, &kid, depthFilter );
                pgpAssertNoErr(err);
                firstUltimatelyTrustedKey = FALSE;
            }
            else {

                err = PGPNewKeyIDFilter( context, &kid, &singletonFilter );
                pgpAssertNoErr(err);

                err = PGPUnionFilters ( singletonFilter, *depthFilter,
                        depthFilter );

                pgpAssertNoErr(err);
            }
        }
        err = PGPKeyIterNext( keyiter, &skey);

    }

    err = PGPFreeKeyList( bothKeylist );
    pgpAssertNoErr(err);
    err = PGPFreeKeyList( secKeylist );
    pgpAssertNoErr(err);

    return 0;
} /* findUltimatelyTrustedKeys */
Ejemplo n.º 4
0
PGPError CreateFilter(HWND hwnd, PGPFilterRef* filter, int* action)
{
	PGPError error = kPGPError_NoErr;
	HWND hwndAttribute, hwndVerb;
	HWND hwndEditSpecifier, hwndComboSpecifier, hwndComboListSpecifier;
	HWND hwndTime;
	int AttributeSelection = 0;
	int VerbSelection = 0;
	int SpecifierSelection = 0;
	char* SpecifierBuffer = NULL;
	DWORD SpecifierLength = 0;
	int Month = 1;
	int Day = 1;
	int Year = 1970;
	BOOL bNegate = FALSE;
	SYSTEMTIME st;

	assert(hwnd);
	assert(filter);
	assert(action);

	// set default action
	*action = ACTION_INTERSECT;

	// find all our window handles
	hwndAttribute			= GetDlgItem(hwnd, IDC_ATTRIBUTE);
	hwndVerb				= GetDlgItem(hwnd, IDC_VERB);
	hwndEditSpecifier		= GetDlgItem(hwnd, IDC_SPECIFIER_EDIT);
	hwndComboSpecifier		= GetDlgItem(hwnd, IDC_SPECIFIER_COMBO);
	hwndComboListSpecifier	= GetDlgItem(hwnd, IDC_SPECIFIER_COMBO_LIST);
	hwndTime				= GetProp(hwnd, "hwndTime");
		
	// find what the user has chosen to search on
	AttributeSelection	= ComboBox_GetCurSel(hwndAttribute);
	VerbSelection		= ComboBox_GetCurSel(hwndVerb);
	SpecifierSelection	= ComboBox_GetCurSel(hwndComboSpecifier);
	if (SpecifierSelection < 0) 
		SpecifierSelection	= ComboBox_GetCurSel(hwndComboListSpecifier);

	// get date currently in time/date picker control
	SendMessage (hwndTime, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
	// struct tm represents months 0-11  so we have to subtract 1
	Month = st.wMonth - 1; 
	// struct tm represents days 1-31  we have don't subtract 1
	Day =	st.wDay;
	// struct tm represents years as offset from 1900
	Year =	st.wYear - 1900;

	// combo is zero based and the string list is not... adjust Selection
	switch( AttributeSelection + IDS_SEARCH_STRINGTABLE_BASE + 1 ) 
	{
		case IDS_ATTRIBUTE_1: // user id
		{
			SpecifierLength = Edit_GetTextLength(hwndEditSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				Edit_GetText(	hwndEditSpecifier, 
								SpecifierBuffer, 
								SpecifierLength);

				// MessageBox(NULL, SpecifierBuffer, "user id", MB_OK);

				switch( VerbSelection )
				{
					case IS_NOT:
						bNegate = TRUE;
					case IS:
					{
						error = PGPNewUserIDStringFilter(
										g_Context, 
										SpecifierBuffer, 
										kPGPMatchEqual,
										filter );

						if( bNegate && IsntPGPError(error) )
						{
							PGPFilterRef NegatedFilter = kPGPInvalidRef;

							error = PGPNegateFilter(*filter, 
													&NegatedFilter);

							if( IsntPGPError(error) )
							{
								*filter = NegatedFilter;
							}
						}

						break;
					}

					case DOES_NOT_CONTAIN:
						bNegate = TRUE;
					case CONTAINS:
					{
						
						error = PGPNewUserIDStringFilter(
										g_Context, 
										SpecifierBuffer, 
										kPGPMatchSubString,
										filter );

						if( bNegate && IsntPGPError(error) )
						{
							PGPFilterRef NegatedFilter = kPGPInvalidRef;

							error = PGPNegateFilter(*filter, 
													&NegatedFilter);

							if( IsntPGPError(error) )
							{
								*filter = NegatedFilter;
							}
						}

						break;
					}

					case IS_NOT_SIGNED_BY:
						bNegate = TRUE;
					case IS_SIGNED_BY:
					{
						PGPKeySetRef	ringSet		= kPGPInvalidRef;
						PGPKeySetRef	filteredSet	= kPGPInvalidRef;


						error = PGPOpenDefaultKeyRings(	g_Context, 
														0, 
														&ringSet);

						if(ringSet && IsntPGPError(error))
						{
							PGPFilterRef userIdFilter = kPGPInvalidRef;

							error = PGPNewUserIDStringFilter(
												g_Context, 
												SpecifierBuffer, 
												kPGPMatchSubString,
												&userIdFilter );

							if( IsntPGPError(error) )
							{
								error = PGPFilterKeySet(ringSet, 
														userIdFilter, 
														&filteredSet);

								PGPFreeFilter(userIdFilter);
							}

							if( IsntPGPError(error) )
							{
								error = KeyIdFilterFromKeySet(	filteredSet,
																filter);
								PGPFreeKeySet(filteredSet);
							}

							if (!PGPRefIsValid (*filter))
							{
								error = kPGPError_Win32_NoSigningKey;
							}

							PGPFreeKeySet(ringSet);
						}

						if( bNegate && IsntPGPError(error) )
						{
							PGPFilterRef NegatedFilter = kPGPInvalidRef;

							error = PGPNegateFilter(*filter, 
													&NegatedFilter);

							if( IsntPGPError(error) )
							{
								*filter = NegatedFilter;
							}
						}

						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}

			break;
		}

		case IDS_ATTRIBUTE_2: // key id
		{
			SpecifierLength = Edit_GetTextLength(hwndEditSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				Edit_GetText(	hwndEditSpecifier, 
								SpecifierBuffer, 
								SpecifierLength);

				//MessageBox(NULL, SpecifierBuffer, "key id", MB_OK);

				switch( VerbSelection )
				{
					case IS_NOT:
						bNegate = TRUE;
					case IS:
					{
						PGPKeyID keyId;

						error = PGPGetKeyIDFromString(	SpecifierBuffer, 
														&keyId );
						if(IsntPGPError(error))
						{
							error = PGPNewKeyIDFilter(	g_Context, 
													&keyId,
													filter );
					
							if( bNegate && IsntPGPError(error) )
							{
								PGPFilterRef NegatedFilter = kPGPInvalidRef;

								error = PGPNegateFilter(*filter, 
														&NegatedFilter);

								if( IsntPGPError(error) )
								{
									*filter = NegatedFilter;
								}
							}
						}

						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}

			break;
		}

		case IDS_ATTRIBUTE_3: // key type
		{
			PGPByte encryptAlgorithm = 0;

			if( SpecifierSelection == DH_KEY_TYPE )
			{
				encryptAlgorithm = kPGPPublicKeyAlgorithm_ElGamal;
			}
			else if( SpecifierSelection == RSA_KEY_TYPE )
			{
				encryptAlgorithm = kPGPPublicKeyAlgorithm_RSA;
			}
			
			error = PGPNewKeyEncryptAlgorithmFilter(g_Context, 
													encryptAlgorithm, 
			 										filter);
			break;
		}

		case IDS_ATTRIBUTE_4: // creation date
		{
			struct tm time;

			memset(&time, 0x00, sizeof(time));

			time.tm_mday	= Day;    /* day of the month - [1,31] */
			time.tm_mon		= Month;  /* months since January - [0,11] */
			time.tm_year	= Year;   /* years since 1900 */

			switch( VerbSelection )
			{

				// In order to simulate an "is date" without the
				// user having to enter the exact hour, min, sec
				// the key was created, we fake it by doing an
				// intersection between the entire day.
				case IS:
				{
					PGPFilterRef filterAM;
					PGPFilterRef filterPM;

					/* midnight AM */
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyCreationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchGreaterOrEqual, 
								&filterAM );

					if(IsPGPError(error))
					{
						break;
					}

					/* just before midnight PM */
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyCreationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchLessOrEqual, 
								&filterPM );

					if(IsntPGPError(error))
					{
						error = PGPIntersectFilters(filterAM,
													filterPM,
													filter);
					}
					
					break;
				}

				case IS_ON_OR_BEFORE:
				{
					
					/* just before midnight PM*/
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyCreationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)),  
								kPGPMatchLessOrEqual, 
								filter );
					break;
				}

				case IS_ON_OR_AFTER:
				{
					/* midnight AM*/
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyCreationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchGreaterOrEqual, 
								filter );

					break;
				}
			}
			break;
		}

		case IDS_ATTRIBUTE_5: // expiration date
		{
			struct tm time;
			
			memset(&time, 0x00, sizeof(time));

			time.tm_mday	= Day;    /* day of the month - [1,31] */
			time.tm_mon		= Month;  /* months since January - [0,11] */
			time.tm_year	= Year;   /* years since 1900 */

			switch( VerbSelection )
			{

				// In order to simulate an "is date" without the
				// user having to enter the exact hour, min, sec
				// the key was created, we fake it by doing an
				// intersection between the entire day.
				case IS:
				{
					PGPFilterRef filterAM;
					PGPFilterRef filterPM;

					/* midnight AM */
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyExpirationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchGreaterOrEqual, 
								&filterAM );

					if(IsPGPError(error))
					{
						break;
					}

					/* just before midnight PM */
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyExpirationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchLessOrEqual, 
								&filterPM );

					if(IsntPGPError(error))
					{
						error = PGPIntersectFilters(filterAM,
													filterPM,
													filter);

					}
					
					break;
				}

				case IS_ON_OR_BEFORE:
				{
					
					/* just before midnight PM*/
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyExpirationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)),  
								kPGPMatchLessOrEqual, 
								filter );
					break;
				}

				case IS_ON_OR_AFTER:
				{
					/* midnight AM*/
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyExpirationTimeFilter( 
								g_Context, 
								PGPGetPGPTimeFromStdTime(mktime(&time)), 
								kPGPMatchGreaterOrEqual, 
								filter );

					break;
				}
			}
			break;
		}

		case IDS_ATTRIBUTE_6: // key
		{
			if( SpecifierSelection == REVOKED )
			{
				error = PGPNewKeyRevokedFilter(	
							g_Context, 
							(PGPBoolean)!VerbSelection, 
							filter );
			}
			else if( SpecifierSelection == DISABLED )
			{
				error = PGPNewKeyDisabledFilter(	
							g_Context, 
							(PGPBoolean)!VerbSelection, 
							filter );
			}
			break;
		}

		case IDS_ATTRIBUTE_7: // key size
		{
			SpecifierLength = ComboBox_GetTextLength(hwndComboSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				ComboBox_GetText(	hwndComboSpecifier, 
									SpecifierBuffer, 
									SpecifierLength);

				//MessageBox(NULL, SpecifierBuffer, "Key Size", MB_OK);

				switch( VerbSelection )
				{
					case IS:
					{
						error = PGPNewKeyEncryptKeySizeFilter( 
											g_Context, 
											atol(SpecifierBuffer), 
			 								kPGPMatchEqual, 
											filter );
						break;
					}

					case IS_AT_LEAST:
					{
						error = PGPNewKeyEncryptKeySizeFilter( 
											g_Context, 
											atol(SpecifierBuffer), 
			 								kPGPMatchGreaterOrEqual, 
											filter );
						break;
					}

					case IS_AT_MOST:
					{
						error = PGPNewKeyEncryptKeySizeFilter( 
											g_Context, 
											atol(SpecifierBuffer), 
			 								kPGPMatchLessOrEqual, 
											filter );
						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}
			break;
		}

		default:
		{
			MessageBox(NULL, "DefaultFilter: Unknown Attribute", 0, MB_OK);
		}
	}

	return error;
}