diff --git a/src/ssb.js.c b/src/ssb.js.c index 1b1e7f50..5fe60b84 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -1281,78 +1281,6 @@ static JSValue _tf_ssb_remove_event_listener(JSContext* context, JSValueConst th return result; } -static JSValue _tf_ssb_hmacsha256_sign(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_UNDEFINED; - tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); - - size_t payload_length = 0; - const char* payload = JS_ToCStringLen(context, &payload_length, argv[0]); - const char* user = JS_ToCString(context, argv[1]); - const char* public_key = JS_ToCString(context, argv[2]); - - uint8_t private_key[crypto_sign_SECRETKEYBYTES]; - if (tf_ssb_db_identity_get_private_key(ssb, user, public_key, private_key, sizeof(private_key))) - { - uint8_t signature[crypto_sign_BYTES]; - unsigned long long siglen; - if (crypto_sign_detached(signature, &siglen, (const uint8_t*)payload, payload_length, private_key) == 0) - { - char signature_base64[crypto_sign_BYTES * 2]; - tf_base64_encode(signature, sizeof(signature), signature_base64, sizeof(signature_base64)); - result = JS_NewString(context, signature_base64); - } - } - else - { - result = JS_ThrowInternalError(context, "Private key not found."); - } - - JS_FreeCString(context, public_key); - JS_FreeCString(context, user); - JS_FreeCString(context, payload); - - return result; -} - -static JSValue _tf_ssb_hmacsha256_verify(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_UNDEFINED; - - size_t public_key_length = 0; - const char* public_key = JS_ToCStringLen(context, &public_key_length, argv[0]); - size_t payload_length = 0; - const char* payload = JS_ToCStringLen(context, &payload_length, argv[1]); - size_t signature_length = 0; - const char* signature = JS_ToCStringLen(context, &signature_length, argv[2]); - - const char* public_key_start = public_key && *public_key == '@' ? public_key + 1 : public_key; - const char* public_key_end = public_key_start ? strstr(public_key_start, ".ed25519") : NULL; - if (public_key_start && !public_key_end) - { - public_key_end = public_key_start + strlen(public_key_start); - } - - uint8_t bin_public_key[crypto_sign_PUBLICKEYBYTES] = { 0 }; - if (tf_base64_decode(public_key_start, public_key_end - public_key_start, bin_public_key, sizeof(bin_public_key)) > 0) - { - uint8_t bin_signature[crypto_sign_BYTES] = { 0 }; - if (tf_base64_decode(signature, signature_length, bin_signature, sizeof(bin_signature)) > 0) - { - if (crypto_sign_verify_detached(bin_signature, (const uint8_t*)payload, payload_length, bin_public_key) == 0) - { - result = JS_TRUE; - } - } - } - - JS_FreeCString(context, signature); - JS_FreeCString(context, payload); - JS_FreeCString(context, public_key); - - return result; -} - static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; @@ -1750,8 +1678,6 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) JS_SetPropertyStr(context, object, "setServerFollowingMe", JS_NewCFunction(context, _tf_ssb_set_server_following_me, "setServerFollowingMe", 3)); JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1)); JS_SetPropertyStr(context, object, "getPrivateKey", JS_NewCFunction(context, _tf_ssb_getPrivateKey, "getPrivateKey", 2)); - JS_SetPropertyStr(context, object, "hmacsha256sign", JS_NewCFunction(context, _tf_ssb_hmacsha256_sign, "hmacsha256sign", 3)); - JS_SetPropertyStr(context, object, "hmacsha256verify", JS_NewCFunction(context, _tf_ssb_hmacsha256_verify, "hmacsha256verify", 3)); JS_SetPropertyStr(context, object, "privateMessageEncrypt", JS_NewCFunction(context, _tf_ssb_private_message_encrypt, "privateMessageEncrypt", 4)); JS_SetPropertyStr(context, object, "privateMessageDecrypt", JS_NewCFunction(context, _tf_ssb_private_message_decrypt, "privateMessageDecrypt", 3)); /* Write. */ diff --git a/src/tests.c b/src/tests.c index 8081a81a..68dd4463 100644 --- a/src/tests.c +++ b/src/tests.c @@ -630,37 +630,6 @@ static void _test_file(const tf_test_options_t* options) unlink("out/test.js"); } -static void _test_sign(const tf_test_options_t* options) -{ - _write_file("out/test.js", - "'use strict';\n" - "let id = ssb.createIdentity('test');\n" - "print(id);\n" - "let sig = ssb.hmacsha256sign('hello', 'test', id);\n" - "print(sig);\n" - "if (!ssb.hmacsha256verify(id, 'hello', sig)) {\n" - " exit(1);\n" - "}\n" - "if (ssb.hmacsha256verify(id, 'world', sig)) {\n" - " exit(1);\n" - "}\n" - "if (ssb.hmacsha256verify(id, 'hello1', sig)) {\n" - " exit(1);\n" - "}\n"); - - unlink("out/test_db0.sqlite"); - char command[256]; - snprintf(command, sizeof(command), "%s run --db-path=out/test_db0.sqlite -s out/test.js" TEST_ARGS, options->exe_path); - tf_printf("%s\n", command); - int result = system(command); - tf_printf("returned %d\n", WEXITSTATUS(result)); - assert(WIFEXITED(result)); - assert(WEXITSTATUS(result) == 0); - unlink("out/test_db0.sqlite"); - - unlink("out/test.js"); -} - static void _test_b64(const tf_test_options_t* options) { _write_file("out/test.js", @@ -925,7 +894,6 @@ void tf_tests(const tf_test_options_t* options) _tf_test_run(options, "float", _test_float, false); _tf_test_run(options, "socket", _test_socket, false); _tf_test_run(options, "file", _test_file, false); - _tf_test_run(options, "sign", _test_sign, false); _tf_test_run(options, "b64", _test_b64, false); _tf_test_run(options, "rooms", tf_ssb_test_rooms, false); _tf_test_run(options, "bench", tf_ssb_test_bench, false); diff --git a/src/util.js.c b/src/util.js.c index 83d93906..ca1a8370 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -244,67 +244,6 @@ bool tf_util_report_error(JSContext* context, JSValue value) return is_error; } -static JSValue _util_parseHttpRequest(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_UNDEFINED; - const char* method = NULL; - size_t method_length = 0; - const char* path = NULL; - size_t path_length = 0; - int minor_version = 0; - struct phr_header headers[100]; - size_t header_count = sizeof(headers) / sizeof(*headers); - int previous_length = 0; - JS_ToInt32(context, &previous_length, argv[1]); - - JSValue buffer = JS_UNDEFINED; - size_t length; - uint8_t* array = tf_util_try_get_array_buffer(context, &length, argv[0]); - if (!array) - { - size_t offset; - size_t element_size; - buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size); - if (!JS_IsException(buffer)) - { - array = tf_util_try_get_array_buffer(context, &length, buffer); - } - } - - if (array) - { - int parse_result = phr_parse_request((const char*)array, length, &method, &method_length, &path, &path_length, &minor_version, headers, &header_count, previous_length); - if (parse_result > 0) - { - result = JS_NewObject(context); - JS_SetPropertyStr(context, result, "bytes_parsed", JS_NewInt32(context, parse_result)); - JS_SetPropertyStr(context, result, "minor_version", JS_NewInt32(context, minor_version)); - JS_SetPropertyStr(context, result, "method", JS_NewStringLen(context, method, method_length)); - JS_SetPropertyStr(context, result, "path", JS_NewStringLen(context, path, path_length)); - JSValue header_object = JS_NewObject(context); - for (int i = 0; i < (int)header_count; i++) - { - char name[256]; - snprintf(name, sizeof(name), "%.*s", (int)headers[i].name_len, headers[i].name); - JS_SetPropertyStr(context, header_object, name, JS_NewStringLen(context, headers[i].value, headers[i].value_len)); - } - JS_SetPropertyStr(context, result, "headers", header_object); - } - else - { - result = JS_NewInt32(context, parse_result); - } - } - else - { - result = JS_ThrowTypeError(context, "Could not convert argument to array."); - } - - JS_FreeValue(context, buffer); - - return result; -} - static JSValue _util_parseHttpResponse(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_UNDEFINED; @@ -365,16 +304,6 @@ static JSValue _util_parseHttpResponse(JSContext* context, JSValueConst this_val return result; } -static JSValue _util_sha1_digest(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - size_t length = 0; - const char* value = JS_ToCStringLen(context, &length, argv[0]); - unsigned char digest[SHA_DIGEST_LENGTH] = { 0 }; - SHA1((const unsigned char*)value, length, digest); - JS_FreeCString(context, value); - return JS_NewArrayBufferCopy(context, digest, sizeof(digest)); -} - JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t size) { JSValue array_buffer = JS_NewArrayBufferCopy(context, data, size); @@ -387,45 +316,6 @@ JSValue tf_util_new_uint8_array(JSContext* context, const uint8_t* data, size_t return result; } -static JSValue _util_mask_bytes(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_UNDEFINED; - uint32_t mask = 0; - JS_ToUint32(context, &mask, argv[1]); - uint64_t double_mask = ((uint64_t)mask << 32) | mask; - - size_t offset = 0; - size_t length = 0; - size_t element_size = 0; - JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size); - if (!JS_IsException(buffer)) - { - size_t size = 0; - const uint8_t* array = tf_util_try_get_array_buffer(context, &size, buffer); - if (array) - { - uint8_t* copy = tf_malloc(size); - size_t i = 0; - for (; i + sizeof(double_mask) < size; i += sizeof(double_mask)) - { - ((uint64_t*)copy)[i / sizeof(double_mask)] = ((const uint64_t*)array)[i / sizeof(double_mask)] ^ double_mask; - } - for (; i + sizeof(mask) < size; i += sizeof(mask)) - { - ((uint32_t*)copy)[i / sizeof(mask)] = ((const uint32_t*)array)[i / sizeof(mask)] ^ mask; - } - for (; i < size; i++) - { - copy[i] = array[i] ^ ((mask >> (8 * (i % 4))) & 0xff); - } - result = tf_util_new_uint8_array(context, copy, size); - tf_free(copy); - } - } - JS_FreeValue(context, buffer); - return result; -} - void tf_util_register(JSContext* context) { JSValue global = JS_GetGlobalObject(context); @@ -436,10 +326,7 @@ void tf_util_register(JSContext* context) JS_SetPropertyStr(context, global, "bip39Words", JS_NewCFunction(context, _util_bip39_words, "bip39Words", 1)); JS_SetPropertyStr(context, global, "bip39Bytes", JS_NewCFunction(context, _util_bip39_bytes, "bip39Bytes", 1)); JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1)); - JS_SetPropertyStr(context, global, "parseHttpRequest", JS_NewCFunction(context, _util_parseHttpRequest, "parseHttpRequest", 2)); JS_SetPropertyStr(context, global, "parseHttpResponse", JS_NewCFunction(context, _util_parseHttpResponse, "parseHttpResponse", 2)); - JS_SetPropertyStr(context, global, "sha1Digest", JS_NewCFunction(context, _util_sha1_digest, "sha1Digest", 1)); - JS_SetPropertyStr(context, global, "maskBytes", JS_NewCFunction(context, _util_mask_bytes, "maskBytes", 2)); JS_FreeValue(context, global); }