clgenwiz.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 2,833 行 · 第 1/5 页

C
2,833 行
字号

#if 1
	/* if we didn't find CSP-generated container, 
	then create our own container name. 
	It is worth only for the old driver of iKey, 
	so one day this code should go away. 
	*/
	if( *CSP && cont == NULL && 
		conts2_n == conts1_n && conts1_n == 0 &&
		strstr(pti->manufacturerID, "Rainbow") )  
		CLIEGenerateNewUUID( &cont );
#endif
        
    /* We found new key container, which contains just 
	   generated keys. Tell it to SDK. */
	if( cont )  {
		if( IsntPGPError(err) )  {
			PGPKeySetRef keyset;
			
			err = PGPNewOneKeySet(key, &keyset); 
			
			if( IsntPGPError(err) )  {
				err = PGPExport (ctx,
				   /* The key is not exported, 
				      it is used for the additional info */
				   PGPOExportKeySet (ctx, keyset),
				   PGPOExportFormat (ctx, 
				   kPGPExportFormat_TokenKeyContainer),
				   PGPOOutputToken (ctx,
						-1 /* SDK will determine, based on keyID */),
				   PGPOKeyContainer(ctx, cont, strlen(cont)),
				   PGPOPassphrase (ctx, pszPassPhrase),
				   PGPOLastOption (ctx)
				);
			}
			
			PGPFreeKeySet(keyset);
		}
	}

    free( cont );

	return err;
}


//	______________________________________________
//
//  thread for actually creating key

static VOID
sKeyGenerationThread (
	void	*pArgs)
{
	PKEYGENINFO			pkgi			= (PKEYGENINFO) pArgs;
	BOOL				bNewDefaultKey	= TRUE;
	PGPUInt32			AlgFlags		= 0;
	PGPSize				prefDataSize	= 0;
	PGPKeySetRef		keysetADK		= kInvalidPGPKeySetRef;
	PGPKeySetRef		keysetRevoker	= kInvalidPGPKeySetRef;
	PGPOptionListRef	optionlist		= kInvalidPGPOptionListRef;
	PGPPrefRef			prefref			= kInvalidPGPPrefRef;
	PGPCipherAlgorithm*	pAlgs			= NULL;
	PGPPrefArray *		prefArray		= NULL;
	PGPUInt32			uKeyFlags		= 0;

	PGPKeyDBObjRef		keyDefault;
	PGPKeyDBObjRef		subkey;
	PGPKeyDBObjRef		useridCorp;
	PGPBoolean			bFastGen;
	PGPContextRef		ctx;
	PGPByte				enforce;
	PGPError			err;
	INT					uTrustLevel;
	PGPUInt32			numAlgs;

    const tokenNum = 0; /*Always token 0 for now*/

    char *conts1 = NULL;
    char *conts2 = NULL;
    PGPUInt32 conts1_n=0;
    PGPUInt32 conts2_n=0;

    PGPTokenInfo pti = { sizeof(PGPTokenInfo) };
    char CSP[80];
	const BOOL bHasSubKey = (pkgi->uSubKeyAlg != 0);

	ctx = pkgi->context;

	sSetThreadExecutionState (ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
	Sleep (100);

	//
	//	setup and perform key generation
	//
	pkgi->uCurrentTask = 0;
	sSetCurrentTaskInfo (pkgi, ICON_ARROW);

	PGPclGetDefaultPrivateKey (pkgi->keydbMain, &keyDefault);
	if (PGPKeyDBObjRefIsValid (keyDefault))
		bNewDefaultKey = FALSE;

	// construct userid
	err = sCreatePGPUserID (&pkgi->pszUserID,
			pkgi->pszFullName, pkgi->pszEmail); CKERR;

	// get client preferences
	err = PGPclPeekClientLibPrefRefs (&prefref, NULL); CKERR;
	PGPGetPrefBoolean (prefref, kPGPPrefFastKeyGen, &bFastGen);
	PGPGetPrefArray (prefref,
			kPGPPrefAllowedAlgorithmsList, &prefArray);

	numAlgs	= prefArray->numElements;
	pAlgs = (PGPCipherAlgorithm *) PGPNewData (PGPPeekContextMemoryMgr (ctx),
			(sizeof(PGPCipherAlgorithm) * (numAlgs+2)), 
			kPGPMemoryMgrFlags_Clear);

	if (IsntNull(pAlgs))
	{
		PGPUInt32	uOut		= 0;
		PGPUInt32	uIn;

		for (uIn=0; uIn<numAlgs; uIn++)
		{
			pAlgs[uOut] = (PGPCipherAlgorithm) prefArray->elements[uIn].data;

			// if AES(256) is in the list, make sure the other AES 
			// variants are too
			if (pAlgs[uOut++] == kPGPCipherAlgorithm_AES256)
			{
				pAlgs[uOut++] = kPGPCipherAlgorithm_AES192;
				pAlgs[uOut++] = kPGPCipherAlgorithm_AES128;
			}
		}

		numAlgs = uOut;
	}
	else
	{
		numAlgs = 0;
		err = kPGPError_OutOfMemory;
		CKERR;
	}

	// build list of common options
	err = PGPBuildOptionList (ctx, &optionlist,
			PGPOKeyDBRef (ctx, pkgi->keydbMain),
			PGPOKeyGenParams (ctx, pkgi->uKeyAlg, pkgi->uKeySize),
			PGPOKeyGenFast (ctx, bFastGen),
			PGPOKeyGenName (ctx, pkgi->pszUserID, lstrlen (pkgi->pszUserID)),
			PGPOPassphrase (ctx, pkgi->pszPassPhrase),
			PGPOExpiration (ctx, pkgi->uExpireDays),
			PGPOPreferredAlgorithms (ctx, pAlgs, numAlgs),
			PGPOEventHandler (ctx, sKeyGenCallback, pkgi),
			PGPOLastOption (ctx)); CKERR;

	// add key flags
	if (pkgi->uKeyFlags != 0)
	{
		err = PGPAppendOptionList (optionlist,
				PGPOKeyFlags (ctx, pkgi->uKeyFlags),
				PGPOLastOption (ctx)); CKERR;
	}

	// add ADK option
	if ((PGPKeyDBObjRefIsValid (pkgi->keyADK)) &&
		(pkgi->uKeyType != kPGPPublicKeyAlgorithm_RSA))
	{
		if (pkgi->bEnforceADK)
			enforce = 0x80;
		else
			enforce = 0x00;
		err = PGPNewOneKeySet (pkgi->keyADK, &keysetADK); CKERR;
		err = PGPAppendOptionList (optionlist,
				PGPOAdditionalRecipientRequestKeySet (ctx, keysetADK, enforce),
				PGPOLastOption (ctx)); CKERR;
	}

	// add revoker key option
	if ((PGPKeyDBObjRefIsValid (pkgi->keyRevoker)) &&
		(pkgi->uKeyType != kPGPPublicKeyAlgorithm_RSA))
	{
		err = PGPNewOneKeySet (pkgi->keyRevoker, &keysetRevoker); CKERR;
		err = PGPAppendOptionList (optionlist,
				PGPORevocationKeySet (ctx, keysetRevoker),
				PGPOLastOption (ctx)); CKERR;
	}

	// verify token capabilities and add token option
	if (pkgi->bOnToken)
	{
		UINT	uNumTokens;

		PGPCountTokens (g_context, &uNumTokens);

		if (uNumTokens == 0)
		{
			err = kPGPError_Win32_TokenNotInserted;
			goto done;
		}
		else
		{
			err = PGPGetTokenInfo (g_context, tokenNum, &pti);
			if (IsPGPError (err))
			{
				err = kPGPError_Win32_TokenNotInserted;
				goto done;
			}

			if (pkgi->uKeyAlg != kPGPPublicKeyAlgorithm_RSA)
			{
				err = kPGPError_Win32_TokenParameterError;
				goto done;
			}

			if ((pkgi->uKeySize < pti.minRsaKeySize) ||
				(pkgi->uKeySize > pti.maxRsaKeySize))
			{
				err = kPGPError_Win32_TokenParameterError;
				goto done;
			}
		}

		err = PGPAppendOptionList (optionlist,
				PGPOKeyGenOnToken (ctx, tokenNum),
				PGPOLastOption (ctx)); CKERR;

		PGPclIETokenProvToCSP (pti.manufacturerID, CSP, sizeof(CSP));

		if (pti.numPrivKeys > 0)  
		{
			/* This is an optimization for all vendors and a 
			requirement for some vendors, who may crash. 
			If token has no private keys, then it has no key 
			containers. */
			PGPclIEGetExistingKeyContainersRemote (CSP, &conts1, &conts1_n);
		}
	}

	// generate key using specified options
	err = PGPGenerateKey (ctx,
			&(pkgi->keyNew), optionlist, PGPOLastOption (ctx));

	// note: PGPGenerateKey returns kPGPError_OutOfMemory when user aborts!
	if (err == kPGPError_OutOfMemory)
		err = kPGPError_UserAbort;

	if (IsPGPError (err) || pkgi->bCancelPending)
		sSetCurrentTaskInfo (pkgi, ICON_ERROR);
	else
		sSetCurrentTaskInfo (pkgi, ICON_CHECK);

    if (IsntPGPError(err) && pkgi->bOnToken)  
		PGPclIEGetExistingKeyContainersRemote (CSP, &conts2, &conts2_n);
    
	//
	//	setup and perform subkey generation
	//
	pkgi->uCurrentTask++;
	if (IsntPGPError (err) && !pkgi->bCancelPending)
	{
		sSetCurrentTaskInfo (pkgi, ICON_ARROW);

		if (bHasSubKey)
		{
			if (PGPOptionListRefIsValid (optionlist))
				PGPFreeOptionList (optionlist);

			// build list of common options
			err = PGPBuildOptionList (ctx, &optionlist,
				PGPOKeyGenMasterKey (ctx, pkgi->keyNew),
				PGPOKeyGenParams (ctx, pkgi->uSubKeyAlg, pkgi->uSubKeySize),
				PGPOKeyGenFast (ctx, bFastGen),
				PGPOPassphrase (ctx, pkgi->pszPassPhrase),
				PGPOExpiration (ctx, pkgi->uExpireDays),
				PGPOEventHandler (ctx, sKeyGenCallback, pkgi),
				PGPOLastOption (ctx)); CKERR;

			// add subkey flags
			if (pkgi->uSubKeyFlags != 0)
			{
				err = PGPAppendOptionList (optionlist,
						PGPOKeyFlags (ctx, pkgi->uSubKeyFlags),
						PGPOLastOption (ctx)); CKERR;
			}

			// add token option
			if (pkgi->bOnToken)
			{
				UINT	uNumTokens;

				PGPCountTokens (g_context, &uNumTokens);

				if (uNumTokens == 0)
					err = kPGPError_Win32_TokenNotInserted;
				else
				{
					err = PGPAppendOptionList (optionlist,
							PGPOKeyGenOnToken (ctx, 0),
							PGPOLastOption (ctx));
				}
				CKERR;
    		}

			err = PGPGenerateSubKey (
				ctx, &subkey, optionlist,
				PGPOLastOption (ctx));

			// note: PGPGenerateSubKey returns kPGPError_OutOfMemory
			// when user aborts!
			if (err == kPGPError_OutOfMemory)
				err = kPGPError_UserAbort;

		}
	}

	if (IsntPGPError(err) && pkgi->bOnToken)
	{
		err = PGPclProcessKeyConts (ctx, pkgi->keyNew, 
			pkgi->pszPassPhrase, 
			CSP, &pti, conts1, conts1_n, conts2, conts2_n);
	}

    PGPclIEFreeMem (conts1);
    PGPclIEFreeMem (conts2);

	if (IsPGPError (err) || pkgi->bCancelPending)
		sSetCurrentTaskInfo (pkgi, ICON_ERROR);
	else
	{
		sSetCurrentTaskInfo (pkgi, ICON_CHECK);
		pkgi->iFinalResult = kPGPclKeyCreate_NewKey;
		if (bNewDefaultKey)
			pkgi->iFinalResult |= kPGPclKeyCreate_NewDefaultKey;
		if (pkgi->bOnToken)
			pkgi->iFinalResult |= kPGPclKeyCreate_OnToken;
	}

	//
	// add MS Exchange userid
	//
	if (IsntPGPError (err) && !pkgi->bCancelPending)
	{
		if (pkgi->bIsExchangeUser &&
			(pkgi->bAutoAddExchangeUserID ||
			 pkgi->bLookupExchangeUserID))
		{
			pkgi->uCurrentTask++;
			sSetCurrentTaskInfo (pkgi, ICON_ARROW);
			pkgi->bTaskError = FALSE;

			sLookupExchangeUserID (pkgi->hwndWiz, pkgi);

			if (pkgi->pszExchangeUserID)
			{
				// make sure we have enough entropy
				PGPclRandom (ctx, pkgi->hwndWiz, 0);

				err = PGPAddUserID (pkgi->keyNew, pkgi->pszExchangeUserID,
						PGPOPassphrase (ctx, pkgi->pszPassPhrase),
						PGPOLastOption (ctx));
			}
			
			if (IsNull (pkgi->pszExchangeUserID) ||
				IsPGPError (err))
			{
				pkgi->bTaskError = TRUE;
				sSetCurrentTaskInfo (pkgi, ICON_ERROR);
			}
			else
				sSetCurrentTaskInfo (pkgi, ICON_CHECK);
		}
	}

	//
	// add Lotus Notes userid
	//
	if (IsntPGPError (err) && !pkgi->bCancelPending)
	{
		if (pkgi->bAddNotesUserID &&
			pkgi->pszNotesFullName &&
			pkgi->pszNotesEmail)
		{
			if (pkgi->pszUserID)
				clFree (pkgi->pszUserID);

			// construct userid
			err = sCreatePGPUserID (&pkgi->pszUserID,
					pkgi->pszNotesFullName, pkgi->pszNotesEmail);

			if (IsntPGPError (err))
			{
				// make sure we have enough entropy
				PGPclRandom (ctx, pkgi->hwndWiz, 0);

				err = PGPAddUserID (pkgi->keyNew, pkgi->pszUserID,
						PGPOPassphrase (ctx, pkgi->pszPassPhrase),
						PGPOLastOption (ctx));
			}
		}
	}

	//
	// sign and trust corporate key
	//
	if (IsntPGPError (err) && !pkgi->bCancelPending)
	{
		if (PGPKeyDBObjRefIsValid (pkgi->keyCorp))
		{
			err = PGPGetPrimaryUserID (pkgi->keyCorp, &useridCorp);
			if (IsntPGPError (err))
			{
				if (pkgi->bMetaCorpKey)
					uTrustLevel = pkgi->uMetaTrustLevel;
				else
					uTrustLevel = 0;

				// make sure we have enough entropy
				PGPclRandom (ctx, pkgi->hwndWiz, 0);

				err = PGPCertifyUserID (useridCorp, pkgi->keyNew,
						PGPOPassphrase (ctx, pkgi->pszPassPhrase),
						PGPOExpiration (ctx, 0),
						PGPOExportable (ctx, FALSE),
						PGPOSigTrust (ctx, uTrustLevel, kPGPKeyTrust_Complete),
						PGPOLastOption (ctx));

				if (IsntPGPError (err))
				{
					err = PGPSetKeyTrust (
							pkgi->keyCorp, kPGPKeyTrust_Complete);

					// ignore errors here.  If key is axiomatic,
					// setting trust will cause an error.
					err = kPGPError_NoErr;
				}
			}
		}
	}

	//
	// sign root CA key
	//
	if (IsntPGPError (err) && !pkgi->bCancelPending)
	{
		if (PGPKeyDBObjRefIsValid (pkgi->keyRootCA))
		{
			err = PGPGetPrimaryUserID (pkgi->keyRootCA, &useridCorp);
			if (IsntPGPError (err))
			{
				uTrustLevel = 0;

				// make sure we have enough entropy
				PGPclRandom (ctx, pkgi->hwndWiz, 0);

				err = PGPCertifyUserID (useridCorp, pkgi->keyNew,
						PGPOPassphrase (ctx, pkgi->pszPassPhrase),
						PGPOExpiration (ctx, 0),
						PGPOExportable (ctx, FALSE),
						PGPOSigTrust (ctx, uTrustLevel, kPGPKeyTrust_Complete),
						PGPOLastOption (ctx));
			}
		}
	}

	//
	//	send key to server
	//
	if (pkgi->bDoSend)
	{
		PGPKeySetRef	keysetSend	= kInvalidPGPKeySetRef;

		pkgi->uCurrentTask++;
		sSetCurrentTaskInfo (pkgi, ICON_ARROW);
		pkgi->bTaskError = TRUE;

		if (IsntPGPError (err) && !pkgi->bCancelPending)
		{
			err = PGPNewOneKeySet (pkgi->keyNew, &keysetSend);
			if (IsntPGPError (err))
			{
				pkgi->bTaskComplete = FALSE;
				pkgi->bTaskError = FALSE;

				err = PGPclSendKeysToRootServerNotify (
						pkgi->context, pkgi->tlscontext,
						pkgi->hwndWiz, pkgi->keydbMain, keysetSend);

				if (IsntPGPError (PGPclErrorBox (pkgi->hwndWiz, err)))
				{
					while (!pkgi->bTaskComplete)
						Sleep (200);
				}
				else
					pkgi->bTaskError = TRUE;

				PGPFreeKeySet (keysetSend);
			}

			err = kPGPError_NoErr;
		}

		if (pkgi->bTaskError)
		{
			sSetCurrentTaskInfo (pkgi, ICON_ERROR);
			pkgi->bDoSend = FALSE;
		}
		else
			sSetCurrentTaskInfo (pkgi, ICON_CHECK);
	}

	//
	//	setup and send certificate request
	//
	if (pkgi->bDoCertRequest)
	{
		PGPKeySetRef	keysetSend	= kInvalidPGPKeySetRef;

		pkgi->uCurrentTask++;
		sSetCurrentTaskInfo (pkgi, ICON_ARROW);
		pkgi->bTaskError = TRUE;

		if (IsntPGPError (err) && !pkgi->bCancelPending)
		{
			err = PGPNewOneKeySet (pkgi->keyNew, &keysetSend);
			if (IsntPGPError (err))
			{
				pkgi->bTaskComplete = FALSE;
				pkgi->bTaskError = FALSE;

				err = PGPclSendCertificateRequestToServerNotify (
						pkgi->context, pkgi->tlscontext, pkgi->hwndWiz,
						pkgi->keydbMain, kInvalidPGPKeyDBObjRef,
						keysetSend, pkgi->pszPassPhrase);

				if (IsntPGPError (PGPclErrorBox (pkgi->hwndWiz, err)))
				{
					while (!pkgi->bTaskComplete)
						Sleep (200);
				}
				else
					pkgi->bTaskError = TRUE;

				PGPFreeKeySet (keysetSend);
			}

			err = kPGPError_NoErr;
		}

		if (pkgi->bTaskError)
		{
			sSetCurrentTaskInfo (pkgi, ICON_ERROR);
			pkgi->bDoCertRequest = FALSE;
		}
		else
			sSetCurrentTaskInfo (pkgi, ICON_CHECK);
	}

	//
	//	setup and send reconstruction blob
	//
	if (pkgi->bDoReconstruction)
	{
		pkgi->uCurrentTask++;
		sSetCurrentTaskInfo (pkgi, ICON_ARROW);
		pkgi->bTaskError = TRUE;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?