@@ -921,6 +921,7 @@ v8::Local<v8::Function> KeyObjectHandle::Initialize(Environment* env) {
921921 env->SetProtoMethod (t, " initEDRaw" , InitEDRaw);
922922 env->SetProtoMethod (t, " initJwk" , InitJWK);
923923 env->SetProtoMethod (t, " keyDetail" , GetKeyDetail);
924+ env->SetProtoMethod (t, " equals" , Equals);
924925
925926 auto function = t->GetFunction (env->context ()).ToLocalChecked ();
926927 env->set_crypto_key_object_handle_constructor (function);
@@ -939,6 +940,7 @@ void KeyObjectHandle::RegisterExternalReferences(
939940 registry->Register (InitEDRaw);
940941 registry->Register (InitJWK);
941942 registry->Register (GetKeyDetail);
943+ registry->Register (Equals);
942944}
943945
944946MaybeLocal<Object> KeyObjectHandle::Create (
@@ -1134,6 +1136,54 @@ void KeyObjectHandle::InitEDRaw(const FunctionCallbackInfo<Value>& args) {
11341136 args.GetReturnValue ().Set (true );
11351137}
11361138
1139+ void KeyObjectHandle::Equals (const FunctionCallbackInfo<Value>& args) {
1140+ KeyObjectHandle* self_handle;
1141+ KeyObjectHandle* arg_handle;
1142+ ASSIGN_OR_RETURN_UNWRAP (&self_handle, args.Holder ());
1143+ ASSIGN_OR_RETURN_UNWRAP (&arg_handle, args[0 ].As <Object>());
1144+ std::shared_ptr<KeyObjectData> key = self_handle->Data ();
1145+ std::shared_ptr<KeyObjectData> key2 = arg_handle->Data ();
1146+
1147+ KeyType key_type = key->GetKeyType ();
1148+ CHECK_EQ (key_type, key2->GetKeyType ());
1149+
1150+ bool ret;
1151+ switch (key_type) {
1152+ case kKeyTypeSecret : {
1153+ size_t size = key->GetSymmetricKeySize ();
1154+ if (size == key2->GetSymmetricKeySize ()) {
1155+ ret = CRYPTO_memcmp (
1156+ key->GetSymmetricKey (),
1157+ key2->GetSymmetricKey (),
1158+ size) == 0 ;
1159+ } else {
1160+ ret = false ;
1161+ }
1162+ break ;
1163+ }
1164+ case kKeyTypePublic :
1165+ case kKeyTypePrivate : {
1166+ EVP_PKEY* pkey = key->GetAsymmetricKey ().get ();
1167+ EVP_PKEY* pkey2 = key2->GetAsymmetricKey ().get ();
1168+ #if OPENSSL_VERSION_MAJOR >= 3
1169+ int ok = EVP_PKEY_eq (pkey, pkey2);
1170+ #else
1171+ int ok = EVP_PKEY_cmp (pkey, pkey2);
1172+ #endif
1173+ if (ok == -2 ) {
1174+ Environment* env = Environment::GetCurrent (args);
1175+ return THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION (env);
1176+ }
1177+ ret = ok == 1 ;
1178+ break ;
1179+ }
1180+ default :
1181+ UNREACHABLE (" unsupported key type" );
1182+ }
1183+
1184+ args.GetReturnValue ().Set (ret);
1185+ }
1186+
11371187void KeyObjectHandle::GetKeyDetail (const FunctionCallbackInfo<Value>& args) {
11381188 Environment* env = Environment::GetCurrent (args);
11391189 KeyObjectHandle* key;
0 commit comments