pkcs7.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*!
  2. \ingroup PKCS7
  3. \brief This function initializes a PKCS7 structure with a DER-formatted
  4. certificate. To initialize an empty PKCS7 structure, one can pass in a NULL
  5. cert and 0 for certSz.
  6. \return 0 Returned on successfully initializing the PKCS7 structure
  7. \return MEMORY_E Returned if there is an error allocating memory
  8. with XMALLOC
  9. \return ASN_PARSE_E Returned if there is an error parsing the cert header
  10. \return ASN_OBJECT_ID_E Returned if there is an error parsing the
  11. encryption type from the cert
  12. \return ASN_EXPECT_0_E Returned if there is a formatting error in the
  13. encryption specification of the cert file
  14. \return ASN_BEFORE_DATE_E Returned if the date is before the certificate
  15. start date
  16. \return ASN_AFTER_DATE_E Returned if the date is after the certificate
  17. expiration date
  18. \return ASN_BITSTR_E Returned if there is an error parsing a bit string
  19. from the certificate
  20. \return ASN_NTRU_KEY_E Returned if there is an error parsing the NTRU
  21. key from the certificate
  22. \return ECC_CURVE_OID_E Returned if there is an error parsing the ECC
  23. key from the certificate
  24. \return ASN_UNKNOWN_OID_E Returned if the certificate is using an unknown
  25. key object id
  26. \return ASN_VERSION_E Returned if the ALLOW_V1_EXTENSIONS option is not
  27. defined and the certificate is a V1 or V2 certificate
  28. \return BAD_FUNC_ARG Returned if there is an error processing the
  29. certificate extension
  30. \return ASN_CRIT_EXT_E Returned if an unfamiliar critical extension is
  31. encountered in processing the certificate
  32. \return ASN_SIG_OID_E Returned if the signature encryption type is not
  33. the same as the encryption type of the certificate in the provided file
  34. \return ASN_SIG_CONFIRM_E Returned if confirming the certification
  35. signature fails
  36. \return ASN_NAME_INVALID_E Returned if the certificate’s name is not
  37. permitted by the CA name constraints
  38. \return ASN_NO_SIGNER_E Returned if there is no CA signer to verify
  39. the certificate’s authenticity
  40. \param pkcs7 pointer to the PKCS7 structure in which to
  41. store the decoded cert
  42. \param cert pointer to a buffer containing a DER formatted ASN.1
  43. certificate with which to initialize the PKCS7 structure
  44. \param certSz size of the certificate buffer
  45. _Example_
  46. \code
  47. PKCS7 pkcs7;
  48. byte derBuff[] = { }; // initialize with DER-encoded certificate
  49. if ( wc_PKCS7_InitWithCert(&pkcs7, derBuff, sizeof(derBuff)) != 0 ) {
  50. // error parsing certificate into pkcs7 format
  51. }
  52. \endcode
  53. \sa wc_PKCS7_Free
  54. */
  55. WOLFSSL_API int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz);
  56. /*!
  57. \ingroup PKCS7
  58. \brief This function releases any memory allocated by a PKCS7 initializer.
  59. \return none No returns.
  60. \param pkcs7 pointer to the PKCS7 structure to free
  61. _Example_
  62. \code
  63. PKCS7 pkcs7;
  64. // initialize and use PKCS7 object
  65. wc_PKCS7_Free(pkcs7);
  66. \endcode
  67. \sa wc_PKCS7_InitWithCert
  68. */
  69. WOLFSSL_API void wc_PKCS7_Free(PKCS7* pkcs7);
  70. /*!
  71. \ingroup PKCS7
  72. \brief This function builds the PKCS7 data content type, encoding the
  73. PKCS7 structure into a buffer containing a parsable PKCS7 data packet.
  74. \return Success On successfully encoding the PKCS7 data into the buffer,
  75. returns the index parsed up to in the PKCS7 structure. This index also
  76. corresponds to the bytes written to the output buffer.
  77. \return BUFFER_E Returned if the given buffer is not large enough to hold
  78. the encoded certificate
  79. \param pkcs7 pointer to the PKCS7 structure to encode
  80. \param output pointer to the buffer in which to store the encoded
  81. certificate
  82. \param outputSz size available in the output buffer
  83. _Example_
  84. \code
  85. PKCS7 pkcs7;
  86. int ret;
  87. byte derBuff[] = { }; // initialize with DER-encoded certificate
  88. byte pkcs7Buff[FOURK_BUF];
  89. wc_PKCS7_InitWithCert(&pkcs7, derBuff, sizeof(derBuff));
  90. // update message and data to encode
  91. pkcs7.privateKey = key;
  92. pkcs7.privateKeySz = keySz;
  93. pkcs7.content = data;
  94. pkcs7.contentSz = dataSz;
  95. ... etc.
  96. ret = wc_PKCS7_EncodeData(&pkcs7, pkcs7Buff, sizeof(pkcs7Buff));
  97. if ( ret != 0 ) {
  98. // error encoding into output buffer
  99. }
  100. \endcode
  101. \sa wc_PKCS7_InitWithCert
  102. */
  103. WOLFSSL_API int wc_PKCS7_EncodeData(PKCS7* pkcs7, byte* output,
  104. word32 outputSz);
  105. /*!
  106. \ingroup PKCS7
  107. \brief This function builds the PKCS7 signed data content type, encoding
  108. the PKCS7 structure into a buffer containing a parsable PKCS7
  109. signed data packet.
  110. \return Success On successfully encoding the PKCS7 data into the buffer,
  111. returns the index parsed up to in the PKCS7 structure. This index also
  112. corresponds to the bytes written to the output buffer.
  113. \return BAD_FUNC_ARG Returned if the PKCS7 structure is missing one or
  114. more required elements to generate a signed data packet
  115. \return MEMORY_E Returned if there is an error allocating memory
  116. \return PUBLIC_KEY_E Returned if there is an error parsing the public key
  117. \return RSA_BUFFER_E Returned if buffer error, output too small or input
  118. too large
  119. \return BUFFER_E Returned if the given buffer is not large enough to hold
  120. the encoded certificate
  121. \return MP_INIT_E may be returned if there is an error generating
  122. the signature
  123. \return MP_READ_E may be returned if there is an error generating
  124. the signature
  125. \return MP_CMP_E may be returned if there is an error generating
  126. the signature
  127. \return MP_INVMOD_E may be returned if there is an error generating
  128. the signature
  129. \return MP_EXPTMOD_E may be returned if there is an error generating
  130. the signature
  131. \return MP_MOD_E may be returned if there is an error generating
  132. the signature
  133. \return MP_MUL_E may be returned if there is an error generating
  134. the signature
  135. \return MP_ADD_E may be returned if there is an error generating
  136. the signature
  137. \return MP_MULMOD_E may be returned if there is an error generating
  138. the signature
  139. \return MP_TO_E may be returned if there is an error generating
  140. the signature
  141. \return MP_MEM may be returned if there is an error generating the signature
  142. \param pkcs7 pointer to the PKCS7 structure to encode
  143. \param output pointer to the buffer in which to store the
  144. encoded certificate
  145. \param outputSz size available in the output buffer
  146. _Example_
  147. \code
  148. PKCS7 pkcs7;
  149. int ret;
  150. byte derBuff[] = { }; // initialize with DER-encoded certificate
  151. byte pkcs7Buff[FOURK_BUF];
  152. wc_PKCS7_InitWithCert(&pkcs7, derBuff, sizeof(derBuff));
  153. // update message and data to encode
  154. pkcs7.privateKey = key;
  155. pkcs7.privateKeySz = keySz;
  156. pkcs7.content = data;
  157. pkcs7.contentSz = dataSz;
  158. ... etc.
  159. ret = wc_PKCS7_EncodeSignedData(&pkcs7, pkcs7Buff, sizeof(pkcs7Buff));
  160. if ( ret != 0 ) {
  161. // error encoding into output buffer
  162. }
  163. \endcode
  164. \sa wc_PKCS7_InitWithCert
  165. \sa wc_PKCS7_VerifySignedData
  166. */
  167. WOLFSSL_API int wc_PKCS7_EncodeSignedData(PKCS7* pkcs7,
  168. byte* output, word32 outputSz);
  169. /*!
  170. \ingroup PKCS7
  171. \brief This function takes in a transmitted PKCS7 signed data message,
  172. extracts the certificate list and certificate revocation list, and then
  173. verifies the signature. It stores the extracted content in the given
  174. PKCS7 structure.
  175. \return 0 Returned on successfully extracting the information
  176. from the message
  177. \return BAD_FUNC_ARG Returned if one of the input parameters is invalid
  178. \return ASN_PARSE_E Returned if there is an error parsing from the
  179. given pkiMsg
  180. \return PKCS7_OID_E Returned if the given pkiMsg is not a signed data type
  181. \return ASN_VERSION_E Returned if the PKCS7 signer info is not version 1
  182. \return MEMORY_E Returned if there is an error allocating memory
  183. \return PUBLIC_KEY_E Returned if there is an error parsing the public key
  184. \return RSA_BUFFER_E Returned if buffer error, output too small or
  185. input too large
  186. \return BUFFER_E Returned if the given buffer is not large enough to
  187. hold the encoded certificate
  188. \return MP_INIT_E may be returned if there is an error generating
  189. the signature
  190. \return MP_READ_E may be returned if there is an error generating
  191. the signature
  192. \return MP_CMP_E may be returned if there is an error generating
  193. the signature
  194. \return MP_INVMOD_E may be returned if there is an error generating
  195. the signature
  196. \return MP_EXPTMOD_E may be returned if there is an error generating
  197. the signature
  198. \return MP_MOD_E may be returned if there is an error generating
  199. the signature
  200. \return MP_MUL_E may be returned if there is an error generating
  201. the signature
  202. \return MP_ADD_E may be returned if there is an error generating
  203. the signature
  204. \return MP_MULMOD_E may be returned if there is an error generating
  205. the signature
  206. \return MP_TO_E may be returned if there is an error generating
  207. the signature
  208. \return MP_MEM may be returned if there is an error generating the signature
  209. \param pkcs7 pointer to the PKCS7 structure in which to store the parsed
  210. certificates
  211. \param pkiMsg pointer to the buffer containing the signed message to verify
  212. and decode
  213. \param pkiMsgSz size of the signed message
  214. _Example_
  215. \code
  216. PKCS7 pkcs7;
  217. int ret;
  218. byte derBuff[] = { }; // initialize with DER-encoded certificate
  219. byte pkcs7Buff[FOURK_BUF];
  220. wc_PKCS7_InitWithCert(&pkcs7, derBuff, sizeof(derBuff));
  221. // update message and data to encode
  222. pkcs7.privateKey = key;
  223. pkcs7.privateKeySz = keySz;
  224. pkcs7.content = data;
  225. pkcs7.contentSz = dataSz;
  226. ... etc.
  227. ret = wc_PKCS7_EncodeSignedData(&pkcs7, pkcs7Buff, sizeof(pkcs7Buff));
  228. if ( ret != 0 ) {
  229. // error encoding into output buffer
  230. }
  231. \endcode
  232. \sa wc_PKCS7_InitWithCert
  233. \sa wc_PKCS7_EncodeSignedData
  234. */
  235. WOLFSSL_API int wc_PKCS7_VerifySignedData(PKCS7* pkcs7,
  236. byte* pkiMsg, word32 pkiMsgSz);
  237. /*!
  238. \ingroup PKCS7
  239. \brief This function builds the PKCS7 enveloped data content type, encoding
  240. the PKCS7 structure into a buffer containing a parsable PKCS7 enveloped
  241. data packet.
  242. \return Success Returned on successfully encoding the message in enveloped
  243. data format, returns the size written to the output buffer
  244. \return BAD_FUNC_ARG: Returned if one of the input parameters is invalid,
  245. or if the PKCS7 structure is missing required elements
  246. \return ALGO_ID_E Returned if the PKCS7 structure is using an unsupported
  247. algorithm type. Currently, only DESb and DES3b are supported
  248. \return BUFFER_E Returned if the given output buffer is too small to store
  249. the output data
  250. \return MEMORY_E Returned if there is an error allocating memory
  251. \return RNG_FAILURE_E Returned if there is an error initializing the random
  252. number generator for encryption
  253. \return DRBG_FAILED Returned if there is an error generating numbers with
  254. the random number generator used for encryption
  255. \param pkcs7 pointer to the PKCS7 structure to encode
  256. \param output pointer to the buffer in which to store the encoded
  257. certificate
  258. \param outputSz size available in the output buffer
  259. _Example_
  260. \code
  261. PKCS7 pkcs7;
  262. int ret;
  263. byte derBuff[] = { }; // initialize with DER-encoded certificate
  264. byte pkcs7Buff[FOURK_BUF];
  265. wc_PKCS7_InitWithCert(&pkcs7, derBuff, sizeof(derBuff));
  266. // update message and data to encode
  267. pkcs7.privateKey = key;
  268. pkcs7.privateKeySz = keySz;
  269. pkcs7.content = data;
  270. pkcs7.contentSz = dataSz;
  271. ... etc.
  272. ret = wc_PKCS7_EncodeEnvelopedData(&pkcs7, pkcs7Buff, sizeof(pkcs7Buff));
  273. if ( ret != 0 ) {
  274. // error encoding into output buffer
  275. }
  276. \endcode
  277. \sa wc_PKCS7_InitWithCert
  278. \sa wc_PKCS7_DecodeEnvelopedData
  279. */
  280. WOLFSSL_API int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7,
  281. byte* output, word32 outputSz);
  282. /*!
  283. \ingroup PKCS7
  284. \brief This function unwraps and decrypts a PKCS7 enveloped data content
  285. type, decoding the message into output. It uses the private key of the
  286. PKCS7 object passed in to decrypt the message.
  287. \return On successfully extracting the information from the message,
  288. returns the bytes written to output
  289. \return BAD_FUNC_ARG Returned if one of the input parameters is invalid
  290. \return ASN_PARSE_E Returned if there is an error parsing from the
  291. given pkiMsg
  292. \return PKCS7_OID_E Returned if the given pkiMsg is not an enveloped
  293. data type
  294. \return ASN_VERSION_E Returned if the PKCS7 signer info is not version 0
  295. \return MEMORY_E Returned if there is an error allocating memory
  296. \return ALGO_ID_E Returned if the PKCS7 structure is using an unsupported
  297. algorithm type. Currently, only DESb and DES3b are supported for
  298. encryption, with RSAk for signature generation
  299. \return PKCS7_RECIP_E Returned if there is no recipient found in the
  300. enveloped data that matches the recipient provided
  301. \return RSA_BUFFER_E Returned if there is an error during RSA signature
  302. verification due to buffer error, output too small or input too large.
  303. \return MP_INIT_E may be returned if there is an error during signature
  304. verification
  305. \return MP_READ_E may be returned if there is an error during signature
  306. verification
  307. \return MP_CMP_E may be returned if there is an error during signature
  308. verification
  309. \return MP_INVMOD_E may be returned if there is an error during signature
  310. verification
  311. \return MP_EXPTMOD_E may be returned if there is an error during signature
  312. verification
  313. \return MP_MOD_E may be returned if there is an error during signature
  314. verification
  315. \return MP_MUL_E may be returned if there is an error during signature
  316. verification
  317. \return MP_ADD_E may be returned if there is an error during signature
  318. verification
  319. \return MP_MULMOD_E may be returned if there is an error during signature
  320. verification
  321. \return MP_TO_E may be returned if there is an error during signature
  322. verification
  323. \return MP_MEM may be returned if there is an error during signature
  324. verification
  325. \param pkcs7 pointer to the PKCS7 structure containing the private key with
  326. which to decode the enveloped data package
  327. \param pkiMsg pointer to the buffer containing the enveloped data package
  328. \param pkiMsgSz size of the enveloped data package
  329. \param output pointer to the buffer in which to store the decoded message
  330. \param outputSz size available in the output buffer
  331. _Example_
  332. \code
  333. PKCS7 pkcs7;
  334. byte received[] = { }; // initialize with received enveloped message
  335. byte decoded[FOURK_BUF];
  336. int decodedSz;
  337. // initialize pkcs7 with certificate
  338. // update key
  339. pkcs7.privateKey = key;
  340. pkcs7.privateKeySz = keySz;
  341. decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, received,
  342. sizeof(received),decoded, sizeof(decoded));
  343. if ( decodedSz != 0 ) {
  344. // error decoding message
  345. }
  346. \endcode
  347. \sa wc_PKCS7_InitWithCert
  348. \sa wc_PKCS7_EncodeEnvelopedData
  349. */
  350. WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg,
  351. word32 pkiMsgSz, byte* output,
  352. word32 outputSz);