Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pkcs7.h 7.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.
  34. OPENSSL_EXPORT int PKCS7_get_raw_certificates(
  35. STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, CRYPTO_BUFFER_POOL *pool);
  36. // PKCS7_get_certificates behaves like |PKCS7_get_raw_certificates| but parses
  37. // them into |X509| objects.
  38. OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs);
  39. // PKCS7_bundle_certificates appends a PKCS#7, SignedData structure containing
  40. // |certs| to |out|. It returns one on success and zero on error.
  41. OPENSSL_EXPORT int PKCS7_bundle_certificates(
  42. CBB *out, const STACK_OF(X509) *certs);
  43. // PKCS7_get_CRLs parses a PKCS#7, SignedData structure from |cbs| and appends
  44. // the included CRLs to |out_crls|. It returns one on success and zero on error.
  45. // |cbs| is advanced passed the structure.
  46. //
  47. // Note that a SignedData structure may contain no CRLs, in which case this
  48. // function succeeds but does not append any CRLs.
  49. OPENSSL_EXPORT int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs);
  50. // PKCS7_bundle_CRLs appends a PKCS#7, SignedData structure containing
  51. // |crls| to |out|. It returns one on success and zero on error.
  52. OPENSSL_EXPORT int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls);
  53. // PKCS7_get_PEM_certificates reads a PEM-encoded, PKCS#7, SignedData structure
  54. // from |pem_bio| and appends the included certificates to |out_certs|. It
  55. // returns one on success and zero on error.
  56. //
  57. // Note that a SignedData structure may contain no certificates, in which case
  58. // this function succeeds but does not append any certificates.
  59. OPENSSL_EXPORT int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs,
  60. BIO *pem_bio);
  61. // PKCS7_get_PEM_CRLs reads a PEM-encoded, PKCS#7, SignedData structure from
  62. // |pem_bio| and appends the included CRLs to |out_crls|. It returns one on
  63. // success and zero on error.
  64. //
  65. // Note that a SignedData structure may contain no CRLs, in which case this
  66. // function succeeds but does not append any CRLs.
  67. OPENSSL_EXPORT int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls,
  68. BIO *pem_bio);
  69. // Deprecated functions.
  70. //
  71. // These functions are a compatibility layer over a subset of OpenSSL's PKCS#7
  72. // API. It intentionally does not implement the whole thing, only the minimum
  73. // needed to build cryptography.io.
  74. typedef struct {
  75. STACK_OF(X509) *cert;
  76. STACK_OF(X509_CRL) *crl;
  77. } PKCS7_SIGNED;
  78. typedef struct {
  79. STACK_OF(X509) *cert;
  80. STACK_OF(X509_CRL) *crl;
  81. } PKCS7_SIGN_ENVELOPE;
  82. typedef void PKCS7_ENVELOPE;
  83. typedef void PKCS7_DIGEST;
  84. typedef void PKCS7_ENCRYPT;
  85. typedef struct {
  86. uint8_t *ber_bytes;
  87. size_t ber_len;
  88. // Unlike OpenSSL, the following fields are immutable. They filled in when the
  89. // object is parsed and ignored in serialization.
  90. ASN1_OBJECT *type;
  91. union {
  92. char *ptr;
  93. ASN1_OCTET_STRING *data;
  94. PKCS7_SIGNED *sign;
  95. PKCS7_ENVELOPE *enveloped;
  96. PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
  97. PKCS7_DIGEST *digest;
  98. PKCS7_ENCRYPT *encrypted;
  99. ASN1_TYPE *other;
  100. } d;
  101. } PKCS7;
  102. // d2i_PKCS7 parses a BER-encoded, PKCS#7 signed data ContentInfo structure from
  103. // |len| bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the
  104. // result is in |*out|. Note that, even if |*out| is already non-NULL on entry,
  105. // it will not be written to. Rather, a fresh |PKCS7| is allocated and the
  106. // previous one is freed. On successful exit, |*inp| is advanced past the BER
  107. // structure. It returns the result or NULL on error.
  108. OPENSSL_EXPORT PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp,
  109. size_t len);
  110. // d2i_PKCS7_bio behaves like |d2i_PKCS7| but reads the input from |bio|. If
  111. // the length of the object is indefinite the full contents of |bio| are read.
  112. //
  113. // If the function fails then some unknown amount of data may have been read
  114. // from |bio|.
  115. OPENSSL_EXPORT PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out);
  116. // i2d_PKCS7 is a dummy function which copies the contents of |p7|. If |out| is
  117. // not NULL then the result is written to |*out| and |*out| is advanced just
  118. // past the output. It returns the number of bytes in the result, whether
  119. // written or not, or a negative value on error.
  120. OPENSSL_EXPORT int i2d_PKCS7(const PKCS7 *p7, uint8_t **out);
  121. // i2d_PKCS7_bio writes |p7| to |bio|. It returns one on success and zero on
  122. // error.
  123. OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7);
  124. // PKCS7_free releases memory associated with |p7|.
  125. OPENSSL_EXPORT void PKCS7_free(PKCS7 *p7);
  126. // PKCS7_type_is_data returns zero.
  127. OPENSSL_EXPORT int PKCS7_type_is_data(const PKCS7 *p7);
  128. // PKCS7_type_is_digest returns zero.
  129. OPENSSL_EXPORT int PKCS7_type_is_digest(const PKCS7 *p7);
  130. // PKCS7_type_is_encrypted returns zero.
  131. OPENSSL_EXPORT int PKCS7_type_is_encrypted(const PKCS7 *p7);
  132. // PKCS7_type_is_enveloped returns zero.
  133. OPENSSL_EXPORT int PKCS7_type_is_enveloped(const PKCS7 *p7);
  134. // PKCS7_type_is_signed returns one. (We only supporte signed data
  135. // ContentInfos.)
  136. OPENSSL_EXPORT int PKCS7_type_is_signed(const PKCS7 *p7);
  137. // PKCS7_type_is_signedAndEnveloped returns zero.
  138. OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7);
  139. // PKCS7_DETACHED indicates that the PKCS#7 file specifies its data externally.
  140. #define PKCS7_DETACHED 0x40
  141. // The following flags cause |PKCS7_sign| to fail.
  142. #define PKCS7_TEXT 0x1
  143. #define PKCS7_NOCERTS 0x2
  144. #define PKCS7_NOSIGS 0x4
  145. #define PKCS7_NOCHAIN 0x8
  146. #define PKCS7_NOINTERN 0x10
  147. #define PKCS7_NOVERIFY 0x20
  148. #define PKCS7_BINARY 0x80
  149. #define PKCS7_NOATTR 0x100
  150. #define PKCS7_NOSMIMECAP 0x200
  151. #define PKCS7_STREAM 0x1000
  152. // PKCS7_sign assembles |certs| into a PKCS#7 signed data ContentInfo with
  153. // external data and no signatures. It returns a newly-allocated |PKCS7| on
  154. // success or NULL on error. |sign_cert| and |pkey| must be NULL. |data| is
  155. // ignored. |flags| must be equal to |PKCS7_DETACHED|.
  156. //
  157. // Note this function only implements a subset of the corresponding OpenSSL
  158. // function. It is provided for backwards compatibility only.
  159. OPENSSL_EXPORT PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey,
  160. STACK_OF(X509) *certs, BIO *data, int flags);
  161. #if defined(__cplusplus)
  162. } // extern C
  163. extern "C++" {
  164. BSSL_NAMESPACE_BEGIN
  165. BORINGSSL_MAKE_DELETER(PKCS7, PKCS7_free)
  166. BSSL_NAMESPACE_END
  167. } // extern C++
  168. #endif
  169. #define PKCS7_R_BAD_PKCS7_VERSION 100
  170. #define PKCS7_R_NOT_PKCS7_SIGNED_DATA 101
  171. #define PKCS7_R_NO_CERTIFICATES_INCLUDED 102
  172. #define PKCS7_R_NO_CRLS_INCLUDED 103
  173. #endif // OPENSSL_HEADER_PKCS7_H