2
0

x509_trs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 1999-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/x509v3.h>
  12. #include "crypto/x509.h"
  13. DEFINE_STACK_OF(X509_TRUST)
  14. DEFINE_STACK_OF(ASN1_OBJECT)
  15. static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b);
  16. static void trtable_free(X509_TRUST *p);
  17. static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
  18. static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
  19. static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
  20. static int obj_trust(int id, X509 *x, int flags);
  21. static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
  22. /*
  23. * WARNING: the following table should be kept in order of trust and without
  24. * any gaps so we can just subtract the minimum trust value to get an index
  25. * into the table
  26. */
  27. static X509_TRUST trstandard[] = {
  28. {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL},
  29. {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth,
  30. NULL},
  31. {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth,
  32. NULL},
  33. {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect,
  34. NULL},
  35. {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign,
  36. NULL},
  37. {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign,
  38. NULL},
  39. {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP,
  40. NULL},
  41. {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
  42. };
  43. #define X509_TRUST_COUNT OSSL_NELEM(trstandard)
  44. static STACK_OF(X509_TRUST) *trtable = NULL;
  45. static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b)
  46. {
  47. return (*a)->trust - (*b)->trust;
  48. }
  49. int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
  50. int) {
  51. int (*oldtrust) (int, X509 *, int);
  52. oldtrust = default_trust;
  53. default_trust = trust;
  54. return oldtrust;
  55. }
  56. int X509_check_trust(X509 *x, int id, int flags)
  57. {
  58. X509_TRUST *pt;
  59. int idx;
  60. /* We get this as a default value */
  61. if (id == X509_TRUST_DEFAULT)
  62. return obj_trust(NID_anyExtendedKeyUsage, x,
  63. flags | X509_TRUST_DO_SS_COMPAT);
  64. idx = X509_TRUST_get_by_id(id);
  65. if (idx == -1)
  66. return default_trust(id, x, flags);
  67. pt = X509_TRUST_get0(idx);
  68. return pt->check_trust(pt, x, flags);
  69. }
  70. int X509_TRUST_get_count(void)
  71. {
  72. if (!trtable)
  73. return X509_TRUST_COUNT;
  74. return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
  75. }
  76. X509_TRUST *X509_TRUST_get0(int idx)
  77. {
  78. if (idx < 0)
  79. return NULL;
  80. if (idx < (int)X509_TRUST_COUNT)
  81. return trstandard + idx;
  82. return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
  83. }
  84. int X509_TRUST_get_by_id(int id)
  85. {
  86. X509_TRUST tmp;
  87. int idx;
  88. if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
  89. return id - X509_TRUST_MIN;
  90. if (trtable == NULL)
  91. return -1;
  92. tmp.trust = id;
  93. idx = sk_X509_TRUST_find(trtable, &tmp);
  94. if (idx < 0)
  95. return -1;
  96. return idx + X509_TRUST_COUNT;
  97. }
  98. int X509_TRUST_set(int *t, int trust)
  99. {
  100. if (X509_TRUST_get_by_id(trust) == -1) {
  101. X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST);
  102. return 0;
  103. }
  104. *t = trust;
  105. return 1;
  106. }
  107. int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
  108. const char *name, int arg1, void *arg2)
  109. {
  110. int idx;
  111. X509_TRUST *trtmp;
  112. /*
  113. * This is set according to what we change: application can't set it
  114. */
  115. flags &= ~X509_TRUST_DYNAMIC;
  116. /* This will always be set for application modified trust entries */
  117. flags |= X509_TRUST_DYNAMIC_NAME;
  118. /* Get existing entry if any */
  119. idx = X509_TRUST_get_by_id(id);
  120. /* Need a new entry */
  121. if (idx == -1) {
  122. if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
  123. X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
  124. return 0;
  125. }
  126. trtmp->flags = X509_TRUST_DYNAMIC;
  127. } else
  128. trtmp = X509_TRUST_get0(idx);
  129. /* OPENSSL_free existing name if dynamic */
  130. if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
  131. OPENSSL_free(trtmp->name);
  132. /* dup supplied name */
  133. if ((trtmp->name = OPENSSL_strdup(name)) == NULL) {
  134. X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
  135. goto err;
  136. }
  137. /* Keep the dynamic flag of existing entry */
  138. trtmp->flags &= X509_TRUST_DYNAMIC;
  139. /* Set all other flags */
  140. trtmp->flags |= flags;
  141. trtmp->trust = id;
  142. trtmp->check_trust = ck;
  143. trtmp->arg1 = arg1;
  144. trtmp->arg2 = arg2;
  145. /* If its a new entry manage the dynamic table */
  146. if (idx == -1) {
  147. if (trtable == NULL
  148. && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
  149. X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
  150. goto err;;
  151. }
  152. if (!sk_X509_TRUST_push(trtable, trtmp)) {
  153. X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE);
  154. goto err;
  155. }
  156. }
  157. return 1;
  158. err:
  159. if (idx == -1) {
  160. OPENSSL_free(trtmp->name);
  161. OPENSSL_free(trtmp);
  162. }
  163. return 0;
  164. }
  165. static void trtable_free(X509_TRUST *p)
  166. {
  167. if (p == NULL)
  168. return;
  169. if (p->flags & X509_TRUST_DYNAMIC) {
  170. if (p->flags & X509_TRUST_DYNAMIC_NAME)
  171. OPENSSL_free(p->name);
  172. OPENSSL_free(p);
  173. }
  174. }
  175. void X509_TRUST_cleanup(void)
  176. {
  177. sk_X509_TRUST_pop_free(trtable, trtable_free);
  178. trtable = NULL;
  179. }
  180. int X509_TRUST_get_flags(const X509_TRUST *xp)
  181. {
  182. return xp->flags;
  183. }
  184. char *X509_TRUST_get0_name(const X509_TRUST *xp)
  185. {
  186. return xp->name;
  187. }
  188. int X509_TRUST_get_trust(const X509_TRUST *xp)
  189. {
  190. return xp->trust;
  191. }
  192. static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
  193. {
  194. /*
  195. * Declare the chain verified if the desired trust OID is not rejected in
  196. * any auxiliary trust info for this certificate, and the OID is either
  197. * expressly trusted, or else either "anyEKU" is trusted, or the
  198. * certificate is self-signed.
  199. */
  200. flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU;
  201. return obj_trust(trust->arg1, x, flags);
  202. }
  203. static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
  204. {
  205. /*
  206. * Declare the chain verified only if the desired trust OID is not
  207. * rejected and is expressly trusted. Neither "anyEKU" nor "compat"
  208. * trust in self-signed certificates apply.
  209. */
  210. flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU);
  211. return obj_trust(trust->arg1, x, flags);
  212. }
  213. static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
  214. {
  215. /* Call for side-effect of computing hash and caching extensions */
  216. if (X509_check_purpose(x, -1, 0) != 1)
  217. return X509_TRUST_UNTRUSTED;
  218. if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && (x->ex_flags & EXFLAG_SS))
  219. return X509_TRUST_TRUSTED;
  220. else
  221. return X509_TRUST_UNTRUSTED;
  222. }
  223. static int obj_trust(int id, X509 *x, int flags)
  224. {
  225. X509_CERT_AUX *ax = x->aux;
  226. int i;
  227. if (ax && ax->reject) {
  228. for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
  229. ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i);
  230. int nid = OBJ_obj2nid(obj);
  231. if (nid == id || (nid == NID_anyExtendedKeyUsage &&
  232. (flags & X509_TRUST_OK_ANY_EKU)))
  233. return X509_TRUST_REJECTED;
  234. }
  235. }
  236. if (ax && ax->trust) {
  237. for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
  238. ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i);
  239. int nid = OBJ_obj2nid(obj);
  240. if (nid == id || (nid == NID_anyExtendedKeyUsage &&
  241. (flags & X509_TRUST_OK_ANY_EKU)))
  242. return X509_TRUST_TRUSTED;
  243. }
  244. /*
  245. * Reject when explicit trust EKU are set and none match.
  246. *
  247. * Returning untrusted is enough for for full chains that end in
  248. * self-signed roots, because when explicit trust is specified it
  249. * suppresses the default blanket trust of self-signed objects.
  250. *
  251. * But for partial chains, this is not enough, because absent a similar
  252. * trust-self-signed policy, non matching EKUs are indistinguishable
  253. * from lack of EKU constraints.
  254. *
  255. * Therefore, failure to match any trusted purpose must trigger an
  256. * explicit reject.
  257. */
  258. return X509_TRUST_REJECTED;
  259. }
  260. if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
  261. return X509_TRUST_UNTRUSTED;
  262. /*
  263. * Not rejected, and there is no list of accepted uses, try compat.
  264. */
  265. return trust_compat(NULL, x, flags);
  266. }