Added API for external hashing of ServerKeyExchange, and signature algorithm identifi...
[BearSSL] / tools / server.c
index 67d2fa6..7d24d6b 100644 (file)
@@ -228,7 +228,15 @@ usage_server(void)
        fprintf(stderr,
 "   -hf names       add support for some hash functions (comma-separated)\n");
        fprintf(stderr,
        fprintf(stderr,
 "   -hf names       add support for some hash functions (comma-separated)\n");
        fprintf(stderr,
+"   -cbhash         test hashing in policy callback\n");
+       fprintf(stderr,
 "   -serverpref     enforce server's preferences for cipher suites\n");
 "   -serverpref     enforce server's preferences for cipher suites\n");
+       fprintf(stderr,
+"   -noreneg        prohibit renegotiations\n");
+       fprintf(stderr,
+"   -alpn name      add protocol name to list of protocols (ALPN extension)\n");
+       fprintf(stderr,
+"   -strictalpn     fail on ALPN mismatch\n");
        exit(EXIT_FAILURE);
 }
 
        exit(EXIT_FAILURE);
 }
 
@@ -239,6 +247,7 @@ typedef struct {
        size_t chain_len;
        int cert_signer_algo;
        private_key *sk;
        size_t chain_len;
        int cert_signer_algo;
        private_key *sk;
+       int cbhash;
 } policy_context;
 
 static void
 } policy_context;
 
 static void
@@ -264,10 +273,10 @@ print_hashes(unsigned chashes)
        }
 }
 
        }
 }
 
-static int
+static unsigned
 choose_hash(unsigned chashes)
 {
 choose_hash(unsigned chashes)
 {
-       int hash_id;
+       unsigned hash_id;
 
        for (hash_id = 6; hash_id >= 2; hash_id --) {
                if (((chashes >> hash_id) & 1) != 0) {
 
        for (hash_id = 6; hash_id >= 2; hash_id --) {
                if (((chashes >> hash_id) & 1) != 0) {
@@ -351,9 +360,21 @@ sp_choose(const br_ssl_server_policy_class **pctx,
                                if (br_ssl_engine_get_version(&cc->eng)
                                        < BR_TLS12)
                                {
                                if (br_ssl_engine_get_version(&cc->eng)
                                        < BR_TLS12)
                                {
-                                       choices->hash_id = 0;
+                                       if (pc->cbhash) {
+                                               choices->algo_id = 0x0001;
+                                       } else {
+                                               choices->algo_id = 0xFF00;
+                                       }
                                } else {
                                } else {
-                                       choices->hash_id = choose_hash(chashes);
+                                       unsigned id;
+
+                                       id = choose_hash(chashes);
+                                       if (pc->cbhash) {
+                                               choices->algo_id =
+                                                       (id << 8) + 0x01;
+                                       } else {
+                                               choices->algo_id = 0xFF00 + id;
+                                       }
                                }
                                goto choose_ok;
                        }
                                }
                                goto choose_ok;
                        }
@@ -364,10 +385,23 @@ sp_choose(const br_ssl_server_policy_class **pctx,
                                if (br_ssl_engine_get_version(&cc->eng)
                                        < BR_TLS12)
                                {
                                if (br_ssl_engine_get_version(&cc->eng)
                                        < BR_TLS12)
                                {
-                                       choices->hash_id = br_sha1_ID;
+                                       if (pc->cbhash) {
+                                               choices->algo_id = 0x0203;
+                                       } else {
+                                               choices->algo_id =
+                                                       0xFF00 + br_sha1_ID;
+                                       }
                                } else {
                                } else {
-                                       choices->hash_id =
-                                               choose_hash(chashes >> 8);
+                                       unsigned id;
+
+                                       id = choose_hash(chashes >> 8);
+                                       if (pc->cbhash) {
+                                               choices->algo_id =
+                                                       (id << 8) + 0x03;
+                                       } else {
+                                               choices->algo_id =
+                                                       0xFF00 + id;
+                                       }
                                }
                                goto choose_ok;
                        }
                                }
                                goto choose_ok;
                        }
@@ -428,13 +462,40 @@ sp_do_keyx(const br_ssl_server_policy_class **pctx,
 
 static size_t
 sp_do_sign(const br_ssl_server_policy_class **pctx,
 
 static size_t
 sp_do_sign(const br_ssl_server_policy_class **pctx,
-       int hash_id, size_t hv_len, unsigned char *data, size_t len)
+       unsigned algo_id, unsigned char *data, size_t hv_len, size_t len)
 {
        policy_context *pc;
        unsigned char hv[64];
 
        pc = (policy_context *)pctx;
 {
        policy_context *pc;
        unsigned char hv[64];
 
        pc = (policy_context *)pctx;
-       memcpy(hv, data, hv_len);
+       if (algo_id >= 0xFF00) {
+               algo_id &= 0xFF;
+               memcpy(hv, data, hv_len);
+       } else {
+               const br_hash_class *hc;
+               br_hash_compat_context zc;
+
+               if (pc->verbose) {
+                       fprintf(stderr, "Callback hashing, algo = 0x%04X,"
+                               " data_len = %lu\n",
+                               algo_id, (unsigned long)hv_len);
+               }
+               algo_id >>= 8;
+               hc = get_hash_impl(algo_id);
+               if (hc == NULL) {
+                       if (pc->verbose) {
+                               fprintf(stderr,
+                                       "ERROR: unsupported hash function %u\n",
+                                       algo_id);
+                       }
+                       return 0;
+               }
+               hc->init(&zc.vtable);
+               hc->update(&zc.vtable, data, hv_len);
+               hc->out(&zc.vtable, hv);
+               hv_len = (hc->desc >> BR_HASHDESC_OUT_OFF)
+                       & BR_HASHDESC_OUT_MASK;
+       }
        switch (pc->sk->key_type) {
                size_t sig_len;
                uint32_t x;
        switch (pc->sk->key_type) {
                size_t sig_len;
                uint32_t x;
@@ -442,12 +503,12 @@ sp_do_sign(const br_ssl_server_policy_class **pctx,
                const br_hash_class *hc;
 
        case BR_KEYTYPE_RSA:
                const br_hash_class *hc;
 
        case BR_KEYTYPE_RSA:
-               hash_oid = get_hash_oid(hash_id);
-               if (hash_oid == NULL && hash_id != 0) {
+               hash_oid = get_hash_oid(algo_id);
+               if (hash_oid == NULL && algo_id != 0) {
                        if (pc->verbose) {
                                fprintf(stderr, "ERROR: cannot RSA-sign with"
                        if (pc->verbose) {
                                fprintf(stderr, "ERROR: cannot RSA-sign with"
-                                       " unknown hash function: %d\n",
-                                       hash_id);
+                                       " unknown hash function: %u\n",
+                                       algo_id);
                        }
                        return 0;
                }
                        }
                        return 0;
                }
@@ -473,12 +534,12 @@ sp_do_sign(const br_ssl_server_policy_class **pctx,
                return sig_len;
 
        case BR_KEYTYPE_EC:
                return sig_len;
 
        case BR_KEYTYPE_EC:
-               hc = get_hash_impl(hash_id);
+               hc = get_hash_impl(algo_id);
                if (hc == NULL) {
                        if (pc->verbose) {
                                fprintf(stderr, "ERROR: cannot ECDSA-sign with"
                if (hc == NULL) {
                        if (pc->verbose) {
                                fprintf(stderr, "ERROR: cannot ECDSA-sign with"
-                                       " unknown hash function: %d\n",
-                                       hash_id);
+                                       " unknown hash function: %u\n",
+                                       algo_id);
                        }
                        return 0;
                }
                        }
                        return 0;
                }
@@ -512,6 +573,12 @@ static const br_ssl_server_policy_class policy_vtable = {
        sp_do_sign
 };
 
        sp_do_sign
 };
 
+void
+free_alpn(void *alpn)
+{
+       xfree(*(char **)alpn);
+}
+
 /* see brssl.h */
 int
 do_server(int argc, char *argv[])
 /* see brssl.h */
 int
 do_server(int argc, char *argv[])
@@ -527,11 +594,13 @@ do_server(int argc, char *argv[])
        size_t num_suites;
        uint16_t *suite_ids;
        unsigned hfuns;
        size_t num_suites;
        uint16_t *suite_ids;
        unsigned hfuns;
+       int cbhash;
        br_x509_certificate *chain;
        size_t chain_len;
        int cert_signer_algo;
        private_key *sk;
        anchor_list anchors = VEC_INIT;
        br_x509_certificate *chain;
        size_t chain_len;
        int cert_signer_algo;
        private_key *sk;
        anchor_list anchors = VEC_INIT;
+       VECTOR(const char *) alpn_names = VEC_INIT;
        br_x509_minimal_context xc;
        const br_hash_class *dnhash;
        size_t u;
        br_x509_minimal_context xc;
        const br_hash_class *dnhash;
        size_t u;
@@ -554,6 +623,7 @@ do_server(int argc, char *argv[])
        suites = NULL;
        num_suites = 0;
        hfuns = 0;
        suites = NULL;
        num_suites = 0;
        hfuns = 0;
+       cbhash = 0;
        suite_ids = NULL;
        chain = NULL;
        chain_len = 0;
        suite_ids = NULL;
        chain = NULL;
        chain_len = 0;
@@ -777,10 +847,22 @@ do_server(int argc, char *argv[])
                                goto server_exit_error;
                        }
                        hfuns |= x;
                                goto server_exit_error;
                        }
                        hfuns |= x;
+               } else if (eqstr(arg, "-cbhash")) {
+                       cbhash = 1;
                } else if (eqstr(arg, "-serverpref")) {
                        flags |= BR_OPT_ENFORCE_SERVER_PREFERENCES;
                } else if (eqstr(arg, "-noreneg")) {
                        flags |= BR_OPT_NO_RENEGOTIATION;
                } else if (eqstr(arg, "-serverpref")) {
                        flags |= BR_OPT_ENFORCE_SERVER_PREFERENCES;
                } else if (eqstr(arg, "-noreneg")) {
                        flags |= BR_OPT_NO_RENEGOTIATION;
+               } else if (eqstr(arg, "-alpn")) {
+                       if (++ i >= argc) {
+                               fprintf(stderr,
+                                       "ERROR: no argument for '-alpn'\n");
+                               usage_server();
+                               goto server_exit_error;
+                       }
+                       VEC_ADD(alpn_names, xstrdup(argv[i]));
+               } else if (eqstr(arg, "-strictalpn")) {
+                       flags |= BR_OPT_FAIL_ON_ALPN_MISMATCH;
                } else {
                        fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
                        usage_server();
                } else {
                        fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
                        usage_server();
@@ -999,6 +1081,11 @@ do_server(int argc, char *argv[])
        br_ssl_session_cache_lru_init(&lru, cache, cache_len);
        br_ssl_server_set_cache(&cc, &lru.vtable);
 
        br_ssl_session_cache_lru_init(&lru, cache, cache_len);
        br_ssl_server_set_cache(&cc, &lru.vtable);
 
+       if (VEC_LEN(alpn_names) != 0) {
+               br_ssl_engine_set_protocol_names(&cc.eng,
+                       &VEC_ELT(alpn_names, 0), VEC_LEN(alpn_names));
+       }
+
        /*
         * Set the policy handler (that chooses the actual cipher suite,
         * selects the certificate chain, and runs the private key
        /*
         * Set the policy handler (that chooses the actual cipher suite,
         * selects the certificate chain, and runs the private key
@@ -1010,6 +1097,7 @@ do_server(int argc, char *argv[])
        pc.chain_len = chain_len;
        pc.cert_signer_algo = cert_signer_algo;
        pc.sk = sk;
        pc.chain_len = chain_len;
        pc.cert_signer_algo = cert_signer_algo;
        pc.sk = sk;
+       pc.cbhash = cbhash;
        br_ssl_server_set_policy(&cc, &pc.vtable);
 
        /*
        br_ssl_server_set_policy(&cc, &pc.vtable);
 
        /*
@@ -1091,6 +1179,7 @@ server_exit:
        free_certificates(chain, chain_len);
        free_private_key(sk);
        VEC_CLEAREXT(anchors, &free_ta_contents);
        free_certificates(chain, chain_len);
        free_private_key(sk);
        VEC_CLEAREXT(anchors, &free_ta_contents);
+       VEC_CLEAREXT(alpn_names, &free_alpn);
        xfree(iobuf);
        xfree(cache);
        if (fd >= 0) {
        xfree(iobuf);
        xfree(cache);
        if (fd >= 0) {