x_x509.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 1995-2021 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. ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
  17. ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
  18. ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER),
  19. ASN1_EMBED(X509_CINF, signature, X509_ALGOR),
  20. ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
  21. ASN1_EMBED(X509_CINF, validity, X509_VAL),
  22. ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
  23. ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
  24. ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
  25. ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
  26. ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3)
  27. } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
  28. IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
  29. /* X509 top level structure needs a bit of customisation */
  30. extern void ossl_policy_cache_free(X509_POLICY_CACHE *cache);
  31. static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  32. void *exarg)
  33. {
  34. X509 *ret = (X509 *)*pval;
  35. switch (operation) {
  36. case ASN1_OP_D2I_PRE:
  37. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
  38. X509_CERT_AUX_free(ret->aux);
  39. ASN1_OCTET_STRING_free(ret->skid);
  40. AUTHORITY_KEYID_free(ret->akid);
  41. CRL_DIST_POINTS_free(ret->crldp);
  42. ossl_policy_cache_free(ret->policy_cache);
  43. GENERAL_NAMES_free(ret->altname);
  44. NAME_CONSTRAINTS_free(ret->nc);
  45. #ifndef OPENSSL_NO_RFC3779
  46. sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
  47. ASIdentifiers_free(ret->rfc3779_asid);
  48. #endif
  49. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  50. /* fall through */
  51. case ASN1_OP_NEW_POST:
  52. ret->ex_cached = 0;
  53. ret->ex_kusage = 0;
  54. ret->ex_xkusage = 0;
  55. ret->ex_nscert = 0;
  56. ret->ex_flags = 0;
  57. ret->ex_pathlen = -1;
  58. ret->ex_pcpathlen = -1;
  59. ret->skid = NULL;
  60. ret->akid = NULL;
  61. ret->policy_cache = NULL;
  62. ret->altname = NULL;
  63. ret->nc = NULL;
  64. #ifndef OPENSSL_NO_RFC3779
  65. ret->rfc3779_addr = NULL;
  66. ret->rfc3779_asid = NULL;
  67. #endif
  68. ret->distinguishing_id = NULL;
  69. ret->aux = NULL;
  70. ret->crldp = NULL;
  71. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
  72. return 0;
  73. break;
  74. case ASN1_OP_FREE_POST:
  75. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
  76. X509_CERT_AUX_free(ret->aux);
  77. ASN1_OCTET_STRING_free(ret->skid);
  78. AUTHORITY_KEYID_free(ret->akid);
  79. CRL_DIST_POINTS_free(ret->crldp);
  80. ossl_policy_cache_free(ret->policy_cache);
  81. GENERAL_NAMES_free(ret->altname);
  82. NAME_CONSTRAINTS_free(ret->nc);
  83. #ifndef OPENSSL_NO_RFC3779
  84. sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
  85. ASIdentifiers_free(ret->rfc3779_asid);
  86. #endif
  87. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  88. OPENSSL_free(ret->propq);
  89. break;
  90. case ASN1_OP_DUP_POST:
  91. {
  92. X509 *old = exarg;
  93. if (!ossl_x509_set0_libctx(ret, old->libctx, old->propq))
  94. return 0;
  95. }
  96. break;
  97. case ASN1_OP_GET0_LIBCTX:
  98. {
  99. OSSL_LIB_CTX **libctx = exarg;
  100. *libctx = ret->libctx;
  101. }
  102. break;
  103. case ASN1_OP_GET0_PROPQ:
  104. {
  105. const char **propq = exarg;
  106. *propq = ret->propq;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. return 1;
  113. }
  114. ASN1_SEQUENCE_ref(X509, x509_cb) = {
  115. ASN1_EMBED(X509, cert_info, X509_CINF),
  116. ASN1_EMBED(X509, sig_alg, X509_ALGOR),
  117. ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
  118. } ASN1_SEQUENCE_END_ref(X509, X509)
  119. IMPLEMENT_ASN1_FUNCTIONS(X509)
  120. IMPLEMENT_ASN1_DUP_FUNCTION(X509)
  121. /*
  122. * This should only be used if the X509 object was embedded inside another
  123. * asn1 object and it needs a libctx to operate.
  124. * Use X509_new_ex() instead if possible.
  125. */
  126. int ossl_x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq)
  127. {
  128. if (x != NULL) {
  129. x->libctx = libctx;
  130. OPENSSL_free(x->propq);
  131. x->propq = NULL;
  132. if (propq != NULL) {
  133. x->propq = OPENSSL_strdup(propq);
  134. if (x->propq == NULL)
  135. return 0;
  136. }
  137. }
  138. return 1;
  139. }
  140. X509 *X509_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
  141. {
  142. X509 *cert = NULL;
  143. cert = (X509 *)ASN1_item_new_ex(X509_it(), libctx, propq);
  144. if (!ossl_x509_set0_libctx(cert, libctx, propq)) {
  145. X509_free(cert);
  146. cert = NULL;
  147. }
  148. return cert;
  149. }
  150. int X509_set_ex_data(X509 *r, int idx, void *arg)
  151. {
  152. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  153. }
  154. void *X509_get_ex_data(const X509 *r, int idx)
  155. {
  156. return CRYPTO_get_ex_data(&r->ex_data, idx);
  157. }
  158. /*
  159. * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
  160. * extra info tagged on the end. Since these functions set how a certificate
  161. * is trusted they should only be used when the certificate comes from a
  162. * reliable source such as local storage.
  163. */
  164. X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
  165. {
  166. const unsigned char *q;
  167. X509 *ret;
  168. int freeret = 0;
  169. /* Save start position */
  170. q = *pp;
  171. if (a == NULL || *a == NULL)
  172. freeret = 1;
  173. ret = d2i_X509(a, &q, length);
  174. /* If certificate unreadable then forget it */
  175. if (ret == NULL)
  176. return NULL;
  177. /* update length */
  178. length -= q - *pp;
  179. if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
  180. goto err;
  181. *pp = q;
  182. return ret;
  183. err:
  184. if (freeret) {
  185. X509_free(ret);
  186. if (a)
  187. *a = NULL;
  188. }
  189. return NULL;
  190. }
  191. /*
  192. * Serialize trusted certificate to *pp or just return the required buffer
  193. * length if pp == NULL. We ultimately want to avoid modifying *pp in the
  194. * error path, but that depends on similar hygiene in lower-level functions.
  195. * Here we avoid compounding the problem.
  196. */
  197. static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp)
  198. {
  199. int length, tmplen;
  200. unsigned char *start = pp != NULL ? *pp : NULL;
  201. /*
  202. * This might perturb *pp on error, but fixing that belongs in i2d_X509()
  203. * not here. It should be that if a == NULL length is zero, but we check
  204. * both just in case.
  205. */
  206. length = i2d_X509(a, pp);
  207. if (length <= 0 || a == NULL)
  208. return length;
  209. tmplen = i2d_X509_CERT_AUX(a->aux, pp);
  210. if (tmplen < 0) {
  211. if (start != NULL)
  212. *pp = start;
  213. return tmplen;
  214. }
  215. length += tmplen;
  216. return length;
  217. }
  218. /*
  219. * Serialize trusted certificate to *pp, or just return the required buffer
  220. * length if pp == NULL.
  221. *
  222. * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
  223. * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
  224. * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
  225. * allocated buffer.
  226. */
  227. int i2d_X509_AUX(const X509 *a, unsigned char **pp)
  228. {
  229. int length;
  230. unsigned char *tmp;
  231. /* Buffer provided by caller */
  232. if (pp == NULL || *pp != NULL)
  233. return i2d_x509_aux_internal(a, pp);
  234. /* Obtain the combined length */
  235. if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
  236. return length;
  237. /* Allocate requisite combined storage */
  238. *pp = tmp = OPENSSL_malloc(length);
  239. if (tmp == NULL)
  240. return -1;
  241. /* Encode, but keep *pp at the originally malloced pointer */
  242. length = i2d_x509_aux_internal(a, &tmp);
  243. if (length <= 0) {
  244. OPENSSL_free(*pp);
  245. *pp = NULL;
  246. }
  247. return length;
  248. }
  249. int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
  250. {
  251. x->cert_info.enc.modified = 1;
  252. return i2d_X509_CINF(&x->cert_info, pp);
  253. }
  254. void X509_get0_signature(const ASN1_BIT_STRING **psig,
  255. const X509_ALGOR **palg, const X509 *x)
  256. {
  257. if (psig)
  258. *psig = &x->signature;
  259. if (palg)
  260. *palg = &x->sig_alg;
  261. }
  262. int X509_get_signature_nid(const X509 *x)
  263. {
  264. return OBJ_obj2nid(x->sig_alg.algorithm);
  265. }
  266. void X509_set0_distinguishing_id(X509 *x, ASN1_OCTET_STRING *d_id)
  267. {
  268. ASN1_OCTET_STRING_free(x->distinguishing_id);
  269. x->distinguishing_id = d_id;
  270. }
  271. ASN1_OCTET_STRING *X509_get0_distinguishing_id(X509 *x)
  272. {
  273. return x->distinguishing_id;
  274. }