pkcs7.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_PKCS7_H
  15. #define OPENSSL_HEADER_PKCS7_H
  16. #include <openssl/base.h>
  17. #include <openssl/stack.h>
  18. #if defined(__cplusplus)
  19. extern "C" {
  20. #endif
  21. // PKCS#7.
  22. //
  23. // This library contains functions for extracting information from PKCS#7
  24. // structures (RFC 2315).
  25. DECLARE_STACK_OF(CRYPTO_BUFFER)
  26. DECLARE_STACK_OF(X509)
  27. DECLARE_STACK_OF(X509_CRL)
  28. // PKCS7_get_raw_certificates parses a PKCS#7, SignedData structure from |cbs|
  29. // and appends the included certificates to |out_certs|. It returns one on
  30. // success and zero on error. |cbs| is advanced passed the structure.
  31. //
  32. // Note that a SignedData structure may contain no certificates, in which case
  33. // this function succeeds but does not append any certificates. Additionally,
  34. // certificates in SignedData structures are unordered. Callers should not
  35. // assume a particular order in |*out_certs| and may need to search for matches
  36. // or run path-building algorithms.
  37. OPENSSL_EXPORT int PKCS7_get_raw_certificates(
  38. STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, CRYPTO_BUFFER_POOL *pool);
  39. // PKCS7_get_certificates behaves like |PKCS7_get_raw_certificates| but parses
  40. // them into |X509| objects.
  41. OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs);
  42. // PKCS7_bundle_raw_certificates appends a PKCS#7, SignedData structure
  43. // containing |certs| to |out|. It returns one on success and zero on error.
  44. // Note that certificates in SignedData structures are unordered. The order in
  45. // |certs| will not be preserved.
  46. OPENSSL_EXPORT int PKCS7_bundle_raw_certificates(
  47. CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs);
  48. // PKCS7_bundle_certificates behaves like |PKCS7_bundle_raw_certificates| but
  49. // takes |X509| objects as input.
  50. OPENSSL_EXPORT int PKCS7_bundle_certificates(
  51. CBB *out, const STACK_OF(X509) *certs);
  52. // PKCS7_get_CRLs parses a PKCS#7, SignedData structure from |cbs| and appends
  53. // the included CRLs to |out_crls|. It returns one on success and zero on error.
  54. // |cbs| is advanced passed the structure.
  55. //
  56. // Note that a SignedData structure may contain no CRLs, in which case this
  57. // function succeeds but does not append any CRLs. Additionally, CRLs in
  58. // SignedData structures are unordered. Callers should not assume an order in
  59. // |*out_crls| and may need to search for matches.
  60. OPENSSL_EXPORT int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs);
  61. // PKCS7_bundle_CRLs appends a PKCS#7, SignedData structure containing
  62. // |crls| to |out|. It returns one on success and zero on error. Note that CRLs
  63. // in SignedData structures are unordered. The order in |crls| will not be
  64. // preserved.
  65. OPENSSL_EXPORT int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls);
  66. // PKCS7_get_PEM_certificates reads a PEM-encoded, PKCS#7, SignedData structure
  67. // from |pem_bio| and appends the included certificates to |out_certs|. It
  68. // returns one on success and zero on error.
  69. //
  70. // Note that a SignedData structure may contain no certificates, in which case
  71. // this function succeeds but does not append any certificates. Additionally,
  72. // certificates in SignedData structures are unordered. Callers should not
  73. // assume a particular order in |*out_certs| and may need to search for matches
  74. // or run path-building algorithms.
  75. OPENSSL_EXPORT int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs,
  76. BIO *pem_bio);
  77. // PKCS7_get_PEM_CRLs reads a PEM-encoded, PKCS#7, SignedData structure from
  78. // |pem_bio| and appends the included CRLs to |out_crls|. It returns one on
  79. // success and zero on error.
  80. //
  81. // Note that a SignedData structure may contain no CRLs, in which case this
  82. // function succeeds but does not append any CRLs. Additionally, CRLs in
  83. // SignedData structures are unordered. Callers should not assume an order in
  84. // |*out_crls| and may need to search for matches.
  85. OPENSSL_EXPORT int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls,
  86. BIO *pem_bio);
  87. // Deprecated functions.
  88. //
  89. // These functions are a compatibility layer over a subset of OpenSSL's PKCS#7
  90. // API. It intentionally does not implement the whole thing, only the minimum
  91. // needed to build cryptography.io.
  92. typedef struct {
  93. STACK_OF(X509) *cert;
  94. STACK_OF(X509_CRL) *crl;
  95. } PKCS7_SIGNED;
  96. typedef struct {
  97. STACK_OF(X509) *cert;
  98. STACK_OF(X509_CRL) *crl;
  99. } PKCS7_SIGN_ENVELOPE;
  100. typedef void PKCS7_ENVELOPE;
  101. typedef void PKCS7_DIGEST;
  102. typedef void PKCS7_ENCRYPT;
  103. typedef void PKCS7_SIGNER_INFO;
  104. typedef struct {
  105. uint8_t *ber_bytes;
  106. size_t ber_len;
  107. // Unlike OpenSSL, the following fields are immutable. They filled in when the
  108. // object is parsed and ignored in serialization.
  109. ASN1_OBJECT *type;
  110. union {
  111. char *ptr;
  112. ASN1_OCTET_STRING *data;
  113. PKCS7_SIGNED *sign;
  114. PKCS7_ENVELOPE *enveloped;
  115. PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
  116. PKCS7_DIGEST *digest;
  117. PKCS7_ENCRYPT *encrypted;
  118. ASN1_TYPE *other;
  119. } d;
  120. } PKCS7;
  121. // d2i_PKCS7 parses a BER-encoded, PKCS#7 signed data ContentInfo structure from
  122. // |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
  123. OPENSSL_EXPORT PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp,
  124. size_t len);
  125. // d2i_PKCS7_bio behaves like |d2i_PKCS7| but reads the input from |bio|. If
  126. // the length of the object is indefinite the full contents of |bio| are read.
  127. //
  128. // If the function fails then some unknown amount of data may have been read
  129. // from |bio|.
  130. OPENSSL_EXPORT PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out);
  131. // i2d_PKCS7 marshals |p7| as a DER-encoded PKCS#7 ContentInfo structure, as
  132. // described in |i2d_SAMPLE|.
  133. OPENSSL_EXPORT int i2d_PKCS7(const PKCS7 *p7, uint8_t **out);
  134. // i2d_PKCS7_bio writes |p7| to |bio|. It returns one on success and zero on
  135. // error.
  136. OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7);
  137. // PKCS7_free releases memory associated with |p7|.
  138. OPENSSL_EXPORT void PKCS7_free(PKCS7 *p7);
  139. // PKCS7_type_is_data returns zero.
  140. OPENSSL_EXPORT int PKCS7_type_is_data(const PKCS7 *p7);
  141. // PKCS7_type_is_digest returns zero.
  142. OPENSSL_EXPORT int PKCS7_type_is_digest(const PKCS7 *p7);
  143. // PKCS7_type_is_encrypted returns zero.
  144. OPENSSL_EXPORT int PKCS7_type_is_encrypted(const PKCS7 *p7);
  145. // PKCS7_type_is_enveloped returns zero.
  146. OPENSSL_EXPORT int PKCS7_type_is_enveloped(const PKCS7 *p7);
  147. // PKCS7_type_is_signed returns one. (We only supporte signed data
  148. // ContentInfos.)
  149. OPENSSL_EXPORT int PKCS7_type_is_signed(const PKCS7 *p7);
  150. // PKCS7_type_is_signedAndEnveloped returns zero.
  151. OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7);
  152. // PKCS7_DETACHED indicates that the PKCS#7 file specifies its data externally.
  153. #define PKCS7_DETACHED 0x40
  154. // The following flags cause |PKCS7_sign| to fail.
  155. #define PKCS7_TEXT 0x1
  156. #define PKCS7_NOCERTS 0x2
  157. #define PKCS7_NOSIGS 0x4
  158. #define PKCS7_NOCHAIN 0x8
  159. #define PKCS7_NOINTERN 0x10
  160. #define PKCS7_NOVERIFY 0x20
  161. #define PKCS7_BINARY 0x80
  162. #define PKCS7_NOATTR 0x100
  163. #define PKCS7_NOSMIMECAP 0x200
  164. #define PKCS7_STREAM 0x1000
  165. #define PKCS7_PARTIAL 0x4000
  166. // PKCS7_sign can operate in two modes to provide some backwards compatibility:
  167. //
  168. // The first mode assembles |certs| into a PKCS#7 signed data ContentInfo with
  169. // external data and no signatures. It returns a newly-allocated |PKCS7| on
  170. // success or NULL on error. |sign_cert| and |pkey| must be NULL. |data| is
  171. // ignored. |flags| must be equal to |PKCS7_DETACHED|. Additionally,
  172. // certificates in SignedData structures are unordered. The order of |certs|
  173. // will not be preserved.
  174. //
  175. // The second mode generates a detached RSA SHA-256 signature of |data| using
  176. // |pkey| and produces a PKCS#7 SignedData structure containing it. |certs|
  177. // must be NULL and |flags| must be exactly |PKCS7_NOATTR | PKCS7_BINARY |
  178. // PKCS7_NOCERTS | PKCS7_DETACHED|.
  179. //
  180. // Note this function only implements a subset of the corresponding OpenSSL
  181. // function. It is provided for backwards compatibility only.
  182. OPENSSL_EXPORT PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey,
  183. STACK_OF(X509) *certs, BIO *data, int flags);
  184. #if defined(__cplusplus)
  185. } // extern C
  186. extern "C++" {
  187. BSSL_NAMESPACE_BEGIN
  188. BORINGSSL_MAKE_DELETER(PKCS7, PKCS7_free)
  189. BSSL_NAMESPACE_END
  190. } // extern C++
  191. #endif
  192. #define PKCS7_R_BAD_PKCS7_VERSION 100
  193. #define PKCS7_R_NOT_PKCS7_SIGNED_DATA 101
  194. #define PKCS7_R_NO_CERTIFICATES_INCLUDED 102
  195. #define PKCS7_R_NO_CRLS_INCLUDED 103
  196. #endif // OPENSSL_HEADER_PKCS7_H