x_x509.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 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. 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. /* fall thru */
  50. case ASN1_OP_NEW_POST:
  51. ret->ex_cached = 0;
  52. ret->ex_kusage = 0;
  53. ret->ex_xkusage = 0;
  54. ret->ex_nscert = 0;
  55. ret->ex_flags = 0;
  56. ret->ex_pathlen = -1;
  57. ret->ex_pcpathlen = -1;
  58. ret->skid = NULL;
  59. ret->akid = NULL;
  60. ret->policy_cache = NULL;
  61. ret->altname = NULL;
  62. ret->nc = NULL;
  63. #ifndef OPENSSL_NO_RFC3779
  64. ret->rfc3779_addr = NULL;
  65. ret->rfc3779_asid = NULL;
  66. #endif
  67. ret->aux = NULL;
  68. ret->crldp = NULL;
  69. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
  70. return 0;
  71. break;
  72. case ASN1_OP_FREE_POST:
  73. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
  74. X509_CERT_AUX_free(ret->aux);
  75. ASN1_OCTET_STRING_free(ret->skid);
  76. AUTHORITY_KEYID_free(ret->akid);
  77. CRL_DIST_POINTS_free(ret->crldp);
  78. policy_cache_free(ret->policy_cache);
  79. GENERAL_NAMES_free(ret->altname);
  80. NAME_CONSTRAINTS_free(ret->nc);
  81. #ifndef OPENSSL_NO_RFC3779
  82. sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
  83. ASIdentifiers_free(ret->rfc3779_asid);
  84. #endif
  85. break;
  86. }
  87. return 1;
  88. }
  89. ASN1_SEQUENCE_ref(X509, x509_cb) = {
  90. ASN1_EMBED(X509, cert_info, X509_CINF),
  91. ASN1_EMBED(X509, sig_alg, X509_ALGOR),
  92. ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
  93. } ASN1_SEQUENCE_END_ref(X509, X509)
  94. IMPLEMENT_ASN1_FUNCTIONS(X509)
  95. IMPLEMENT_ASN1_DUP_FUNCTION(X509)
  96. int X509_set_ex_data(X509 *r, int idx, void *arg)
  97. {
  98. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  99. }
  100. void *X509_get_ex_data(X509 *r, int idx)
  101. {
  102. return CRYPTO_get_ex_data(&r->ex_data, idx);
  103. }
  104. /*
  105. * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
  106. * extra info tagged on the end. Since these functions set how a certificate
  107. * is trusted they should only be used when the certificate comes from a
  108. * reliable source such as local storage.
  109. */
  110. X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
  111. {
  112. const unsigned char *q;
  113. X509 *ret;
  114. int freeret = 0;
  115. /* Save start position */
  116. q = *pp;
  117. if (a == NULL || *a == NULL)
  118. freeret = 1;
  119. ret = d2i_X509(a, &q, length);
  120. /* If certificate unreadable then forget it */
  121. if (ret == NULL)
  122. return NULL;
  123. /* update length */
  124. length -= q - *pp;
  125. if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
  126. goto err;
  127. *pp = q;
  128. return ret;
  129. err:
  130. if (freeret) {
  131. X509_free(ret);
  132. if (a)
  133. *a = NULL;
  134. }
  135. return NULL;
  136. }
  137. /*
  138. * Serialize trusted certificate to *pp or just return the required buffer
  139. * length if pp == NULL. We ultimately want to avoid modifying *pp in the
  140. * error path, but that depends on similar hygiene in lower-level functions.
  141. * Here we avoid compounding the problem.
  142. */
  143. static int i2d_x509_aux_internal(X509 *a, unsigned char **pp)
  144. {
  145. int length, tmplen;
  146. unsigned char *start = pp != NULL ? *pp : NULL;
  147. /*
  148. * This might perturb *pp on error, but fixing that belongs in i2d_X509()
  149. * not here. It should be that if a == NULL length is zero, but we check
  150. * both just in case.
  151. */
  152. length = i2d_X509(a, pp);
  153. if (length <= 0 || a == NULL)
  154. return length;
  155. tmplen = i2d_X509_CERT_AUX(a->aux, pp);
  156. if (tmplen < 0) {
  157. if (start != NULL)
  158. *pp = start;
  159. return tmplen;
  160. }
  161. length += tmplen;
  162. return length;
  163. }
  164. /*
  165. * Serialize trusted certificate to *pp, or just return the required buffer
  166. * length if pp == NULL.
  167. *
  168. * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
  169. * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
  170. * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
  171. * allocated buffer.
  172. */
  173. int i2d_X509_AUX(X509 *a, unsigned char **pp)
  174. {
  175. int length;
  176. unsigned char *tmp;
  177. /* Buffer provided by caller */
  178. if (pp == NULL || *pp != NULL)
  179. return i2d_x509_aux_internal(a, pp);
  180. /* Obtain the combined length */
  181. if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
  182. return length;
  183. /* Allocate requisite combined storage */
  184. *pp = tmp = OPENSSL_malloc(length);
  185. if (tmp == NULL) {
  186. X509err(X509_F_I2D_X509_AUX, ERR_R_MALLOC_FAILURE);
  187. return -1;
  188. }
  189. /* Encode, but keep *pp at the originally malloced pointer */
  190. length = i2d_x509_aux_internal(a, &tmp);
  191. if (length <= 0) {
  192. OPENSSL_free(*pp);
  193. *pp = NULL;
  194. }
  195. return length;
  196. }
  197. int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
  198. {
  199. x->cert_info.enc.modified = 1;
  200. return i2d_X509_CINF(&x->cert_info, pp);
  201. }
  202. void X509_get0_signature(const ASN1_BIT_STRING **psig,
  203. const X509_ALGOR **palg, const X509 *x)
  204. {
  205. if (psig)
  206. *psig = &x->signature;
  207. if (palg)
  208. *palg = &x->sig_alg;
  209. }
  210. int X509_get_signature_nid(const X509 *x)
  211. {
  212. return OBJ_obj2nid(x->sig_alg.algorithm);
  213. }