CryptPkcs7VerifyCommon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /** @file
  2. PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.
  3. Caution: This module requires additional review when modified.
  4. This library will have external input - signature (e.g. UEFI Authenticated
  5. Variable). It may by input in SMM mode.
  6. This external input must be validated carefully to avoid security issue like
  7. buffer overflow, integer overflow.
  8. WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated
  9. Variable and will do basic check for data structure.
  10. Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
  11. SPDX-License-Identifier: BSD-2-Clause-Patent
  12. **/
  13. #include "InternalCryptLib.h"
  14. #include <openssl/objects.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/pkcs7.h>
  18. UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
  19. /**
  20. Check input P7Data is a wrapped ContentInfo structure or not. If not construct
  21. a new structure to wrap P7Data.
  22. Caution: This function may receive untrusted input.
  23. UEFI Authenticated Variable is external input, so this function will do basic
  24. check for PKCS#7 data structure.
  25. @param[in] P7Data Pointer to the PKCS#7 message to verify.
  26. @param[in] P7Length Length of the PKCS#7 message in bytes.
  27. @param[out] WrapFlag If TRUE P7Data is a ContentInfo structure, otherwise
  28. return FALSE.
  29. @param[out] WrapData If return status of this function is TRUE:
  30. 1) when WrapFlag is TRUE, pointer to P7Data.
  31. 2) when WrapFlag is FALSE, pointer to a new ContentInfo
  32. structure. It's caller's responsibility to free this
  33. buffer.
  34. @param[out] WrapDataSize Length of ContentInfo structure in bytes.
  35. @retval TRUE The operation is finished successfully.
  36. @retval FALSE The operation is failed due to lack of resources.
  37. **/
  38. BOOLEAN
  39. WrapPkcs7Data (
  40. IN CONST UINT8 *P7Data,
  41. IN UINTN P7Length,
  42. OUT BOOLEAN *WrapFlag,
  43. OUT UINT8 **WrapData,
  44. OUT UINTN *WrapDataSize
  45. )
  46. {
  47. BOOLEAN Wrapped;
  48. UINT8 *SignedData;
  49. //
  50. // Check whether input P7Data is a wrapped ContentInfo structure or not.
  51. //
  52. Wrapped = FALSE;
  53. if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {
  54. if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {
  55. if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {
  56. Wrapped = TRUE;
  57. }
  58. }
  59. }
  60. if (Wrapped) {
  61. *WrapData = (UINT8 *) P7Data;
  62. *WrapDataSize = P7Length;
  63. } else {
  64. //
  65. // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.
  66. //
  67. *WrapDataSize = P7Length + 19;
  68. *WrapData = malloc (*WrapDataSize);
  69. if (*WrapData == NULL) {
  70. *WrapFlag = Wrapped;
  71. return FALSE;
  72. }
  73. SignedData = *WrapData;
  74. //
  75. // Part1: 0x30, 0x82.
  76. //
  77. SignedData[0] = 0x30;
  78. SignedData[1] = 0x82;
  79. //
  80. // Part2: Length1 = P7Length + 19 - 4, in big endian.
  81. //
  82. SignedData[2] = (UINT8) (((UINT16) (*WrapDataSize - 4)) >> 8);
  83. SignedData[3] = (UINT8) (((UINT16) (*WrapDataSize - 4)) & 0xff);
  84. //
  85. // Part3: 0x06, 0x09.
  86. //
  87. SignedData[4] = 0x06;
  88. SignedData[5] = 0x09;
  89. //
  90. // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.
  91. //
  92. CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));
  93. //
  94. // Part5: 0xA0, 0x82.
  95. //
  96. SignedData[15] = 0xA0;
  97. SignedData[16] = 0x82;
  98. //
  99. // Part6: Length2 = P7Length, in big endian.
  100. //
  101. SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);
  102. SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);
  103. //
  104. // Part7: P7Data.
  105. //
  106. CopyMem (SignedData + 19, P7Data, P7Length);
  107. }
  108. *WrapFlag = Wrapped;
  109. return TRUE;
  110. }
  111. /**
  112. Pop single certificate from STACK_OF(X509).
  113. If X509Stack, Cert, or CertSize is NULL, then return FALSE.
  114. @param[in] X509Stack Pointer to a X509 stack object.
  115. @param[out] Cert Pointer to a X509 certificate.
  116. @param[out] CertSize Length of output X509 certificate in bytes.
  117. @retval TRUE The X509 stack pop succeeded.
  118. @retval FALSE The pop operation failed.
  119. **/
  120. BOOLEAN
  121. X509PopCertificate (
  122. IN VOID *X509Stack,
  123. OUT UINT8 **Cert,
  124. OUT UINTN *CertSize
  125. )
  126. {
  127. BIO *CertBio;
  128. X509 *X509Cert;
  129. STACK_OF(X509) *CertStack;
  130. BOOLEAN Status;
  131. INT32 Result;
  132. BUF_MEM *Ptr;
  133. INT32 Length;
  134. VOID *Buffer;
  135. Status = FALSE;
  136. if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) {
  137. return Status;
  138. }
  139. CertStack = (STACK_OF(X509) *) X509Stack;
  140. X509Cert = sk_X509_pop (CertStack);
  141. if (X509Cert == NULL) {
  142. return Status;
  143. }
  144. Buffer = NULL;
  145. CertBio = BIO_new (BIO_s_mem ());
  146. if (CertBio == NULL) {
  147. return Status;
  148. }
  149. Result = i2d_X509_bio (CertBio, X509Cert);
  150. if (Result == 0) {
  151. goto _Exit;
  152. }
  153. BIO_get_mem_ptr (CertBio, &Ptr);
  154. Length = (INT32)(Ptr->length);
  155. if (Length <= 0) {
  156. goto _Exit;
  157. }
  158. Buffer = malloc (Length);
  159. if (Buffer == NULL) {
  160. goto _Exit;
  161. }
  162. Result = BIO_read (CertBio, Buffer, Length);
  163. if (Result != Length) {
  164. goto _Exit;
  165. }
  166. *Cert = Buffer;
  167. *CertSize = Length;
  168. Status = TRUE;
  169. _Exit:
  170. BIO_free (CertBio);
  171. if (!Status && (Buffer != NULL)) {
  172. free (Buffer);
  173. }
  174. return Status;
  175. }
  176. /**
  177. Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
  178. Cryptographic Message Syntax Standard". The input signed data could be wrapped
  179. in a ContentInfo structure.
  180. If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
  181. return FALSE. If P7Length overflow, then return FALSE.
  182. Caution: This function may receive untrusted input.
  183. UEFI Authenticated Variable is external input, so this function will do basic
  184. check for PKCS#7 data structure.
  185. @param[in] P7Data Pointer to the PKCS#7 message to verify.
  186. @param[in] P7Length Length of the PKCS#7 message in bytes.
  187. @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
  188. It's caller's responsibility to free the buffer with
  189. Pkcs7FreeSigners().
  190. This data structure is EFI_CERT_STACK type.
  191. @param[out] StackLength Length of signer's certificates in bytes.
  192. @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
  193. It's caller's responsibility to free the buffer with
  194. Pkcs7FreeSigners().
  195. @param[out] CertLength Length of the trusted certificate in bytes.
  196. @retval TRUE The operation is finished successfully.
  197. @retval FALSE Error occurs during the operation.
  198. **/
  199. BOOLEAN
  200. EFIAPI
  201. Pkcs7GetSigners (
  202. IN CONST UINT8 *P7Data,
  203. IN UINTN P7Length,
  204. OUT UINT8 **CertStack,
  205. OUT UINTN *StackLength,
  206. OUT UINT8 **TrustedCert,
  207. OUT UINTN *CertLength
  208. )
  209. {
  210. PKCS7 *Pkcs7;
  211. BOOLEAN Status;
  212. UINT8 *SignedData;
  213. CONST UINT8 *Temp;
  214. UINTN SignedDataSize;
  215. BOOLEAN Wrapped;
  216. STACK_OF(X509) *Stack;
  217. UINT8 Index;
  218. UINT8 *CertBuf;
  219. UINT8 *OldBuf;
  220. UINTN BufferSize;
  221. UINTN OldSize;
  222. UINT8 *SingleCert;
  223. UINTN SingleCertSize;
  224. if ((P7Data == NULL) || (CertStack == NULL) || (StackLength == NULL) ||
  225. (TrustedCert == NULL) || (CertLength == NULL) || (P7Length > INT_MAX)) {
  226. return FALSE;
  227. }
  228. Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
  229. if (!Status) {
  230. return Status;
  231. }
  232. Status = FALSE;
  233. Pkcs7 = NULL;
  234. Stack = NULL;
  235. CertBuf = NULL;
  236. OldBuf = NULL;
  237. SingleCert = NULL;
  238. //
  239. // Retrieve PKCS#7 Data (DER encoding)
  240. //
  241. if (SignedDataSize > INT_MAX) {
  242. goto _Exit;
  243. }
  244. Temp = SignedData;
  245. Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);
  246. if (Pkcs7 == NULL) {
  247. goto _Exit;
  248. }
  249. //
  250. // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
  251. //
  252. if (!PKCS7_type_is_signed (Pkcs7)) {
  253. goto _Exit;
  254. }
  255. Stack = PKCS7_get0_signers(Pkcs7, NULL, PKCS7_BINARY);
  256. if (Stack == NULL) {
  257. goto _Exit;
  258. }
  259. //
  260. // Convert CertStack to buffer in following format:
  261. // UINT8 CertNumber;
  262. // UINT32 Cert1Length;
  263. // UINT8 Cert1[];
  264. // UINT32 Cert2Length;
  265. // UINT8 Cert2[];
  266. // ...
  267. // UINT32 CertnLength;
  268. // UINT8 Certn[];
  269. //
  270. BufferSize = sizeof (UINT8);
  271. OldSize = BufferSize;
  272. for (Index = 0; ; Index++) {
  273. Status = X509PopCertificate (Stack, &SingleCert, &SingleCertSize);
  274. if (!Status) {
  275. break;
  276. }
  277. OldSize = BufferSize;
  278. OldBuf = CertBuf;
  279. BufferSize = OldSize + SingleCertSize + sizeof (UINT32);
  280. CertBuf = malloc (BufferSize);
  281. if (CertBuf == NULL) {
  282. goto _Exit;
  283. }
  284. if (OldBuf != NULL) {
  285. CopyMem (CertBuf, OldBuf, OldSize);
  286. free (OldBuf);
  287. OldBuf = NULL;
  288. }
  289. WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) SingleCertSize);
  290. CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, SingleCertSize);
  291. free (SingleCert);
  292. SingleCert = NULL;
  293. }
  294. if (CertBuf != NULL) {
  295. //
  296. // Update CertNumber.
  297. //
  298. CertBuf[0] = Index;
  299. *CertLength = BufferSize - OldSize - sizeof (UINT32);
  300. *TrustedCert = malloc (*CertLength);
  301. if (*TrustedCert == NULL) {
  302. goto _Exit;
  303. }
  304. CopyMem (*TrustedCert, CertBuf + OldSize + sizeof (UINT32), *CertLength);
  305. *CertStack = CertBuf;
  306. *StackLength = BufferSize;
  307. Status = TRUE;
  308. }
  309. _Exit:
  310. //
  311. // Release Resources
  312. //
  313. if (!Wrapped) {
  314. free (SignedData);
  315. }
  316. if (Pkcs7 != NULL) {
  317. PKCS7_free (Pkcs7);
  318. }
  319. if (Stack != NULL) {
  320. sk_X509_pop_free(Stack, X509_free);
  321. }
  322. if (SingleCert != NULL) {
  323. free (SingleCert);
  324. }
  325. if (!Status && (CertBuf != NULL)) {
  326. free (CertBuf);
  327. *CertStack = NULL;
  328. }
  329. if (OldBuf != NULL) {
  330. free (OldBuf);
  331. }
  332. return Status;
  333. }
  334. /**
  335. Wrap function to use free() to free allocated memory for certificates.
  336. @param[in] Certs Pointer to the certificates to be freed.
  337. **/
  338. VOID
  339. EFIAPI
  340. Pkcs7FreeSigners (
  341. IN UINT8 *Certs
  342. )
  343. {
  344. if (Certs == NULL) {
  345. return;
  346. }
  347. free (Certs);
  348. }
  349. /**
  350. Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
  351. Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
  352. unchained to the signer's certificates.
  353. The input signed data could be wrapped in a ContentInfo structure.
  354. @param[in] P7Data Pointer to the PKCS#7 message.
  355. @param[in] P7Length Length of the PKCS#7 message in bytes.
  356. @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
  357. certificate. It's caller's responsibility to free the buffer
  358. with Pkcs7FreeSigners().
  359. This data structure is EFI_CERT_STACK type.
  360. @param[out] ChainLength Length of the chained certificates list buffer in bytes.
  361. @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
  362. responsibility to free the buffer with Pkcs7FreeSigners().
  363. This data structure is EFI_CERT_STACK type.
  364. @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
  365. @retval TRUE The operation is finished successfully.
  366. @retval FALSE Error occurs during the operation.
  367. **/
  368. BOOLEAN
  369. EFIAPI
  370. Pkcs7GetCertificatesList (
  371. IN CONST UINT8 *P7Data,
  372. IN UINTN P7Length,
  373. OUT UINT8 **SignerChainCerts,
  374. OUT UINTN *ChainLength,
  375. OUT UINT8 **UnchainCerts,
  376. OUT UINTN *UnchainLength
  377. )
  378. {
  379. BOOLEAN Status;
  380. UINT8 *NewP7Data;
  381. UINTN NewP7Length;
  382. BOOLEAN Wrapped;
  383. UINT8 Index;
  384. PKCS7 *Pkcs7;
  385. X509_STORE_CTX *CertCtx;
  386. STACK_OF(X509) *CtxChain;
  387. STACK_OF(X509) *CtxUntrusted;
  388. X509 *CtxCert;
  389. STACK_OF(X509) *Signers;
  390. X509 *Signer;
  391. X509 *Cert;
  392. X509 *Issuer;
  393. X509_NAME *IssuerName;
  394. UINT8 *CertBuf;
  395. UINT8 *OldBuf;
  396. UINTN BufferSize;
  397. UINTN OldSize;
  398. UINT8 *SingleCert;
  399. UINTN CertSize;
  400. //
  401. // Initializations
  402. //
  403. Status = FALSE;
  404. NewP7Data = NULL;
  405. Pkcs7 = NULL;
  406. CertCtx = NULL;
  407. CtxChain = NULL;
  408. CtxCert = NULL;
  409. CtxUntrusted = NULL;
  410. Cert = NULL;
  411. SingleCert = NULL;
  412. CertBuf = NULL;
  413. OldBuf = NULL;
  414. Signers = NULL;
  415. ZeroMem (&CertCtx, sizeof (CertCtx));
  416. //
  417. // Parameter Checking
  418. //
  419. if ((P7Data == NULL) || (SignerChainCerts == NULL) || (ChainLength == NULL) ||
  420. (UnchainCerts == NULL) || (UnchainLength == NULL) || (P7Length > INT_MAX)) {
  421. return Status;
  422. }
  423. *SignerChainCerts = NULL;
  424. *ChainLength = 0;
  425. *UnchainCerts = NULL;
  426. *UnchainLength = 0;
  427. //
  428. // Construct a new PKCS#7 data wrapping with ContentInfo structure if needed.
  429. //
  430. Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &NewP7Data, &NewP7Length);
  431. if (!Status || (NewP7Length > INT_MAX)) {
  432. goto _Error;
  433. }
  434. //
  435. // Decodes PKCS#7 SignedData
  436. //
  437. Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &NewP7Data, (int) NewP7Length);
  438. if ((Pkcs7 == NULL) || (!PKCS7_type_is_signed (Pkcs7))) {
  439. goto _Error;
  440. }
  441. //
  442. // Obtains Signer's Certificate from PKCS#7 data
  443. // NOTE: Only one signer case will be handled in this function, which means SignerInfos
  444. // should include only one signer's certificate.
  445. //
  446. Signers = PKCS7_get0_signers (Pkcs7, NULL, PKCS7_BINARY);
  447. if ((Signers == NULL) || (sk_X509_num (Signers) != 1)) {
  448. goto _Error;
  449. }
  450. Signer = sk_X509_value (Signers, 0);
  451. CertCtx = X509_STORE_CTX_new ();
  452. if (CertCtx == NULL) {
  453. goto _Error;
  454. }
  455. if (!X509_STORE_CTX_init (CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {
  456. goto _Error;
  457. }
  458. //
  459. // Initialize Chained & Untrusted stack
  460. //
  461. CtxChain = X509_STORE_CTX_get0_chain (CertCtx);
  462. CtxCert = X509_STORE_CTX_get0_cert (CertCtx);
  463. if (CtxChain == NULL) {
  464. if (((CtxChain = sk_X509_new_null ()) == NULL) ||
  465. (!sk_X509_push (CtxChain, CtxCert))) {
  466. goto _Error;
  467. }
  468. }
  469. CtxUntrusted = X509_STORE_CTX_get0_untrusted (CertCtx);
  470. if (CtxUntrusted != NULL) {
  471. (VOID)sk_X509_delete_ptr (CtxUntrusted, Signer);
  472. }
  473. //
  474. // Build certificates stack chained from Signer's certificate.
  475. //
  476. Cert = Signer;
  477. for (; ;) {
  478. //
  479. // Self-Issue checking
  480. //
  481. Issuer = NULL;
  482. if (X509_STORE_CTX_get1_issuer (&Issuer, CertCtx, Cert) == 1) {
  483. if (X509_cmp (Issuer, Cert) == 0) {
  484. break;
  485. }
  486. }
  487. //
  488. // Found the issuer of the current certificate
  489. //
  490. if (CtxUntrusted != NULL) {
  491. Issuer = NULL;
  492. IssuerName = X509_get_issuer_name (Cert);
  493. Issuer = X509_find_by_subject (CtxUntrusted, IssuerName);
  494. if (Issuer != NULL) {
  495. if (!sk_X509_push (CtxChain, Issuer)) {
  496. goto _Error;
  497. }
  498. (VOID)sk_X509_delete_ptr (CtxUntrusted, Issuer);
  499. Cert = Issuer;
  500. continue;
  501. }
  502. }
  503. break;
  504. }
  505. //
  506. // Converts Chained and Untrusted Certificate to Certificate Buffer in following format:
  507. // UINT8 CertNumber;
  508. // UINT32 Cert1Length;
  509. // UINT8 Cert1[];
  510. // UINT32 Cert2Length;
  511. // UINT8 Cert2[];
  512. // ...
  513. // UINT32 CertnLength;
  514. // UINT8 Certn[];
  515. //
  516. if (CtxChain != NULL) {
  517. BufferSize = sizeof (UINT8);
  518. CertBuf = NULL;
  519. for (Index = 0; ; Index++) {
  520. Status = X509PopCertificate (CtxChain, &SingleCert, &CertSize);
  521. if (!Status) {
  522. break;
  523. }
  524. OldSize = BufferSize;
  525. OldBuf = CertBuf;
  526. BufferSize = OldSize + CertSize + sizeof (UINT32);
  527. CertBuf = malloc (BufferSize);
  528. if (CertBuf == NULL) {
  529. Status = FALSE;
  530. goto _Error;
  531. }
  532. if (OldBuf != NULL) {
  533. CopyMem (CertBuf, OldBuf, OldSize);
  534. free (OldBuf);
  535. OldBuf = NULL;
  536. }
  537. WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);
  538. CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);
  539. free (SingleCert);
  540. SingleCert = NULL;
  541. }
  542. if (CertBuf != NULL) {
  543. //
  544. // Update CertNumber.
  545. //
  546. CertBuf[0] = Index;
  547. *SignerChainCerts = CertBuf;
  548. *ChainLength = BufferSize;
  549. }
  550. }
  551. if (CtxUntrusted != NULL) {
  552. BufferSize = sizeof (UINT8);
  553. CertBuf = NULL;
  554. for (Index = 0; ; Index++) {
  555. Status = X509PopCertificate (CtxUntrusted, &SingleCert, &CertSize);
  556. if (!Status) {
  557. break;
  558. }
  559. OldSize = BufferSize;
  560. OldBuf = CertBuf;
  561. BufferSize = OldSize + CertSize + sizeof (UINT32);
  562. CertBuf = malloc (BufferSize);
  563. if (CertBuf == NULL) {
  564. Status = FALSE;
  565. goto _Error;
  566. }
  567. if (OldBuf != NULL) {
  568. CopyMem (CertBuf, OldBuf, OldSize);
  569. free (OldBuf);
  570. OldBuf = NULL;
  571. }
  572. WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);
  573. CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);
  574. free (SingleCert);
  575. SingleCert = NULL;
  576. }
  577. if (CertBuf != NULL) {
  578. //
  579. // Update CertNumber.
  580. //
  581. CertBuf[0] = Index;
  582. *UnchainCerts = CertBuf;
  583. *UnchainLength = BufferSize;
  584. }
  585. }
  586. Status = TRUE;
  587. _Error:
  588. //
  589. // Release Resources.
  590. //
  591. if (!Wrapped && (NewP7Data != NULL)) {
  592. free (NewP7Data);
  593. }
  594. if (Pkcs7 != NULL) {
  595. PKCS7_free (Pkcs7);
  596. }
  597. sk_X509_free (Signers);
  598. if (CertCtx != NULL) {
  599. X509_STORE_CTX_cleanup (CertCtx);
  600. X509_STORE_CTX_free (CertCtx);
  601. }
  602. if (SingleCert != NULL) {
  603. free (SingleCert);
  604. }
  605. if (OldBuf != NULL) {
  606. free (OldBuf);
  607. }
  608. if (!Status && (CertBuf != NULL)) {
  609. free (CertBuf);
  610. *SignerChainCerts = NULL;
  611. *UnchainCerts = NULL;
  612. }
  613. return Status;
  614. }
  615. /**
  616. Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
  617. Cryptographic Message Syntax Standard". The input signed data could be wrapped
  618. in a ContentInfo structure.
  619. If P7Data, TrustedCert or InData is NULL, then return FALSE.
  620. If P7Length, CertLength or DataLength overflow, then return FALSE.
  621. Caution: This function may receive untrusted input.
  622. UEFI Authenticated Variable is external input, so this function will do basic
  623. check for PKCS#7 data structure.
  624. @param[in] P7Data Pointer to the PKCS#7 message to verify.
  625. @param[in] P7Length Length of the PKCS#7 message in bytes.
  626. @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
  627. is used for certificate chain verification.
  628. @param[in] CertLength Length of the trusted certificate in bytes.
  629. @param[in] InData Pointer to the content to be verified.
  630. @param[in] DataLength Length of InData in bytes.
  631. @retval TRUE The specified PKCS#7 signed data is valid.
  632. @retval FALSE Invalid PKCS#7 signed data.
  633. **/
  634. BOOLEAN
  635. EFIAPI
  636. Pkcs7Verify (
  637. IN CONST UINT8 *P7Data,
  638. IN UINTN P7Length,
  639. IN CONST UINT8 *TrustedCert,
  640. IN UINTN CertLength,
  641. IN CONST UINT8 *InData,
  642. IN UINTN DataLength
  643. )
  644. {
  645. PKCS7 *Pkcs7;
  646. BIO *DataBio;
  647. BOOLEAN Status;
  648. X509 *Cert;
  649. X509_STORE *CertStore;
  650. UINT8 *SignedData;
  651. CONST UINT8 *Temp;
  652. UINTN SignedDataSize;
  653. BOOLEAN Wrapped;
  654. //
  655. // Check input parameters.
  656. //
  657. if (P7Data == NULL || TrustedCert == NULL || InData == NULL ||
  658. P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) {
  659. return FALSE;
  660. }
  661. Pkcs7 = NULL;
  662. DataBio = NULL;
  663. Cert = NULL;
  664. CertStore = NULL;
  665. //
  666. // Register & Initialize necessary digest algorithms for PKCS#7 Handling
  667. //
  668. if (EVP_add_digest (EVP_md5 ()) == 0) {
  669. return FALSE;
  670. }
  671. if (EVP_add_digest (EVP_sha1 ()) == 0) {
  672. return FALSE;
  673. }
  674. if (EVP_add_digest (EVP_sha256 ()) == 0) {
  675. return FALSE;
  676. }
  677. if (EVP_add_digest (EVP_sha384 ()) == 0) {
  678. return FALSE;
  679. }
  680. if (EVP_add_digest (EVP_sha512 ()) == 0) {
  681. return FALSE;
  682. }
  683. if (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA) == 0) {
  684. return FALSE;
  685. }
  686. Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
  687. if (!Status) {
  688. return Status;
  689. }
  690. Status = FALSE;
  691. //
  692. // Retrieve PKCS#7 Data (DER encoding)
  693. //
  694. if (SignedDataSize > INT_MAX) {
  695. goto _Exit;
  696. }
  697. Temp = SignedData;
  698. Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);
  699. if (Pkcs7 == NULL) {
  700. goto _Exit;
  701. }
  702. //
  703. // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
  704. //
  705. if (!PKCS7_type_is_signed (Pkcs7)) {
  706. goto _Exit;
  707. }
  708. //
  709. // Read DER-encoded root certificate and Construct X509 Certificate
  710. //
  711. Temp = TrustedCert;
  712. Cert = d2i_X509 (NULL, &Temp, (long) CertLength);
  713. if (Cert == NULL) {
  714. goto _Exit;
  715. }
  716. //
  717. // Setup X509 Store for trusted certificate
  718. //
  719. CertStore = X509_STORE_new ();
  720. if (CertStore == NULL) {
  721. goto _Exit;
  722. }
  723. if (!(X509_STORE_add_cert (CertStore, Cert))) {
  724. goto _Exit;
  725. }
  726. //
  727. // For generic PKCS#7 handling, InData may be NULL if the content is present
  728. // in PKCS#7 structure. So ignore NULL checking here.
  729. //
  730. DataBio = BIO_new (BIO_s_mem ());
  731. if (DataBio == NULL) {
  732. goto _Exit;
  733. }
  734. if (BIO_write (DataBio, InData, (int) DataLength) <= 0) {
  735. goto _Exit;
  736. }
  737. //
  738. // Allow partial certificate chains, terminated by a non-self-signed but
  739. // still trusted intermediate certificate. Also disable time checks.
  740. //
  741. X509_STORE_set_flags (CertStore,
  742. X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);
  743. //
  744. // OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and
  745. // doesn't support the extended key usage for Authenticode Code Signing.
  746. // Bypass the certificate purpose checking by enabling any purposes setting.
  747. //
  748. X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY);
  749. //
  750. // Verifies the PKCS#7 signedData structure
  751. //
  752. Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);
  753. _Exit:
  754. //
  755. // Release Resources
  756. //
  757. BIO_free (DataBio);
  758. X509_free (Cert);
  759. X509_STORE_free (CertStore);
  760. PKCS7_free (Pkcs7);
  761. if (!Wrapped) {
  762. OPENSSL_free (SignedData);
  763. }
  764. return Status;
  765. }