25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

358 satır
10 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 <assert.h>
  16. #include <limits.h>
  17. #include <openssl/bytestring.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/obj.h>
  21. #include <openssl/pem.h>
  22. #include <openssl/stack.h>
  23. #include "../bytestring/internal.h"
  24. /* 1.2.840.113549.1.7.1 */
  25. static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  26. 0x0d, 0x01, 0x07, 0x01};
  27. /* 1.2.840.113549.1.7.2 */
  28. static const uint8_t kPKCS7SignedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
  29. 0x0d, 0x01, 0x07, 0x02};
  30. /* pkcs7_parse_header reads the non-certificate/non-CRL prefix of a PKCS#7
  31. * SignedData blob from |cbs| and sets |*out| to point to the rest of the
  32. * input. If the input is in BER format, then |*der_bytes| will be set to a
  33. * pointer that needs to be freed by the caller once they have finished
  34. * processing |*out| (which will be pointing into |*der_bytes|).
  35. *
  36. * It returns one on success or zero on error. On error, |*der_bytes| is
  37. * NULL. */
  38. static int pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs) {
  39. size_t der_len;
  40. CBS in, content_info, content_type, wrapped_signed_data, signed_data;
  41. uint64_t version;
  42. /* The input may be in BER format. */
  43. *der_bytes = NULL;
  44. if (!CBS_asn1_ber_to_der(cbs, der_bytes, &der_len)) {
  45. return 0;
  46. }
  47. if (*der_bytes != NULL) {
  48. CBS_init(&in, *der_bytes, der_len);
  49. } else {
  50. CBS_init(&in, CBS_data(cbs), CBS_len(cbs));
  51. }
  52. /* See https://tools.ietf.org/html/rfc2315#section-7 */
  53. if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE) ||
  54. !CBS_get_asn1(&content_info, &content_type, CBS_ASN1_OBJECT)) {
  55. goto err;
  56. }
  57. if (!CBS_mem_equal(&content_type, kPKCS7SignedData,
  58. sizeof(kPKCS7SignedData))) {
  59. OPENSSL_PUT_ERROR(X509, X509_R_NOT_PKCS7_SIGNED_DATA);
  60. goto err;
  61. }
  62. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  63. if (!CBS_get_asn1(&content_info, &wrapped_signed_data,
  64. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  65. !CBS_get_asn1(&wrapped_signed_data, &signed_data, CBS_ASN1_SEQUENCE) ||
  66. !CBS_get_asn1_uint64(&signed_data, &version) ||
  67. !CBS_get_asn1(&signed_data, NULL /* digests */, CBS_ASN1_SET) ||
  68. !CBS_get_asn1(&signed_data, NULL /* content */, CBS_ASN1_SEQUENCE)) {
  69. goto err;
  70. }
  71. if (version < 1) {
  72. OPENSSL_PUT_ERROR(X509, X509_R_BAD_PKCS7_VERSION);
  73. goto err;
  74. }
  75. CBS_init(out, CBS_data(&signed_data), CBS_len(&signed_data));
  76. return 1;
  77. err:
  78. OPENSSL_free(*der_bytes);
  79. *der_bytes = NULL;
  80. return 0;
  81. }
  82. int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs) {
  83. CBS signed_data, certificates;
  84. uint8_t *der_bytes = NULL;
  85. int ret = 0;
  86. const size_t initial_certs_len = sk_X509_num(out_certs);
  87. if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs)) {
  88. return 0;
  89. }
  90. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  91. if (!CBS_get_asn1(&signed_data, &certificates,
  92. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  93. OPENSSL_PUT_ERROR(X509, X509_R_NO_CERTIFICATES_INCLUDED);
  94. goto err;
  95. }
  96. while (CBS_len(&certificates) > 0) {
  97. CBS cert;
  98. X509 *x509;
  99. const uint8_t *inp;
  100. if (!CBS_get_asn1_element(&certificates, &cert, CBS_ASN1_SEQUENCE)) {
  101. goto err;
  102. }
  103. if (CBS_len(&cert) > LONG_MAX) {
  104. goto err;
  105. }
  106. inp = CBS_data(&cert);
  107. x509 = d2i_X509(NULL, &inp, (long)CBS_len(&cert));
  108. if (!x509) {
  109. goto err;
  110. }
  111. assert(inp == CBS_data(&cert) + CBS_len(&cert));
  112. if (sk_X509_push(out_certs, x509) == 0) {
  113. X509_free(x509);
  114. goto err;
  115. }
  116. }
  117. ret = 1;
  118. err:
  119. OPENSSL_free(der_bytes);
  120. if (!ret) {
  121. while (sk_X509_num(out_certs) != initial_certs_len) {
  122. X509 *x509 = sk_X509_pop(out_certs);
  123. X509_free(x509);
  124. }
  125. }
  126. return ret;
  127. }
  128. int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs) {
  129. CBS signed_data, crls;
  130. uint8_t *der_bytes = NULL;
  131. int ret = 0;
  132. const size_t initial_crls_len = sk_X509_CRL_num(out_crls);
  133. if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs)) {
  134. return 0;
  135. }
  136. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  137. /* Even if only CRLs are included, there may be an empty certificates block.
  138. * OpenSSL does this, for example. */
  139. if (CBS_peek_asn1_tag(&signed_data,
  140. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) &&
  141. !CBS_get_asn1(&signed_data, NULL /* certificates */,
  142. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  143. goto err;
  144. }
  145. if (!CBS_get_asn1(&signed_data, &crls,
  146. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) {
  147. OPENSSL_PUT_ERROR(X509, X509_R_NO_CRLS_INCLUDED);
  148. goto err;
  149. }
  150. while (CBS_len(&crls) > 0) {
  151. CBS crl_data;
  152. X509_CRL *crl;
  153. const uint8_t *inp;
  154. if (!CBS_get_asn1_element(&crls, &crl_data, CBS_ASN1_SEQUENCE)) {
  155. goto err;
  156. }
  157. if (CBS_len(&crl_data) > LONG_MAX) {
  158. goto err;
  159. }
  160. inp = CBS_data(&crl_data);
  161. crl = d2i_X509_CRL(NULL, &inp, (long)CBS_len(&crl_data));
  162. if (!crl) {
  163. goto err;
  164. }
  165. assert(inp == CBS_data(&crl_data) + CBS_len(&crl_data));
  166. if (sk_X509_CRL_push(out_crls, crl) == 0) {
  167. X509_CRL_free(crl);
  168. goto err;
  169. }
  170. }
  171. ret = 1;
  172. err:
  173. OPENSSL_free(der_bytes);
  174. if (!ret) {
  175. while (sk_X509_CRL_num(out_crls) != initial_crls_len) {
  176. X509_CRL_free(sk_X509_CRL_pop(out_crls));
  177. }
  178. }
  179. return ret;
  180. }
  181. int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio) {
  182. uint8_t *data;
  183. long len;
  184. int ret;
  185. /* Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM
  186. * internally will actually allow several other values too, including
  187. * "CERTIFICATE". */
  188. if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */,
  189. PEM_STRING_PKCS7, pem_bio,
  190. NULL /* password callback */,
  191. NULL /* password callback argument */)) {
  192. return 0;
  193. }
  194. CBS cbs;
  195. CBS_init(&cbs, data, len);
  196. ret = PKCS7_get_certificates(out_certs, &cbs);
  197. OPENSSL_free(data);
  198. return ret;
  199. }
  200. int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio) {
  201. uint8_t *data;
  202. long len;
  203. int ret;
  204. /* Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM
  205. * internally will actually allow several other values too, including
  206. * "CERTIFICATE". */
  207. if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */,
  208. PEM_STRING_PKCS7, pem_bio,
  209. NULL /* password callback */,
  210. NULL /* password callback argument */)) {
  211. return 0;
  212. }
  213. CBS cbs;
  214. CBS_init(&cbs, data, len);
  215. ret = PKCS7_get_CRLs(out_crls, &cbs);
  216. OPENSSL_free(data);
  217. return ret;
  218. }
  219. /* pkcs7_bundle writes a PKCS#7, SignedData structure to |out| and then calls
  220. * |cb| with a CBB to which certificate or CRL data can be written, and the
  221. * opaque context pointer, |arg|. The callback can return zero to indicate an
  222. * error.
  223. *
  224. * pkcs7_bundle returns one on success or zero on error. */
  225. static int pkcs7_bundle(CBB *out, int (*cb)(CBB *out, const void *arg),
  226. const void *arg) {
  227. CBB outer_seq, oid, wrapped_seq, seq, version_bytes, digest_algos_set,
  228. content_info;
  229. /* See https://tools.ietf.org/html/rfc2315#section-7 */
  230. if (!CBB_add_asn1(out, &outer_seq, CBS_ASN1_SEQUENCE) ||
  231. !CBB_add_asn1(&outer_seq, &oid, CBS_ASN1_OBJECT) ||
  232. !CBB_add_bytes(&oid, kPKCS7SignedData, sizeof(kPKCS7SignedData)) ||
  233. !CBB_add_asn1(&outer_seq, &wrapped_seq,
  234. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  235. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  236. !CBB_add_asn1(&wrapped_seq, &seq, CBS_ASN1_SEQUENCE) ||
  237. !CBB_add_asn1(&seq, &version_bytes, CBS_ASN1_INTEGER) ||
  238. !CBB_add_u8(&version_bytes, 1) ||
  239. !CBB_add_asn1(&seq, &digest_algos_set, CBS_ASN1_SET) ||
  240. !CBB_add_asn1(&seq, &content_info, CBS_ASN1_SEQUENCE) ||
  241. !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) ||
  242. !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) ||
  243. !cb(&seq, arg)) {
  244. return 0;
  245. }
  246. return CBB_flush(out);
  247. }
  248. static int pkcs7_bundle_certificates_cb(CBB *out, const void *arg) {
  249. const STACK_OF(X509) *certs = arg;
  250. size_t i;
  251. CBB certificates;
  252. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  253. if (!CBB_add_asn1(out, &certificates,
  254. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  255. return 0;
  256. }
  257. for (i = 0; i < sk_X509_num(certs); i++) {
  258. X509 *x509 = sk_X509_value(certs, i);
  259. uint8_t *buf;
  260. int len = i2d_X509(x509, NULL);
  261. if (len < 0 ||
  262. !CBB_add_space(&certificates, &buf, len) ||
  263. i2d_X509(x509, &buf) < 0) {
  264. return 0;
  265. }
  266. }
  267. return CBB_flush(out);
  268. }
  269. int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) {
  270. return pkcs7_bundle(out, pkcs7_bundle_certificates_cb, certs);
  271. }
  272. static int pkcs7_bundle_crls_cb(CBB *out, const void *arg) {
  273. const STACK_OF(X509_CRL) *crls = arg;
  274. size_t i;
  275. CBB crl_data;
  276. /* See https://tools.ietf.org/html/rfc2315#section-9.1 */
  277. if (!CBB_add_asn1(out, &crl_data,
  278. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) {
  279. return 0;
  280. }
  281. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  282. X509_CRL *crl = sk_X509_CRL_value(crls, i);
  283. uint8_t *buf;
  284. int len = i2d_X509_CRL(crl, NULL);
  285. if (len < 0 ||
  286. !CBB_add_space(&crl_data, &buf, len) ||
  287. i2d_X509_CRL(crl, &buf) < 0) {
  288. return 0;
  289. }
  290. }
  291. return CBB_flush(out);
  292. }
  293. int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) {
  294. return pkcs7_bundle(out, pkcs7_bundle_crls_cb, crls);
  295. }