You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

305 regels
9.0 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 1999. */
  3. /* ====================================================================
  4. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * licensing@OpenSSL.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com). */
  54. #include <openssl/buf.h>
  55. #include <openssl/err.h>
  56. #include <openssl/mem.h>
  57. #include <openssl/obj.h>
  58. #include <openssl/x509v3.h>
  59. static int tr_cmp(const X509_TRUST **a,
  60. const X509_TRUST **b);
  61. static void trtable_free(X509_TRUST *p);
  62. static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
  63. static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
  64. static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
  65. static int obj_trust(int id, X509 *x, int flags);
  66. static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
  67. /* WARNING: the following table should be kept in order of trust
  68. * and without any gaps so we can just subtract the minimum trust
  69. * value to get an index into the table
  70. */
  71. static X509_TRUST trstandard[] = {
  72. {X509_TRUST_COMPAT, 0, trust_compat, (char *) "compatible", 0, NULL},
  73. {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, (char *) "SSL Client", NID_client_auth, NULL},
  74. {X509_TRUST_SSL_SERVER, 0, trust_1oidany, (char *) "SSL Server", NID_server_auth, NULL},
  75. {X509_TRUST_EMAIL, 0, trust_1oidany, (char *) "S/MIME email", NID_email_protect, NULL},
  76. {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, (char *) "Object Signer", NID_code_sign, NULL},
  77. {X509_TRUST_OCSP_SIGN, 0, trust_1oid, (char *) "OCSP responder", NID_OCSP_sign, NULL},
  78. {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, (char *) "OCSP request", NID_ad_OCSP, NULL},
  79. {X509_TRUST_TSA, 0, trust_1oidany, (char *) "TSA server", NID_time_stamp, NULL}
  80. };
  81. #define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST))
  82. static STACK_OF(X509_TRUST) *trtable = NULL;
  83. static int tr_cmp(const X509_TRUST **a,
  84. const X509_TRUST **b)
  85. {
  86. return (*a)->trust - (*b)->trust;
  87. }
  88. int (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
  89. {
  90. int (*oldtrust)(int , X509 *, int);
  91. oldtrust = default_trust;
  92. default_trust = trust;
  93. return oldtrust;
  94. }
  95. int X509_check_trust(X509 *x, int id, int flags)
  96. {
  97. X509_TRUST *pt;
  98. int idx;
  99. if(id == -1) return 1;
  100. /* We get this as a default value */
  101. if (id == 0)
  102. {
  103. int rv;
  104. rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
  105. if (rv != X509_TRUST_UNTRUSTED)
  106. return rv;
  107. return trust_compat(NULL, x, 0);
  108. }
  109. idx = X509_TRUST_get_by_id(id);
  110. if(idx == -1) return default_trust(id, x, flags);
  111. pt = X509_TRUST_get0(idx);
  112. return pt->check_trust(pt, x, flags);
  113. }
  114. int X509_TRUST_get_count(void)
  115. {
  116. if(!trtable) return X509_TRUST_COUNT;
  117. return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
  118. }
  119. X509_TRUST * X509_TRUST_get0(int idx)
  120. {
  121. if(idx < 0) return NULL;
  122. if(idx < (int)X509_TRUST_COUNT) return trstandard + idx;
  123. return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
  124. }
  125. int X509_TRUST_get_by_id(int id)
  126. {
  127. X509_TRUST tmp;
  128. size_t idx;
  129. if((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
  130. return id - X509_TRUST_MIN;
  131. tmp.trust = id;
  132. if(!trtable) return -1;
  133. if (!sk_X509_TRUST_find(trtable, &idx, &tmp)) {
  134. return -1;
  135. }
  136. return idx + X509_TRUST_COUNT;
  137. }
  138. int X509_TRUST_set(int *t, int trust)
  139. {
  140. if(X509_TRUST_get_by_id(trust) == -1) {
  141. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_TRUST);
  142. return 0;
  143. }
  144. *t = trust;
  145. return 1;
  146. }
  147. int X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
  148. char *name, int arg1, void *arg2)
  149. {
  150. int idx;
  151. X509_TRUST *trtmp;
  152. char *name_dup;
  153. /* This is set according to what we change: application can't set it */
  154. flags &= ~X509_TRUST_DYNAMIC;
  155. /* This will always be set for application modified trust entries */
  156. flags |= X509_TRUST_DYNAMIC_NAME;
  157. /* Get existing entry if any */
  158. idx = X509_TRUST_get_by_id(id);
  159. /* Need a new entry */
  160. if(idx == -1) {
  161. if(!(trtmp = OPENSSL_malloc(sizeof(X509_TRUST)))) {
  162. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  163. return 0;
  164. }
  165. trtmp->flags = X509_TRUST_DYNAMIC;
  166. } else trtmp = X509_TRUST_get0(idx);
  167. /* Duplicate the supplied name. */
  168. name_dup = BUF_strdup(name);
  169. if (name_dup == NULL) {
  170. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  171. if (idx == -1)
  172. OPENSSL_free(trtmp);
  173. return 0;
  174. }
  175. /* OPENSSL_free existing name if dynamic */
  176. if (trtmp->flags & X509_TRUST_DYNAMIC_NAME) OPENSSL_free(trtmp->name);
  177. trtmp->name = name_dup;
  178. /* Keep the dynamic flag of existing entry */
  179. trtmp->flags &= X509_TRUST_DYNAMIC;
  180. /* Set all other flags */
  181. trtmp->flags |= flags;
  182. trtmp->trust = id;
  183. trtmp->check_trust = ck;
  184. trtmp->arg1 = arg1;
  185. trtmp->arg2 = arg2;
  186. /* If its a new entry manage the dynamic table */
  187. if(idx == -1) {
  188. if(!trtable && !(trtable = sk_X509_TRUST_new(tr_cmp))) {
  189. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  190. trtable_free(trtmp);
  191. return 0;
  192. }
  193. if (!sk_X509_TRUST_push(trtable, trtmp)) {
  194. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  195. trtable_free(trtmp);
  196. return 0;
  197. }
  198. }
  199. return 1;
  200. }
  201. static void trtable_free(X509_TRUST *p)
  202. {
  203. if(!p) return;
  204. if (p->flags & X509_TRUST_DYNAMIC)
  205. {
  206. if (p->flags & X509_TRUST_DYNAMIC_NAME)
  207. OPENSSL_free(p->name);
  208. OPENSSL_free(p);
  209. }
  210. }
  211. void X509_TRUST_cleanup(void)
  212. {
  213. unsigned int i;
  214. for(i = 0; i < X509_TRUST_COUNT; i++) trtable_free(trstandard + i);
  215. sk_X509_TRUST_pop_free(trtable, trtable_free);
  216. trtable = NULL;
  217. }
  218. int X509_TRUST_get_flags(X509_TRUST *xp)
  219. {
  220. return xp->flags;
  221. }
  222. char *X509_TRUST_get0_name(X509_TRUST *xp)
  223. {
  224. return xp->name;
  225. }
  226. int X509_TRUST_get_trust(X509_TRUST *xp)
  227. {
  228. return xp->trust;
  229. }
  230. static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
  231. {
  232. if(x->aux && (x->aux->trust || x->aux->reject))
  233. return obj_trust(trust->arg1, x, flags);
  234. /* we don't have any trust settings: for compatibility
  235. * we return trusted if it is self signed
  236. */
  237. return trust_compat(trust, x, flags);
  238. }
  239. static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
  240. {
  241. if(x->aux) return obj_trust(trust->arg1, x, flags);
  242. return X509_TRUST_UNTRUSTED;
  243. }
  244. static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
  245. {
  246. X509_check_purpose(x, -1, 0);
  247. if(x->ex_flags & EXFLAG_SS) return X509_TRUST_TRUSTED;
  248. else return X509_TRUST_UNTRUSTED;
  249. }
  250. static int obj_trust(int id, X509 *x, int flags)
  251. {
  252. ASN1_OBJECT *obj;
  253. size_t i;
  254. X509_CERT_AUX *ax;
  255. ax = x->aux;
  256. if(!ax) return X509_TRUST_UNTRUSTED;
  257. if(ax->reject) {
  258. for(i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
  259. obj = sk_ASN1_OBJECT_value(ax->reject, i);
  260. if(OBJ_obj2nid(obj) == id) return X509_TRUST_REJECTED;
  261. }
  262. }
  263. if(ax->trust) {
  264. for(i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
  265. obj = sk_ASN1_OBJECT_value(ax->trust, i);
  266. if(OBJ_obj2nid(obj) == id) return X509_TRUST_TRUSTED;
  267. }
  268. }
  269. return X509_TRUST_UNTRUSTED;
  270. }