您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

155 行
4.7 KiB

  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/x509.h>
  15. #include <openssl/bytestring.h>
  16. #include <openssl/err.h>
  17. #include <openssl/obj.h>
  18. #include <openssl/stack.h>
  19. #include "../bytestring/internal.h"
  20. int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs) {
  21. uint8_t *der_bytes = NULL;
  22. size_t der_len;
  23. CBS in, content_info, content_type, wrapped_signed_data, signed_data,
  24. certificates;
  25. const size_t initial_certs_len = sk_X509_num(out_certs);
  26. uint64_t version;
  27. int ret = 0;
  28. /* The input may be in BER format. */
  29. if (!CBS_asn1_ber_to_der(cbs, &der_bytes, &der_len)) {
  30. return 0;
  31. }
  32. if (der_bytes != NULL) {
  33. CBS_init(&in, der_bytes, der_len);
  34. } else {
  35. CBS_init(&in, CBS_data(cbs), CBS_len(cbs));
  36. }
  37. /* See https://tools.ietf.org/html/rfc2315#section-7 */
  38. if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE) ||
  39. !CBS_get_asn1(&content_info, &content_type, CBS_ASN1_OBJECT)) {
  40. goto err;
  41. }
  42. if (OBJ_cbs2nid(&content_type) != NID_pkcs7_signed) {
  43. OPENSSL_PUT_ERROR(X509, PKCS7_get_certificates,
  44. X509_R_NOT_PKCS7_SIGNED_DATA);
  45. goto err;
  46. }
  47. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  48. if (!CBS_get_asn1(&content_info, &wrapped_signed_data,
  49. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  50. !CBS_get_asn1(&wrapped_signed_data, &signed_data, CBS_ASN1_SEQUENCE) ||
  51. !CBS_get_asn1_uint64(&signed_data, &version) ||
  52. !CBS_get_asn1(&signed_data, NULL /* digests */, CBS_ASN1_SET) ||
  53. !CBS_get_asn1(&signed_data, NULL /* content */, CBS_ASN1_SEQUENCE)) {
  54. goto err;
  55. }
  56. if (version < 1) {
  57. OPENSSL_PUT_ERROR(X509, PKCS7_get_certificates,
  58. X509_R_BAD_PKCS7_VERSION);
  59. goto err;
  60. }
  61. if (!CBS_get_asn1(&signed_data, &certificates,
  62. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  63. OPENSSL_PUT_ERROR(X509, PKCS7_get_certificates,
  64. X509_R_NO_CERTIFICATES_INCLUDED);
  65. goto err;
  66. }
  67. while (CBS_len(&certificates) > 0) {
  68. CBS cert;
  69. X509 *x509;
  70. const uint8_t *inp;
  71. if (!CBS_get_asn1_element(&certificates, &cert, CBS_ASN1_SEQUENCE)) {
  72. goto err;
  73. }
  74. inp = CBS_data(&cert);
  75. x509 = d2i_X509(NULL, &inp, CBS_len(&cert));
  76. if (!x509) {
  77. goto err;
  78. }
  79. if (inp != CBS_data(&cert) + CBS_len(&cert)) {
  80. /* This suggests a disconnect between the two ASN.1 parsers. */
  81. goto err;
  82. }
  83. sk_X509_push(out_certs, x509);
  84. }
  85. ret = 1;
  86. err:
  87. if (der_bytes) {
  88. OPENSSL_free(der_bytes);
  89. }
  90. if (!ret) {
  91. while (sk_X509_num(out_certs) != initial_certs_len) {
  92. X509 *x509 = sk_X509_pop(out_certs);
  93. X509_free(x509);
  94. }
  95. }
  96. return ret;
  97. }
  98. int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) {
  99. CBB outer_seq, wrapped_seq, seq, version_bytes, digest_algos_set,
  100. content_info, certificates;
  101. size_t i;
  102. /* See https://tools.ietf.org/html/rfc2315#section-7 */
  103. if (!CBB_add_asn1(out, &outer_seq, CBS_ASN1_SEQUENCE) ||
  104. !OBJ_nid2cbb(&outer_seq, NID_pkcs7_signed) ||
  105. !CBB_add_asn1(&outer_seq, &wrapped_seq,
  106. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  107. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  108. !CBB_add_asn1(&wrapped_seq, &seq, CBS_ASN1_SEQUENCE) ||
  109. !CBB_add_asn1(&seq, &version_bytes, CBS_ASN1_INTEGER) ||
  110. !CBB_add_u8(&version_bytes, 1) ||
  111. !CBB_add_asn1(&seq, &digest_algos_set, CBS_ASN1_SET) ||
  112. !CBB_add_asn1(&seq, &content_info, CBS_ASN1_SEQUENCE) ||
  113. !OBJ_nid2cbb(&content_info, NID_pkcs7_data) ||
  114. !CBB_add_asn1(&seq, &certificates,
  115. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  116. return 0;
  117. }
  118. for (i = 0; i < sk_X509_num(certs); i++) {
  119. X509 *x509 = sk_X509_value(certs, i);
  120. uint8_t *buf;
  121. int len = i2d_X509(x509, NULL);
  122. if (len < 0 ||
  123. !CBB_add_space(&certificates, &buf, len) ||
  124. i2d_X509(x509, &buf) < 0) {
  125. return 0;
  126. }
  127. }
  128. return CBB_flush(out);
  129. }