選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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