//+------------------------------------------------------------------------- // // Microsoft Windows // // Copyright (C) Microsoft Corporation, 1995 - 1999 // // File: makectl.cpp // // Contents: Make a CTL // // See Usage() for list of options. // // // Functions: wmain // // History: 17-June-97 xiaohs created // //-------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include "wincrypt.h" #include "mssip.h" #include "softpub.h" #include "resource.h" #include "toolutl.h" #include "cryptui.h" //the UI version of the tool //-------------------------------------------------------------------------- // // Global Data // //---------------------------------------------------------------------------- HMODULE hModule=NULL; BYTE **g_rgpHash=NULL; DWORD *g_rgcbHash=NULL; DWORD g_dwCount=0; DWORD g_dwMsgAndCertEncodingType=CRYPT_ASN_ENCODING | PKCS_7_ASN_ENCODING; DWORD g_dwCertEncodingType=CRYPT_ASN_ENCODING; //--------------------------------------------------------------------------- // Get the hModule hanlder and init two DLLMain. // //--------------------------------------------------------------------------- BOOL InitModule() { if(!(hModule=GetModuleHandle(NULL))) return FALSE; return TRUE; } static void Usage(void) { IDSwprintf(hModule, IDS_SYNTAX); IDSwprintf(hModule, IDS_SYNTAX1); IDSwprintf(hModule, IDS_OPTIONS); IDSwprintf(hModule, IDS_OPTION_U_DESC); IDSwprintf(hModule, IDS_OPTION_U_DESC1); IDSwprintf(hModule, IDS_OPTION_U_DESC2); IDSwprintf(hModule,IDS_OPTION_S_DESC); IDSwprintf(hModule,IDS_OPTION_R_DESC); IDS_IDS_IDS_IDSwprintf(hModule,IDS_OPTION_MORE_VALUE,IDS_R_CU,IDS_R_LM,IDS_R_CU); } //---------------------------------------------------------------------------- // //Build the CTL_INFO struct and encode/sign it with no signer info //---------------------------------------------------------------------------- HRESULT BuildAndEncodeCTL(DWORD dwMsgEncodingType, LPSTR szOid, DWORD dwCount, BYTE **rgpHash, DWORD *rgcbHash, BYTE **ppbEncodedCTL, DWORD *pcbEncodedCTL) { HRESULT hr=E_FAIL; CMSG_SIGNED_ENCODE_INFO sSignInfo; CTL_INFO CTLInfo; DWORD dwIndex=0; if(dwCount==0 || !rgpHash || !ppbEncodedCTL || !pcbEncodedCTL) return E_INVALIDARG; //init *ppbEncodedCTL=NULL; *pcbEncodedCTL=0; memset(&sSignInfo, 0, sizeof(CMSG_SIGNED_ENCODE_INFO)); sSignInfo.cbSize = sizeof(CMSG_SIGNED_ENCODE_INFO); memset(&CTLInfo, 0, sizeof(CTL_INFO)); //set up CTL CTLInfo.dwVersion=CTL_V1; CTLInfo.SubjectUsage.cUsageIdentifier = 1; CTLInfo.SubjectUsage.rgpszUsageIdentifier = (LPSTR *)&szOid; GetSystemTimeAsFileTime(&(CTLInfo.ThisUpdate)); CTLInfo.SubjectAlgorithm.pszObjId=szOID_OIWSEC_sha1; CTLInfo.cCTLEntry=dwCount; CTLInfo.rgCTLEntry=(CTL_ENTRY *)ToolUtlAlloc(sizeof(CTL_ENTRY)*dwCount); if(!(CTLInfo.rgCTLEntry)) { hr=E_OUTOFMEMORY; goto CLEANUP; } //memset memset(CTLInfo.rgCTLEntry, 0, sizeof(CTL_ENTRY)*dwCount); for(dwIndex=0; dwIndex= (DWORD)((argc-1))) { IDSwprintf(hModule,IDS_TOO_FEW_PARAM); goto ErrorReturn; } if(!fAllocated) { if(S_OK != WSZtoSZ(wargv[dwIndex], &szOid)) goto ErrorReturn; fAllocated=TRUE; } else { IDSwprintf(hModule,IDS_TOO_MANY_PARAM); goto ErrorReturn; } } //check for -s options else if(IDSwcsicmp(hModule, &(pwszOption[1]),IDS_OPTION_S)==0) { fSystemstore=TRUE; } //check for -r options else if(IDSwcsicmp(hModule, &(pwszOption[1]),IDS_OPTION_R)==0) { dwIndex++; if(dwIndex >= (DWORD)((argc-1))) { IDSwprintf(hModule,IDS_TOO_FEW_PARAM); goto ErrorReturn; } if(NULL==wszStoreLocation) { wszStoreLocation=wargv[dwIndex]; if(IDSwcsicmp(hModule, wszStoreLocation, IDS_R_CU)==0) dwStoreFlag=CERT_SYSTEM_STORE_CURRENT_USER; else { if(IDSwcsicmp(hModule,wszStoreLocation, IDS_R_LM)==0) dwStoreFlag=CERT_SYSTEM_STORE_LOCAL_MACHINE; else { IDSwprintf(hModule, IDS_INVALID_R); goto ErrorReturn; } } } else { IDSwprintf(hModule,IDS_TOO_MANY_PARAM); goto ErrorReturn; } } else { //print out the Usage Usage(); return ReturnStatus; } } else { //build the cert hash from the store if(S_OK !=(hr=GetCertFromStore(wargv[dwIndex], fSystemstore, dwStoreFlag))) goto ErrorReturn; //int for the next cycle fSystemstore=FALSE; wszStoreLocation=NULL; dwStoreFlag=CERT_SYSTEM_STORE_CURRENT_USER; hr=E_FAIL; } } if(0==g_dwCount) { IDSwprintf(hModule, IDS_TOO_FEW_PARAM); hr=E_FAIL; goto ErrorReturn; } //set up the CTL_INFO structure if(S_OK!=(hr=BuildAndEncodeCTL(g_dwMsgAndCertEncodingType, szOid, g_dwCount, g_rgpHash, g_rgcbHash, &pbEncodedCTL, &cbEncodedCTL))) { IDSwprintf(hModule, IDS_ERR_ENCODE_CTL); goto ErrorReturn; } //get the output file name pwszOutputFilename = wargv[argc-1]; if(S_OK!=(hr=OpenAndWriteToFile(pwszOutputFilename, pbEncodedCTL, cbEncodedCTL))) { IDSwprintf(hModule, IDS_ERR_SAVE_CTL); goto ErrorReturn; } //mark succeed ReturnStatus = 0; hr=S_OK; IDSwprintf(hModule, IDS_SUCCEEDED); goto CommonReturn; ErrorReturn: ReturnStatus = -1; //print out an error msg IDSwprintf(hModule, IDS_FAILED,hr,hr); CommonReturn: if(g_rgpHash) { for(dwIndex=0; dwIndex