t1_ext.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Find a custom extension from the list. */
  58. static custom_ext_method *custom_ext_find(custom_ext_methods *exts,
  59. unsigned int ext_type)
  60. {
  61. size_t i;
  62. custom_ext_method *meth = exts->meths;
  63. for (i = 0; i < exts->meths_count; i++, meth++) {
  64. if (ext_type == meth->ext_type)
  65. return meth;
  66. }
  67. return NULL;
  68. }
  69. /*
  70. * Initialise custom extensions flags to indicate neither sent nor received.
  71. */
  72. void custom_ext_init(custom_ext_methods *exts)
  73. {
  74. size_t i;
  75. custom_ext_method *meth = exts->meths;
  76. for (i = 0; i < exts->meths_count; i++, meth++)
  77. meth->ext_flags = 0;
  78. }
  79. /* Pass received custom extension data to the application for parsing. */
  80. int custom_ext_parse(SSL *s, int server,
  81. unsigned int ext_type,
  82. const unsigned char *ext_data, size_t ext_size, int *al)
  83. {
  84. custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
  85. custom_ext_method *meth;
  86. meth = custom_ext_find(exts, ext_type);
  87. /* If not found return success */
  88. if (!meth)
  89. return 1;
  90. if (!server) {
  91. /*
  92. * If it's ServerHello we can't have any extensions not sent in
  93. * ClientHello.
  94. */
  95. if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
  96. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  97. return 0;
  98. }
  99. }
  100. /* If already present it's a duplicate */
  101. if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
  102. *al = TLS1_AD_DECODE_ERROR;
  103. return 0;
  104. }
  105. meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
  106. /* If no parse function set return success */
  107. if (!meth->parse_cb)
  108. return 1;
  109. return meth->parse_cb(s, ext_type, ext_data, ext_size, al,
  110. meth->parse_arg);
  111. }
  112. /*
  113. * Request custom extension data from the application and add to the return
  114. * buffer.
  115. */
  116. int custom_ext_add(SSL *s, int server,
  117. unsigned char **pret, unsigned char *limit, int *al)
  118. {
  119. custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
  120. custom_ext_method *meth;
  121. unsigned char *ret = *pret;
  122. size_t i;
  123. for (i = 0; i < exts->meths_count; i++) {
  124. const unsigned char *out = NULL;
  125. size_t outlen = 0;
  126. meth = exts->meths + i;
  127. if (server) {
  128. /*
  129. * For ServerHello only send extensions present in ClientHello.
  130. */
  131. if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
  132. continue;
  133. /* If callback absent for server skip it */
  134. if (!meth->add_cb)
  135. continue;
  136. }
  137. if (meth->add_cb) {
  138. int cb_retval = 0;
  139. cb_retval = meth->add_cb(s, meth->ext_type,
  140. &out, &outlen, al, meth->add_arg);
  141. if (cb_retval < 0)
  142. return 0; /* error */
  143. if (cb_retval == 0)
  144. continue; /* skip this extension */
  145. }
  146. if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
  147. return 0;
  148. s2n(meth->ext_type, ret);
  149. s2n(outlen, ret);
  150. if (outlen) {
  151. memcpy(ret, out, outlen);
  152. ret += outlen;
  153. }
  154. /*
  155. * We can't send duplicates: code logic should prevent this.
  156. */
  157. OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
  158. /*
  159. * Indicate extension has been sent: this is both a sanity check to
  160. * ensure we don't send duplicate extensions and indicates that it is
  161. * not an error if the extension is present in ServerHello.
  162. */
  163. meth->ext_flags |= SSL_EXT_FLAG_SENT;
  164. if (meth->free_cb)
  165. meth->free_cb(s, meth->ext_type, out, meth->add_arg);
  166. }
  167. *pret = ret;
  168. return 1;
  169. }
  170. /* Copy table of custom extensions */
  171. int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
  172. {
  173. if (src->meths_count) {
  174. dst->meths =
  175. OPENSSL_memdup(src->meths,
  176. sizeof(custom_ext_method) * src->meths_count);
  177. if (dst->meths == NULL)
  178. return 0;
  179. dst->meths_count = src->meths_count;
  180. }
  181. return 1;
  182. }
  183. void custom_exts_free(custom_ext_methods *exts)
  184. {
  185. OPENSSL_free(exts->meths);
  186. }
  187. /* Set callbacks for a custom extension. */
  188. static int custom_ext_meth_add(custom_ext_methods *exts,
  189. unsigned int ext_type,
  190. custom_ext_add_cb add_cb,
  191. custom_ext_free_cb free_cb,
  192. void *add_arg,
  193. custom_ext_parse_cb parse_cb, void *parse_arg)
  194. {
  195. custom_ext_method *meth;
  196. /*
  197. * Check application error: if add_cb is not set free_cb will never be
  198. * called.
  199. */
  200. if (!add_cb && free_cb)
  201. return 0;
  202. /* Don't add if extension supported internally. */
  203. if (SSL_extension_supported(ext_type))
  204. return 0;
  205. /* Extension type must fit in 16 bits */
  206. if (ext_type > 0xffff)
  207. return 0;
  208. /* Search for duplicate */
  209. if (custom_ext_find(exts, ext_type))
  210. return 0;
  211. exts->meths = OPENSSL_realloc(exts->meths,
  212. (exts->meths_count +
  213. 1) * sizeof(custom_ext_method));
  214. if (!exts->meths) {
  215. exts->meths_count = 0;
  216. return 0;
  217. }
  218. meth = exts->meths + exts->meths_count;
  219. memset(meth, 0, sizeof(*meth));
  220. meth->parse_cb = parse_cb;
  221. meth->add_cb = add_cb;
  222. meth->free_cb = free_cb;
  223. meth->ext_type = ext_type;
  224. meth->add_arg = add_arg;
  225. meth->parse_arg = parse_arg;
  226. exts->meths_count++;
  227. return 1;
  228. }
  229. /* Application level functions to add custom extension callbacks */
  230. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  231. custom_ext_add_cb add_cb,
  232. custom_ext_free_cb free_cb,
  233. void *add_arg,
  234. custom_ext_parse_cb parse_cb,
  235. void *parse_arg)
  236. {
  237. return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type,
  238. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  239. }
  240. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  241. custom_ext_add_cb add_cb,
  242. custom_ext_free_cb free_cb,
  243. void *add_arg,
  244. custom_ext_parse_cb parse_cb,
  245. void *parse_arg)
  246. {
  247. return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
  248. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  249. }
  250. int SSL_extension_supported(unsigned int ext_type)
  251. {
  252. switch (ext_type) {
  253. /* Internally supported extensions. */
  254. case TLSEXT_TYPE_application_layer_protocol_negotiation:
  255. case TLSEXT_TYPE_ec_point_formats:
  256. case TLSEXT_TYPE_elliptic_curves:
  257. case TLSEXT_TYPE_heartbeat:
  258. case TLSEXT_TYPE_next_proto_neg:
  259. case TLSEXT_TYPE_padding:
  260. case TLSEXT_TYPE_renegotiate:
  261. case TLSEXT_TYPE_server_name:
  262. case TLSEXT_TYPE_session_ticket:
  263. case TLSEXT_TYPE_signature_algorithms:
  264. case TLSEXT_TYPE_srp:
  265. case TLSEXT_TYPE_status_request:
  266. case TLSEXT_TYPE_use_srtp:
  267. #ifdef TLSEXT_TYPE_encrypt_then_mac
  268. case TLSEXT_TYPE_encrypt_then_mac:
  269. #endif
  270. return 1;
  271. default:
  272. return 0;
  273. }
  274. }