pkcs7.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. size_t der_len;
  38. CBS in, content_info, content_type, wrapped_signed_data, signed_data;
  39. uint64_t version;
  40. // The input may be in BER format.
  41. *der_bytes = NULL;
  42. if (!CBS_asn1_ber_to_der(cbs, der_bytes, &der_len)) {
  43. return 0;
  44. }
  45. if (*der_bytes != NULL) {
  46. CBS_init(&in, *der_bytes, der_len);
  47. } else {
  48. CBS_init(&in, CBS_data(cbs), CBS_len(cbs));
  49. }
  50. // See https://tools.ietf.org/html/rfc2315#section-7
  51. if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE) ||
  52. !CBS_get_asn1(&content_info, &content_type, CBS_ASN1_OBJECT)) {
  53. goto err;
  54. }
  55. if (!CBS_mem_equal(&content_type, kPKCS7SignedData,
  56. sizeof(kPKCS7SignedData))) {
  57. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NOT_PKCS7_SIGNED_DATA);
  58. goto err;
  59. }
  60. // See https://tools.ietf.org/html/rfc2315#section-9.1
  61. if (!CBS_get_asn1(&content_info, &wrapped_signed_data,
  62. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  63. !CBS_get_asn1(&wrapped_signed_data, &signed_data, CBS_ASN1_SEQUENCE) ||
  64. !CBS_get_asn1_uint64(&signed_data, &version) ||
  65. !CBS_get_asn1(&signed_data, NULL /* digests */, CBS_ASN1_SET) ||
  66. !CBS_get_asn1(&signed_data, NULL /* content */, CBS_ASN1_SEQUENCE)) {
  67. goto err;
  68. }
  69. if (version < 1) {
  70. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_BAD_PKCS7_VERSION);
  71. goto err;
  72. }
  73. CBS_init(out, CBS_data(&signed_data), CBS_len(&signed_data));
  74. return 1;
  75. err:
  76. OPENSSL_free(*der_bytes);
  77. *der_bytes = NULL;
  78. return 0;
  79. }
  80. int PKCS7_get_raw_certificates(STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs,
  81. CRYPTO_BUFFER_POOL *pool) {
  82. CBS signed_data, certificates;
  83. uint8_t *der_bytes = NULL;
  84. int ret = 0;
  85. const size_t initial_certs_len = sk_CRYPTO_BUFFER_num(out_certs);
  86. if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs)) {
  87. return 0;
  88. }
  89. // See https://tools.ietf.org/html/rfc2315#section-9.1
  90. if (!CBS_get_asn1(&signed_data, &certificates,
  91. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  92. OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CERTIFICATES_INCLUDED);
  93. goto err;
  94. }
  95. while (CBS_len(&certificates) > 0) {
  96. CBS cert;
  97. if (!CBS_get_asn1_element(&certificates, &cert, CBS_ASN1_SEQUENCE)) {
  98. goto err;
  99. }
  100. CRYPTO_BUFFER *buf = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
  101. if (buf == NULL ||
  102. !sk_CRYPTO_BUFFER_push(out_certs, buf)) {
  103. CRYPTO_BUFFER_free(buf);
  104. goto err;
  105. }
  106. }
  107. ret = 1;
  108. err:
  109. OPENSSL_free(der_bytes);
  110. if (!ret) {
  111. while (sk_CRYPTO_BUFFER_num(out_certs) != initial_certs_len) {
  112. CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_pop(out_certs);
  113. CRYPTO_BUFFER_free(buf);
  114. }
  115. }
  116. return ret;
  117. }
  118. int pkcs7_bundle(CBB *out, int (*cb)(CBB *out, const void *arg),
  119. const void *arg) {
  120. CBB outer_seq, oid, wrapped_seq, seq, version_bytes, digest_algos_set,
  121. content_info;
  122. // See https://tools.ietf.org/html/rfc2315#section-7
  123. if (!CBB_add_asn1(out, &outer_seq, CBS_ASN1_SEQUENCE) ||
  124. !CBB_add_asn1(&outer_seq, &oid, CBS_ASN1_OBJECT) ||
  125. !CBB_add_bytes(&oid, kPKCS7SignedData, sizeof(kPKCS7SignedData)) ||
  126. !CBB_add_asn1(&outer_seq, &wrapped_seq,
  127. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  128. // See https://tools.ietf.org/html/rfc2315#section-9.1
  129. !CBB_add_asn1(&wrapped_seq, &seq, CBS_ASN1_SEQUENCE) ||
  130. !CBB_add_asn1(&seq, &version_bytes, CBS_ASN1_INTEGER) ||
  131. !CBB_add_u8(&version_bytes, 1) ||
  132. !CBB_add_asn1(&seq, &digest_algos_set, CBS_ASN1_SET) ||
  133. !CBB_add_asn1(&seq, &content_info, CBS_ASN1_SEQUENCE) ||
  134. !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) ||
  135. !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) ||
  136. !cb(&seq, arg)) {
  137. return 0;
  138. }
  139. return CBB_flush(out);
  140. }