You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

234 line
6.2 KiB

  1. /* Copyright (c) 2017, 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. #include <openssl/pkcs7.h>
  15. #include <assert.h>
  16. #include <limits.h>
  17. #include <openssl/bytestring.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/pool.h>
  22. #include <openssl/stack.h>
  23. #include <openssl/x509.h>
  24. #include "internal.h"
  25. int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs) {
  26. int ret = 0;
  27. const size_t initial_certs_len = sk_X509_num(out_certs);
  28. STACK_OF(CRYPTO_BUFFER) *raw = sk_CRYPTO_BUFFER_new_null();
  29. if (raw == NULL ||
  30. !PKCS7_get_raw_certificates(raw, cbs, NULL)) {
  31. goto err;
  32. }
  33. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(raw); i++) {
  34. CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_value(raw, i);
  35. X509 *x509 = X509_parse_from_buffer(buf);
  36. if (x509 == NULL ||
  37. !sk_X509_push(out_certs, x509)) {
  38. X509_free(x509);
  39. goto err;
  40. }
  41. }
  42. ret = 1;
  43. err:
  44. sk_CRYPTO_BUFFER_pop_free(raw, CRYPTO_BUFFER_free);
  45. if (!ret) {
  46. while (sk_X509_num(out_certs) != initial_certs_len) {
  47. X509 *x509 = sk_X509_pop(out_certs);
  48. X509_free(x509);
  49. }
  50. }
  51. return ret;
  52. }
  53. int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs) {
  54. CBS signed_data, crls;
  55. uint8_t *der_bytes = NULL;
  56. int ret = 0;
  57. const size_t initial_crls_len = sk_X509_CRL_num(out_crls);
  58. if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs)) {
  59. return 0;
  60. }
  61. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  62. /* Even if only CRLs are included, there may be an empty certificates block.
  63. * OpenSSL does this, for example. */
  64. if (CBS_peek_asn1_tag(&signed_data,
  65. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) &&
  66. !CBS_get_asn1(&signed_data, NULL /* certificates */,
  67. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  68. goto err;
  69. }
  70. if (!CBS_get_asn1(&signed_data, &crls,
  71. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) {
  72. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CRLS_INCLUDED);
  73. goto err;
  74. }
  75. while (CBS_len(&crls) > 0) {
  76. CBS crl_data;
  77. X509_CRL *crl;
  78. const uint8_t *inp;
  79. if (!CBS_get_asn1_element(&crls, &crl_data, CBS_ASN1_SEQUENCE)) {
  80. goto err;
  81. }
  82. if (CBS_len(&crl_data) > LONG_MAX) {
  83. goto err;
  84. }
  85. inp = CBS_data(&crl_data);
  86. crl = d2i_X509_CRL(NULL, &inp, (long)CBS_len(&crl_data));
  87. if (!crl) {
  88. goto err;
  89. }
  90. assert(inp == CBS_data(&crl_data) + CBS_len(&crl_data));
  91. if (sk_X509_CRL_push(out_crls, crl) == 0) {
  92. X509_CRL_free(crl);
  93. goto err;
  94. }
  95. }
  96. ret = 1;
  97. err:
  98. OPENSSL_free(der_bytes);
  99. if (!ret) {
  100. while (sk_X509_CRL_num(out_crls) != initial_crls_len) {
  101. X509_CRL_free(sk_X509_CRL_pop(out_crls));
  102. }
  103. }
  104. return ret;
  105. }
  106. int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio) {
  107. uint8_t *data;
  108. long len;
  109. int ret;
  110. /* Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM
  111. * internally will actually allow several other values too, including
  112. * "CERTIFICATE". */
  113. if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */,
  114. PEM_STRING_PKCS7, pem_bio,
  115. NULL /* password callback */,
  116. NULL /* password callback argument */)) {
  117. return 0;
  118. }
  119. CBS cbs;
  120. CBS_init(&cbs, data, len);
  121. ret = PKCS7_get_certificates(out_certs, &cbs);
  122. OPENSSL_free(data);
  123. return ret;
  124. }
  125. int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio) {
  126. uint8_t *data;
  127. long len;
  128. int ret;
  129. /* Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM
  130. * internally will actually allow several other values too, including
  131. * "CERTIFICATE". */
  132. if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */,
  133. PEM_STRING_PKCS7, pem_bio,
  134. NULL /* password callback */,
  135. NULL /* password callback argument */)) {
  136. return 0;
  137. }
  138. CBS cbs;
  139. CBS_init(&cbs, data, len);
  140. ret = PKCS7_get_CRLs(out_crls, &cbs);
  141. OPENSSL_free(data);
  142. return ret;
  143. }
  144. static int pkcs7_bundle_certificates_cb(CBB *out, const void *arg) {
  145. const STACK_OF(X509) *certs = arg;
  146. size_t i;
  147. CBB certificates;
  148. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  149. if (!CBB_add_asn1(out, &certificates,
  150. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  151. return 0;
  152. }
  153. for (i = 0; i < sk_X509_num(certs); i++) {
  154. X509 *x509 = sk_X509_value(certs, i);
  155. uint8_t *buf;
  156. int len = i2d_X509(x509, NULL);
  157. if (len < 0 ||
  158. !CBB_add_space(&certificates, &buf, len) ||
  159. i2d_X509(x509, &buf) < 0) {
  160. return 0;
  161. }
  162. }
  163. return CBB_flush(out);
  164. }
  165. int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) {
  166. return pkcs7_bundle(out, pkcs7_bundle_certificates_cb, certs);
  167. }
  168. static int pkcs7_bundle_crls_cb(CBB *out, const void *arg) {
  169. const STACK_OF(X509_CRL) *crls = arg;
  170. size_t i;
  171. CBB crl_data;
  172. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  173. if (!CBB_add_asn1(out, &crl_data,
  174. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) {
  175. return 0;
  176. }
  177. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  178. X509_CRL *crl = sk_X509_CRL_value(crls, i);
  179. uint8_t *buf;
  180. int len = i2d_X509_CRL(crl, NULL);
  181. if (len < 0 ||
  182. !CBB_add_space(&crl_data, &buf, len) ||
  183. i2d_X509_CRL(crl, &buf) < 0) {
  184. return 0;
  185. }
  186. }
  187. return CBB_flush(out);
  188. }
  189. int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) {
  190. return pkcs7_bundle(out, pkcs7_bundle_crls_cb, crls);
  191. }