Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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