验证文件数字签名

工作中碰到需要判断一个PE文件是否是所确认的文件,而不是被替换过的。直接判断文件名的话有些不保险,别人只要修改下文件名,就可以以假乱真。

因而需要判断额外的信息;由于文件有数字签名,判断数字签名因而是一个比较好的方法,但是如果只是判断数字签名是否有效也不够,别人只要用自己的证书重新签名就可以了,所以需要判断证书签名者信息。

验证文件数字签名是否有效可以使用函数 WinVerifyTrust

取得文件数字签名证书信息需要使用函数 CryptQueryObject

下面是一段从网上搜到的获得文件数字签名证书信息的代码:

[cpp]  view plain copy
  1. 1 #include <windows.h>  
  2.   2 #include <wincrypt.h>  
  3.   3 #include <wintrust.h>  
  4.   4 #include <stdio.h>  
  5.   5 #include <tchar.h>  
  6.   6  #pragma comment(lib, "crypt32.lib")  
  7.   7  #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)  
  8.   8 typedef struct {  
  9.   9     LPWSTR lpszProgramName;  
  10.  10     LPWSTR lpszPublisherLink;  
  11.  11     LPWSTR lpszMoreInfoLink;  
  12.  12 } SPROG_PUBLISHERINFO, *PSPROG_PUBLISHERINFO;  
  13.  13 BOOL GetProgAndPublisherInfo(PCMSG_SIGNER_INFO pSignerInfo,  
  14.  14                              PSPROG_PUBLISHERINFO Info);  
  15.  15 BOOL GetDateOfTimeStamp(PCMSG_SIGNER_INFO pSignerInfo, SYSTEMTIME *st);  
  16.  16 BOOL PrintCertificateInfo(PCCERT_CONTEXT pCertContext);  
  17.  17 BOOL GetTimeStampSignerInfo(PCMSG_SIGNER_INFO pSignerInfo,  
  18.  18                             PCMSG_SIGNER_INFO *pCounterSignerInfo);  
  19.  19  int _tmain(int argc, TCHAR *argv[])  
  20.  20 {  
  21.  21     WCHAR szFileName[MAX_PATH];  
  22.  22     HCERTSTORE hStore = NULL;  
  23.  23     HCRYPTMSG hMsg = NULL;  
  24.  24     PCCERT_CONTEXT pCertContext = NULL;  
  25.  25     BOOL fResult;  
  26.  26     DWORD dwEncoding, dwContentType, dwFormatType;  
  27.  27     PCMSG_SIGNER_INFO pSignerInfo = NULL;  
  28.  28     PCMSG_SIGNER_INFO pCounterSignerInfo = NULL;  
  29.  29     DWORD dwSignerInfo;  
  30.  30     CERT_INFO CertInfo;  
  31.  31     SPROG_PUBLISHERINFO ProgPubInfo;  
  32.  32     SYSTEMTIME st;  
  33.  33     ZeroMemory(&ProgPubInfo, sizeof(ProgPubInfo));  
  34.  34     __try  
  35.  35     {  
  36.  36         if (argc != 2)  
  37.  37         {  
  38.  38             _tprintf(_T("Usage: SignedFileInfo <filename>\n"));  
  39.  39             return 0;  
  40.  40         }  
  41.  41 #ifdef UNICODE  
  42.  42         lstrcpynW(szFileName, argv[1], MAX_PATH);  
  43.  43  #else  
  44.  44         if (mbstowcs(szFileName, argv[1], MAX_PATH) == -1)  
  45.  45         {  
  46.  46             printf("Unable to convert to unicode.\n");  
  47.  47             __leave;  
  48.  48         }  
  49.  49  #endif  
  50.  50         // Get message handle and store handle from the signed file.  
  51.  51          fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,  
  52.  52             szFileName,  
  53.  53             CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,  
  54.  54             CERT_QUERY_FORMAT_FLAG_BINARY,  
  55.  55             0,  
  56.  56             &dwEncoding,  
  57.  57             &dwContentType,  
  58.  58             &dwFormatType,  
  59.  59             &hStore,  
  60.  60             &hMsg,  
  61.  61             NULL);  
  62.  62         if (!fResult)  
  63.  63         {  
  64.  64             _tprintf(_T("CryptQueryObject failed with %x\n"), GetLastError());  
  65.  65             __leave;  
  66.  66         }  
  67.  67         // Get signer information size.  
  68.  68          fResult = CryptMsgGetParam(hMsg,  
  69.  69             CMSG_SIGNER_INFO_PARAM,  
  70.  70             0,  
  71.  71             NULL,  
  72.  72             &dwSignerInfo);  
  73.  73         if (!fResult)  
  74.  74         {  
  75.  75             _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());  
  76.  76             __leave;  
  77.  77         }  
  78.  78         // Allocate memory for signer information.  
  79.  79          pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);  
  80.  80         if (!pSignerInfo)  
  81.  81         {  
  82.  82             _tprintf(_T("Unable to allocate memory for Signer Info.\n"));  
  83.  83             __leave;  
  84.  84         }  
  85.  85         // Get Signer Information.  
  86.  86         fResult = CryptMsgGetParam(hMsg,  
  87.  87             CMSG_SIGNER_INFO_PARAM,  
  88.  88             0,  
  89.  89             (PVOID)pSignerInfo,  
  90.  90             &dwSignerInfo);  
  91.  91         if (!fResult)  
  92.  92         {  
  93.  93             _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());  
  94.  94             __leave;  
  95.  95         }  
  96.  96         // Get program name and publisher information from  
  97.  97         // signer info structure.  
  98.  98         if (GetProgAndPublisherInfo(pSignerInfo, &ProgPubInfo))  
  99.  99         {  
  100. 100             if (ProgPubInfo.lpszProgramName != NULL)  
  101. 101             {  
  102. 102                 wprintf(L"Program Name : %s\n",  
  103. 103                     ProgPubInfo.lpszProgramName);  
  104. 104             }  
  105. 105             if (ProgPubInfo.lpszPublisherLink != NULL)  
  106. 106             {  
  107. 107                 wprintf(L"Publisher Link : %s\n",  
  108. 108                     ProgPubInfo.lpszPublisherLink);  
  109. 109             }  
  110. 110             if (ProgPubInfo.lpszMoreInfoLink != NULL)  
  111. 111             {  
  112. 112                 wprintf(L"MoreInfo Link : %s\n",  
  113. 113                     ProgPubInfo.lpszMoreInfoLink);  
  114. 114             }  
  115. 115         }  
  116. 116         _tprintf(_T("\n"));  
  117. 117         // Search for the signer certificate in the temporary  
  118. 118         // certificate store.  
  119. 119         CertInfo.Issuer = pSignerInfo->Issuer;  
  120. 120         CertInfo.SerialNumber = pSignerInfo->SerialNumber;  
  121. 121         pCertContext = CertFindCertificateInStore(hStore,  
  122. 122             ENCODING,  
  123. 123             0,  
  124. 124             CERT_FIND_SUBJECT_CERT,  
  125. 125             (PVOID)&CertInfo,  
  126. 126             NULL);  
  127. 127         if (!pCertContext)  
  128. 128         {  
  129. 129             _tprintf(_T("CertFindCertificateInStore failed with %x\n"),  
  130. 130                 GetLastError());  
  131. 131             __leave;  
  132. 132         }  
  133. 133         // Print Signer certificate information.  
  134. 134         _tprintf(_T("Signer Certificate:\n\n"));  
  135. 135         PrintCertificateInfo(pCertContext);  
  136. 136         _tprintf(_T("\n"));  
  137. 137         // Get the timestamp certificate signerinfo structure.  
  138. 138         if (GetTimeStampSignerInfo(pSignerInfo, &pCounterSignerInfo))  
  139. 139         {  
  140. 140             // Search for Timestamp certificate in the temporary  
  141. 141             // certificate store.  
  142. 142             CertInfo.Issuer = pCounterSignerInfo->Issuer;  
  143. 143             CertInfo.SerialNumber = pCounterSignerInfo->SerialNumber;  
  144. 144             pCertContext = CertFindCertificateInStore(hStore,  
  145. 145                 ENCODING,  
  146. 146                 0,  
  147. 147                 CERT_FIND_SUBJECT_CERT,  
  148. 148                 (PVOID)&CertInfo,  
  149. 149                 NULL);  
  150. 150             if (!pCertContext)  
  151. 151             {  
  152. 152                 _tprintf(_T("CertFindCertificateInStore failed with %x\n"),  
  153. 153                     GetLastError());  
  154. 154                 __leave;  
  155. 155             }  
  156. 156             // Print timestamp certificate information.  
  157. 157             _tprintf(_T("TimeStamp Certificate:\n\n"));  
  158. 158             PrintCertificateInfo(pCertContext);  
  159. 159             _tprintf(_T("\n"));  
  160. 160             // Find Date of timestamp.  
  161. 161             if (GetDateOfTimeStamp(pCounterSignerInfo, &st))  
  162. 162             {  
  163. 163                 _tprintf(_T("Date of TimeStamp : %02d/%02d/%04d %02d:%02d\n"),  
  164. 164                     st.wMonth,  
  165. 165                     st.wDay,  
  166. 166                     st.wYear,  
  167. 167                     st.wHour,  
  168. 168                     st.wMinute);  
  169. 169             }  
  170. 170             _tprintf(_T("\n"));  
  171. 171         }  
  172. 172     }  
  173. 173     __finally  
  174. 174     {  
  175. 175         // Clean up.  
  176. 176         if (ProgPubInfo.lpszProgramName != NULL)  
  177. 177             LocalFree(ProgPubInfo.lpszProgramName);  
  178. 178         if (ProgPubInfo.lpszPublisherLink != NULL)  
  179. 179             LocalFree(ProgPubInfo.lpszPublisherLink);  
  180. 180         if (ProgPubInfo.lpszMoreInfoLink != NULL)  
  181. 181             LocalFree(ProgPubInfo.lpszMoreInfoLink);  
  182. 182         if (pSignerInfo != NULL) LocalFree(pSignerInfo);  
  183. 183         if (pCounterSignerInfo != NULL) LocalFree(pCounterSignerInfo);  
  184. 184         if (pCertContext != NULL) CertFreeCertificateContext(pCertContext);  
  185. 185         if (hStore != NULL) CertCloseStore(hStore, 0);  
  186. 186         if (hMsg != NULL) CryptMsgClose(hMsg);  
  187. 187     }  
  188. 188     return 0;  
  189. 189 }  
  190. 190 BOOL PrintCertificateInfo(PCCERT_CONTEXT pCertContext)  
  191. 191 {  
  192. 192     BOOL fReturn = FALSE;  
  193. 193     LPTSTR szName = NULL;  
  194. 194     DWORD dwData;  
  195. 195     __try  
  196. 196     {  
  197. 197         // Print Serial Number.  
  198. 198         _tprintf(_T("Serial Number: "));  
  199. 199         dwData = pCertContext->pCertInfo->SerialNumber.cbData;  
  200. 200         for (DWORD n = 0; n < dwData; n++)  
  201. 201         {  
  202. 202             _tprintf(_T("%02x "),  
  203. 203                 pCertContext->pCertInfo->SerialNumber.pbData[dwData - (n + 1)]);  
  204. 204         }  
  205. 205         _tprintf(_T("\n"));  
  206. 206         // Get Issuer name size.  
  207. 207         if (!(dwData = CertGetNameString(pCertContext,  
  208. 208             CERT_NAME_SIMPLE_DISPLAY_TYPE,  
  209. 209             CERT_NAME_ISSUER_FLAG,  
  210. 210             NULL,  
  211. 211             NULL,  
  212. 212             0)))  
  213. 213         {  
  214. 214             _tprintf(_T("CertGetNameString failed.\n"));  
  215. 215             __leave;  
  216. 216         }  
  217. 217         // Allocate memory for Issuer name.  
  218. 218         szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));  
  219. 219         if (!szName)  
  220. 220         {  
  221. 221             _tprintf(_T("Unable to allocate memory for issuer name.\n"));  
  222. 222             __leave;  
  223. 223         }  
  224. 224         // Get Issuer name.  
  225. 225         if (!(CertGetNameString(pCertContext,  
  226. 226             CERT_NAME_SIMPLE_DISPLAY_TYPE,  
  227. 227             CERT_NAME_ISSUER_FLAG,  
  228. 228             NULL,  
  229. 229             szName,  
  230. 230             dwData)))  
  231. 231         {  
  232. 232             _tprintf(_T("CertGetNameString failed.\n"));  
  233. 233             __leave;  
  234. 234         }  
  235. 235         // print Issuer name.  
  236. 236         _tprintf(_T("Issuer Name: %s\n"), szName);  
  237. 237         LocalFree(szName);  
  238. 238         szName = NULL;  
  239. 239         // Get Subject name size.  
  240. 240         if (!(dwData = CertGetNameString(pCertContext,  
  241. 241             CERT_NAME_SIMPLE_DISPLAY_TYPE,  
  242. 242             0,  
  243. 243             NULL,  
  244. 244             NULL,  
  245. 245             0)))  
  246. 246         {  
  247. 247             _tprintf(_T("CertGetNameString failed.\n"));  
  248. 248             __leave;  
  249. 249         }  
  250. 250         // Allocate memory for subject name.  
  251. 251         szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));  
  252. 252         if (!szName)  
  253. 253         {  
  254. 254             _tprintf(_T("Unable to allocate memory for subject name.\n"));  
  255. 255             __leave;  
  256. 256         }  
  257. 257         // Get subject name.  
  258. 258         if (!(CertGetNameString(pCertContext,  
  259. 259             CERT_NAME_SIMPLE_DISPLAY_TYPE,  
  260. 260             0,  
  261. 261             NULL,  
  262. 262             szName,  
  263. 263             dwData)))  
  264. 264         {  
  265. 265             _tprintf(_T("CertGetNameString failed.\n"));  
  266. 266             __leave;  
  267. 267         }  
  268. 268         // Print Subject Name.  
  269. 269         _tprintf(_T("Subject Name: %s\n"), szName);  
  270. 270         fReturn = TRUE;  
  271. 271     }  
  272. 272     __finally  
  273. 273     {  
  274. 274         if (szName != NULL) LocalFree(szName);  
  275. 275     }  
  276. 276     return fReturn;  
  277. 277 }  
  278. 278 LPWSTR AllocateAndCopyWideString(LPCWSTR inputString)  
  279. 279 {  
  280. 280     LPWSTR outputString = NULL;  
  281. 281     outputString = (LPWSTR)LocalAlloc(LPTR,  
  282. 282         (wcslen(inputString) + 1) * sizeof(WCHAR));  
  283. 283     if (outputString != NULL)  
  284. 284     {  
  285. 285         lstrcpyW(outputString, inputString);  
  286. 286     }  
  287. 287     return outputString;  
  288. 288 }  
  289. 289 BOOL GetProgAndPublisherInfo(PCMSG_SIGNER_INFO pSignerInfo,  
  290. 290                              PSPROG_PUBLISHERINFO Info)  
  291. 291 {  
  292. 292     BOOL fReturn = FALSE;  
  293. 293     PSPC_SP_OPUS_INFO OpusInfo = NULL;  
  294. 294     DWORD dwData;  
  295. 295     BOOL fResult;  
  296. 296     __try  
  297. 297     {  
  298. 298         // Loop through authenticated attributes and find  
  299. 299         // SPC_SP_OPUS_INFO_OBJID OID.  
  300. 300         for (DWORD n = 0; n < pSignerInfo->AuthAttrs.cAttr; n++)  
  301. 301         {  
  302. 302             if (lstrcmpA(SPC_SP_OPUS_INFO_OBJID,  
  303. 303                 pSignerInfo->AuthAttrs.rgAttr[n].pszObjId) == 0)  
  304. 304             {  
  305. 305                 // Get Size of SPC_SP_OPUS_INFO structure.  
  306. 306                 fResult = CryptDecodeObject(ENCODING,  
  307. 307                     SPC_SP_OPUS_INFO_OBJID,  
  308. 308                     pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,  
  309. 309                     pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,  
  310. 310                     0,  
  311. 311                     NULL,  
  312. 312                     &dwData);  
  313. 313                 if (!fResult)  
  314. 314                 {  
  315. 315                     _tprintf(_T("CryptDecodeObject failed with %x\n"),  
  316. 316                         GetLastError());  
  317. 317                     __leave;  
  318. 318                 }  
  319. 319                 // Allocate memory for SPC_SP_OPUS_INFO structure.  
  320. 320                 OpusInfo = (PSPC_SP_OPUS_INFO)LocalAlloc(LPTR, dwData);  
  321. 321                 if (!OpusInfo)  
  322. 322                 {  
  323. 323                     _tprintf(_T("Unable to allocate memory for Publisher Info.\n"));  
  324. 324                     __leave;  
  325. 325                 }  
  326. 326                 // Decode and get SPC_SP_OPUS_INFO structure.  
  327. 327                 fResult = CryptDecodeObject(ENCODING,  
  328. 328                     SPC_SP_OPUS_INFO_OBJID,  
  329. 329                     pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,  
  330. 330                     pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,  
  331. 331                     0,  
  332. 332                     OpusInfo,  
  333. 333                     &dwData);  
  334. 334                 if (!fResult)  
  335. 335                 {  
  336. 336                     _tprintf(_T("CryptDecodeObject failed with %x\n"),  
  337. 337                         GetLastError());  
  338. 338                     __leave;  
  339. 339                 }  
  340. 340                 // Fill in Program Name if present.  
  341. 341                 if (OpusInfo->pwszProgramName)  
  342. 342                 {  
  343. 343                     Info->lpszProgramName =  
  344. 344                         AllocateAndCopyWideString(OpusInfo->pwszProgramName);  
  345. 345                 }  
  346. 346                 else  
  347. 347                     Info->lpszProgramName = NULL;  
  348. 348                 // Fill in Publisher Information if present.  
  349. 349                 if (OpusInfo->pPublisherInfo)  
  350. 350                 {  
  351. 351                     switch (OpusInfo->pPublisherInfo->dwLinkChoice)  
  352. 352                     {  
  353. 353                     case SPC_URL_LINK_CHOICE:  
  354. 354                         Info->lpszPublisherLink =  
  355. 355                             AllocateAndCopyWideString(OpusInfo->pPublisherInfo->pwszUrl);  
  356. 356                         break;  
  357. 357                     case SPC_FILE_LINK_CHOICE:  
  358. 358                         Info->lpszPublisherLink =  
  359. 359                             AllocateAndCopyWideString(OpusInfo->pPublisherInfo->pwszFile);  
  360. 360                         break;  
  361. 361                     default:  
  362. 362                         Info->lpszPublisherLink = NULL;  
  363. 363                         break;  
  364. 364                     }  
  365. 365                 }  
  366. 366                 else  
  367. 367                 {  
  368. 368                     Info->lpszPublisherLink = NULL;  
  369. 369                 }  
  370. 370                 // Fill in More Info if present.  
  371. 371                 if (OpusInfo->pMoreInfo)  
  372. 372                 {  
  373. 373                     switch (OpusInfo->pMoreInfo->dwLinkChoice)  
  374. 374                     {  
  375. 375                     case SPC_URL_LINK_CHOICE:  
  376. 376                         Info->lpszMoreInfoLink =  
  377. 377                             AllocateAndCopyWideString(OpusInfo->pMoreInfo->pwszUrl);  
  378. 378                         break;  
  379. 379                     case SPC_FILE_LINK_CHOICE:  
  380. 380                         Info->lpszMoreInfoLink =  
  381. 381                             AllocateAndCopyWideString(OpusInfo->pMoreInfo->pwszFile);  
  382. 382                         break;  
  383. 383                     default:  
  384. 384                         Info->lpszMoreInfoLink = NULL;  
  385. 385                         break;  
  386. 386                     }  
  387. 387                 }  
  388. 388                 else  
  389. 389                 {  
  390. 390                     Info->lpszMoreInfoLink = NULL;  
  391. 391                 }  
  392. 392                 fReturn = TRUE;  
  393. 393                 break// Break from for loop.  
  394. 394             } // lstrcmp SPC_SP_OPUS_INFO_OBJID  
  395. 395         } // for  
  396. 396     }  
  397. 397     __finally  
  398. 398     {  
  399. 399         if (OpusInfo != NULL) LocalFree(OpusInfo);  
  400. 400     }  
  401. 401     return fReturn;  
  402. 402 }  
  403. 403 BOOL GetDateOfTimeStamp(PCMSG_SIGNER_INFO pSignerInfo, SYSTEMTIME *st)  
  404. 404 {  
  405. 405     BOOL fResult;  
  406. 406     FILETIME lft, ft;  
  407. 407     DWORD dwData;  
  408. 408     BOOL fReturn = FALSE;  
  409. 409     // Loop through authenticated attributes and find  
  410. 410     // szOID_RSA_signingTime OID.  
  411. 411     for (DWORD n = 0; n < pSignerInfo->AuthAttrs.cAttr; n++)  
  412. 412     {  
  413. 413         if (lstrcmpA(szOID_RSA_signingTime,  
  414. 414             pSignerInfo->AuthAttrs.rgAttr[n].pszObjId) == 0)  
  415. 415         {  
  416. 416             // Decode and get FILETIME structure.  
  417. 417             dwData = sizeof(ft);  
  418. 418             fResult = CryptDecodeObject(ENCODING,  
  419. 419                 szOID_RSA_signingTime,  
  420. 420                 pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].pbData,  
  421. 421                 pSignerInfo->AuthAttrs.rgAttr[n].rgValue[0].cbData,  
  422. 422                 0,  
  423. 423                 (PVOID)&ft,  
  424. 424                 &dwData);  
  425. 425             if (!fResult)  
  426. 426             {  
  427. 427                 _tprintf(_T("CryptDecodeObject failed with %x\n"),  
  428. 428                     GetLastError());  
  429. 429                 break;  
  430. 430             }  
  431. 431             // Convert to local time.  
  432. 432             FileTimeToLocalFileTime(&ft, &lft);  
  433. 433             FileTimeToSystemTime(&lft, st);  
  434. 434             fReturn = TRUE;  
  435. 435             break// Break from for loop.  
  436. 436         } //lstrcmp szOID_RSA_signingTime  
  437. 437     } // for  
  438. 438     return fReturn;  
  439. 439 }  
  440. 440 BOOL GetTimeStampSignerInfo(PCMSG_SIGNER_INFO pSignerInfo, PCMSG_SIGNER_INFO *pCounterSignerInfo)  
  441. 441 {  
  442. 442     PCCERT_CONTEXT pCertContext = NULL;  
  443. 443     BOOL fReturn = FALSE;  
  444. 444     BOOL fResult;  
  445. 445     DWORD dwSize;  
  446. 446     __try  
  447. 447     {  
  448. 448         *pCounterSignerInfo = NULL;  
  449. 449         // Loop through unathenticated attributes for  
  450. 450         // szOID_RSA_counterSign OID.  
  451. 451         for (DWORD n = 0; n < pSignerInfo->UnauthAttrs.cAttr; n++)  
  452. 452         {  
  453. 453             if (lstrcmpA(pSignerInfo->UnauthAttrs.rgAttr[n].pszObjId,  
  454. 454                 szOID_RSA_counterSign) == 0)  
  455. 455             {  
  456. 456                 // Get size of CMSG_SIGNER_INFO structure.  
  457. 457                 fResult = CryptDecodeObject(ENCODING,  
  458. 458                     PKCS7_SIGNER_INFO,  
  459. 459                     pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].pbData,  
  460. 460                     pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].cbData,  
  461. 461                     0,  
  462. 462                     NULL,  
  463. 463                     &dwSize);  
  464. 464                 if (!fResult)  
  465. 465                 {  
  466. 466                     _tprintf(_T("CryptDecodeObject failed with %x\n"),  
  467. 467                         GetLastError());  
  468. 468                     __leave;  
  469. 469                 }  
  470. 470                 // Allocate memory for CMSG_SIGNER_INFO.  
  471. 471                 *pCounterSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSize);  
  472. 472                 if (!*pCounterSignerInfo)  
  473. 473                 {  
  474. 474                     _tprintf(_T("Unable to allocate memory for timestamp info.\n"));  
  475. 475                     __leave;  
  476. 476                 }  
  477. 477                 // Decode and get CMSG_SIGNER_INFO structure  
  478. 478                 // for timestamp certificate.  
  479. 479                 fResult = CryptDecodeObject(ENCODING,  
  480. 480                     PKCS7_SIGNER_INFO,  
  481. 481                     pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].pbData,  
  482. 482                     pSignerInfo->UnauthAttrs.rgAttr[n].rgValue[0].cbData,  
  483. 483                     0,  
  484. 484                     (PVOID)*pCounterSignerInfo,  
  485. 485                     &dwSize);  
  486. 486                 if (!fResult)  
  487. 487                 {  
  488. 488                     _tprintf(_T("CryptDecodeObject failed with %x\n"),  
  489. 489                         GetLastError());  
  490. 490                     __leave;  
  491. 491                 }  
  492. 492                 fReturn = TRUE;  
  493. 493                 break// Break from for loop.  
  494. 494             }  
  495. 495         }  
  496. 496     }  
  497. 497     __finally  
  498. 498     {  
  499. 499         // Clean up.  
  500. 500         if (pCertContext != NULL) CertFreeCertificateContext(pCertContext);  
  501. 501     }  
  502. 502     return fReturn;  
  503. 503 }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值