t1_ext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* ssl/t1_ext.c */
  2. /* ====================================================================
  3. * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* Custom extension utility functions */
  56. #include "ssl_locl.h"
  57. #ifndef OPENSSL_NO_TLSEXT
  58. /* Find a custom extension from the list. */
  59. static custom_ext_method *custom_ext_find(custom_ext_methods *exts,
  60. unsigned int ext_type)
  61. {
  62. size_t i;
  63. custom_ext_method *meth = exts->meths;
  64. for (i = 0; i < exts->meths_count; i++, meth++) {
  65. if (ext_type == meth->ext_type)
  66. return meth;
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * Initialise custom extensions flags to indicate neither sent nor received.
  72. */
  73. void custom_ext_init(custom_ext_methods *exts)
  74. {
  75. size_t i;
  76. custom_ext_method *meth = exts->meths;
  77. for (i = 0; i < exts->meths_count; i++, meth++)
  78. meth->ext_flags = 0;
  79. }
  80. /* Pass received custom extension data to the application for parsing. */
  81. int custom_ext_parse(SSL *s, int server,
  82. unsigned int ext_type,
  83. const unsigned char *ext_data, size_t ext_size, int *al)
  84. {
  85. custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
  86. custom_ext_method *meth;
  87. meth = custom_ext_find(exts, ext_type);
  88. /* If not found return success */
  89. if (!meth)
  90. return 1;
  91. if (!server) {
  92. /*
  93. * If it's ServerHello we can't have any extensions not sent in
  94. * ClientHello.
  95. */
  96. if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
  97. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  98. return 0;
  99. }
  100. }
  101. /* If already present it's a duplicate */
  102. if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
  103. *al = TLS1_AD_DECODE_ERROR;
  104. return 0;
  105. }
  106. meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
  107. /* If no parse function set return success */
  108. if (!meth->parse_cb)
  109. return 1;
  110. return meth->parse_cb(s, ext_type, ext_data, ext_size, al,
  111. meth->parse_arg);
  112. }
  113. /*
  114. * Request custom extension data from the application and add to the return
  115. * buffer.
  116. */
  117. int custom_ext_add(SSL *s, int server,
  118. unsigned char **pret, unsigned char *limit, int *al)
  119. {
  120. custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
  121. custom_ext_method *meth;
  122. unsigned char *ret = *pret;
  123. size_t i;
  124. for (i = 0; i < exts->meths_count; i++) {
  125. const unsigned char *out = NULL;
  126. size_t outlen = 0;
  127. meth = exts->meths + i;
  128. if (server) {
  129. /*
  130. * For ServerHello only send extensions present in ClientHello.
  131. */
  132. if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
  133. continue;
  134. /* If callback absent for server skip it */
  135. if (!meth->add_cb)
  136. continue;
  137. }
  138. if (meth->add_cb) {
  139. int cb_retval = 0;
  140. cb_retval = meth->add_cb(s, meth->ext_type,
  141. &out, &outlen, al, meth->add_arg);
  142. if (cb_retval < 0)
  143. return 0; /* error */
  144. if (cb_retval == 0)
  145. continue; /* skip this extension */
  146. }
  147. if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
  148. return 0;
  149. s2n(meth->ext_type, ret);
  150. s2n(outlen, ret);
  151. if (outlen) {
  152. memcpy(ret, out, outlen);
  153. ret += outlen;
  154. }
  155. /*
  156. * We can't send duplicates: code logic should prevent this.
  157. */
  158. OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
  159. /*
  160. * Indicate extension has been sent: this is both a sanity check to
  161. * ensure we don't send duplicate extensions and indicates that it is
  162. * not an error if the extension is present in ServerHello.
  163. */
  164. meth->ext_flags |= SSL_EXT_FLAG_SENT;
  165. if (meth->free_cb)
  166. meth->free_cb(s, meth->ext_type, out, meth->add_arg);
  167. }
  168. *pret = ret;
  169. return 1;
  170. }
  171. /* Copy the flags from src to dst for any extensions that exist in both */
  172. int custom_exts_copy_flags(custom_ext_methods *dst,
  173. const custom_ext_methods *src)
  174. {
  175. size_t i;
  176. custom_ext_method *methsrc = src->meths;
  177. for (i = 0; i < src->meths_count; i++, methsrc++) {
  178. custom_ext_method *methdst = custom_ext_find(dst, methsrc->ext_type);
  179. if (methdst == NULL)
  180. continue;
  181. methdst->ext_flags = methsrc->ext_flags;
  182. }
  183. return 1;
  184. }
  185. /* Copy table of custom extensions */
  186. int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
  187. {
  188. if (src->meths_count) {
  189. dst->meths =
  190. BUF_memdup(src->meths,
  191. sizeof(custom_ext_method) * src->meths_count);
  192. if (dst->meths == NULL)
  193. return 0;
  194. dst->meths_count = src->meths_count;
  195. }
  196. return 1;
  197. }
  198. void custom_exts_free(custom_ext_methods *exts)
  199. {
  200. if (exts->meths)
  201. OPENSSL_free(exts->meths);
  202. }
  203. /* Set callbacks for a custom extension. */
  204. static int custom_ext_meth_add(custom_ext_methods *exts,
  205. unsigned int ext_type,
  206. custom_ext_add_cb add_cb,
  207. custom_ext_free_cb free_cb,
  208. void *add_arg,
  209. custom_ext_parse_cb parse_cb, void *parse_arg)
  210. {
  211. custom_ext_method *meth;
  212. /*
  213. * Check application error: if add_cb is not set free_cb will never be
  214. * called.
  215. */
  216. if (!add_cb && free_cb)
  217. return 0;
  218. /* Don't add if extension supported internally. */
  219. if (SSL_extension_supported(ext_type))
  220. return 0;
  221. /* Extension type must fit in 16 bits */
  222. if (ext_type > 0xffff)
  223. return 0;
  224. /* Search for duplicate */
  225. if (custom_ext_find(exts, ext_type))
  226. return 0;
  227. meth = OPENSSL_realloc(exts->meths,
  228. (exts->meths_count + 1)
  229. * sizeof(custom_ext_method));
  230. if (meth == NULL)
  231. return 0;
  232. exts->meths = meth;
  233. meth += exts->meths_count;
  234. memset(meth, 0, sizeof(custom_ext_method));
  235. meth->parse_cb = parse_cb;
  236. meth->add_cb = add_cb;
  237. meth->free_cb = free_cb;
  238. meth->ext_type = ext_type;
  239. meth->add_arg = add_arg;
  240. meth->parse_arg = parse_arg;
  241. exts->meths_count++;
  242. return 1;
  243. }
  244. /* Application level functions to add custom extension callbacks */
  245. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  246. custom_ext_add_cb add_cb,
  247. custom_ext_free_cb free_cb,
  248. void *add_arg,
  249. custom_ext_parse_cb parse_cb,
  250. void *parse_arg)
  251. {
  252. return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type,
  253. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  254. }
  255. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  256. custom_ext_add_cb add_cb,
  257. custom_ext_free_cb free_cb,
  258. void *add_arg,
  259. custom_ext_parse_cb parse_cb,
  260. void *parse_arg)
  261. {
  262. return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
  263. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  264. }
  265. int SSL_extension_supported(unsigned int ext_type)
  266. {
  267. switch (ext_type) {
  268. /* Internally supported extensions. */
  269. case TLSEXT_TYPE_application_layer_protocol_negotiation:
  270. case TLSEXT_TYPE_ec_point_formats:
  271. case TLSEXT_TYPE_elliptic_curves:
  272. case TLSEXT_TYPE_heartbeat:
  273. # ifndef OPENSSL_NO_NEXTPROTONEG
  274. case TLSEXT_TYPE_next_proto_neg:
  275. # endif
  276. case TLSEXT_TYPE_padding:
  277. case TLSEXT_TYPE_renegotiate:
  278. case TLSEXT_TYPE_server_name:
  279. case TLSEXT_TYPE_session_ticket:
  280. case TLSEXT_TYPE_signature_algorithms:
  281. case TLSEXT_TYPE_srp:
  282. case TLSEXT_TYPE_status_request:
  283. case TLSEXT_TYPE_use_srtp:
  284. # ifdef TLSEXT_TYPE_opaque_prf_input
  285. case TLSEXT_TYPE_opaque_prf_input:
  286. # endif
  287. # ifdef TLSEXT_TYPE_encrypt_then_mac
  288. case TLSEXT_TYPE_encrypt_then_mac:
  289. # endif
  290. return 1;
  291. default:
  292. return 0;
  293. }
  294. }
  295. #endif