x509_trs.c 8.7 KB

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