From c4f48bc47d94af9825ede8453a1886a1d99171b1 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sat, 9 Sep 2023 23:29:48 +0000 Subject: [PATCH] ws-handshake: Use own base64 and SHA1 implementations --- meson.build | 1 + src/ws-handshake.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/meson.build b/meson.build index 5b817d6..a3482a3 100644 --- a/meson.build +++ b/meson.build @@ -95,6 +95,7 @@ sources = [ 'src/encoder.c', 'src/cursor.c', 'src/logging.c', + 'src/base64.c', ] dependencies = [ diff --git a/src/ws-handshake.c b/src/ws-handshake.c index 700b042..5eb55fb 100644 --- a/src/ws-handshake.c +++ b/src/ws-handshake.c @@ -1,8 +1,7 @@ #include "websocket.h" #include "http.h" - -#include -#include +#include "crypto.h" +#include "base64.h" #include #include @@ -61,16 +60,16 @@ ssize_t ws_handshake(char* output, size_t output_maxlen, const char* input) if (have_versions && !strstr(versions, ",13,")) goto failure; - struct sha1_ctx ctx; - sha1_init(&ctx); - sha1_update(&ctx, strlen(challenge), (const uint8_t*)challenge); - sha1_update(&ctx, strlen(magic_uuid), (const uint8_t*)magic_uuid); + uint8_t hash[20]; + crypto_hash_many(hash, sizeof(hash), CRYPTO_HASH_SHA1, + (struct crypto_data_entry[]){ + { (uint8_t*)challenge, strlen(challenge) }, + { (uint8_t*)magic_uuid, strlen(magic_uuid) }, + {} + }); - uint8_t hash[SHA1_DIGEST_SIZE]; - sha1_digest(&ctx, sizeof(hash), hash); - - char response[BASE64_ENCODE_RAW_LENGTH(SHA1_DIGEST_SIZE) + 1] = {}; - base64_encode_raw(response, SHA1_DIGEST_SIZE, hash); + char response[BASE64_ENCODED_SIZE(sizeof(hash))] = {}; + base64_encode(response, hash, sizeof(hash)); size_t len = snprintf(output, output_maxlen, "HTTP/1.1 101 Switching Protocols\r\n"