1 #ifndef SRC_CRYPTO_CRYPTO_RSA_H_ 2 #define SRC_CRYPTO_CRYPTO_RSA_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "crypto/crypto_cipher.h" 7 #include "crypto/crypto_keygen.h" 8 #include "crypto/crypto_keys.h" 9 #include "crypto/crypto_util.h" 10 #include "env.h" 11 #include "memory_tracker.h" 12 #include "v8.h" 13 14 namespace node { 15 namespace crypto { 16 enum RSAKeyVariant { 17 kKeyVariantRSA_SSA_PKCS1_v1_5, 18 kKeyVariantRSA_PSS, 19 kKeyVariantRSA_OAEP 20 }; 21 22 struct RsaKeyPairParams final : public MemoryRetainer { 23 RSAKeyVariant variant; 24 unsigned int modulus_bits; 25 unsigned int exponent; 26 27 // The following options are used for RSA-PSS. If any of them are set, a 28 // RSASSA-PSS-params sequence will be added to the key. 29 const EVP_MD* md = nullptr; 30 const EVP_MD* mgf1_md = nullptr; 31 int saltlen = -1; 32 33 SET_NO_MEMORY_INFO() 34 SET_MEMORY_INFO_NAME(RsaKeyPairParams) 35 SET_SELF_SIZE(RsaKeyPairParams) 36 }; 37 38 using RsaKeyPairGenConfig = KeyPairGenConfig<RsaKeyPairParams>; 39 40 struct RsaKeyGenTraits final { 41 using AdditionalParameters = RsaKeyPairGenConfig; 42 static constexpr const char* JobName = "RsaKeyPairGenJob"; 43 44 static EVPKeyCtxPointer Setup(RsaKeyPairGenConfig* params); 45 46 static v8::Maybe<bool> AdditionalConfig( 47 CryptoJobMode mode, 48 const v8::FunctionCallbackInfo<v8::Value>& args, 49 unsigned int* offset, 50 RsaKeyPairGenConfig* params); 51 }; 52 53 using RSAKeyPairGenJob = KeyGenJob<KeyPairGenTraits<RsaKeyGenTraits>>; 54 55 struct RSAKeyExportConfig final : public MemoryRetainer { 56 RSAKeyVariant variant = kKeyVariantRSA_SSA_PKCS1_v1_5; 57 SET_NO_MEMORY_INFO() 58 SET_MEMORY_INFO_NAME(RSAKeyExportConfig) 59 SET_SELF_SIZE(RSAKeyExportConfig) 60 }; 61 62 struct RSAKeyExportTraits final { 63 static constexpr const char* JobName = "RSAKeyExportJob"; 64 using AdditionalParameters = RSAKeyExportConfig; 65 66 static v8::Maybe<bool> AdditionalConfig( 67 const v8::FunctionCallbackInfo<v8::Value>& args, 68 unsigned int offset, 69 RSAKeyExportConfig* config); 70 71 static WebCryptoKeyExportStatus DoExport( 72 std::shared_ptr<KeyObjectData> key_data, 73 WebCryptoKeyFormat format, 74 const RSAKeyExportConfig& params, 75 ByteSource* out); 76 }; 77 78 using RSAKeyExportJob = KeyExportJob<RSAKeyExportTraits>; 79 80 struct RSACipherConfig final : public MemoryRetainer { 81 CryptoJobMode mode; 82 ByteSource label; 83 int padding = 0; 84 const EVP_MD* digest = nullptr; 85 86 RSACipherConfig() = default; 87 88 RSACipherConfig(RSACipherConfig&& other) noexcept; 89 90 void MemoryInfo(MemoryTracker* tracker) const override; 91 SET_MEMORY_INFO_NAME(RSACipherConfig) 92 SET_SELF_SIZE(RSACipherConfig) 93 }; 94 95 struct RSACipherTraits final { 96 static constexpr const char* JobName = "RSACipherJob"; 97 using AdditionalParameters = RSACipherConfig; 98 99 static v8::Maybe<bool> AdditionalConfig( 100 CryptoJobMode mode, 101 const v8::FunctionCallbackInfo<v8::Value>& args, 102 unsigned int offset, 103 WebCryptoCipherMode cipher_mode, 104 RSACipherConfig* config); 105 106 static WebCryptoCipherStatus DoCipher( 107 Environment* env, 108 std::shared_ptr<KeyObjectData> key_data, 109 WebCryptoCipherMode cipher_mode, 110 const RSACipherConfig& params, 111 const ByteSource& in, 112 ByteSource* out); 113 }; 114 115 using RSACipherJob = CipherJob<RSACipherTraits>; 116 117 v8::Maybe<bool> ExportJWKRsaKey( 118 Environment* env, 119 std::shared_ptr<KeyObjectData> key, 120 v8::Local<v8::Object> target); 121 122 std::shared_ptr<KeyObjectData> ImportJWKRsaKey( 123 Environment* env, 124 v8::Local<v8::Object> jwk, 125 const v8::FunctionCallbackInfo<v8::Value>& args, 126 unsigned int offset); 127 128 v8::Maybe<bool> GetRsaKeyDetail( 129 Environment* env, 130 std::shared_ptr<KeyObjectData> key, 131 v8::Local<v8::Object> target); 132 133 namespace RSAAlg { 134 void Initialize(Environment* env, v8::Local<v8::Object> target); 135 void RegisterExternalReferences(ExternalReferenceRegistry* registry); 136 } // namespace RSAAlg 137 } // namespace crypto 138 } // namespace node 139 140 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 141 #endif // SRC_CRYPTO_CRYPTO_RSA_H_ 142