x_x509.c 6.2 KB

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