- size_t serverinfo_length,
- unsigned short extension_type,
- const unsigned char **extension_data,
- unsigned short *extension_length)
- {
- *extension_data = NULL;
- *extension_length = 0;
- if (serverinfo == NULL || serverinfo_length == 0)
- return 0;
- for (;;)
- {
- unsigned short type = 0; /* uint16 */
- unsigned short len = 0; /* uint16 */
-
- /* end of serverinfo */
- if (serverinfo_length == 0)
- return -1; /* Extension not found */
-
- /* read 2-byte type field */
- if (serverinfo_length < 2)
- return 0; /* Error */
- type = (serverinfo[0] << 8) + serverinfo[1];
- serverinfo += 2;
- serverinfo_length -= 2;
-
- /* read 2-byte len field */
- if (serverinfo_length < 2)
- return 0; /* Error */
- len = (serverinfo[0] << 8) + serverinfo[1];
- serverinfo += 2;
- serverinfo_length -= 2;
-
- if (len > serverinfo_length)
- return 0; /* Error */
-
- if (type == extension_type)
- {
- *extension_data = serverinfo;
- *extension_length = len;
- return 1; /* Success */
- }
-
- serverinfo += len;
- serverinfo_length -= len;
- }
- return 0; /* Error */
- }
-
-static int serverinfo_srv_first_cb(SSL *s, unsigned short ext_type,
- const unsigned char *in,
- unsigned short inlen, int *al,
- void *arg)
- {
-
- if (inlen != 0)
- {
- *al = SSL_AD_DECODE_ERROR;
- return 0;
- }
-
- return 1;
- }
-
-static int serverinfo_srv_second_cb(SSL *s, unsigned short ext_type,
- const unsigned char **out, unsigned short *outlen,
- int *al, void *arg)
- {
- const unsigned char *serverinfo = NULL;
- size_t serverinfo_length = 0;
-
- /* Is there serverinfo data for the chosen server cert? */
- if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
- &serverinfo_length)) != 0)
- {
- /* Find the relevant extension from the serverinfo */
- int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
- ext_type, out, outlen);
- if (retval == 0)
- return 0; /* Error */
- if (retval == -1)
- return -1; /* No extension found, don't send extension */
- return 1; /* Send extension */
- }
- return -1; /* No serverinfo data found, don't send extension */
- }
-
-/* With a NULL context, this function just checks that the serverinfo data
- parses correctly. With a non-NULL context, it registers callbacks for
- the included extensions. */
-static int serverinfo_process_buffer(const unsigned char *serverinfo,
- size_t serverinfo_length, SSL_CTX *ctx)
- {
- if (serverinfo == NULL || serverinfo_length == 0)
- return 0;
- for (;;)
- {
- unsigned short ext_type = 0; /* uint16 */
- unsigned short len = 0; /* uint16 */
-
- /* end of serverinfo */
- if (serverinfo_length == 0)
- return 1;
-
- /* read 2-byte type field */
- if (serverinfo_length < 2)
- return 0;
- /* FIXME: check for types we understand explicitly? */
-
- /* Register callbacks for extensions */
- ext_type = (serverinfo[0] << 8) + serverinfo[1];
- if (ctx && !SSL_CTX_set_custom_srv_ext(ctx, ext_type,
- serverinfo_srv_first_cb,
- serverinfo_srv_second_cb, NULL))
- return 0;
-
- serverinfo += 2;
- serverinfo_length -= 2;
-
- /* read 2-byte len field */
- if (serverinfo_length < 2)
- return 0;
- len = (serverinfo[0] << 8) + serverinfo[1];
- serverinfo += 2;
- serverinfo_length -= 2;
-
- if (len > serverinfo_length)
- return 0;
-
- serverinfo += len;
- serverinfo_length -= len;
- }
- }
+ size_t serverinfo_length,
+ unsigned int extension_type,
+ const unsigned char **extension_data,
+ size_t *extension_length)
+{
+ *extension_data = NULL;
+ *extension_length = 0;
+ if (serverinfo == NULL || serverinfo_length == 0)
+ return 0;
+ for (;;) {
+ unsigned int type = 0;
+ size_t len = 0;
+
+ /* end of serverinfo */
+ if (serverinfo_length == 0)
+ return -1; /* Extension not found */
+
+ /* read 2-byte type field */
+ if (serverinfo_length < 2)
+ return 0; /* Error */
+ type = (serverinfo[0] << 8) + serverinfo[1];
+ serverinfo += 2;
+ serverinfo_length -= 2;
+
+ /* read 2-byte len field */
+ if (serverinfo_length < 2)
+ return 0; /* Error */
+ len = (serverinfo[0] << 8) + serverinfo[1];
+ serverinfo += 2;
+ serverinfo_length -= 2;
+
+ if (len > serverinfo_length)
+ return 0; /* Error */
+
+ if (type == extension_type) {
+ *extension_data = serverinfo;
+ *extension_length = len;
+ return 1; /* Success */
+ }
+
+ serverinfo += len;
+ serverinfo_length -= len;
+ }
+ /* Unreachable */
+}
+
+static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
+ const unsigned char *in,
+ size_t inlen, int *al, void *arg)
+{
+
+ if (inlen != 0) {
+ *al = SSL_AD_DECODE_ERROR;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
+ const unsigned char **out, size_t *outlen,
+ int *al, void *arg)
+{
+ const unsigned char *serverinfo = NULL;
+ size_t serverinfo_length = 0;
+
+ /* Is there serverinfo data for the chosen server cert? */
+ if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
+ &serverinfo_length)) != 0) {
+ /* Find the relevant extension from the serverinfo */
+ int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
+ ext_type, out, outlen);
+ if (retval == 0)
+ return 0; /* Error */
+ if (retval == -1)
+ return -1; /* No extension found, don't send extension */
+ return 1; /* Send extension */
+ }
+ return -1; /* No serverinfo data found, don't send
+ * extension */
+}
+
+/*
+ * With a NULL context, this function just checks that the serverinfo data
+ * parses correctly. With a non-NULL context, it registers callbacks for
+ * the included extensions.
+ */
+static int serverinfo_process_buffer(const unsigned char *serverinfo,
+ size_t serverinfo_length, SSL_CTX *ctx)
+{
+ if (serverinfo == NULL || serverinfo_length == 0)
+ return 0;
+ for (;;) {
+ unsigned int ext_type = 0;
+ size_t len = 0;
+
+ /* end of serverinfo */
+ if (serverinfo_length == 0)
+ return 1;
+
+ /* read 2-byte type field */
+ if (serverinfo_length < 2)
+ return 0;
+ /* FIXME: check for types we understand explicitly? */
+
+ /* Register callbacks for extensions */
+ ext_type = (serverinfo[0] << 8) + serverinfo[1];
+ if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
+ serverinfo_srv_add_cb,
+ NULL, NULL,
+ serverinfo_srv_parse_cb,
+ NULL))
+ return 0;
+
+ serverinfo += 2;
+ serverinfo_length -= 2;
+
+ /* read 2-byte len field */
+ if (serverinfo_length < 2)
+ return 0;
+ len = (serverinfo[0] << 8) + serverinfo[1];
+ serverinfo += 2;
+ serverinfo_length -= 2;
+
+ if (len > serverinfo_length)
+ return 0;
+
+ serverinfo += len;
+ serverinfo_length -= len;
+ }
+}