From 27b92bdf93f764151f0329ac8d1c4b2fb230e307 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 15:58:10 +0200 Subject: [PATCH 1/8] Formatting: JsThemis native extension Add native extension of JsThemis to "make fmt" target group. This requires quite a bit of effort because clang-tidy insists on having access to *all* source code involved in complation, including all those NodeJS headers and its modules. First of all, we have to add some hardcoded paths to NodeJS headers. I have not found a proper way to get these paths so we just hardcode them and hope that this is fine. Then we need to have header file for which we have to install the nan module. We do a little hack to achieve that, pretending to 'format' the installation path for Node modules. --- Makefile | 1 + src/wrappers/themis/jsthemis/jsthemis.mk | 54 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/wrappers/themis/jsthemis/jsthemis.mk diff --git a/Makefile b/Makefile index ccdff0224..40b1e84a5 100644 --- a/Makefile +++ b/Makefile @@ -240,6 +240,7 @@ endif ifndef ERROR include src/soter/soter.mk include src/themis/themis.mk +include src/wrappers/themis/jsthemis/jsthemis.mk include jni/themis_jni.mk endif diff --git a/src/wrappers/themis/jsthemis/jsthemis.mk b/src/wrappers/themis/jsthemis/jsthemis.mk new file mode 100644 index 000000000..1259f75ea --- /dev/null +++ b/src/wrappers/themis/jsthemis/jsthemis.mk @@ -0,0 +1,54 @@ +# +# Copyright (c) 2019 Cossack Labs Limited +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +JSTHEMIS_SRC = $(SRC_PATH)/wrappers/themis/jsthemis +JSTHEMIS_OBJ = $(OBJ_PATH)/wrappers/themis/jsthemis + +JSTHEMIS_SOURCES = $(wildcard $(JSTHEMIS_SRC)/*.cpp) +JSTHEMIS_HEADERS = $(wildcard $(JSTHEMIS_SRC)/*.hpp) + +# Unfortunately, clang-tidy requires full compilation flags to be able to work and +# node-gyp add quite a few custom flags with include search paths. It also requires +# some Node modules for compilation. We recreate the same environment here as for +# the "jsthemis_install" target. However, we can't do that without NPM present. +# Therefore we format JsThemis code only if NodeJS is actually installed. +ifdef NPM_VERSION + +# A hack to install "nan" before formatting any JsThemis files +FMT_FIXUP += $(JSTHEMIS_OBJ)/node_modules/nan +FMT_CHECK += $(JSTHEMIS_OBJ)/node_modules/nan + +$(JSTHEMIS_OBJ)/node_modules/nan: + @mkdir -p $(JSTHEMIS_OBJ) && cd $(JSTHEMIS_OBJ) && npm install nan && cd - + +FMT_FIXUP += $(patsubst $(SRC_PATH)/%,$(OBJ_PATH)/%.fmt_fixup,$(JSTHEMIS_SOURCES) $(JSTHEMIS_HEADERS)) +FMT_CHECK += $(patsubst $(SRC_PATH)/%,$(OBJ_PATH)/%.fmt_check,$(JSTHEMIS_SOURCES) $(JSTHEMIS_HEADERS)) + +JSTHEMIS_CFLAGS += $(CFLAGS) +JSTHEMIS_CFLAGS += -I$(JSTHEMIS_OBJ)/node_modules/nan + +ifdef IS_LINUX +JSTHEMIS_CFLAGS += -I/usr/include/nodejs/src +JSTHEMIS_CFLAGS += -I/usr/include/nodejs/deps/v8/include +endif + +$(JSTHEMIS_OBJ)/%.hpp.fmt_fixup $(JSTHEMIS_OBJ)/%.cpp.fmt_fixup: \ + CMD = $(CLANG_TIDY) -fix $< -- $(JSTHEMIS_CFLAGS) 2>/dev/null && $(CLANG_FORMAT) -i $< && touch $@ + +$(JSTHEMIS_OBJ)/%.hpp.fmt_check $(JSTHEMIS_OBJ)/%.cpp.fmt_check: \ + CMD = $(CLANG_FORMAT) $< | diff -u $< - && $(CLANG_TIDY) $< -- $(JSTHEMIS_CFLAGS) 2>/dev/null && touch $@ + +endif # ifdef NPM_VERSION From 2639ee8d669afe2ed985564a38964da292bfa655 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 16:04:46 +0200 Subject: [PATCH 2/8] Optimize, group, and reorder includes in JsThemis This will make automated formatting a bit more nice. --- src/wrappers/themis/jsthemis/addon.cpp | 1 + .../themis/jsthemis/secure_cell_context_imprint.cpp | 6 ++++-- .../themis/jsthemis/secure_cell_context_imprint.hpp | 3 ++- src/wrappers/themis/jsthemis/secure_cell_seal.cpp | 6 ++++-- src/wrappers/themis/jsthemis/secure_cell_seal.hpp | 3 ++- .../themis/jsthemis/secure_cell_token_protect.cpp | 6 ++++-- .../themis/jsthemis/secure_cell_token_protect.hpp | 3 ++- src/wrappers/themis/jsthemis/secure_comparator.cpp | 9 ++++++--- src/wrappers/themis/jsthemis/secure_comparator.hpp | 4 +++- src/wrappers/themis/jsthemis/secure_keygen.cpp | 6 ++++-- src/wrappers/themis/jsthemis/secure_keygen.hpp | 4 +++- src/wrappers/themis/jsthemis/secure_message.cpp | 6 ++++-- src/wrappers/themis/jsthemis/secure_message.hpp | 3 ++- src/wrappers/themis/jsthemis/secure_session.cpp | 9 ++++++--- src/wrappers/themis/jsthemis/secure_session.hpp | 4 +++- 15 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/wrappers/themis/jsthemis/addon.cpp b/src/wrappers/themis/jsthemis/addon.cpp index b232568ae..826468d06 100644 --- a/src/wrappers/themis/jsthemis/addon.cpp +++ b/src/wrappers/themis/jsthemis/addon.cpp @@ -15,6 +15,7 @@ */ #include + #include "errors.hpp" #include "secure_message.hpp" #include "secure_keygen.hpp" diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp index 8edcdf4f4..40f932f7d 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp @@ -14,11 +14,13 @@ * limitations under the License. */ +#include "secure_cell_context_imprint.hpp" + #include + #include -#include + #include "errors.hpp" -#include "secure_cell_context_imprint.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp index a2a8cd5db..5d823aaab 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp @@ -17,9 +17,10 @@ #ifndef JSTHEMIS_SECURE_CELL_CONTEXT_IMPRINT_HPP_ #define JSTHEMIS_SECURE_CELL_CONTEXT_IMPRINT_HPP_ -#include #include +#include + namespace jsthemis{ class SecureCellContextImprint : public Nan::ObjectWrap { diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp index 2f6fca853..5cab78aa2 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp @@ -14,11 +14,13 @@ * limitations under the License. */ +#include "secure_cell_seal.hpp" + #include + #include -#include + #include "errors.hpp" -#include "secure_cell_seal.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp index 1c5c7aa88..ca22c1523 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp @@ -17,9 +17,10 @@ #ifndef JSTHEMIS_SECURE_CELL_SEAL_HPP_ #define JSTHEMIS_SECURE_CELL_SEAL_HPP_ -#include #include +#include + namespace jsthemis{ class SecureCellSeal : public Nan::ObjectWrap { diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp index ed6090d14..cfb6b800d 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp @@ -14,11 +14,13 @@ * limitations under the License. */ +#include "secure_cell_token_protect.hpp" + #include + #include -#include + #include "errors.hpp" -#include "secure_cell_token_protect.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp index d0f3fc38a..4886ff6c3 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp @@ -17,9 +17,10 @@ #ifndef JSTHEMIS_SECURE_CELL_TOKEN_PROTECT_HPP_ #define JSTHEMIS_SECURE_CELL_TOKEN_PROTECT_HPP_ -#include #include +#include + namespace jsthemis{ class SecureCellTokenProtect : public Nan::ObjectWrap { diff --git a/src/wrappers/themis/jsthemis/secure_comparator.cpp b/src/wrappers/themis/jsthemis/secure_comparator.cpp index 1cf8bfa7b..2234d2a9c 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.cpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.cpp @@ -14,12 +14,15 @@ * limitations under the License. */ +#include "secure_comparator.hpp" + +#include + #include + #include -#include -#include + #include "errors.hpp" -#include "secure_comparator.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_comparator.hpp b/src/wrappers/themis/jsthemis/secure_comparator.hpp index 6e837aa0f..154454573 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.hpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.hpp @@ -17,8 +17,10 @@ #ifndef JSTHEMIS_SECURE_COMPARATOR_HPP_ #define JSTHEMIS_SECURE_COMPARATOR_HPP_ -#include #include + +#include + #include namespace jsthemis{ diff --git a/src/wrappers/themis/jsthemis/secure_keygen.cpp b/src/wrappers/themis/jsthemis/secure_keygen.cpp index a71860471..33769cad5 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.cpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.cpp @@ -14,12 +14,14 @@ * limitations under the License. */ +#include "secure_keygen.hpp" + #include + #include -#include + #include "common.hpp" #include "errors.hpp" -#include "secure_keygen.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_keygen.hpp b/src/wrappers/themis/jsthemis/secure_keygen.hpp index 034383063..7156fa16e 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.hpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.hpp @@ -17,8 +17,10 @@ #ifndef JSTHEMIS_KEY_PAIR_HPP_ #define JSTHEMIS_KEY_PAIR_HPP_ -#include #include + +#include + #include namespace jsthemis{ diff --git a/src/wrappers/themis/jsthemis/secure_message.cpp b/src/wrappers/themis/jsthemis/secure_message.cpp index d81286453..39f262cec 100644 --- a/src/wrappers/themis/jsthemis/secure_message.cpp +++ b/src/wrappers/themis/jsthemis/secure_message.cpp @@ -14,12 +14,14 @@ * limitations under the License. */ +#include "secure_message.hpp" + #include + #include -#include + #include "errors.hpp" #include "secure_keygen.hpp" -#include "secure_message.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_message.hpp b/src/wrappers/themis/jsthemis/secure_message.hpp index 36b5c3e49..57c60835c 100644 --- a/src/wrappers/themis/jsthemis/secure_message.hpp +++ b/src/wrappers/themis/jsthemis/secure_message.hpp @@ -17,9 +17,10 @@ #ifndef JSTHEMIS_SECURE_MESSAGE_HPP_ #define JSTHEMIS_SECURE_MESSAGE_HPP_ -#include #include +#include + namespace jsthemis{ class SecureMessage : public Nan::ObjectWrap { diff --git a/src/wrappers/themis/jsthemis/secure_session.cpp b/src/wrappers/themis/jsthemis/secure_session.cpp index 556afac00..f4bb9b85c 100644 --- a/src/wrappers/themis/jsthemis/secure_session.cpp +++ b/src/wrappers/themis/jsthemis/secure_session.cpp @@ -14,12 +14,15 @@ * limitations under the License. */ +#include "secure_session.hpp" + +#include + #include + #include -#include -#include + #include "errors.hpp" -#include "secure_session.hpp" namespace jsthemis { diff --git a/src/wrappers/themis/jsthemis/secure_session.hpp b/src/wrappers/themis/jsthemis/secure_session.hpp index dd5af73cf..dec7e6237 100644 --- a/src/wrappers/themis/jsthemis/secure_session.hpp +++ b/src/wrappers/themis/jsthemis/secure_session.hpp @@ -17,8 +17,10 @@ #ifndef JSTHEMIS_SECURE_SESSION_HPP_ #define JSTHEMIS_SECURE_SESSION_HPP_ -#include #include + +#include + #include namespace jsthemis{ From 02fade088656590751b2d12fc4eb85e6cd221788 Mon Sep 17 00:00:00 2001 From: clang-format Date: Tue, 5 Mar 2019 16:06:51 +0200 Subject: [PATCH 3/8] Reformat JsThemis sources --- src/wrappers/themis/jsthemis/addon.cpp | 27 +- src/wrappers/themis/jsthemis/common.hpp | 14 +- src/wrappers/themis/jsthemis/errors.cpp | 111 +++-- src/wrappers/themis/jsthemis/errors.hpp | 18 +- .../jsthemis/secure_cell_context_imprint.cpp | 244 +++++----- .../jsthemis/secure_cell_context_imprint.hpp | 14 +- .../themis/jsthemis/secure_cell_seal.cpp | 241 ++++++---- .../themis/jsthemis/secure_cell_seal.hpp | 14 +- .../jsthemis/secure_cell_token_protect.cpp | 275 ++++++----- .../jsthemis/secure_cell_token_protect.hpp | 14 +- .../themis/jsthemis/secure_comparator.cpp | 209 ++++---- .../themis/jsthemis/secure_comparator.hpp | 16 +- .../themis/jsthemis/secure_keygen.cpp | 223 +++++---- .../themis/jsthemis/secure_keygen.hpp | 25 +- .../themis/jsthemis/secure_message.cpp | 450 ++++++++++-------- .../themis/jsthemis/secure_message.hpp | 20 +- .../themis/jsthemis/secure_session.cpp | 364 +++++++------- .../themis/jsthemis/secure_session.hpp | 22 +- 18 files changed, 1287 insertions(+), 1014 deletions(-) diff --git a/src/wrappers/themis/jsthemis/addon.cpp b/src/wrappers/themis/jsthemis/addon.cpp index 826468d06..3a462023a 100644 --- a/src/wrappers/themis/jsthemis/addon.cpp +++ b/src/wrappers/themis/jsthemis/addon.cpp @@ -17,23 +17,24 @@ #include #include "errors.hpp" -#include "secure_message.hpp" -#include "secure_keygen.hpp" -#include "secure_session.hpp" -#include "secure_cell_seal.hpp" #include "secure_cell_context_imprint.hpp" +#include "secure_cell_seal.hpp" #include "secure_cell_token_protect.hpp" #include "secure_comparator.hpp" +#include "secure_keygen.hpp" +#include "secure_message.hpp" +#include "secure_session.hpp" -void InitAll(v8::Handle exports) { - jsthemis::Errors::Init(exports); - jsthemis::SecureMessage::Init(exports); - jsthemis::KeyPair::Init(exports); - jsthemis::SecureSession::Init(exports); - jsthemis::SecureCellSeal::Init(exports); - jsthemis::SecureCellContextImprint::Init(exports); - jsthemis::SecureCellTokenProtect::Init(exports); - jsthemis::SecureComparator::Init(exports); +void InitAll(v8::Handle exports) +{ + jsthemis::Errors::Init(exports); + jsthemis::SecureMessage::Init(exports); + jsthemis::KeyPair::Init(exports); + jsthemis::SecureSession::Init(exports); + jsthemis::SecureCellSeal::Init(exports); + jsthemis::SecureCellContextImprint::Init(exports); + jsthemis::SecureCellTokenProtect::Init(exports); + jsthemis::SecureComparator::Init(exports); } NODE_MODULE(jsthemis, InitAll) diff --git a/src/wrappers/themis/jsthemis/common.hpp b/src/wrappers/themis/jsthemis/common.hpp index b14660d40..e0fe53a10 100644 --- a/src/wrappers/themis/jsthemis/common.hpp +++ b/src/wrappers/themis/jsthemis/common.hpp @@ -17,11 +17,13 @@ #ifndef JSTHEMIS_COMMON_HPP_ #define JSTHEMIS_COMMON_HPP_ -#define RETURN_BUFFER(buf, length) \ - v8::Local globalObj = v8::Context::GetCurrent()->Global(); \ - v8::Local bufferConstructor = v8::Local::Cast(globalObj->Get(v8::String::New("Buffer"))); \ - v8::Handle constructorArgs[3] = { buf->handle_, v8::Integer::New(length), v8::Integer::New(0) }; \ - v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); \ - return scope.Close(actualBuffer) +#define RETURN_BUFFER(buf, length) \ + v8::Local globalObj = v8::Context::GetCurrent()->Global(); \ + v8::Local bufferConstructor = \ + v8::Local::Cast(globalObj->Get(v8::String::New("Buffer"))); \ + v8::Handle constructorArgs[3] = {buf->handle_, v8::Integer::New(length), \ + v8::Integer::New(0)}; \ + v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); \ + return scope.Close(actualBuffer) #endif /* JSTHEMIS_COMMON_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/errors.cpp b/src/wrappers/themis/jsthemis/errors.cpp index 217e96e68..e4edb0c42 100644 --- a/src/wrappers/themis/jsthemis/errors.cpp +++ b/src/wrappers/themis/jsthemis/errors.cpp @@ -21,116 +21,129 @@ #include #include -namespace jsthemis { +namespace jsthemis +{ - static inline void ExportStatusCode(v8::Handle& exports, const char* name, themis_status_t status) { +static inline void ExportStatusCode(v8::Handle& exports, const char* name, + themis_status_t status) +{ exports->Set(Nan::New(name).ToLocalChecked(), Nan::New(status)); - } +} - void Errors::Init(v8::Handle exports) { - ExportStatusCode(exports, "SUCCESS", THEMIS_SUCCESS); - ExportStatusCode(exports, "FAIL", THEMIS_FAIL); - ExportStatusCode(exports, "INVALID_PARAMETER", THEMIS_INVALID_PARAMETER); - ExportStatusCode(exports, "NO_MEMORY", THEMIS_NO_MEMORY); - ExportStatusCode(exports, "BUFFER_TOO_SMALL", THEMIS_BUFFER_TOO_SMALL); - ExportStatusCode(exports, "DATA_CORRUPT", THEMIS_DATA_CORRUPT); - ExportStatusCode(exports, "INVALID_SIGNATURE", THEMIS_INVALID_SIGNATURE); - ExportStatusCode(exports, "NOT_SUPPORTED", THEMIS_NOT_SUPPORTED); +void Errors::Init(v8::Handle exports) +{ + ExportStatusCode(exports, "SUCCESS", THEMIS_SUCCESS); + ExportStatusCode(exports, "FAIL", THEMIS_FAIL); + ExportStatusCode(exports, "INVALID_PARAMETER", THEMIS_INVALID_PARAMETER); + ExportStatusCode(exports, "NO_MEMORY", THEMIS_NO_MEMORY); + ExportStatusCode(exports, "BUFFER_TOO_SMALL", THEMIS_BUFFER_TOO_SMALL); + ExportStatusCode(exports, "DATA_CORRUPT", THEMIS_DATA_CORRUPT); + ExportStatusCode(exports, "INVALID_SIGNATURE", THEMIS_INVALID_SIGNATURE); + ExportStatusCode(exports, "NOT_SUPPORTED", THEMIS_NOT_SUPPORTED); ExportStatusCode(exports, "SSESSION_KA_NOT_FINISHED", THEMIS_SSESSION_KA_NOT_FINISHED); ExportStatusCode(exports, "SSESSION_TRANSPORT_ERROR", THEMIS_SSESSION_TRANSPORT_ERROR); - ExportStatusCode(exports, "SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR", THEMIS_SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR); + ExportStatusCode(exports, "SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR", + THEMIS_SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR); ExportStatusCode(exports, "SCOMPARE_NOT_READY", THEMIS_SCOMPARE_NOT_READY); - } +} - static const char* ErrorDescription(themis_status_t status) { +static const char* ErrorDescription(themis_status_t status) +{ switch (status) { case THEMIS_SUCCESS: - return "success"; + return "success"; case THEMIS_FAIL: - return "failure"; + return "failure"; case THEMIS_INVALID_PARAMETER: - return "invalid parameter"; + return "invalid parameter"; case THEMIS_NO_MEMORY: - return "out of memory"; + return "out of memory"; case THEMIS_BUFFER_TOO_SMALL: - return "buffer too small"; + return "buffer too small"; case THEMIS_DATA_CORRUPT: - return "corrupted data"; + return "corrupted data"; case THEMIS_INVALID_SIGNATURE: - return "invalid signature"; + return "invalid signature"; case THEMIS_NOT_SUPPORTED: - return "operation not supported"; + return "operation not supported"; default: - return "unknown error"; + return "unknown error"; } - } +} - static const char* ErrorDescriptionSecureSession(themis_status_t status) { +static const char* ErrorDescriptionSecureSession(themis_status_t status) +{ switch (status) { case THEMIS_SSESSION_SEND_OUTPUT_TO_PEER: - return "send key agreement data to peer"; + return "send key agreement data to peer"; case THEMIS_SSESSION_KA_NOT_FINISHED: - return "key agreement not finished"; + return "key agreement not finished"; case THEMIS_SSESSION_TRANSPORT_ERROR: - return "transport layer error"; + return "transport layer error"; case THEMIS_SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR: - return "failed to get public key for ID"; + return "failed to get public key for ID"; default: - return ErrorDescription(status); + return ErrorDescription(status); } - } +} - static const char* ErrorDescriptionSecureComparator(themis_status_t status) { +static const char* ErrorDescriptionSecureComparator(themis_status_t status) +{ switch (status) { case THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER: - return "send comparison data to peer"; + return "send comparison data to peer"; case THEMIS_SCOMPARE_NOT_READY: - return "comparator not ready"; + return "comparator not ready"; case THEMIS_SCOMPARE_MATCH: - return "data matches"; + return "data matches"; case THEMIS_SCOMPARE_NO_MATCH: - return "data does not match"; + return "data does not match"; default: - return ErrorDescription(status); + return ErrorDescription(status); } - } +} - static v8::Local WithStatus(v8::Local error, themis_status_t status) { +static v8::Local WithStatus(v8::Local error, themis_status_t status) +{ v8::Local object = error.As(); object->Set(Nan::New("code").ToLocalChecked(), Nan::New(status)); return error; - } +} - void ThrowError(const char* domain, themis_status_t status) { +void ThrowError(const char* domain, themis_status_t status) +{ std::string message; message += domain; message += ": "; message += ErrorDescription(status); Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status)); - } +} - void ThrowParameterError(const char* domain, const char* description) { +void ThrowParameterError(const char* domain, const char* description) +{ std::string message; message += domain; message += ": "; message += description; Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), THEMIS_INVALID_PARAMETER)); - } +} - void ThrowSecureSessionError(const char* domain, themis_status_t status) { +void ThrowSecureSessionError(const char* domain, themis_status_t status) +{ std::string message; message += domain; message += ": "; message += ErrorDescriptionSecureSession(status); Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status)); - } +} - void ThrowSecureComparatorError(const char* domain, themis_status_t status) { +void ThrowSecureComparatorError(const char* domain, themis_status_t status) +{ std::string message; message += domain; message += ": "; message += ErrorDescriptionSecureComparator(status); Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status)); - } +} } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/errors.hpp b/src/wrappers/themis/jsthemis/errors.hpp index f07018dac..747e3d97b 100644 --- a/src/wrappers/themis/jsthemis/errors.hpp +++ b/src/wrappers/themis/jsthemis/errors.hpp @@ -21,21 +21,23 @@ #include -namespace jsthemis { +namespace jsthemis +{ - namespace Errors { +namespace Errors +{ - void Init(v8::Handle exports); +void Init(v8::Handle exports); - } // namespace Errors +} // namespace Errors - void ThrowError(const char* domain, themis_status_t status); +void ThrowError(const char* domain, themis_status_t status); - void ThrowParameterError(const char* domain, const char* description); +void ThrowParameterError(const char* domain, const char* description); - void ThrowSecureSessionError(const char* domain, themis_status_t status); +void ThrowSecureSessionError(const char* domain, themis_status_t status); - void ThrowSecureComparatorError(const char* domain, themis_status_t status); +void ThrowSecureComparatorError(const char* domain, themis_status_t status); } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp index 40f932f7d..0041c341f 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp @@ -22,18 +22,25 @@ #include "errors.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent SecureCellContextImprint::constructor; +Nan::Persistent SecureCellContextImprint::constructor; - SecureCellContextImprint::SecureCellContextImprint(const std::vector& key) : - key_(key){} +SecureCellContextImprint::SecureCellContextImprint(const std::vector& key) + : key_(key) +{ +} - SecureCellContextImprint::~SecureCellContextImprint() {} +SecureCellContextImprint::~SecureCellContextImprint() +{ +} - void SecureCellContextImprint::Init(v8::Handle exports) { +void SecureCellContextImprint::Init(v8::Handle exports) +{ // Prepare constructor template - v8::Local tpl = Nan::New(SecureCellContextImprint::New); + v8::Local tpl = + Nan::New(SecureCellContextImprint::New); tpl->SetClassName(Nan::New("SecureCellContextImprint").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype @@ -41,131 +48,152 @@ namespace jsthemis { Nan::SetPrototypeMethod(tpl, "decrypt", decrypt); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureCellContextImprint").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureCellContextImprint::New(const Nan::FunctionCallbackInfo& args) { +void SecureCellContextImprint::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Context Imprint) constructor", "not enough arguments, expected master key"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Context Imprint) constructor", "master key is not a byte buffer"); - args.GetReturnValue().SetUndefined(); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Context Imprint) constructor", "master key is empty"); - args.GetReturnValue().SetUndefined(); - return; - } - std::vector key((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - SecureCellContextImprint* obj = new SecureCellContextImprint(key); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Context Imprint) constructor", + "not enough arguments, expected master key"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Context Imprint) constructor", + "master key is not a byte buffer"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Context Imprint) constructor", "master key is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + std::vector key( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + SecureCellContextImprint* obj = new SecureCellContextImprint(key); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); } else { - const int argc = 1; - v8::Local argv[argc] = { args[0]}; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + const int argc = 1; + v8::Local argv[argc] = {args[0]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } +} - void SecureCellContextImprint::encrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellContextImprint::encrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellContextImprint* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<2){ - ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "not enough arguments, missing message and context"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 2) { + ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", + "not enough arguments, missing message and context"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[1])==0){ - ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "context is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[1]) == 0) { + ThrowParameterError("Secure Cell (Context Imprint) failed to encrypt", "context is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - const uint8_t* context=(const uint8_t*)(node::Buffer::Data(args[1])); - size_t context_length=node::Buffer::Length(args[1]); - status=themis_secure_cell_encrypt_context_imprint(&(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), context, context_length, NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + const uint8_t* context = (const uint8_t*)(node::Buffer::Data(args[1])); + size_t context_length = node::Buffer::Length(args[1]); + status = themis_secure_cell_encrypt_context_imprint( + &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), context, context_length, NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); - status=themis_secure_cell_encrypt_context_imprint(&(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), context, context_length, data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)(malloc(length)); + status = themis_secure_cell_encrypt_context_imprint( + &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), context, context_length, data, &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} - void SecureCellContextImprint::decrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellContextImprint::decrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellContextImprint* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<2){ - ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "not enough arguments, expected message and context"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 2) { + ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", + "not enough arguments, expected message and context"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[1])==0){ - ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "context is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[1]) == 0) { + ThrowParameterError("Secure Cell (Context Imprint) failed to decrypt", "context is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - const uint8_t* context=(const uint8_t*)(node::Buffer::Data(args[1])); - size_t context_length=node::Buffer::Length(args[1]); - status=themis_secure_cell_decrypt_context_imprint(&(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), context, context_length, NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + const uint8_t* context = (const uint8_t*)(node::Buffer::Data(args[1])); + size_t context_length = node::Buffer::Length(args[1]); + status = themis_secure_cell_decrypt_context_imprint( + &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), context, context_length, NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); - status=themis_secure_cell_decrypt_context_imprint(&(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), context, context_length, data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)(malloc(length)); + status = themis_secure_cell_decrypt_context_imprint( + &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), context, context_length, data, &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} -} //end jsthemis +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp index 5d823aaab..1aba2a8ac 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp @@ -21,13 +21,15 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureCellContextImprint : public Nan::ObjectWrap { - public: +class SecureCellContextImprint : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: +private: explicit SecureCellContextImprint(const std::vector& key); ~SecureCellContextImprint(); @@ -38,7 +40,7 @@ namespace jsthemis{ static Nan::Persistent constructor; std::vector key_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_CELL_CONTEXT_IMPRINT_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp index 5cab78aa2..525c62231 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp @@ -22,16 +22,22 @@ #include "errors.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent SecureCellSeal::constructor; +Nan::Persistent SecureCellSeal::constructor; - SecureCellSeal::SecureCellSeal(const std::vector& key) : - key_(key){} +SecureCellSeal::SecureCellSeal(const std::vector& key) + : key_(key) +{ +} - SecureCellSeal::~SecureCellSeal() {} +SecureCellSeal::~SecureCellSeal() +{ +} - void SecureCellSeal::Init(v8::Handle exports) { +void SecureCellSeal::Init(v8::Handle exports) +{ // Prepare constructor template v8::Local tpl = Nan::New(New); tpl->SetClassName(Nan::New("SecureCellSeal").ToLocalChecked()); @@ -41,128 +47,153 @@ namespace jsthemis { Nan::SetPrototypeMethod(tpl, "decrypt", decrypt); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureCellSeal").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureCellSeal::New(const Nan::FunctionCallbackInfo& args) { +void SecureCellSeal::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Seal) constructor", "not enough arguments, expected master key"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Seal) constructor", "master key is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Seal) constructor", "master key is empty"); - args.GetReturnValue().SetUndefined(); - return; - } - std::vector key((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - SecureCellSeal* obj = new SecureCellSeal(key); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Seal) constructor", + "not enough arguments, expected master key"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Seal) constructor", + "master key is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Seal) constructor", "master key is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + std::vector key( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + SecureCellSeal* obj = new SecureCellSeal(key); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); } else { - const int argc = 1; - v8::Local argv[argc] = { args[0]}; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + const int argc = 1; + v8::Local argv[argc] = {args[0]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } +} - void SecureCellSeal::encrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellSeal::encrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellSeal* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Seal) failed to encrypt", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Seal) failed to encrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Seal) failed to encrypt", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Seal) failed to encrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Seal) failed to encrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - const uint8_t* context=NULL; - size_t context_length=0; - if(args.Length()==2){ - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Seal) failed to encrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Seal) failed to encrypt", "message is empty"); args.GetReturnValue().SetUndefined(); return; - } - context = (const uint8_t*)(node::Buffer::Data(args[1])); - context_length = node::Buffer::Length(args[1]); } - status=themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Seal) failed to encrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + const uint8_t* context = NULL; + size_t context_length = 0; + if (args.Length() == 2) { + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Seal) failed to encrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + context = (const uint8_t*)(node::Buffer::Data(args[1])); + context_length = node::Buffer::Length(args[1]); + } + status = + themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Seal) failed to encrypt", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); - status=themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Seal) failed to encrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)(malloc(length)); + status = + themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), data, &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Seal) failed to encrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} - void SecureCellSeal::decrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellSeal::decrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellSeal* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Seal) failed to decrypt", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Seal) failed to decrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Seal) failed to decrypt", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Seal) failed to decrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Seal) failed to decrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - const uint8_t* context=NULL; - size_t context_length=0; - if(args.Length()==2){ - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Seal) failed to decrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Seal) failed to decrypt", "message is empty"); args.GetReturnValue().SetUndefined(); return; - } - context = (const uint8_t*)(node::Buffer::Data(args[1])); - context_length = node::Buffer::Length(args[1]); } - status=themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Seal) failed to decrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + const uint8_t* context = NULL; + size_t context_length = 0; + if (args.Length() == 2) { + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Seal) failed to decrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + context = (const uint8_t*)(node::Buffer::Data(args[1])); + context_length = node::Buffer::Length(args[1]); + } + status = + themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Seal) failed to decrypt", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)malloc(length); - status=themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Seal) failed to decrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)malloc(length); + status = + themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), data, &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Seal) failed to decrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } -} //end jsthemis +} +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp index ca22c1523..fe382297d 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp @@ -21,13 +21,15 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureCellSeal : public Nan::ObjectWrap { - public: +class SecureCellSeal : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: +private: explicit SecureCellSeal(const std::vector& key); ~SecureCellSeal(); @@ -38,7 +40,7 @@ namespace jsthemis{ static Nan::Persistent constructor; std::vector key_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_CELL_SEAL_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp index cfb6b800d..6e4f2def5 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp @@ -22,18 +22,25 @@ #include "errors.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent SecureCellTokenProtect::constructor; +Nan::Persistent SecureCellTokenProtect::constructor; - SecureCellTokenProtect::SecureCellTokenProtect(const std::vector& key) : - key_(key){} +SecureCellTokenProtect::SecureCellTokenProtect(const std::vector& key) + : key_(key) +{ +} - SecureCellTokenProtect::~SecureCellTokenProtect() {} +SecureCellTokenProtect::~SecureCellTokenProtect() +{ +} - void SecureCellTokenProtect::Init(v8::Handle exports) { +void SecureCellTokenProtect::Init(v8::Handle exports) +{ // Prepare constructor template - v8::Local tpl = Nan::New(SecureCellTokenProtect::New); + v8::Local tpl = + Nan::New(SecureCellTokenProtect::New); tpl->SetClassName(Nan::New("SecureCellTokenProtect").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype @@ -41,144 +48,174 @@ namespace jsthemis { Nan::SetPrototypeMethod(tpl, "decrypt", decrypt); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureCellTokenProtect").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureCellTokenProtect::New(const Nan::FunctionCallbackInfo& args) { +void SecureCellTokenProtect::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Token Protect) constructor", "not enough arguments, expected master key"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) constructor", "master key is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Token Protect) constructor", "master key is empty"); - args.GetReturnValue().SetUndefined(); - return; - } - std::vector key((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - SecureCellTokenProtect* obj = new SecureCellTokenProtect(key); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Token Protect) constructor", + "not enough arguments, expected master key"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) constructor", + "master key is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Token Protect) constructor", "master key is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + std::vector key( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + SecureCellTokenProtect* obj = new SecureCellTokenProtect(key); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); } else { - const int argc = 1; - v8::Local argv[argc] = { args[0]}; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + const int argc = 1; + v8::Local argv[argc] = {args[0]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } +} - void SecureCellTokenProtect::encrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellTokenProtect::encrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellTokenProtect* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - size_t token_length=0; - const uint8_t* context=NULL; - size_t context_length=0; - if(args.Length()==2){ - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", "message is empty"); args.GetReturnValue().SetUndefined(); return; - } - context = (const uint8_t*)(node::Buffer::Data(args[1])); - context_length = node::Buffer::Length(args[1]); } - status=themis_secure_cell_encrypt_token_protect(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &token_length, NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Token Protect) failed to encrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + size_t token_length = 0; + const uint8_t* context = NULL; + size_t context_length = 0; + if (args.Length() == 2) { + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) failed to encrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + context = (const uint8_t*)(node::Buffer::Data(args[1])); + context_length = node::Buffer::Length(args[1]); } - uint8_t* data=(uint8_t*)(malloc(length)); - uint8_t* token=(uint8_t*)(malloc(token_length)); - status=themis_secure_cell_encrypt_token_protect(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), token, &token_length, data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Token Protect) failed to encrypt", status); - free(data); - free(token); - args.GetReturnValue().SetUndefined(); - return; + status = themis_secure_cell_encrypt_token_protect( + &(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, + &token_length, NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Token Protect) failed to encrypt", status); + args.GetReturnValue().SetUndefined(); + return; + } + uint8_t* data = (uint8_t*)(malloc(length)); + uint8_t* token = (uint8_t*)(malloc(token_length)); + status = themis_secure_cell_encrypt_token_protect( + &(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), token, + &token_length, data, &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Token Protect) failed to encrypt", status); + free(data); + free(token); + args.GetReturnValue().SetUndefined(); + return; } v8::Local retobj = Nan::New(); - Nan::Set(retobj, Nan::New("data").ToLocalChecked(), Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - Nan::Set(retobj, Nan::New("token").ToLocalChecked(), Nan::NewBuffer((char*)(token), token_length).ToLocalChecked()); + Nan::Set(retobj, Nan::New("data").ToLocalChecked(), + Nan::NewBuffer((char*)(data), length).ToLocalChecked()); + Nan::Set(retobj, Nan::New("token").ToLocalChecked(), + Nan::NewBuffer((char*)(token), token_length).ToLocalChecked()); args.GetReturnValue().Set(retobj); - } +} - void SecureCellTokenProtect::decrypt(const Nan::FunctionCallbackInfo& args) { +void SecureCellTokenProtect::decrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureCellTokenProtect* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<2){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "not enough arguments, expected message and token"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 2) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", + "not enough arguments, expected message and token"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "token is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[1])==0){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "token is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", + "token is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - size_t length=0; - const uint8_t* context=NULL; - size_t context_length=0; - if(args.Length()==3){ - if(!args[2]->IsUint8Array()){ - ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "context is not a byte buffer, use ByteBuffer or Uint8Array"); + if (node::Buffer::Length(args[1]) == 0) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", "token is empty"); args.GetReturnValue().SetUndefined(); return; - } - context = (const uint8_t*)(node::Buffer::Data(args[2])); - context_length = node::Buffer::Length(args[2]); } - status=themis_secure_cell_decrypt_token_protect(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Cell (Token Protect) failed to decrypt", status); - args.GetReturnValue().SetUndefined(); - return; + size_t length = 0; + const uint8_t* context = NULL; + size_t context_length = 0; + if (args.Length() == 3) { + if (!args[2]->IsUint8Array()) { + ThrowParameterError("Secure Cell (Token Protect) failed to decrypt", + "context is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + context = (const uint8_t*)(node::Buffer::Data(args[2])); + context_length = node::Buffer::Length(args[2]); } - uint8_t* data=(uint8_t*)(malloc(length)); - status=themis_secure_cell_decrypt_token_protect(&(obj->key_)[0], obj->key_.size(), context, context_length, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Cell (Token Protect) failed to decrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + status = themis_secure_cell_decrypt_token_protect( + &(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), + (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), NULL, + &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Cell (Token Protect) failed to decrypt", status); + args.GetReturnValue().SetUndefined(); + return; + } + uint8_t* data = (uint8_t*)(malloc(length)); + status = themis_secure_cell_decrypt_token_protect( + &(obj->key_)[0], obj->key_.size(), context, context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), + (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), data, + &length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Cell (Token Protect) failed to decrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } -} //end jsthemis +} +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp index 4886ff6c3..b8f3a9fde 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp @@ -21,13 +21,15 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureCellTokenProtect : public Nan::ObjectWrap { - public: +class SecureCellTokenProtect : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: +private: explicit SecureCellTokenProtect(const std::vector& key); ~SecureCellTokenProtect(); @@ -38,7 +40,7 @@ namespace jsthemis{ static Nan::Persistent constructor; std::vector key_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_CELL_TOKEN_PROTECT_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_comparator.cpp b/src/wrappers/themis/jsthemis/secure_comparator.cpp index 2234d2a9c..c3fefc4ae 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.cpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.cpp @@ -24,141 +24,162 @@ #include "errors.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent SecureComparator::constructor; +Nan::Persistent SecureComparator::constructor; - SecureComparator::SecureComparator(const std::vector& secret): - comparator_(NULL){ - comparator_=secure_comparator_create(); - if(!comparator_){ - ThrowError("Secure Comparator constructor", THEMIS_FAIL); - return; +SecureComparator::SecureComparator(const std::vector& secret) + : comparator_(NULL) +{ + comparator_ = secure_comparator_create(); + if (!comparator_) { + ThrowError("Secure Comparator constructor", THEMIS_FAIL); + return; } - themis_status_t status=secure_comparator_append_secret(comparator_, &secret[0], secret.size()); - if(THEMIS_SUCCESS!=status){ - ThrowError("Secure Comparator failed to append secret", status); - return; + themis_status_t status = + secure_comparator_append_secret(comparator_, &secret[0], secret.size()); + if (THEMIS_SUCCESS != status) { + ThrowError("Secure Comparator failed to append secret", status); + return; } - } +} - SecureComparator::~SecureComparator() { - if(comparator_){ - secure_comparator_destroy(comparator_); - comparator_=NULL; +SecureComparator::~SecureComparator() +{ + if (comparator_) { + secure_comparator_destroy(comparator_); + comparator_ = NULL; } - } +} - void SecureComparator::Init(v8::Handle exports) { +void SecureComparator::Init(v8::Handle exports) +{ // Prepare constructor template v8::Local tpl = Nan::New(New); tpl->SetClassName(Nan::New("SecureComparator").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype Nan::SetPrototypeMethod(tpl, "beginCompare", beginCompare); - Nan::SetPrototypeMethod(tpl, "proceedCompare",proceedCompare); + Nan::SetPrototypeMethod(tpl, "proceedCompare", proceedCompare); Nan::SetPrototypeMethod(tpl, "isMatch", isMatch); Nan::SetPrototypeMethod(tpl, "isCompareComplete", isCompareComplete); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureComparator").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureComparator::New(const Nan::FunctionCallbackInfo& args) { +void SecureComparator::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<1){ - ThrowParameterError("Secure Comparator constructor", "not enough arguments, expected secret to compare"); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Comparator constructor", "secret is not a byte buffer, use ByteBuffer or Uint8Array"); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Comparator constructor", "secret is empty"); - return; - } - std::vector secret((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - SecureComparator* obj = new SecureComparator(secret); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); + if (args.Length() < 1) { + ThrowParameterError("Secure Comparator constructor", + "not enough arguments, expected secret to compare"); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Comparator constructor", + "secret is not a byte buffer, use ByteBuffer or Uint8Array"); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Comparator constructor", "secret is empty"); + return; + } + std::vector secret( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + SecureComparator* obj = new SecureComparator(secret); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); } else { - const int argc = 3; - v8::Local argv[argc] = { args[0], args[1], args[2]}; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + const int argc = 3; + v8::Local argv[argc] = {args[0], args[1], args[2]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } +} - void SecureComparator::beginCompare(const Nan::FunctionCallbackInfo& args) { +void SecureComparator::beginCompare(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureComparator* obj = Nan::ObjectWrap::Unwrap(args.This()); - size_t length=0; + size_t length = 0; status = secure_comparator_begin_compare(obj->comparator_, NULL, &length); - if(THEMIS_BUFFER_TOO_SMALL != status){ - ThrowSecureComparatorError("Secure Comparator failed to begin comparison", status); - args.GetReturnValue().SetUndefined(); - return; + if (THEMIS_BUFFER_TOO_SMALL != status) { + ThrowSecureComparatorError("Secure Comparator failed to begin comparison", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); + uint8_t* data = (uint8_t*)(malloc(length)); status = secure_comparator_begin_compare(obj->comparator_, data, &length); - if(THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER!=status){ - ThrowSecureComparatorError("Secure Comparator failed to begin comparison", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + if (THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER != status) { + ThrowSecureComparatorError("Secure Comparator failed to begin comparison", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} - void SecureComparator::proceedCompare(const Nan::FunctionCallbackInfo& args){ +void SecureComparator::proceedCompare(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureComparator* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Comparator failed to proceed comparison", "not enough arguments, expected message"); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Comparator failed to proceed comparison", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Comparator failed to proceed comparison", + "not enough arguments, expected message"); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Comparator failed to proceed comparison", "message is empty"); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Comparator failed to proceed comparison", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + return; } - size_t length=0; - status = secure_comparator_proceed_compare(obj->comparator_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &length); - if(THEMIS_BUFFER_TOO_SMALL!=status){ - ThrowSecureComparatorError("Secure Comparator failed to proceed comparison", status); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Comparator failed to proceed comparison", "message is empty"); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); - status = secure_comparator_proceed_compare(obj->comparator_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), data, &length); - if(THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER!=status){ - if(THEMIS_SUCCESS != status){ + size_t length = 0; + status = secure_comparator_proceed_compare(obj->comparator_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &length); + if (THEMIS_BUFFER_TOO_SMALL != status) { ThrowSecureComparatorError("Secure Comparator failed to proceed comparison", status); - } - free(data); - args.GetReturnValue().SetUndefined(); - return; + args.GetReturnValue().SetUndefined(); + return; + } + uint8_t* data = (uint8_t*)(malloc(length)); + status = secure_comparator_proceed_compare(obj->comparator_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), data, &length); + if (THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER != status) { + if (THEMIS_SUCCESS != status) { + ThrowSecureComparatorError("Secure Comparator failed to proceed comparison", status); + } + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} - void SecureComparator::isMatch(const Nan::FunctionCallbackInfo& args){ +void SecureComparator::isMatch(const Nan::FunctionCallbackInfo& args) +{ SecureComparator* obj = Nan::ObjectWrap::Unwrap(args.This()); themis_status_t res = secure_comparator_get_result(obj->comparator_); - if(THEMIS_SCOMPARE_NOT_READY == res){ - ThrowSecureComparatorError("Secure Comparator not ready", THEMIS_SCOMPARE_NOT_READY); - args.GetReturnValue().SetUndefined(); - return; + if (THEMIS_SCOMPARE_NOT_READY == res) { + ThrowSecureComparatorError("Secure Comparator not ready", THEMIS_SCOMPARE_NOT_READY); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::New((THEMIS_SCOMPARE_NO_MATCH==res)?false:true)); - } + args.GetReturnValue().Set(Nan::New(THEMIS_SCOMPARE_NO_MATCH != res)); +} - void SecureComparator::isCompareComplete(const Nan::FunctionCallbackInfo& args){ +void SecureComparator::isCompareComplete(const Nan::FunctionCallbackInfo& args) +{ SecureComparator* obj = Nan::ObjectWrap::Unwrap(args.This()); - args.GetReturnValue().Set(Nan::New((THEMIS_SCOMPARE_NOT_READY==secure_comparator_get_result(obj->comparator_))?false:true)); - } - -} //end jsthemis + args.GetReturnValue().Set(Nan::New( + THEMIS_SCOMPARE_NOT_READY != secure_comparator_get_result(obj->comparator_))); +} + +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_comparator.hpp b/src/wrappers/themis/jsthemis/secure_comparator.hpp index 154454573..ed3346ba9 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.hpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.hpp @@ -23,13 +23,15 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureComparator : public Nan::ObjectWrap { - public: +class SecureComparator : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: +private: explicit SecureComparator(const std::vector& secret); ~SecureComparator(); @@ -41,9 +43,9 @@ namespace jsthemis{ static Nan::Persistent constructor; - private: +private: secure_comparator_t* comparator_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_Comparator_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_keygen.cpp b/src/wrappers/themis/jsthemis/secure_keygen.cpp index 33769cad5..0b510e6c4 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.cpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.cpp @@ -23,17 +23,23 @@ #include "common.hpp" #include "errors.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent KeyPair::constructor; +Nan::Persistent KeyPair::constructor; - KeyPair::KeyPair(const std::vector& private_key, const std::vector& public_key) : - private_key_(private_key), - public_key_(public_key){} - - KeyPair::~KeyPair() {} +KeyPair::KeyPair(const std::vector& private_key, const std::vector& public_key) + : private_key_(private_key) + , public_key_(public_key) +{ +} - void KeyPair::Init(v8::Handle exports) { +KeyPair::~KeyPair() +{ +} + +void KeyPair::Init(v8::Handle exports) +{ // Prepare constructor template v8::Local tpl = Nan::New(KeyPair::New); tpl->SetClassName(Nan::New("KeyPair").ToLocalChecked()); @@ -43,109 +49,130 @@ namespace jsthemis { Nan::SetPrototypeMethod(tpl, "public", public_key); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("KeyPair").ToLocalChecked(), tpl->GetFunction()); - } +} - void KeyPair::New(const Nan::FunctionCallbackInfo& args) { +void KeyPair::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()==2){ - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Key Pair constructor", "private key is not a byte buffer, use ByteBuffer or Uint8Array"); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Key Pair constructor", "private key is empty"); - return; - } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Key Pair constructor", "public key is not a byte buffer, use ByteBuffer or Uint8Array"); - return; - } - if(node::Buffer::Length(args[1])==0){ - ThrowParameterError("Key Pair constructor", "public key is empty"); - return; + if (args.Length() == 2) { + if (!args[0]->IsUint8Array()) { + ThrowParameterError( + "Key Pair constructor", + "private key is not a byte buffer, use ByteBuffer or Uint8Array"); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Key Pair constructor", "private key is empty"); + return; + } + if (!args[1]->IsUint8Array()) { + ThrowParameterError( + "Key Pair constructor", + "public key is not a byte buffer, use ByteBuffer or Uint8Array"); + return; + } + if (node::Buffer::Length(args[1]) == 0) { + ThrowParameterError("Key Pair constructor", "public key is empty"); + return; + } + std::vector private_key( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector public_key( + (uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); + KeyPair* obj = new KeyPair(private_key, public_key); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); + } else if (args.Length() == 0) { + themis_status_t status = THEMIS_FAIL; + size_t private_key_length, public_key_length; + status = themis_gen_ec_key_pair(NULL, &private_key_length, NULL, &public_key_length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Key Pair generation failed", status); + args.GetReturnValue().SetUndefined(); + } + std::vector prk(private_key_length); + std::vector puk(public_key_length); + status = + themis_gen_ec_key_pair(&prk[0], &private_key_length, &puk[0], &public_key_length); + if (status != THEMIS_SUCCESS) { + ThrowError("Key Pair generation failed", status); + args.GetReturnValue().SetUndefined(); + } + KeyPair* obj = new KeyPair(prk, puk); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); + } else { + ThrowParameterError( + "Key Pair constructor", + "invalid argument count, expected no arguments or private and public keys"); + args.GetReturnValue().SetUndefined(); } - std::vector private_key((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - std::vector public_key((uint8_t*)(node::Buffer::Data(args[1])), (uint8_t*)(node::Buffer::Data(args[1])+node::Buffer::Length(args[1]))); - KeyPair* obj = new KeyPair(private_key, public_key); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); - }else if(args.Length()==0){ - themis_status_t status = THEMIS_FAIL; - size_t private_key_length, public_key_length; - status=themis_gen_ec_key_pair(NULL, &private_key_length, NULL, &public_key_length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Key Pair generation failed", status); - args.GetReturnValue().SetUndefined(); - } - std::vector prk(private_key_length); - std::vector puk(public_key_length); - status=themis_gen_ec_key_pair(&prk[0], &private_key_length, &puk[0], &public_key_length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Key Pair generation failed", status); - args.GetReturnValue().SetUndefined(); - } - KeyPair* obj = new KeyPair(prk, puk); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); - } else { - ThrowParameterError("Key Pair constructor", "invalid argument count, expected no arguments or private and public keys"); - args.GetReturnValue().SetUndefined(); - } } else { - const int argc = 2; - v8::Local argv[argc] = { args[0], args[1] }; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + const int argc = 2; + v8::Local argv[argc] = {args[0], args[1]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } +} - void KeyPair::private_key(const Nan::FunctionCallbackInfo& args) { +void KeyPair::private_key(const Nan::FunctionCallbackInfo& args) +{ KeyPair* obj = Nan::ObjectWrap::Unwrap(args.This()); - args.GetReturnValue().Set(Nan::CopyBuffer((char*)(&(obj->private_key_)[0]), obj->private_key_.size()).ToLocalChecked()); - } + args.GetReturnValue().Set( + Nan::CopyBuffer((char*)(&(obj->private_key_)[0]), obj->private_key_.size()) + .ToLocalChecked()); +} - void KeyPair::public_key(const Nan::FunctionCallbackInfo& args){ +void KeyPair::public_key(const Nan::FunctionCallbackInfo& args) +{ KeyPair* obj = Nan::ObjectWrap::Unwrap(args.This()); - args.GetReturnValue().Set(Nan::CopyBuffer((char*)(&(obj->public_key_)[0]), obj->public_key_.size()).ToLocalChecked()); - } - - themis_status_t ValidateKey(const std::vector& key){ - if(key.empty()){ - return false; + args.GetReturnValue().Set( + Nan::CopyBuffer((char*)(&(obj->public_key_)[0]), obj->public_key_.size()).ToLocalChecked()); +} + +themis_status_t ValidateKey(const std::vector& key) +{ + if (key.empty()) { + return false; } return themis_is_valid_asym_key(&key[0], key.size()); - } - - bool IsValidKey(const std::vector& key){ - return ValidateKey(key)==THEMIS_SUCCESS; - } - - bool IsPrivateKey(const std::vector& key){ - if(!key.empty()){ - themis_key_kind_t kind=themis_get_asym_key_kind(&key[0], key.size()); - switch(kind){ - case THEMIS_KEY_EC_PRIVATE: - case THEMIS_KEY_RSA_PRIVATE: - return true; - default: - break; - } +} + +bool IsValidKey(const std::vector& key) +{ + return ValidateKey(key) == THEMIS_SUCCESS; +} + +bool IsPrivateKey(const std::vector& key) +{ + if (!key.empty()) { + themis_key_kind_t kind = themis_get_asym_key_kind(&key[0], key.size()); + switch (kind) { + case THEMIS_KEY_EC_PRIVATE: + case THEMIS_KEY_RSA_PRIVATE: + return true; + default: + break; + } } return false; - } - - bool IsPublicKey(const std::vector& key){ - if(!key.empty()){ - themis_key_kind_t kind=themis_get_asym_key_kind(&key[0], key.size()); - switch(kind){ - case THEMIS_KEY_EC_PUBLIC: - case THEMIS_KEY_RSA_PUBLIC: - return true; - default: - break; - } +} + +bool IsPublicKey(const std::vector& key) +{ + if (!key.empty()) { + themis_key_kind_t kind = themis_get_asym_key_kind(&key[0], key.size()); + switch (kind) { + case THEMIS_KEY_EC_PUBLIC: + case THEMIS_KEY_RSA_PUBLIC: + return true; + default: + break; + } } return false; - } +} -} //end jsthemis +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_keygen.hpp b/src/wrappers/themis/jsthemis/secure_keygen.hpp index 7156fa16e..a28c618df 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.hpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.hpp @@ -23,14 +23,17 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class KeyPair : public Nan::ObjectWrap { - public: +class KeyPair : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: - explicit KeyPair(const std::vector& private_key, const std::vector& public_key); +private: + explicit KeyPair(const std::vector& private_key, + const std::vector& public_key); explicit KeyPair(); ~KeyPair(); @@ -42,12 +45,12 @@ namespace jsthemis{ std::vector private_key_; std::vector public_key_; - }; +}; - themis_status_t ValidateKey(const std::vector& key); - bool IsValidKey(const std::vector& key); - bool IsPrivateKey(const std::vector& key); - bool IsPublicKey(const std::vector& key); +themis_status_t ValidateKey(const std::vector& key); +bool IsValidKey(const std::vector& key); +bool IsPrivateKey(const std::vector& key); +bool IsPublicKey(const std::vector& key); -} +} // namespace jsthemis #endif /* JSTHEMIS_KEY_PAIR_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_message.cpp b/src/wrappers/themis/jsthemis/secure_message.cpp index 39f262cec..29d5a4804 100644 --- a/src/wrappers/themis/jsthemis/secure_message.cpp +++ b/src/wrappers/themis/jsthemis/secure_message.cpp @@ -23,17 +23,24 @@ #include "errors.hpp" #include "secure_keygen.hpp" -namespace jsthemis { +namespace jsthemis +{ - Nan::Persistent SecureMessage::constructor; +Nan::Persistent SecureMessage::constructor; - SecureMessage::SecureMessage(const std::vector& private_key, const std::vector& peer_public_key) : - private_key_(private_key), - peer_public_key_(peer_public_key){} +SecureMessage::SecureMessage(const std::vector& private_key, + const std::vector& peer_public_key) + : private_key_(private_key) + , peer_public_key_(peer_public_key) +{ +} - SecureMessage::~SecureMessage() {} +SecureMessage::~SecureMessage() +{ +} - void SecureMessage::Init(v8::Handle exports) { +void SecureMessage::Init(v8::Handle exports) +{ // Prepare constructor template v8::Local tpl = Nan::New(SecureMessage::New); tpl->SetClassName(Nan::New("SecureMessage").ToLocalChecked()); @@ -45,238 +52,287 @@ namespace jsthemis { Nan::SetPrototypeMethod(tpl, "verify", SecureMessage::verify); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureMessage").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureMessage::New(const Nan::FunctionCallbackInfo& args) { +void SecureMessage::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<2){ - ThrowParameterError("Secure Message constructor", "not enough arguments, expected private and public key"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Message constructor", "private key is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Message constructor", "public key is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - std::vector private_key((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - std::vector public_key((uint8_t*)(node::Buffer::Data(args[1])), (uint8_t*)(node::Buffer::Data(args[1])+node::Buffer::Length(args[1]))); - if(!ValidateKeys(private_key, public_key)){ - args.GetReturnValue().SetUndefined(); + if (args.Length() < 2) { + ThrowParameterError("Secure Message constructor", + "not enough arguments, expected private and public key"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Message constructor", + "private key is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Message constructor", + "public key is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + std::vector private_key( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector public_key( + (uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); + if (!ValidateKeys(private_key, public_key)) { + args.GetReturnValue().SetUndefined(); + return; + } + SecureMessage* obj = new SecureMessage(private_key, public_key); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); return; - } - SecureMessage* obj = new SecureMessage(private_key, public_key); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); - return; - } else { - const int argc = 2; - v8::Local argv[argc] = { args[0], args[1] }; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } + const int argc = 2; + v8::Local argv[argc] = {args[0], args[1]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); +} - bool SecureMessage::ValidateKeys(const std::vector& private_key, const std::vector& public_key) { - if(!private_key.empty()){ - if(!IsValidKey(private_key)){ - ThrowParameterError("Secure Message constructor", "invalid private key"); - return false; - } - if(!IsPrivateKey(private_key)){ - ThrowParameterError("Secure Message constructor", "using public key instead of private key"); - return false; - } +bool SecureMessage::ValidateKeys(const std::vector& private_key, + const std::vector& public_key) +{ + if (!private_key.empty()) { + if (!IsValidKey(private_key)) { + ThrowParameterError("Secure Message constructor", "invalid private key"); + return false; + } + if (!IsPrivateKey(private_key)) { + ThrowParameterError("Secure Message constructor", + "using public key instead of private key"); + return false; + } } - if(!public_key.empty()){ - if(!IsValidKey(public_key)){ - ThrowParameterError("Secure Message constructor", "invalid public key"); - return false; - } - if(!IsPublicKey(public_key)){ - ThrowParameterError("Secure Message constructor", "using private key instead of public key"); - return false; - } + if (!public_key.empty()) { + if (!IsValidKey(public_key)) { + ThrowParameterError("Secure Message constructor", "invalid public key"); + return false; + } + if (!IsPublicKey(public_key)) { + ThrowParameterError("Secure Message constructor", + "using private key instead of public key"); + return false; + } } return true; - } +} - void SecureMessage::encrypt(const Nan::FunctionCallbackInfo& args) { +void SecureMessage::encrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureMessage* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(obj->private_key_.empty()){ - ThrowParameterError("Secure Message failed to encrypt message", "private key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->private_key_.empty()) { + ThrowParameterError("Secure Message failed to encrypt message", "private key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(obj->peer_public_key_.empty()){ - ThrowParameterError("Secure Message failed to encrypt message", "public key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->peer_public_key_.empty()) { + ThrowParameterError("Secure Message failed to encrypt message", "public key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(args.Length()<1){ - ThrowParameterError("Secure Message failed to encrypt message", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Message failed to encrypt message", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Message failed to encrypt message", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Message failed to encrypt message", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Message failed to encrypt message", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Message failed to encrypt message", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t encrypted_length=0; - status=themis_secure_message_encrypt(&(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &encrypted_length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Message failed to encrypt message", status); - args.GetReturnValue().SetUndefined(); - return; + size_t encrypted_length = 0; + status = themis_secure_message_encrypt( + &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &encrypted_length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Message failed to encrypt message", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* encrypted_data=(uint8_t*)(malloc(encrypted_length)); - status=themis_secure_message_encrypt(&(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), encrypted_data, &encrypted_length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Message failed to encrypt message", status); - free(encrypted_data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* encrypted_data = (uint8_t*)(malloc(encrypted_length)); + status = themis_secure_message_encrypt( + &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), encrypted_data, &encrypted_length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Message failed to encrypt message", status); + free(encrypted_data); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); - } + args.GetReturnValue().Set( + Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); +} - void SecureMessage::decrypt(const Nan::FunctionCallbackInfo& args){ +void SecureMessage::decrypt(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureMessage* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(obj->private_key_.empty()){ - ThrowParameterError("Secure Message failed to decrypt message", "private key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->private_key_.empty()) { + ThrowParameterError("Secure Message failed to decrypt message", "private key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(obj->peer_public_key_.empty()){ - ThrowParameterError("Secure Message failed to decrypt message", "public key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->peer_public_key_.empty()) { + ThrowParameterError("Secure Message failed to decrypt message", "public key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(args.Length()<1){ - ThrowParameterError("Secure Message failed to decrypt message", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Message failed to decrypt message", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Message failed to decrypt message", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Message failed to decrypt message", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Message failed to decrypt message", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Message failed to decrypt message", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t decrypted_length=0; - status=themis_secure_message_decrypt(&(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &decrypted_length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Message failed to decrypt message", status); - args.GetReturnValue().SetUndefined(); - return; + size_t decrypted_length = 0; + status = themis_secure_message_decrypt( + &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &decrypted_length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Message failed to decrypt message", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* decrypted_data=(uint8_t*)(malloc(decrypted_length)); - status=themis_secure_message_decrypt(&(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), decrypted_data, &decrypted_length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Message failed to decrypt message", status); - free(decrypted_data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* decrypted_data = (uint8_t*)(malloc(decrypted_length)); + status = themis_secure_message_decrypt( + &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), decrypted_data, &decrypted_length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Message failed to decrypt message", status); + free(decrypted_data); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); - } - - void SecureMessage::sign(const Nan::FunctionCallbackInfo& args){ + args.GetReturnValue().Set( + Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); +} + +void SecureMessage::sign(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureMessage* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(obj->peer_public_key_.empty()){ - ThrowParameterError("Secure Message failed to sign message", "public key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->peer_public_key_.empty()) { + ThrowParameterError("Secure Message failed to sign message", "public key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(args.Length()<1){ - ThrowParameterError("Secure Message failed to sign message", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Message failed to sign message", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Message failed to sign message", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Message failed to sign message", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Message failed to sign message", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Message failed to sign message", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t encrypted_length=0; - status=themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &encrypted_length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Message failed to sign message", status); - args.GetReturnValue().SetUndefined(); - return; + size_t encrypted_length = 0; + status = themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &encrypted_length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Message failed to sign message", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* encrypted_data=(uint8_t*)(malloc(encrypted_length)); - status=themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), encrypted_data, &encrypted_length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Message failed to sign message", status); - free(encrypted_data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* encrypted_data = (uint8_t*)(malloc(encrypted_length)); + status = themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), encrypted_data, + &encrypted_length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Message failed to sign message", status); + free(encrypted_data); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); - } + args.GetReturnValue().Set( + Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); +} - void SecureMessage::verify(const Nan::FunctionCallbackInfo& args){ +void SecureMessage::verify(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureMessage* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(obj->private_key_.empty()){ - ThrowParameterError("Secure Message failed to verify signature", "private key is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (obj->private_key_.empty()) { + ThrowParameterError("Secure Message failed to verify signature", "private key is empty"); + args.GetReturnValue().SetUndefined(); + return; } - if(args.Length()<1){ - ThrowParameterError("Secure Message failed to verify signature", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Message failed to verify signature", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Message failed to verify signature", "message is not byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Message failed to verify signature", + "message is not byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Message failed to verify signature", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Message failed to verify signature", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; } - size_t decrypted_length=0; - status=themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &decrypted_length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowError("Secure Message failed to verify signature", status); - args.GetReturnValue().SetUndefined(); - return; + size_t decrypted_length = 0; + status = themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &decrypted_length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowError("Secure Message failed to verify signature", status); + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* decrypted_data=(uint8_t*)(malloc(decrypted_length)); - status=themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), decrypted_data, &decrypted_length); - if(status!=THEMIS_SUCCESS){ - ThrowError("Secure Message failed to verify signature", status); - free(decrypted_data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* decrypted_data = (uint8_t*)(malloc(decrypted_length)); + status = themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), decrypted_data, + &decrypted_length); + if (status != THEMIS_SUCCESS) { + ThrowError("Secure Message failed to verify signature", status); + free(decrypted_data); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); - } -} //end jsthemis + args.GetReturnValue().Set( + Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); +} +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_message.hpp b/src/wrappers/themis/jsthemis/secure_message.hpp index 57c60835c..c2ac26b62 100644 --- a/src/wrappers/themis/jsthemis/secure_message.hpp +++ b/src/wrappers/themis/jsthemis/secure_message.hpp @@ -21,14 +21,17 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureMessage : public Nan::ObjectWrap { - public: +class SecureMessage : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: - explicit SecureMessage(const std::vector& private_key, const std::vector& peer_public_key); +private: + explicit SecureMessage(const std::vector& private_key, + const std::vector& peer_public_key); ~SecureMessage(); static void New(const Nan::FunctionCallbackInfo& args); @@ -39,11 +42,12 @@ namespace jsthemis{ static Nan::Persistent constructor; - static bool ValidateKeys(const std::vector& private_key, const std::vector& public_key); + static bool ValidateKeys(const std::vector& private_key, + const std::vector& public_key); std::vector private_key_; std::vector peer_public_key_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_MESSAGE_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_session.cpp b/src/wrappers/themis/jsthemis/secure_session.cpp index f4bb9b85c..5a742930f 100644 --- a/src/wrappers/themis/jsthemis/secure_session.cpp +++ b/src/wrappers/themis/jsthemis/secure_session.cpp @@ -24,203 +24,239 @@ #include "errors.hpp" -namespace jsthemis { - - Nan::Persistent SecureSession::constructor; - - int get_public_key_for_id_callback(const void* id, size_t id_length, void* key_buffer, size_t key_buffer_length, void* user_data){ - if(!key_buffer || !key_buffer_length) - return THEMIS_BUFFER_TOO_SMALL; - v8::Local argv[1] = { Nan::CopyBuffer((char*)id, id_length).ToLocalChecked() }; - v8::Local a = Nan::Call(((SecureSession*)(user_data))->id_to_pub_key_callback_, 1, argv).ToLocalChecked(); - if(a->IsUint8Array()){ - v8::Local buffer = a.As(); - if(key_buffer_length < node::Buffer::Length(buffer)){ +namespace jsthemis +{ + +Nan::Persistent SecureSession::constructor; + +int get_public_key_for_id_callback(const void* id, size_t id_length, void* key_buffer, + size_t key_buffer_length, void* user_data) +{ + if (!key_buffer || !key_buffer_length) { return THEMIS_BUFFER_TOO_SMALL; - } - std::memcpy(key_buffer, (const uint8_t*)(node::Buffer::Data(buffer)), node::Buffer::Length(buffer)); - return THEMIS_SUCCESS; - } else if(a->IsNull() || a->IsUndefined()) { - return THEMIS_FAIL; - } else { - return THEMIS_INVALID_PARAMETER; - } - } - - SecureSession::SecureSession(const std::vector& id, const std::vector& private_key, v8::Local get_pub_by_id_callback): - session_(NULL), - id_to_pub_key_callback_(get_pub_by_id_callback){ - callback_.get_public_key_for_id=jsthemis::get_public_key_for_id_callback; - callback_.send_data=NULL; - callback_.receive_data=NULL; - callback_.state_changed=NULL; - callback_.user_data=this; - session_=secure_session_create(&id[0], id.size(), &private_key[0], private_key.size(), &callback_); - } - - SecureSession::~SecureSession() { - if(!session_){ - secure_session_destroy(session_); - session_=NULL; - } - } - - void SecureSession::Init(v8::Handle exports) { + } + v8::Local argv[1] = {Nan::CopyBuffer((char*)id, id_length).ToLocalChecked()}; + v8::Local a = + Nan::Call(((SecureSession*)(user_data))->id_to_pub_key_callback_, 1, argv).ToLocalChecked(); + if (a->IsUint8Array()) { + v8::Local buffer = a.As(); + if (key_buffer_length < node::Buffer::Length(buffer)) { + return THEMIS_BUFFER_TOO_SMALL; + } + std::memcpy(key_buffer, (const uint8_t*)(node::Buffer::Data(buffer)), + node::Buffer::Length(buffer)); + return THEMIS_SUCCESS; + } + if (a->IsNull() || a->IsUndefined()) { + return THEMIS_FAIL; + } + return THEMIS_INVALID_PARAMETER; +} + +SecureSession::SecureSession(const std::vector& id, + const std::vector& private_key, + v8::Local get_pub_by_id_callback) + : session_(NULL) + , id_to_pub_key_callback_(get_pub_by_id_callback) +{ + callback_.get_public_key_for_id = jsthemis::get_public_key_for_id_callback; + callback_.send_data = NULL; + callback_.receive_data = NULL; + callback_.state_changed = NULL; + callback_.user_data = this; + session_ = + secure_session_create(&id[0], id.size(), &private_key[0], private_key.size(), &callback_); +} + +SecureSession::~SecureSession() +{ + if (!session_) { + secure_session_destroy(session_); + session_ = NULL; + } +} + +void SecureSession::Init(v8::Handle exports) +{ // Prepare constructor template v8::Local tpl = Nan::New(New); tpl->SetClassName(Nan::New("SecureSession").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype Nan::SetPrototypeMethod(tpl, "connectRequest", connectRequest); - Nan::SetPrototypeMethod(tpl, "wrap",wrap); + Nan::SetPrototypeMethod(tpl, "wrap", wrap); Nan::SetPrototypeMethod(tpl, "unwrap", unwrap); Nan::SetPrototypeMethod(tpl, "isEstablished", isEstablished); constructor.Reset(tpl->GetFunction()); exports->Set(Nan::New("SecureSession").ToLocalChecked(), tpl->GetFunction()); - } +} - void SecureSession::New(const Nan::FunctionCallbackInfo& args) { +void SecureSession::New(const Nan::FunctionCallbackInfo& args) +{ if (args.IsConstructCall()) { - if(args.Length()<3){ - ThrowParameterError("Secure Session constructor", "not enough arguments, expected client ID, private key, public key callback"); + if (args.Length() < 3) { + ThrowParameterError( + "Secure Session constructor", + "not enough arguments, expected client ID, private key, public key callback"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Session constructor", + "client ID is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Session constructor", "client ID is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[1]->IsUint8Array()) { + ThrowParameterError("Secure Session constructor", + "private key is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[1]) == 0) { + ThrowParameterError("Secure Session constructor", "private key is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + if (!args[2]->IsFunction()) { + ThrowParameterError("Secure Session constructor", + "public key callback is not a function"); + args.GetReturnValue().SetUndefined(); + return; + } + std::vector id( + (uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector private_key( + (uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); + SecureSession* obj = + new SecureSession(id, private_key, v8::Local::Cast(args[2])); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); + } else { + const int argc = 3; + v8::Local argv[argc] = {args[0], args[1], args[2]}; + v8::Local cons = Nan::New(constructor); + args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); + } +} + +void SecureSession::connectRequest(const Nan::FunctionCallbackInfo& args) +{ + themis_status_t status = THEMIS_FAIL; + SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); + size_t length = 0; + status = secure_session_generate_connect_request(obj->session_, NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowSecureSessionError("Secure Session failed to generate connect request", status); args.GetReturnValue().SetUndefined(); return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Session constructor", "client ID is not a byte buffer, use ByteBuffer or Uint8Array"); + } + uint8_t* data = (uint8_t*)(malloc(length)); + status = secure_session_generate_connect_request(obj->session_, data, &length); + if (status != THEMIS_SUCCESS) { + ThrowSecureSessionError("Secure Session failed to generate connect request", status); + free(data); args.GetReturnValue().SetUndefined(); return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Session constructor", "client ID is empty"); + } + args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); +} + +void SecureSession::wrap(const Nan::FunctionCallbackInfo& args) +{ + themis_status_t status = THEMIS_FAIL; + SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); + if (args.Length() < 1) { + ThrowParameterError("Secure Session failed to encrypt", + "not enough arguments, expected message"); args.GetReturnValue().SetUndefined(); return; - } - if(!args[1]->IsUint8Array()){ - ThrowParameterError("Secure Session constructor", "private key is not a byte buffer, use ByteBuffer or Uint8Array"); + } + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Session failed to encrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); args.GetReturnValue().SetUndefined(); return; - } - if(node::Buffer::Length(args[1])==0){ - ThrowParameterError("Secure Session constructor", "private key is empty"); + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Session failed to encrypt", "message is empty"); args.GetReturnValue().SetUndefined(); return; - } - if(!args[2]->IsFunction()){ - ThrowParameterError("Secure Session constructor", "public key callback is not a function"); + } + size_t length = 0; + status = secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + ThrowSecureSessionError("Secure Session failed to encrypt", status); args.GetReturnValue().SetUndefined(); return; - } - std::vector id((uint8_t*)(node::Buffer::Data(args[0])), (uint8_t*)(node::Buffer::Data(args[0])+node::Buffer::Length(args[0]))); - std::vector private_key((uint8_t*)(node::Buffer::Data(args[1])), (uint8_t*)(node::Buffer::Data(args[1])+node::Buffer::Length(args[1]))); - SecureSession* obj = new SecureSession(id, private_key, v8::Local::Cast(args[2])); - obj->Wrap(args.This()); - args.GetReturnValue().Set(args.This()); - } else { - const int argc = 3; - v8::Local argv[argc] = { args[0], args[1], args[2]}; - v8::Local cons = Nan::New(constructor); - args.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } - } - - void SecureSession::connectRequest(const Nan::FunctionCallbackInfo& args) { - themis_status_t status = THEMIS_FAIL; - SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); - size_t length=0; - status=secure_session_generate_connect_request(obj->session_, NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowSecureSessionError("Secure Session failed to generate connect request", status); - args.GetReturnValue().SetUndefined(); - return; - } - uint8_t* data=(uint8_t*)(malloc(length)); - status=secure_session_generate_connect_request(obj->session_, data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowSecureSessionError("Secure Session failed to generate connect request", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)(malloc(length)); + status = secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), data, &length); + if (status != THEMIS_SUCCESS) { + ThrowSecureSessionError("Secure Session failed to encrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } +} - void SecureSession::wrap(const Nan::FunctionCallbackInfo& args){ +void SecureSession::unwrap(const Nan::FunctionCallbackInfo& args) +{ themis_status_t status = THEMIS_FAIL; SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Session failed to encrypt", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Session failed to encrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Session failed to encrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; - } - size_t length=0; - status=secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - ThrowSecureSessionError("Secure Session failed to encrypt", status); - args.GetReturnValue().SetUndefined(); - return; - } - uint8_t* data=(uint8_t*)(malloc(length)); - status=secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), data, &length); - if(status!=THEMIS_SUCCESS){ - ThrowSecureSessionError("Secure Session failed to encrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + if (args.Length() < 1) { + ThrowParameterError("Secure Session failed to decrypt", + "not enough arguments, expected message"); + args.GetReturnValue().SetUndefined(); + return; } - args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } - - void SecureSession::unwrap(const Nan::FunctionCallbackInfo& args){ - themis_status_t status = THEMIS_FAIL; - SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); - if(args.Length()<1){ - ThrowParameterError("Secure Session failed to decrypt", "not enough arguments, expected message"); - args.GetReturnValue().SetUndefined(); - return; - } - if(!args[0]->IsUint8Array()){ - ThrowParameterError("Secure Session failed to decrypt", "message is not a byte buffer, use ByteBuffer or Uint8Array"); - args.GetReturnValue().SetUndefined(); - return; - } - if(node::Buffer::Length(args[0])==0){ - ThrowParameterError("Secure Session failed to decrypt", "message is empty"); - args.GetReturnValue().SetUndefined(); - return; - } - size_t length=0; - status=secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, &length); - if(status!=THEMIS_BUFFER_TOO_SMALL){ - if(status!=THEMIS_SUCCESS) - ThrowSecureSessionError("Secure Session failed to decrypt", status); - args.GetReturnValue().SetUndefined(); - return; + if (!args[0]->IsUint8Array()) { + ThrowParameterError("Secure Session failed to decrypt", + "message is not a byte buffer, use ByteBuffer or Uint8Array"); + args.GetReturnValue().SetUndefined(); + return; + } + if (node::Buffer::Length(args[0]) == 0) { + ThrowParameterError("Secure Session failed to decrypt", "message is empty"); + args.GetReturnValue().SetUndefined(); + return; + } + size_t length = 0; + status = secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), NULL, &length); + if (status != THEMIS_BUFFER_TOO_SMALL) { + if (status != THEMIS_SUCCESS) { + ThrowSecureSessionError("Secure Session failed to decrypt", status); + } + args.GetReturnValue().SetUndefined(); + return; } - uint8_t* data=(uint8_t*)(malloc(length)); - status=secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), data, &length); - if(status!=THEMIS_SUCCESS && status!=THEMIS_SSESSION_SEND_OUTPUT_TO_PEER){ - ThrowSecureSessionError("Secure Session failed to decrypt", status); - free(data); - args.GetReturnValue().SetUndefined(); - return; + uint8_t* data = (uint8_t*)(malloc(length)); + status = secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), data, &length); + if (status != THEMIS_SUCCESS && status != THEMIS_SSESSION_SEND_OUTPUT_TO_PEER) { + ThrowSecureSessionError("Secure Session failed to decrypt", status); + free(data); + args.GetReturnValue().SetUndefined(); + return; } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - } - - void SecureSession::isEstablished(const Nan::FunctionCallbackInfo& args){ +} + +void SecureSession::isEstablished(const Nan::FunctionCallbackInfo& args) +{ SecureSession* obj = Nan::ObjectWrap::Unwrap(args.This()); - args.GetReturnValue().Set(Nan::New(secure_session_is_established(obj->session_))); - } - -} //end jsthemis + args.GetReturnValue().Set(Nan::New(secure_session_is_established(obj->session_))); +} + +} // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_session.hpp b/src/wrappers/themis/jsthemis/secure_session.hpp index dec7e6237..55e0561b1 100644 --- a/src/wrappers/themis/jsthemis/secure_session.hpp +++ b/src/wrappers/themis/jsthemis/secure_session.hpp @@ -23,14 +23,17 @@ #include -namespace jsthemis{ +namespace jsthemis +{ - class SecureSession : public Nan::ObjectWrap { - public: +class SecureSession : public Nan::ObjectWrap +{ +public: static void Init(v8::Handle exports); - private: - explicit SecureSession(const std::vector& id, const std::vector& private_key, v8::Local get_pub_by_id_callback); +private: + explicit SecureSession(const std::vector& id, const std::vector& private_key, + v8::Local get_pub_by_id_callback); ~SecureSession(); static void New(const Nan::FunctionCallbackInfo& args); @@ -41,12 +44,13 @@ namespace jsthemis{ static Nan::Persistent constructor; - private: +private: secure_session_t* session_; secure_session_user_callbacks_t callback_; - public: + +public: Nan::Callback id_to_pub_key_callback_; - }; +}; -} +} // namespace jsthemis #endif /* JSTHEMIS_SECURE_SESSION_HPP_ */ From b1fe49047a0e7aef692b397789f6237a85e971d0 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 16:11:14 +0200 Subject: [PATCH 4/8] Add missing empty lines --- src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp | 1 + src/wrappers/themis/jsthemis/secure_cell_seal.cpp | 1 + src/wrappers/themis/jsthemis/secure_cell_seal.hpp | 1 + src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp | 1 + src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp | 1 + src/wrappers/themis/jsthemis/secure_comparator.hpp | 1 + src/wrappers/themis/jsthemis/secure_keygen.hpp | 1 + src/wrappers/themis/jsthemis/secure_message.cpp | 1 + src/wrappers/themis/jsthemis/secure_message.hpp | 1 + src/wrappers/themis/jsthemis/secure_session.hpp | 1 + 10 files changed, 10 insertions(+) diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp index 1aba2a8ac..064c1893e 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.hpp @@ -43,4 +43,5 @@ class SecureCellContextImprint : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_CELL_CONTEXT_IMPRINT_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp index 525c62231..0abddd275 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp @@ -196,4 +196,5 @@ void SecureCellSeal::decrypt(const Nan::FunctionCallbackInfo& args) } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); } + } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp index fe382297d..aa734f27e 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.hpp @@ -43,4 +43,5 @@ class SecureCellSeal : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_CELL_SEAL_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp index 6e4f2def5..f934f4541 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp @@ -218,4 +218,5 @@ void SecureCellTokenProtect::decrypt(const Nan::FunctionCallbackInfo& } args.GetReturnValue().Set(Nan::NewBuffer((char*)(data), length).ToLocalChecked()); } + } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp index b8f3a9fde..0e9a8a681 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.hpp @@ -43,4 +43,5 @@ class SecureCellTokenProtect : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_CELL_TOKEN_PROTECT_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_comparator.hpp b/src/wrappers/themis/jsthemis/secure_comparator.hpp index ed3346ba9..63b8a42fe 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.hpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.hpp @@ -48,4 +48,5 @@ class SecureComparator : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_Comparator_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_keygen.hpp b/src/wrappers/themis/jsthemis/secure_keygen.hpp index a28c618df..39878fa06 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.hpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.hpp @@ -53,4 +53,5 @@ bool IsPrivateKey(const std::vector& key); bool IsPublicKey(const std::vector& key); } // namespace jsthemis + #endif /* JSTHEMIS_KEY_PAIR_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_message.cpp b/src/wrappers/themis/jsthemis/secure_message.cpp index 29d5a4804..439d3301e 100644 --- a/src/wrappers/themis/jsthemis/secure_message.cpp +++ b/src/wrappers/themis/jsthemis/secure_message.cpp @@ -335,4 +335,5 @@ void SecureMessage::verify(const Nan::FunctionCallbackInfo& args) args.GetReturnValue().Set( Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); } + } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_message.hpp b/src/wrappers/themis/jsthemis/secure_message.hpp index c2ac26b62..b955c3055 100644 --- a/src/wrappers/themis/jsthemis/secure_message.hpp +++ b/src/wrappers/themis/jsthemis/secure_message.hpp @@ -50,4 +50,5 @@ class SecureMessage : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_MESSAGE_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/secure_session.hpp b/src/wrappers/themis/jsthemis/secure_session.hpp index 55e0561b1..7a63c26ee 100644 --- a/src/wrappers/themis/jsthemis/secure_session.hpp +++ b/src/wrappers/themis/jsthemis/secure_session.hpp @@ -53,4 +53,5 @@ class SecureSession : public Nan::ObjectWrap }; } // namespace jsthemis + #endif /* JSTHEMIS_SECURE_SESSION_HPP_ */ From 1eb86585de2133edb0cf3377d2226825458bb653 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 16:36:25 +0200 Subject: [PATCH 5/8] Add NodeJS to format checking image Install npm and nodejs as we need them for checking JsThemis native source code. At least for now. --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index ac1485436..8b678e04f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,6 +28,7 @@ jobs: CLANG_FORMAT: clang-format-7 CLANG_TIDY: clang-tidy-7 steps: + - run: sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs npm - checkout - run: git submodule update --init - run: make fmt_check ENGINE=boringssl From 4c01a70b72d88277a229e91d39a56c693d2f22d8 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 16:41:15 +0200 Subject: [PATCH 6/8] Add a warning if NodeJS is not installed Print out a warning when formatting files if NodeJS is not installed and we are not able to reformat JsThemis files. --- src/wrappers/themis/jsthemis/jsthemis.mk | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wrappers/themis/jsthemis/jsthemis.mk b/src/wrappers/themis/jsthemis/jsthemis.mk index 1259f75ea..39ff2d140 100644 --- a/src/wrappers/themis/jsthemis/jsthemis.mk +++ b/src/wrappers/themis/jsthemis/jsthemis.mk @@ -51,4 +51,15 @@ $(JSTHEMIS_OBJ)/%.hpp.fmt_fixup $(JSTHEMIS_OBJ)/%.cpp.fmt_fixup: \ $(JSTHEMIS_OBJ)/%.hpp.fmt_check $(JSTHEMIS_OBJ)/%.cpp.fmt_check: \ CMD = $(CLANG_FORMAT) $< | diff -u $< - && $(CLANG_TIDY) $< -- $(JSTHEMIS_CFLAGS) 2>/dev/null && touch $@ +else # ifdef NPM_VERSION + +FMT_FIXUP += $(JSTHEMIS_OBJ)/warning_fixup +FMT_CHECK += $(JSTHEMIS_OBJ)/warning_check + +$(JSTHEMIS_OBJ)/warning_fixup: + $(warning NodeJS not installed, JsThemis code will not be formatted) + +$(JSTHEMIS_OBJ)/warning_check: + $(warning NodeJS not installed, JsThemis code will not be checked) + endif # ifdef NPM_VERSION From 037cb34f8b16ef06a1c2804d96e2b4862d6746e7 Mon Sep 17 00:00:00 2001 From: ilammy Date: Tue, 5 Mar 2019 17:13:59 +0200 Subject: [PATCH 7/8] Add symlink to jsthemis.mk for Rust's libthemis-src The current build system for libthemis-src is quite messed up and requires peculiar symlinking to work. Add a symlink for the new file included from the main Makefile. --- .../themis/src/wrappers/themis/jsthemis/jsthemis.mk | 1 + 1 file changed, 1 insertion(+) create mode 120000 src/wrappers/themis/rust/libthemis-src/themis/src/wrappers/themis/jsthemis/jsthemis.mk diff --git a/src/wrappers/themis/rust/libthemis-src/themis/src/wrappers/themis/jsthemis/jsthemis.mk b/src/wrappers/themis/rust/libthemis-src/themis/src/wrappers/themis/jsthemis/jsthemis.mk new file mode 120000 index 000000000..ba52db739 --- /dev/null +++ b/src/wrappers/themis/rust/libthemis-src/themis/src/wrappers/themis/jsthemis/jsthemis.mk @@ -0,0 +1 @@ +../../../../../../../../../../src/wrappers/themis/jsthemis/jsthemis.mk \ No newline at end of file From 61599f256eb7de4c460df0cf0e6b594d94db81b9 Mon Sep 17 00:00:00 2001 From: clang-format Date: Thu, 7 Mar 2019 14:11:18 +0200 Subject: [PATCH 8/8] Reformat code according to the new style --- src/wrappers/themis/jsthemis/common.hpp | 15 +-- src/wrappers/themis/jsthemis/errors.cpp | 6 +- .../jsthemis/secure_cell_context_imprint.cpp | 52 ++++++--- .../themis/jsthemis/secure_cell_seal.cpp | 53 +++++---- .../jsthemis/secure_cell_token_protect.cpp | 72 +++++++----- .../themis/jsthemis/secure_comparator.cpp | 17 +-- .../themis/jsthemis/secure_keygen.cpp | 33 +++--- .../themis/jsthemis/secure_keygen.hpp | 3 +- .../themis/jsthemis/secure_message.cpp | 104 +++++++++++------- .../themis/jsthemis/secure_session.cpp | 60 +++++----- .../themis/jsthemis/secure_session.hpp | 3 +- 11 files changed, 250 insertions(+), 168 deletions(-) diff --git a/src/wrappers/themis/jsthemis/common.hpp b/src/wrappers/themis/jsthemis/common.hpp index e0fe53a10..614d48321 100644 --- a/src/wrappers/themis/jsthemis/common.hpp +++ b/src/wrappers/themis/jsthemis/common.hpp @@ -17,13 +17,14 @@ #ifndef JSTHEMIS_COMMON_HPP_ #define JSTHEMIS_COMMON_HPP_ -#define RETURN_BUFFER(buf, length) \ - v8::Local globalObj = v8::Context::GetCurrent()->Global(); \ - v8::Local bufferConstructor = \ - v8::Local::Cast(globalObj->Get(v8::String::New("Buffer"))); \ - v8::Handle constructorArgs[3] = {buf->handle_, v8::Integer::New(length), \ - v8::Integer::New(0)}; \ - v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); \ +#define RETURN_BUFFER(buf, length) \ + v8::Local globalObj = v8::Context::GetCurrent()->Global(); \ + v8::Local bufferConstructor = v8::Local::Cast( \ + globalObj->Get(v8::String::New("Buffer"))); \ + v8::Handle constructorArgs[3] = {buf->handle_, \ + v8::Integer::New(length), \ + v8::Integer::New(0)}; \ + v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); \ return scope.Close(actualBuffer) #endif /* JSTHEMIS_COMMON_HPP_ */ diff --git a/src/wrappers/themis/jsthemis/errors.cpp b/src/wrappers/themis/jsthemis/errors.cpp index e4edb0c42..17cf5345c 100644 --- a/src/wrappers/themis/jsthemis/errors.cpp +++ b/src/wrappers/themis/jsthemis/errors.cpp @@ -24,8 +24,7 @@ namespace jsthemis { -static inline void ExportStatusCode(v8::Handle& exports, const char* name, - themis_status_t status) +static inline void ExportStatusCode(v8::Handle& exports, const char* name, themis_status_t status) { exports->Set(Nan::New(name).ToLocalChecked(), Nan::New(status)); } @@ -42,7 +41,8 @@ void Errors::Init(v8::Handle exports) ExportStatusCode(exports, "NOT_SUPPORTED", THEMIS_NOT_SUPPORTED); ExportStatusCode(exports, "SSESSION_KA_NOT_FINISHED", THEMIS_SSESSION_KA_NOT_FINISHED); ExportStatusCode(exports, "SSESSION_TRANSPORT_ERROR", THEMIS_SSESSION_TRANSPORT_ERROR); - ExportStatusCode(exports, "SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR", + ExportStatusCode(exports, + "SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR", THEMIS_SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR); ExportStatusCode(exports, "SCOMPARE_NOT_READY", THEMIS_SCOMPARE_NOT_READY); } diff --git a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp index 0041c341f..ff46976d5 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_context_imprint.cpp @@ -39,8 +39,7 @@ SecureCellContextImprint::~SecureCellContextImprint() void SecureCellContextImprint::Init(v8::Handle exports) { // Prepare constructor template - v8::Local tpl = - Nan::New(SecureCellContextImprint::New); + v8::Local tpl = Nan::New(SecureCellContextImprint::New); tpl->SetClassName(Nan::New("SecureCellContextImprint").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype @@ -70,9 +69,8 @@ void SecureCellContextImprint::New(const Nan::FunctionCallbackInfo& a args.GetReturnValue().SetUndefined(); return; } - std::vector key( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector key((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); SecureCellContextImprint* obj = new SecureCellContextImprint(key); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); @@ -119,18 +117,28 @@ void SecureCellContextImprint::encrypt(const Nan::FunctionCallbackInfokey_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), context, context_length, NULL, &length); + status = themis_secure_cell_encrypt_context_imprint(&(obj->key_)[0], + obj->key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + context, + context_length, + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = themis_secure_cell_encrypt_context_imprint( - &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), context, context_length, data, &length); + status = themis_secure_cell_encrypt_context_imprint(&(obj->key_)[0], + obj->key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + context, + context_length, + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Context Imprint) failed to encrypt", status); free(data); @@ -175,18 +183,28 @@ void SecureCellContextImprint::decrypt(const Nan::FunctionCallbackInfokey_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), context, context_length, NULL, &length); + status = themis_secure_cell_decrypt_context_imprint(&(obj->key_)[0], + obj->key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + context, + context_length, + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = themis_secure_cell_decrypt_context_imprint( - &(obj->key_)[0], obj->key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), context, context_length, data, &length); + status = themis_secure_cell_decrypt_context_imprint(&(obj->key_)[0], + obj->key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + context, + context_length, + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Context Imprint) failed to decrypt", status); free(data); diff --git a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp index 0abddd275..9b0850d23 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_seal.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_seal.cpp @@ -69,9 +69,8 @@ void SecureCellSeal::New(const Nan::FunctionCallbackInfo& args) args.GetReturnValue().SetUndefined(); return; } - std::vector key( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector key((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); SecureCellSeal* obj = new SecureCellSeal(key); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); @@ -117,20 +116,28 @@ void SecureCellSeal::encrypt(const Nan::FunctionCallbackInfo& args) context = (const uint8_t*)(node::Buffer::Data(args[1])); context_length = node::Buffer::Length(args[1]); } - status = - themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &length); + status = themis_secure_cell_encrypt_seal(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Seal) failed to encrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = - themis_secure_cell_encrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), data, &length); + status = themis_secure_cell_encrypt_seal(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Seal) failed to encrypt", status); free(data); @@ -174,20 +181,28 @@ void SecureCellSeal::decrypt(const Nan::FunctionCallbackInfo& args) context = (const uint8_t*)(node::Buffer::Data(args[1])); context_length = node::Buffer::Length(args[1]); } - status = - themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &length); + status = themis_secure_cell_decrypt_seal(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Seal) failed to decrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)malloc(length); - status = - themis_secure_cell_decrypt_seal(&(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), data, &length); + status = themis_secure_cell_decrypt_seal(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Seal) failed to decrypt", status); free(data); diff --git a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp index f934f4541..2bd396974 100644 --- a/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp +++ b/src/wrappers/themis/jsthemis/secure_cell_token_protect.cpp @@ -39,8 +39,7 @@ SecureCellTokenProtect::~SecureCellTokenProtect() void SecureCellTokenProtect::Init(v8::Handle exports) { // Prepare constructor template - v8::Local tpl = - Nan::New(SecureCellTokenProtect::New); + v8::Local tpl = Nan::New(SecureCellTokenProtect::New); tpl->SetClassName(Nan::New("SecureCellTokenProtect").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype @@ -70,9 +69,8 @@ void SecureCellTokenProtect::New(const Nan::FunctionCallbackInfo& arg args.GetReturnValue().SetUndefined(); return; } - std::vector key( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector key((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); SecureCellTokenProtect* obj = new SecureCellTokenProtect(key); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); @@ -119,10 +117,16 @@ void SecureCellTokenProtect::encrypt(const Nan::FunctionCallbackInfo& context = (const uint8_t*)(node::Buffer::Data(args[1])); context_length = node::Buffer::Length(args[1]); } - status = themis_secure_cell_encrypt_token_protect( - &(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), NULL, - &token_length, NULL, &length); + status = themis_secure_cell_encrypt_token_protect(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &token_length, + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Token Protect) failed to encrypt", status); args.GetReturnValue().SetUndefined(); @@ -130,10 +134,16 @@ void SecureCellTokenProtect::encrypt(const Nan::FunctionCallbackInfo& } uint8_t* data = (uint8_t*)(malloc(length)); uint8_t* token = (uint8_t*)(malloc(token_length)); - status = themis_secure_cell_encrypt_token_protect( - &(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), token, - &token_length, data, &length); + status = themis_secure_cell_encrypt_token_protect(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + token, + &token_length, + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Token Protect) failed to encrypt", status); free(data); @@ -142,9 +152,11 @@ void SecureCellTokenProtect::encrypt(const Nan::FunctionCallbackInfo& return; } v8::Local retobj = Nan::New(); - Nan::Set(retobj, Nan::New("data").ToLocalChecked(), + Nan::Set(retobj, + Nan::New("data").ToLocalChecked(), Nan::NewBuffer((char*)(data), length).ToLocalChecked()); - Nan::Set(retobj, Nan::New("token").ToLocalChecked(), + Nan::Set(retobj, + Nan::New("token").ToLocalChecked(), Nan::NewBuffer((char*)(token), token_length).ToLocalChecked()); args.GetReturnValue().Set(retobj); } @@ -194,22 +206,32 @@ void SecureCellTokenProtect::decrypt(const Nan::FunctionCallbackInfo& context = (const uint8_t*)(node::Buffer::Data(args[2])); context_length = node::Buffer::Length(args[2]); } - status = themis_secure_cell_decrypt_token_protect( - &(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), - (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), NULL, - &length); + status = themis_secure_cell_decrypt_token_protect(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + (const uint8_t*)(node::Buffer::Data(args[1])), + node::Buffer::Length(args[1]), + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Cell (Token Protect) failed to decrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = themis_secure_cell_decrypt_token_protect( - &(obj->key_)[0], obj->key_.size(), context, context_length, - (const uint8_t*)(node::Buffer::Data(args[0])), node::Buffer::Length(args[0]), - (const uint8_t*)(node::Buffer::Data(args[1])), node::Buffer::Length(args[1]), data, - &length); + status = themis_secure_cell_decrypt_token_protect(&(obj->key_)[0], + obj->key_.size(), + context, + context_length, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + (const uint8_t*)(node::Buffer::Data(args[1])), + node::Buffer::Length(args[1]), + data, + &length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Cell (Token Protect) failed to decrypt", status); free(data); diff --git a/src/wrappers/themis/jsthemis/secure_comparator.cpp b/src/wrappers/themis/jsthemis/secure_comparator.cpp index c3fefc4ae..a35bed8c8 100644 --- a/src/wrappers/themis/jsthemis/secure_comparator.cpp +++ b/src/wrappers/themis/jsthemis/secure_comparator.cpp @@ -37,8 +37,7 @@ SecureComparator::SecureComparator(const std::vector& secret) ThrowError("Secure Comparator constructor", THEMIS_FAIL); return; } - themis_status_t status = - secure_comparator_append_secret(comparator_, &secret[0], secret.size()); + themis_status_t status = secure_comparator_append_secret(comparator_, &secret[0], secret.size()); if (THEMIS_SUCCESS != status) { ThrowError("Secure Comparator failed to append secret", status); return; @@ -85,9 +84,9 @@ void SecureComparator::New(const Nan::FunctionCallbackInfo& args) ThrowParameterError("Secure Comparator constructor", "secret is empty"); return; } - std::vector secret( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector secret((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + + node::Buffer::Length(args[0]))); SecureComparator* obj = new SecureComparator(secret); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); @@ -142,7 +141,9 @@ void SecureComparator::proceedCompare(const Nan::FunctionCallbackInfo size_t length = 0; status = secure_comparator_proceed_compare(obj->comparator_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &length); + node::Buffer::Length(args[0]), + NULL, + &length); if (THEMIS_BUFFER_TOO_SMALL != status) { ThrowSecureComparatorError("Secure Comparator failed to proceed comparison", status); args.GetReturnValue().SetUndefined(); @@ -151,7 +152,9 @@ void SecureComparator::proceedCompare(const Nan::FunctionCallbackInfo uint8_t* data = (uint8_t*)(malloc(length)); status = secure_comparator_proceed_compare(obj->comparator_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), data, &length); + node::Buffer::Length(args[0]), + data, + &length); if (THEMIS_SCOMPARE_SEND_OUTPUT_TO_PEER != status) { if (THEMIS_SUCCESS != status) { ThrowSecureComparatorError("Secure Comparator failed to proceed comparison", status); diff --git a/src/wrappers/themis/jsthemis/secure_keygen.cpp b/src/wrappers/themis/jsthemis/secure_keygen.cpp index 0b510e6c4..9697a309c 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.cpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.cpp @@ -56,9 +56,8 @@ void KeyPair::New(const Nan::FunctionCallbackInfo& args) if (args.IsConstructCall()) { if (args.Length() == 2) { if (!args[0]->IsUint8Array()) { - ThrowParameterError( - "Key Pair constructor", - "private key is not a byte buffer, use ByteBuffer or Uint8Array"); + ThrowParameterError("Key Pair constructor", + "private key is not a byte buffer, use ByteBuffer or Uint8Array"); return; } if (node::Buffer::Length(args[0]) == 0) { @@ -66,21 +65,20 @@ void KeyPair::New(const Nan::FunctionCallbackInfo& args) return; } if (!args[1]->IsUint8Array()) { - ThrowParameterError( - "Key Pair constructor", - "public key is not a byte buffer, use ByteBuffer or Uint8Array"); + ThrowParameterError("Key Pair constructor", + "public key is not a byte buffer, use ByteBuffer or Uint8Array"); return; } if (node::Buffer::Length(args[1]) == 0) { ThrowParameterError("Key Pair constructor", "public key is empty"); return; } - std::vector private_key( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); - std::vector public_key( - (uint8_t*)(node::Buffer::Data(args[1])), - (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); + std::vector private_key((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + + node::Buffer::Length(args[0]))); + std::vector public_key((uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + + node::Buffer::Length(args[1]))); KeyPair* obj = new KeyPair(private_key, public_key); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); @@ -94,8 +92,7 @@ void KeyPair::New(const Nan::FunctionCallbackInfo& args) } std::vector prk(private_key_length); std::vector puk(public_key_length); - status = - themis_gen_ec_key_pair(&prk[0], &private_key_length, &puk[0], &public_key_length); + status = themis_gen_ec_key_pair(&prk[0], &private_key_length, &puk[0], &public_key_length); if (status != THEMIS_SUCCESS) { ThrowError("Key Pair generation failed", status); args.GetReturnValue().SetUndefined(); @@ -104,9 +101,8 @@ void KeyPair::New(const Nan::FunctionCallbackInfo& args) obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { - ThrowParameterError( - "Key Pair constructor", - "invalid argument count, expected no arguments or private and public keys"); + ThrowParameterError("Key Pair constructor", + "invalid argument count, expected no arguments or private and public keys"); args.GetReturnValue().SetUndefined(); } } else { @@ -121,8 +117,7 @@ void KeyPair::private_key(const Nan::FunctionCallbackInfo& args) { KeyPair* obj = Nan::ObjectWrap::Unwrap(args.This()); args.GetReturnValue().Set( - Nan::CopyBuffer((char*)(&(obj->private_key_)[0]), obj->private_key_.size()) - .ToLocalChecked()); + Nan::CopyBuffer((char*)(&(obj->private_key_)[0]), obj->private_key_.size()).ToLocalChecked()); } void KeyPair::public_key(const Nan::FunctionCallbackInfo& args) diff --git a/src/wrappers/themis/jsthemis/secure_keygen.hpp b/src/wrappers/themis/jsthemis/secure_keygen.hpp index 39878fa06..a7633adc2 100644 --- a/src/wrappers/themis/jsthemis/secure_keygen.hpp +++ b/src/wrappers/themis/jsthemis/secure_keygen.hpp @@ -32,8 +32,7 @@ class KeyPair : public Nan::ObjectWrap static void Init(v8::Handle exports); private: - explicit KeyPair(const std::vector& private_key, - const std::vector& public_key); + explicit KeyPair(const std::vector& private_key, const std::vector& public_key); explicit KeyPair(); ~KeyPair(); diff --git a/src/wrappers/themis/jsthemis/secure_message.cpp b/src/wrappers/themis/jsthemis/secure_message.cpp index 439d3301e..0023ee767 100644 --- a/src/wrappers/themis/jsthemis/secure_message.cpp +++ b/src/wrappers/themis/jsthemis/secure_message.cpp @@ -75,12 +75,12 @@ void SecureMessage::New(const Nan::FunctionCallbackInfo& args) args.GetReturnValue().SetUndefined(); return; } - std::vector private_key( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); - std::vector public_key( - (uint8_t*)(node::Buffer::Data(args[1])), - (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); + std::vector private_key((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + + node::Buffer::Length(args[0]))); + std::vector public_key((uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + + node::Buffer::Length(args[1]))); if (!ValidateKeys(private_key, public_key)) { args.GetReturnValue().SetUndefined(); return; @@ -105,8 +105,7 @@ bool SecureMessage::ValidateKeys(const std::vector& private_key, return false; } if (!IsPrivateKey(private_key)) { - ThrowParameterError("Secure Message constructor", - "using public key instead of private key"); + ThrowParameterError("Secure Message constructor", "using public key instead of private key"); return false; } } @@ -116,8 +115,7 @@ bool SecureMessage::ValidateKeys(const std::vector& private_key, return false; } if (!IsPublicKey(public_key)) { - ThrowParameterError("Secure Message constructor", - "using private key instead of public key"); + ThrowParameterError("Secure Message constructor", "using private key instead of public key"); return false; } } @@ -156,28 +154,35 @@ void SecureMessage::encrypt(const Nan::FunctionCallbackInfo& args) return; } size_t encrypted_length = 0; - status = themis_secure_message_encrypt( - &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], - obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &encrypted_length); + status = themis_secure_message_encrypt(&(obj->private_key_)[0], + obj->private_key_.size(), + &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &encrypted_length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Message failed to encrypt message", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* encrypted_data = (uint8_t*)(malloc(encrypted_length)); - status = themis_secure_message_encrypt( - &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], - obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), encrypted_data, &encrypted_length); + status = themis_secure_message_encrypt(&(obj->private_key_)[0], + obj->private_key_.size(), + &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + encrypted_data, + &encrypted_length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Message failed to encrypt message", status); free(encrypted_data); args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( - Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); + args.GetReturnValue().Set(Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); } void SecureMessage::decrypt(const Nan::FunctionCallbackInfo& args) @@ -212,28 +217,35 @@ void SecureMessage::decrypt(const Nan::FunctionCallbackInfo& args) return; } size_t decrypted_length = 0; - status = themis_secure_message_decrypt( - &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], - obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &decrypted_length); + status = themis_secure_message_decrypt(&(obj->private_key_)[0], + obj->private_key_.size(), + &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &decrypted_length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Message failed to decrypt message", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* decrypted_data = (uint8_t*)(malloc(decrypted_length)); - status = themis_secure_message_decrypt( - &(obj->private_key_)[0], obj->private_key_.size(), &(obj->peer_public_key_)[0], - obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), decrypted_data, &decrypted_length); + status = themis_secure_message_decrypt(&(obj->private_key_)[0], + obj->private_key_.size(), + &(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + decrypted_data, + &decrypted_length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Message failed to decrypt message", status); free(decrypted_data); args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( - Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); + args.GetReturnValue().Set(Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); } void SecureMessage::sign(const Nan::FunctionCallbackInfo& args) @@ -263,18 +275,23 @@ void SecureMessage::sign(const Nan::FunctionCallbackInfo& args) return; } size_t encrypted_length = 0; - status = themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), + status = themis_secure_message_sign(&(obj->private_key_)[0], + obj->private_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &encrypted_length); + node::Buffer::Length(args[0]), + NULL, + &encrypted_length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Message failed to sign message", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* encrypted_data = (uint8_t*)(malloc(encrypted_length)); - status = themis_secure_message_sign(&(obj->private_key_)[0], obj->private_key_.size(), + status = themis_secure_message_sign(&(obj->private_key_)[0], + obj->private_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), encrypted_data, + node::Buffer::Length(args[0]), + encrypted_data, &encrypted_length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Message failed to sign message", status); @@ -282,8 +299,7 @@ void SecureMessage::sign(const Nan::FunctionCallbackInfo& args) args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( - Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); + args.GetReturnValue().Set(Nan::NewBuffer((char*)encrypted_data, encrypted_length).ToLocalChecked()); } void SecureMessage::verify(const Nan::FunctionCallbackInfo& args) @@ -313,18 +329,23 @@ void SecureMessage::verify(const Nan::FunctionCallbackInfo& args) return; } size_t decrypted_length = 0; - status = themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), + status = themis_secure_message_verify(&(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &decrypted_length); + node::Buffer::Length(args[0]), + NULL, + &decrypted_length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowError("Secure Message failed to verify signature", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* decrypted_data = (uint8_t*)(malloc(decrypted_length)); - status = themis_secure_message_verify(&(obj->peer_public_key_)[0], obj->peer_public_key_.size(), + status = themis_secure_message_verify(&(obj->peer_public_key_)[0], + obj->peer_public_key_.size(), (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), decrypted_data, + node::Buffer::Length(args[0]), + decrypted_data, &decrypted_length); if (status != THEMIS_SUCCESS) { ThrowError("Secure Message failed to verify signature", status); @@ -332,8 +353,7 @@ void SecureMessage::verify(const Nan::FunctionCallbackInfo& args) args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( - Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); + args.GetReturnValue().Set(Nan::NewBuffer((char*)decrypted_data, decrypted_length).ToLocalChecked()); } } // namespace jsthemis diff --git a/src/wrappers/themis/jsthemis/secure_session.cpp b/src/wrappers/themis/jsthemis/secure_session.cpp index 5a742930f..059da26f4 100644 --- a/src/wrappers/themis/jsthemis/secure_session.cpp +++ b/src/wrappers/themis/jsthemis/secure_session.cpp @@ -29,8 +29,8 @@ namespace jsthemis Nan::Persistent SecureSession::constructor; -int get_public_key_for_id_callback(const void* id, size_t id_length, void* key_buffer, - size_t key_buffer_length, void* user_data) +int get_public_key_for_id_callback( + const void* id, size_t id_length, void* key_buffer, size_t key_buffer_length, void* user_data) { if (!key_buffer || !key_buffer_length) { return THEMIS_BUFFER_TOO_SMALL; @@ -43,7 +43,8 @@ int get_public_key_for_id_callback(const void* id, size_t id_length, void* key_b if (key_buffer_length < node::Buffer::Length(buffer)) { return THEMIS_BUFFER_TOO_SMALL; } - std::memcpy(key_buffer, (const uint8_t*)(node::Buffer::Data(buffer)), + std::memcpy(key_buffer, + (const uint8_t*)(node::Buffer::Data(buffer)), node::Buffer::Length(buffer)); return THEMIS_SUCCESS; } @@ -64,8 +65,7 @@ SecureSession::SecureSession(const std::vector& id, callback_.receive_data = NULL; callback_.state_changed = NULL; callback_.user_data = this; - session_ = - secure_session_create(&id[0], id.size(), &private_key[0], private_key.size(), &callback_); + session_ = secure_session_create(&id[0], id.size(), &private_key[0], private_key.size(), &callback_); } SecureSession::~SecureSession() @@ -95,9 +95,8 @@ void SecureSession::New(const Nan::FunctionCallbackInfo& args) { if (args.IsConstructCall()) { if (args.Length() < 3) { - ThrowParameterError( - "Secure Session constructor", - "not enough arguments, expected client ID, private key, public key callback"); + ThrowParameterError("Secure Session constructor", + "not enough arguments, expected client ID, private key, public key callback"); args.GetReturnValue().SetUndefined(); return; } @@ -124,19 +123,16 @@ void SecureSession::New(const Nan::FunctionCallbackInfo& args) return; } if (!args[2]->IsFunction()) { - ThrowParameterError("Secure Session constructor", - "public key callback is not a function"); + ThrowParameterError("Secure Session constructor", "public key callback is not a function"); args.GetReturnValue().SetUndefined(); return; } - std::vector id( - (uint8_t*)(node::Buffer::Data(args[0])), - (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); - std::vector private_key( - (uint8_t*)(node::Buffer::Data(args[1])), - (uint8_t*)(node::Buffer::Data(args[1]) + node::Buffer::Length(args[1]))); - SecureSession* obj = - new SecureSession(id, private_key, v8::Local::Cast(args[2])); + std::vector id((uint8_t*)(node::Buffer::Data(args[0])), + (uint8_t*)(node::Buffer::Data(args[0]) + node::Buffer::Length(args[0]))); + std::vector private_key((uint8_t*)(node::Buffer::Data(args[1])), + (uint8_t*)(node::Buffer::Data(args[1]) + + node::Buffer::Length(args[1]))); + SecureSession* obj = new SecureSession(id, private_key, v8::Local::Cast(args[2])); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { @@ -191,16 +187,22 @@ void SecureSession::wrap(const Nan::FunctionCallbackInfo& args) return; } size_t length = 0; - status = secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &length); + status = secure_session_wrap(obj->session_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { ThrowSecureSessionError("Secure Session failed to encrypt", status); args.GetReturnValue().SetUndefined(); return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = secure_session_wrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), data, &length); + status = secure_session_wrap(obj->session_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + data, + &length); if (status != THEMIS_SUCCESS) { ThrowSecureSessionError("Secure Session failed to encrypt", status); free(data); @@ -232,8 +234,11 @@ void SecureSession::unwrap(const Nan::FunctionCallbackInfo& args) return; } size_t length = 0; - status = secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), NULL, &length); + status = secure_session_unwrap(obj->session_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + NULL, + &length); if (status != THEMIS_BUFFER_TOO_SMALL) { if (status != THEMIS_SUCCESS) { ThrowSecureSessionError("Secure Session failed to decrypt", status); @@ -242,8 +247,11 @@ void SecureSession::unwrap(const Nan::FunctionCallbackInfo& args) return; } uint8_t* data = (uint8_t*)(malloc(length)); - status = secure_session_unwrap(obj->session_, (const uint8_t*)(node::Buffer::Data(args[0])), - node::Buffer::Length(args[0]), data, &length); + status = secure_session_unwrap(obj->session_, + (const uint8_t*)(node::Buffer::Data(args[0])), + node::Buffer::Length(args[0]), + data, + &length); if (status != THEMIS_SUCCESS && status != THEMIS_SSESSION_SEND_OUTPUT_TO_PEER) { ThrowSecureSessionError("Secure Session failed to decrypt", status); free(data); diff --git a/src/wrappers/themis/jsthemis/secure_session.hpp b/src/wrappers/themis/jsthemis/secure_session.hpp index 7a63c26ee..711e2f195 100644 --- a/src/wrappers/themis/jsthemis/secure_session.hpp +++ b/src/wrappers/themis/jsthemis/secure_session.hpp @@ -32,7 +32,8 @@ class SecureSession : public Nan::ObjectWrap static void Init(v8::Handle exports); private: - explicit SecureSession(const std::vector& id, const std::vector& private_key, + explicit SecureSession(const std::vector& id, + const std::vector& private_key, v8::Local get_pub_by_id_callback); ~SecureSession();