1 #ifndef SRC_CRYPTO_CRYPTO_SIG_H_ 2 #define SRC_CRYPTO_CRYPTO_SIG_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "base_object.h" 7 #include "crypto/crypto_keys.h" 8 #include "crypto/crypto_util.h" 9 #include "env.h" 10 #include "memory_tracker.h" 11 12 namespace node { 13 namespace crypto { 14 static const unsigned int kNoDsaSignature = static_cast<unsigned int>(-1); 15 16 enum DSASigEnc { 17 kSigEncDER, 18 kSigEncP1363 19 }; 20 21 class SignBase : public BaseObject { 22 public: 23 enum Error { 24 kSignOk, 25 kSignUnknownDigest, 26 kSignInit, 27 kSignNotInitialised, 28 kSignUpdate, 29 kSignPrivateKey, 30 kSignPublicKey, 31 kSignMalformedSignature 32 }; 33 34 SignBase(Environment* env, v8::Local<v8::Object> wrap); 35 36 Error Init(const char* sign_type); 37 Error Update(const char* data, size_t len); 38 39 // TODO(joyeecheung): track the memory used by OpenSSL types 40 void MemoryInfo(MemoryTracker* tracker) const override; 41 SET_MEMORY_INFO_NAME(SignBase) 42 SET_SELF_SIZE(SignBase) 43 44 protected: 45 EVPMDPointer mdctx_; 46 }; 47 48 class Sign : public SignBase { 49 public: 50 static void Initialize(Environment* env, v8::Local<v8::Object> target); 51 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 52 53 struct SignResult { 54 Error error; 55 std::unique_ptr<v8::BackingStore> signature; 56 57 explicit SignResult( 58 Error err, 59 std::unique_ptr<v8::BackingStore>&& sig = nullptr) errorSignResult60 : error(err), signature(std::move(sig)) {} 61 }; 62 63 SignResult SignFinal( 64 const ManagedEVPPKey& pkey, 65 int padding, 66 const v8::Maybe<int>& saltlen, 67 DSASigEnc dsa_sig_enc); 68 69 static void SignSync(const v8::FunctionCallbackInfo<v8::Value>& args); 70 71 protected: 72 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 73 static void SignInit(const v8::FunctionCallbackInfo<v8::Value>& args); 74 static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args); 75 static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args); 76 77 Sign(Environment* env, v8::Local<v8::Object> wrap); 78 }; 79 80 class Verify : public SignBase { 81 public: 82 static void Initialize(Environment* env, v8::Local<v8::Object> target); 83 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 84 85 Error VerifyFinal(const ManagedEVPPKey& key, 86 const ByteSource& sig, 87 int padding, 88 const v8::Maybe<int>& saltlen, 89 bool* verify_result); 90 91 static void VerifySync(const v8::FunctionCallbackInfo<v8::Value>& args); 92 93 protected: 94 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 95 static void VerifyInit(const v8::FunctionCallbackInfo<v8::Value>& args); 96 static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args); 97 static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args); 98 99 Verify(Environment* env, v8::Local<v8::Object> wrap); 100 }; 101 102 struct SignConfiguration final : public MemoryRetainer { 103 enum Mode { 104 kSign, 105 kVerify 106 }; 107 enum Flags { 108 kHasNone = 0, 109 kHasSaltLength = 1, 110 kHasPadding = 2 111 }; 112 113 CryptoJobMode job_mode; 114 Mode mode; 115 ManagedEVPPKey key; 116 ByteSource data; 117 ByteSource signature; 118 const EVP_MD* digest = nullptr; 119 int flags = SignConfiguration::kHasNone; 120 int padding = 0; 121 int salt_length = 0; 122 DSASigEnc dsa_encoding = kSigEncDER; 123 124 SignConfiguration() = default; 125 126 explicit SignConfiguration(SignConfiguration&& other) noexcept; 127 128 SignConfiguration& operator=(SignConfiguration&& other) noexcept; 129 130 void MemoryInfo(MemoryTracker* tracker) const override; 131 SET_MEMORY_INFO_NAME(SignConfiguration) 132 SET_SELF_SIZE(SignConfiguration) 133 }; 134 135 struct SignTraits final { 136 using AdditionalParameters = SignConfiguration; 137 static constexpr const char* JobName = "SignJob"; 138 139 // TODO(@jasnell): Sign request vs. Verify request 140 141 static constexpr AsyncWrap::ProviderType Provider = 142 AsyncWrap::PROVIDER_SIGNREQUEST; 143 144 static v8::Maybe<bool> AdditionalConfig( 145 CryptoJobMode mode, 146 const v8::FunctionCallbackInfo<v8::Value>& args, 147 unsigned int offset, 148 SignConfiguration* params); 149 150 static bool DeriveBits( 151 Environment* env, 152 const SignConfiguration& params, 153 ByteSource* out); 154 155 static v8::Maybe<bool> EncodeOutput( 156 Environment* env, 157 const SignConfiguration& params, 158 ByteSource* out, 159 v8::Local<v8::Value>* result); 160 }; 161 162 using SignJob = DeriveBitsJob<SignTraits>; 163 164 } // namespace crypto 165 } // namespace node 166 167 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 168 #endif // SRC_CRYPTO_CRYPTO_SIG_H_ 169