Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <openssl/pkcs7.h>
  15. #include <openssl/bytestring.h>
  16. #include <openssl/err.h>
  17. #include <openssl/mem.h>
  18. #include <openssl/pool.h>
  19. #include <openssl/stack.h>
  20. #include "internal.h"
  21. #include "../bytestring/internal.h"
  22. // 1.2.840.113549.1.7.1
  23. static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  24. 0x0d, 0x01, 0x07, 0x01};
  25. // 1.2.840.113549.1.7.2
  26. static const uint8_t kPKCS7SignedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  27. 0x0d, 0x01, 0x07, 0x02};
  28. // pkcs7_parse_header reads the non-certificate/non-CRL prefix of a PKCS#7
  29. // SignedData blob from |cbs| and sets |*out| to point to the rest of the
  30. // input. If the input is in BER format, then |*der_bytes| will be set to a
  31. // pointer that needs to be freed by the caller once they have finished
  32. // processing |*out| (which will be pointing into |*der_bytes|).
  33. //
  34. // It returns one on success or zero on error. On error, |*der_bytes| is
  35. // NULL.
  36. int pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs) {
  37. CBS in, content_info, content_type, wrapped_signed_data, signed_data;
  38. uint64_t version;
  39. // The input may be in BER format.
  40. *der_bytes = NULL;
  41. if (!CBS_asn1_ber_to_der(cbs, &in, der_bytes) ||
  42. // See https://tools.ietf.org/html/rfc2315#section-7
  43. !CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE) ||
  44. !CBS_get_asn1(&content_info, &content_type, CBS_ASN1_OBJECT)) {
  45. goto err;
  46. }
  47. if (!CBS_mem_equal(&content_type, kPKCS7SignedData,
  48. sizeof(kPKCS7SignedData))) {
  49. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NOT_PKCS7_SIGNED_DATA);
  50. goto err;
  51. }
  52. // See https://tools.ietf.org/html/rfc2315#section-9.1
  53. if (!CBS_get_asn1(&content_info, &wrapped_signed_data,
  54. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  55. !CBS_get_asn1(&wrapped_signed_data, &signed_data, CBS_ASN1_SEQUENCE) ||
  56. !CBS_get_asn1_uint64(&signed_data, &version) ||
  57. !CBS_get_asn1(&signed_data, NULL /* digests */, CBS_ASN1_SET) ||
  58. !CBS_get_asn1(&signed_data, NULL /* content */, CBS_ASN1_SEQUENCE)) {
  59. goto err;
  60. }
  61. if (version < 1) {
  62. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_BAD_PKCS7_VERSION);
  63. goto err;
  64. }
  65. CBS_init(out, CBS_data(&signed_data), CBS_len(&signed_data));
  66. return 1;
  67. err:
  68. OPENSSL_free(*der_bytes);
  69. *der_bytes = NULL;
  70. return 0;
  71. }
  72. int PKCS7_get_raw_certificates(STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs,
  73. CRYPTO_BUFFER_POOL *pool) {
  74. CBS signed_data, certificates;
  75. uint8_t *der_bytes = NULL;
  76. int ret = 0, has_certificates;
  77. const size_t initial_certs_len = sk_CRYPTO_BUFFER_num(out_certs);
  78. // See https://tools.ietf.org/html/rfc2315#section-9.1
  79. if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs) ||
  80. !CBS_get_optional_asn1(
  81. &signed_data, &certificates, &has_certificates,
  82. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  83. goto err;
  84. }
  85. if (!has_certificates) {
  86. CBS_init(&certificates, NULL, 0);
  87. }
  88. while (CBS_len(&certificates) > 0) {
  89. CBS cert;
  90. if (!CBS_get_asn1_element(&certificates, &cert, CBS_ASN1_SEQUENCE)) {
  91. goto err;
  92. }
  93. CRYPTO_BUFFER *buf = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
  94. if (buf == NULL ||
  95. !sk_CRYPTO_BUFFER_push(out_certs, buf)) {
  96. CRYPTO_BUFFER_free(buf);
  97. goto err;
  98. }
  99. }
  100. ret = 1;
  101. err:
  102. OPENSSL_free(der_bytes);
  103. if (!ret) {
  104. while (sk_CRYPTO_BUFFER_num(out_certs) != initial_certs_len) {
  105. CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_pop(out_certs);
  106. CRYPTO_BUFFER_free(buf);
  107. }
  108. }
  109. return ret;
  110. }
  111. int pkcs7_bundle(CBB *out, int (*cb)(CBB *out, const void *arg),
  112. const void *arg) {
  113. CBB outer_seq, oid, wrapped_seq, seq, version_bytes, digest_algos_set,
  114. content_info;
  115. // See https://tools.ietf.org/html/rfc2315#section-7
  116. if (!CBB_add_asn1(out, &outer_seq, CBS_ASN1_SEQUENCE) ||
  117. !CBB_add_asn1(&outer_seq, &oid, CBS_ASN1_OBJECT) ||
  118. !CBB_add_bytes(&oid, kPKCS7SignedData, sizeof(kPKCS7SignedData)) ||
  119. !CBB_add_asn1(&outer_seq, &wrapped_seq,
  120. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  121. // See https://tools.ietf.org/html/rfc2315#section-9.1
  122. !CBB_add_asn1(&wrapped_seq, &seq, CBS_ASN1_SEQUENCE) ||
  123. !CBB_add_asn1(&seq, &version_bytes, CBS_ASN1_INTEGER) ||
  124. !CBB_add_u8(&version_bytes, 1) ||
  125. !CBB_add_asn1(&seq, &digest_algos_set, CBS_ASN1_SET) ||
  126. !CBB_add_asn1(&seq, &content_info, CBS_ASN1_SEQUENCE) ||
  127. !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) ||
  128. !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) ||
  129. !cb(&seq, arg)) {
  130. return 0;
  131. }
  132. return CBB_flush(out);
  133. }