You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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