|
| 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 "JNIDACProvider.h" |
| 19 | +#include "lib/support/logging/CHIPLogging.h" |
| 20 | +#include <credentials/CHIPCert.h> |
| 21 | +#include <crypto/CHIPCryptoPAL.h> |
| 22 | +#include <cstdlib> |
| 23 | +#include <jni.h> |
| 24 | +#include <lib/core/CHIPError.h> |
| 25 | +#include <lib/support/CHIPJNIError.h> |
| 26 | +#include <lib/support/JniReferences.h> |
| 27 | +#include <lib/support/JniTypeWrappers.h> |
| 28 | +#include <lib/support/Span.h> |
| 29 | + |
| 30 | +using namespace chip; |
| 31 | + |
| 32 | +JNIDACProvider::JNIDACProvider(jobject provider) |
| 33 | +{ |
| 34 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 35 | + VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for JNIDACProvider")); |
| 36 | + |
| 37 | + mJNIDACProviderObject = env->NewGlobalRef(provider); |
| 38 | + VerifyOrReturn(mJNIDACProviderObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef JNIDACProvider")); |
| 39 | + |
| 40 | + jclass JNIDACProviderClass = env->GetObjectClass(provider); |
| 41 | + VerifyOrReturn(JNIDACProviderClass != nullptr, ChipLogError(Zcl, "Failed to get JNIDACProvider Java class")); |
| 42 | + |
| 43 | + mGetCertificationDeclarationMethod = env->GetMethodID(JNIDACProviderClass, "GetCertificationDeclaration", "()[B"); |
| 44 | + if (mGetCertificationDeclarationMethod == nullptr) |
| 45 | + { |
| 46 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetCertificationDeclaration' method"); |
| 47 | + env->ExceptionClear(); |
| 48 | + } |
| 49 | + |
| 50 | + mGetFirmwareInformationMethod = env->GetMethodID(JNIDACProviderClass, "GetFirmwareInformation", "()[B"); |
| 51 | + if (mGetFirmwareInformationMethod == nullptr) |
| 52 | + { |
| 53 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetFirmwareInformation' method"); |
| 54 | + env->ExceptionClear(); |
| 55 | + } |
| 56 | + |
| 57 | + mGetDeviceAttestationCertMethod = env->GetMethodID(JNIDACProviderClass, "GetDeviceAttestationCert", "()[B"); |
| 58 | + if (mGetDeviceAttestationCertMethod == nullptr) |
| 59 | + { |
| 60 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetDeviceAttestationCert' method"); |
| 61 | + env->ExceptionClear(); |
| 62 | + } |
| 63 | + |
| 64 | + mGetProductAttestationIntermediateCertMethod = |
| 65 | + env->GetMethodID(JNIDACProviderClass, "GetProductAttestationIntermediateCert", "()[B"); |
| 66 | + if (mGetProductAttestationIntermediateCertMethod == nullptr) |
| 67 | + { |
| 68 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetProductAttestationIntermediateCert' method"); |
| 69 | + env->ExceptionClear(); |
| 70 | + } |
| 71 | + |
| 72 | + mGetDeviceAttestationCertPrivateKeyMethod = env->GetMethodID(JNIDACProviderClass, "GetDeviceAttestationCertPrivateKey", "()[B"); |
| 73 | + if (mGetDeviceAttestationCertPrivateKeyMethod == nullptr) |
| 74 | + { |
| 75 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetDeviceAttestationCertPrivateKey' method"); |
| 76 | + env->ExceptionClear(); |
| 77 | + } |
| 78 | + |
| 79 | + mGetDeviceAttestationCertPublicKeyKeyMethod = |
| 80 | + env->GetMethodID(JNIDACProviderClass, "GetDeviceAttestationCertPublicKeyKey", "()[B"); |
| 81 | + if (mGetDeviceAttestationCertPublicKeyKeyMethod == nullptr) |
| 82 | + { |
| 83 | + ChipLogError(Zcl, "Failed to access JNIDACProvider 'GetDeviceAttestationCertPublicKeyKey' method"); |
| 84 | + env->ExceptionClear(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +CHIP_ERROR JNIDACProvider::GetJavaByteByMethod(jmethodID method, MutableByteSpan & out_buffer) |
| 89 | +{ |
| 90 | + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); |
| 91 | + VerifyOrReturnLogError(mJNIDACProviderObject != nullptr, CHIP_ERROR_INCORRECT_STATE); |
| 92 | + VerifyOrReturnLogError(method != nullptr, CHIP_ERROR_INCORRECT_STATE); |
| 93 | + VerifyOrReturnLogError(env != nullptr, CHIP_JNI_ERROR_NO_ENV); |
| 94 | + |
| 95 | + jbyteArray outArray = (jbyteArray) env->CallObjectMethod(mJNIDACProviderObject, method); |
| 96 | + if (env->ExceptionCheck()) |
| 97 | + { |
| 98 | + ChipLogError(Zcl, "Java exception in get Method"); |
| 99 | + env->ExceptionDescribe(); |
| 100 | + env->ExceptionClear(); |
| 101 | + return CHIP_ERROR_INCORRECT_STATE; |
| 102 | + } |
| 103 | + |
| 104 | + if (outArray == nullptr || env->GetArrayLength(outArray) <= 0) |
| 105 | + { |
| 106 | + out_buffer.reduce_size(0); |
| 107 | + return CHIP_NO_ERROR; |
| 108 | + } |
| 109 | + |
| 110 | + JniByteArray JniOutArray(env, outArray); |
| 111 | + return CopySpanToMutableSpan(JniOutArray.byteSpan(), out_buffer); |
| 112 | +} |
| 113 | + |
| 114 | +CHIP_ERROR JNIDACProvider::GetCertificationDeclaration(MutableByteSpan & out_cd_buffer) |
| 115 | +{ |
| 116 | + ChipLogProgress(Zcl, "Received GetCertificationDeclaration"); |
| 117 | + return GetJavaByteByMethod(mGetCertificationDeclarationMethod, out_cd_buffer); |
| 118 | +} |
| 119 | + |
| 120 | +CHIP_ERROR JNIDACProvider::GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) |
| 121 | +{ |
| 122 | + ChipLogProgress(Zcl, "Received GetFirmwareInformation"); |
| 123 | + return GetJavaByteByMethod(mGetFirmwareInformationMethod, out_firmware_info_buffer); |
| 124 | +} |
| 125 | + |
| 126 | +CHIP_ERROR JNIDACProvider::GetDeviceAttestationCert(MutableByteSpan & out_dac_buffer) |
| 127 | +{ |
| 128 | + ChipLogProgress(Zcl, "Received GetDeviceAttestationCert"); |
| 129 | + return GetJavaByteByMethod(mGetDeviceAttestationCertMethod, out_dac_buffer); |
| 130 | +} |
| 131 | + |
| 132 | +CHIP_ERROR JNIDACProvider::GetProductAttestationIntermediateCert(MutableByteSpan & out_pai_buffer) |
| 133 | +{ |
| 134 | + ChipLogProgress(Zcl, "Received GetProductAttestationIntermediateCert"); |
| 135 | + return GetJavaByteByMethod(mGetProductAttestationIntermediateCertMethod, out_pai_buffer); |
| 136 | +} |
| 137 | + |
| 138 | +// TODO: This should be moved to a method of P256Keypair |
| 139 | +CHIP_ERROR LoadKeypairFromRaw(ByteSpan private_key, ByteSpan public_key, Crypto::P256Keypair & keypair) |
| 140 | +{ |
| 141 | + Crypto::P256SerializedKeypair serialized_keypair; |
| 142 | + ReturnErrorOnFailure(serialized_keypair.SetLength(private_key.size() + public_key.size())); |
| 143 | + memcpy(serialized_keypair.Bytes(), public_key.data(), public_key.size()); |
| 144 | + memcpy(serialized_keypair.Bytes() + public_key.size(), private_key.data(), private_key.size()); |
| 145 | + return keypair.Deserialize(serialized_keypair); |
| 146 | +} |
| 147 | + |
| 148 | +CHIP_ERROR JNIDACProvider::SignWithDeviceAttestationKey(const ByteSpan & digest_to_sign, MutableByteSpan & out_signature_buffer) |
| 149 | +{ |
| 150 | + ChipLogProgress(Zcl, "Received SignWithDeviceAttestationKey"); |
| 151 | + Crypto::P256ECDSASignature signature; |
| 152 | + Crypto::P256Keypair keypair; |
| 153 | + |
| 154 | + VerifyOrReturnError(IsSpanUsable(out_signature_buffer), CHIP_ERROR_INVALID_ARGUMENT); |
| 155 | + VerifyOrReturnError(IsSpanUsable(digest_to_sign), CHIP_ERROR_INVALID_ARGUMENT); |
| 156 | + VerifyOrReturnError(out_signature_buffer.size() >= signature.Capacity(), CHIP_ERROR_BUFFER_TOO_SMALL); |
| 157 | + |
| 158 | + uint8_t privateKeyBuf[Crypto::kP256_PrivateKey_Length]; |
| 159 | + MutableByteSpan privateKeyBufSpan(privateKeyBuf); |
| 160 | + ReturnErrorOnFailure(GetJavaByteByMethod(mGetDeviceAttestationCertPrivateKeyMethod, privateKeyBufSpan)); |
| 161 | + |
| 162 | + uint8_t publicKeyBuf[Crypto::kP256_PublicKey_Length]; |
| 163 | + MutableByteSpan publicKeyBufSpan(publicKeyBuf); |
| 164 | + ReturnErrorOnFailure(GetJavaByteByMethod(mGetDeviceAttestationCertPublicKeyKeyMethod, publicKeyBufSpan)); |
| 165 | + |
| 166 | + // In a non-exemplary implementation, the public key is not needed here. It is used here merely because |
| 167 | + // Crypto::P256Keypair is only (currently) constructable from raw keys if both private/public keys are present. |
| 168 | + ReturnErrorOnFailure(LoadKeypairFromRaw(privateKeyBufSpan, publicKeyBufSpan, keypair)); |
| 169 | + ReturnErrorOnFailure(keypair.ECDSA_sign_hash(digest_to_sign.data(), digest_to_sign.size(), signature)); |
| 170 | + |
| 171 | + return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, out_signature_buffer); |
| 172 | +} |
0 commit comments