|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2021 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "CHIPP256KeypairBridge.h" |
| 19 | +#include "lib/core/CHIPError.h" |
| 20 | +#include "lib/support/CHIPJNIError.h" |
| 21 | +#include "lib/support/JniReferences.h" |
| 22 | +#include "lib/support/JniTypeWrappers.h" |
| 23 | +#include "lib/support/SafeInt.h" |
| 24 | + |
| 25 | +#include <array> |
| 26 | +#include <cstdint> |
| 27 | +#include <cstdlib> |
| 28 | +#include <jni.h> |
| 29 | +#include <string.h> |
| 30 | +#include <type_traits> |
| 31 | + |
| 32 | +using namespace chip; |
| 33 | +using namespace chip::Crypto; |
| 34 | + |
| 35 | +CHIPP256KeypairBridge::~CHIPP256KeypairBridge() {} |
| 36 | + |
| 37 | +CHIP_ERROR CHIPP256KeypairBridge::SetDelegate(jobject delegate) |
| 38 | +{ |
| 39 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 40 | + |
| 41 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 42 | + VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); |
| 43 | + ReturnErrorOnFailure(mDelegate.Init(delegate)); |
| 44 | + jclass keypairDelegateClass; |
| 45 | + err = JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/KeypairDelegate", keypairDelegateClass); |
| 46 | + VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Failed to find class for KeypairDelegate.")); |
| 47 | + SuccessOrExit(err = mKeypairDelegateClass.Init(static_cast<jobject>(keypairDelegateClass))); |
| 48 | + err = JniReferences::GetInstance().FindMethod(env, delegate, "createCertificateSigningRequest", "()[B", |
| 49 | + &mCreateCertificateSigningRequestMethod); |
| 50 | + VerifyOrExit(err == CHIP_NO_ERROR, |
| 51 | + ChipLogError(Controller, "Failed to find KeypairDelegate.createCertificateSigningRequest() method.")); |
| 52 | + |
| 53 | + err = JniReferences::GetInstance().FindMethod(env, delegate, "getPublicKey", "()[B", &mGetPublicKeyMethod); |
| 54 | + VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Failed to find KeypairDelegate.getPublicKey() method.")); |
| 55 | + |
| 56 | + err = JniReferences::GetInstance().FindMethod(env, delegate, "ecdsaSignMessage", "([B)[B", &mEcdsaSignMessageMethod); |
| 57 | + VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Failed to find KeypairDelegate.ecdsaSignMessage() method.")); |
| 58 | + |
| 59 | +exit: |
| 60 | + return err; |
| 61 | +} |
| 62 | + |
| 63 | +CHIP_ERROR CHIPP256KeypairBridge::Initialize(ECPKeyTarget key_target) |
| 64 | +{ |
| 65 | + if (HasKeypair()) |
| 66 | + { |
| 67 | + SetPubkey(); |
| 68 | + } |
| 69 | + return CHIP_NO_ERROR; |
| 70 | +} |
| 71 | + |
| 72 | +CHIP_ERROR CHIPP256KeypairBridge::Serialize(P256SerializedKeypair & output) const |
| 73 | +{ |
| 74 | + if (!HasKeypair()) |
| 75 | + { |
| 76 | + return CHIP_ERROR_INCORRECT_STATE; |
| 77 | + } |
| 78 | + |
| 79 | + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; |
| 80 | +} |
| 81 | + |
| 82 | +CHIP_ERROR CHIPP256KeypairBridge::Deserialize(P256SerializedKeypair & input) |
| 83 | +{ |
| 84 | + if (!HasKeypair()) |
| 85 | + { |
| 86 | + return CHIP_ERROR_INCORRECT_STATE; |
| 87 | + } |
| 88 | + |
| 89 | + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; |
| 90 | +} |
| 91 | + |
| 92 | +CHIP_ERROR CHIPP256KeypairBridge::NewCertificateSigningRequest(uint8_t * csr, size_t & csr_length) const |
| 93 | +{ |
| 94 | + if (!HasKeypair()) |
| 95 | + { |
| 96 | + return CHIP_ERROR_INCORRECT_STATE; |
| 97 | + } |
| 98 | + |
| 99 | + // Not supported for use from within the CHIP SDK. We provide our own |
| 100 | + // implementation that is JVM-specific. |
| 101 | + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; |
| 102 | +} |
| 103 | + |
| 104 | +CHIP_ERROR CHIPP256KeypairBridge::ECDSA_sign_msg(const uint8_t * msg, size_t msg_length, P256ECDSASignature & out_signature) const |
| 105 | +{ |
| 106 | + if (!HasKeypair()) |
| 107 | + { |
| 108 | + return CHIP_ERROR_INCORRECT_STATE; |
| 109 | + } |
| 110 | + |
| 111 | + VerifyOrReturnError(CanCastTo<uint32_t>(msg_length), CHIP_ERROR_INVALID_ARGUMENT); |
| 112 | + |
| 113 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 114 | + jbyteArray jniMsg; |
| 115 | + jobject signedResult = nullptr; |
| 116 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 117 | + VerifyOrReturnError(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV); |
| 118 | + err = JniReferences::GetInstance().N2J_ByteArray(env, msg, static_cast<uint32_t>(msg_length), jniMsg); |
| 119 | + VerifyOrReturnError(err == CHIP_NO_ERROR, err); |
| 120 | + VerifyOrReturnError(jniMsg != nullptr, err); |
| 121 | + VerifyOrReturnError(mDelegate.HasValidObjectRef(), CHIP_ERROR_INCORRECT_STATE); |
| 122 | + signedResult = env->CallObjectMethod(mDelegate.ObjectRef(), mEcdsaSignMessageMethod, jniMsg); |
| 123 | + |
| 124 | + if (env->ExceptionCheck()) |
| 125 | + { |
| 126 | + ChipLogError(Controller, "Java exception in KeypairDelegate.ecdsaSignMessage()"); |
| 127 | + env->ExceptionDescribe(); |
| 128 | + env->ExceptionClear(); |
| 129 | + return CHIP_JNI_ERROR_EXCEPTION_THROWN; |
| 130 | + } |
| 131 | + |
| 132 | + JniByteArray jniSignature(env, static_cast<jbyteArray>(signedResult)); |
| 133 | + MutableByteSpan signatureSpan(out_signature.Bytes(), out_signature.Capacity()); |
| 134 | + ReturnErrorOnFailure(EcdsaAsn1SignatureToRaw(CHIP_CRYPTO_GROUP_SIZE_BYTES, jniSignature.byteSpan(), signatureSpan)); |
| 135 | + ReturnErrorOnFailure(out_signature.SetLength(signatureSpan.size())); |
| 136 | + |
| 137 | + return err; |
| 138 | +} |
| 139 | + |
| 140 | +CHIP_ERROR CHIPP256KeypairBridge::ECDH_derive_secret(const P256PublicKey & remote_public_key, |
| 141 | + P256ECDHDerivedSecret & out_secret) const |
| 142 | +{ |
| 143 | + if (!HasKeypair()) |
| 144 | + { |
| 145 | + return CHIP_ERROR_INCORRECT_STATE; |
| 146 | + } |
| 147 | + |
| 148 | + // Not required for Java SDK. |
| 149 | + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; |
| 150 | +} |
| 151 | + |
| 152 | +CHIP_ERROR CHIPP256KeypairBridge::SetPubkey() |
| 153 | +{ |
| 154 | + jobject publicKey = nullptr; |
| 155 | + JNIEnv * env = nullptr; |
| 156 | + |
| 157 | + VerifyOrReturnError(HasKeypair(), CHIP_ERROR_INCORRECT_STATE); |
| 158 | + |
| 159 | + env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 160 | + VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV); |
| 161 | + VerifyOrReturnError(mDelegate.HasValidObjectRef(), CHIP_ERROR_INCORRECT_STATE); |
| 162 | + publicKey = env->CallObjectMethod(mDelegate.ObjectRef(), mGetPublicKeyMethod); |
| 163 | + if (env->ExceptionCheck()) |
| 164 | + { |
| 165 | + ChipLogError(Controller, "Java exception in KeypairDelegate.getPublicKey()"); |
| 166 | + env->ExceptionDescribe(); |
| 167 | + env->ExceptionClear(); |
| 168 | + return CHIP_JNI_ERROR_EXCEPTION_THROWN; |
| 169 | + } |
| 170 | + |
| 171 | + VerifyOrReturnError(publicKey != nullptr, CHIP_JNI_ERROR_NULL_OBJECT); |
| 172 | + JniByteArray jniPublicKey(env, static_cast<jbyteArray>(publicKey)); |
| 173 | + FixedByteSpan<Crypto::kP256_PublicKey_Length> publicKeySpan = |
| 174 | + FixedByteSpan<kP256_PublicKey_Length>(reinterpret_cast<const uint8_t *>(jniPublicKey.data())); |
| 175 | + mPublicKey = P256PublicKey(publicKeySpan); |
| 176 | + return CHIP_NO_ERROR; |
| 177 | +} |
0 commit comments