x_x509.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/x509v3.h>
  15. #include "crypto/x509.h"
  16. #ifndef OPENSSL_NO_RFC3779
  17. DEFINE_STACK_OF(IPAddressFamily)
  18. #endif
  19. ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
  20. ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
  21. ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER),
  22. ASN1_EMBED(X509_CINF, signature, X509_ALGOR),
  23. ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
  24. ASN1_EMBED(X509_CINF, validity, X509_VAL),
  25. ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
  26. ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
  27. ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
  28. ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
  29. ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3)
  30. } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
  31. IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
  32. /* X509 top level structure needs a bit of customisation */
  33. extern void policy_cache_free(X509_POLICY_CACHE *cache);
  34. static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  35. void *exarg)
  36. {
  37. X509 *ret = (X509 *)*pval;
  38. switch (operation) {
  39. case ASN1_OP_D2I_PRE:
  40. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
  41. X509_CERT_AUX_free(ret->aux);
  42. ASN1_OCTET_STRING_free(ret->skid);
  43. AUTHORITY_KEYID_free(ret->akid);
  44. CRL_DIST_POINTS_free(ret->crldp);
  45. policy_cache_free(ret->policy_cache);
  46. GENERAL_NAMES_free(ret->altname);
  47. NAME_CONSTRAINTS_free(ret->nc);
  48. #ifndef OPENSSL_NO_RFC3779
  49. sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
  50. ASIdentifiers_free(ret->rfc3779_asid);
  51. #endif
  52. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  53. /* fall thru */
  54. case ASN1_OP_NEW_POST:
  55. ret->ex_cached = 0;
  56. ret->ex_kusage = 0;
  57. ret->ex_xkusage = 0;
  58. ret->ex_nscert = 0;
  59. ret->ex_flags = 0;
  60. ret->ex_pathlen = -1;
  61. ret->ex_pcpathlen = -1;
  62. ret->skid = NULL;
  63. ret->akid = NULL;
  64. ret->policy_cache = NULL;
  65. ret->altname = NULL;
  66. ret->nc = NULL;
  67. #ifndef OPENSSL_NO_RFC3779
  68. ret->rfc3779_addr = NULL;
  69. ret->rfc3779_asid = NULL;
  70. #endif
  71. ret->distinguishing_id = NULL;
  72. ret->aux = NULL;
  73. ret->crldp = NULL;
  74. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
  75. return 0;
  76. break;
  77. case ASN1_OP_FREE_POST:
  78. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
  79. X509_CERT_AUX_free(ret->aux);
  80. ASN1_OCTET_STRING_free(ret->skid);
  81. AUTHORITY_KEYID_free(ret->akid);
  82. CRL_DIST_POINTS_free(ret->crldp);
  83. policy_cache_free(ret->policy_cache);
  84. GENERAL_NAMES_free(ret->altname);
  85. NAME_CONSTRAINTS_free(ret->nc);
  86. #ifndef OPENSSL_NO_RFC3779
  87. sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
  88. ASIdentifiers_free(ret->rfc3779_asid);
  89. #endif
  90. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  91. break;
  92. }
  93. return 1;
  94. }
  95. ASN1_SEQUENCE_ref(X509, x509_cb) = {
  96. ASN1_EMBED(X509, cert_info, X509_CINF),
  97. ASN1_EMBED(X509, sig_alg, X509_ALGOR),
  98. ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
  99. } ASN1_SEQUENCE_END_ref(X509, X509)
  100. IMPLEMENT_ASN1_FUNCTIONS(X509)
  101. IMPLEMENT_ASN1_DUP_FUNCTION(X509)
  102. int X509_set_ex_data(X509 *r, int idx, void *arg)
  103. {
  104. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  105. }
  106. void *X509_get_ex_data(const X509 *r, int idx)
  107. {
  108. return CRYPTO_get_ex_data(&r->ex_data, idx);
  109. }
  110. /*
  111. * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
  112. * extra info tagged on the end. Since these functions set how a certificate
  113. * is trusted they should only be used when the certificate comes from a
  114. * reliable source such as local storage.
  115. */
  116. X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
  117. {
  118. const unsigned char *q;
  119. X509 *ret;
  120. int freeret = 0;
  121. /* Save start position */
  122. q = *pp;
  123. if (a == NULL || *a == NULL)
  124. freeret = 1;
  125. ret = d2i_X509(a, &q, length);
  126. /* If certificate unreadable then forget it */
  127. if (ret == NULL)
  128. return NULL;
  129. /* update length */
  130. length -= q - *pp;
  131. if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
  132. goto err;
  133. *pp = q;
  134. return ret;
  135. err:
  136. if (freeret) {
  137. X509_free(ret);
  138. if (a)
  139. *a = NULL;
  140. }
  141. return NULL;
  142. }
  143. /*
  144. * Serialize trusted certificate to *pp or just return the required buffer
  145. * length if pp == NULL. We ultimately want to avoid modifying *pp in the
  146. * error path, but that depends on similar hygiene in lower-level functions.
  147. * Here we avoid compounding the problem.
  148. */
  149. static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp)
  150. {
  151. int length, tmplen;
  152. unsigned char *start = pp != NULL ? *pp : NULL;
  153. /*
  154. * This might perturb *pp on error, but fixing that belongs in i2d_X509()
  155. * not here. It should be that if a == NULL length is zero, but we check
  156. * both just in case.
  157. */
  158. length = i2d_X509(a, pp);
  159. if (length <= 0 || a == NULL)
  160. return length;
  161. tmplen = i2d_X509_CERT_AUX(a->aux, pp);
  162. if (tmplen < 0) {
  163. if (start != NULL)
  164. *pp = start;
  165. return tmplen;
  166. }
  167. length += tmplen;
  168. return length;
  169. }
  170. /*
  171. * Serialize trusted certificate to *pp, or just return the required buffer
  172. * length if pp == NULL.
  173. *
  174. * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
  175. * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
  176. * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
  177. * allocated buffer.
  178. */
  179. int i2d_X509_AUX(const X509 *a, unsigned char **pp)
  180. {
  181. int length;
  182. unsigned char *tmp;
  183. /* Buffer provided by caller */
  184. if (pp == NULL || *pp != NULL)
  185. return i2d_x509_aux_internal(a, pp);
  186. /* Obtain the combined length */
  187. if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
  188. return length;
  189. /* Allocate requisite combined storage */
  190. *pp = tmp = OPENSSL_malloc(length);
  191. if (tmp == NULL) {
  192. X509err(X509_F_I2D_X509_AUX, ERR_R_MALLOC_FAILURE);
  193. return -1;
  194. }
  195. /* Encode, but keep *pp at the originally malloced pointer */
  196. length = i2d_x509_aux_internal(a, &tmp);
  197. if (length <= 0) {
  198. OPENSSL_free(*pp);
  199. *pp = NULL;
  200. }
  201. return length;
  202. }
  203. int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
  204. {
  205. x->cert_info.enc.modified = 1;
  206. return i2d_X509_CINF(&x->cert_info, pp);
  207. }
  208. void X509_get0_signature(const ASN1_BIT_STRING **psig,
  209. const X509_ALGOR **palg, const X509 *x)
  210. {
  211. if (psig)
  212. *psig = &x->signature;
  213. if (palg)
  214. *palg = &x->sig_alg;
  215. }
  216. int X509_get_signature_nid(const X509 *x)
  217. {
  218. return OBJ_obj2nid(x->sig_alg.algorithm);
  219. }
  220. void X509_set0_distinguishing_id(X509 *x, ASN1_OCTET_STRING *d_id)
  221. {
  222. ASN1_OCTET_STRING_free(x->distinguishing_id);
  223. x->distinguishing_id = d_id;
  224. }
  225. ASN1_OCTET_STRING *X509_get0_distinguishing_id(X509 *x)
  226. {
  227. return x->distinguishing_id;
  228. }