|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2021-2022 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 | + * Copyright 2023 NXP |
| 18 | + */ |
| 19 | +#include "DeviceAttestationCreds.h" |
| 20 | + |
| 21 | +#include <crypto/CHIPCryptoPAL.h> |
| 22 | +#include <lib/core/CHIPError.h> |
| 23 | +#include <lib/support/Span.h> |
| 24 | +#include <trusty_matter.h> |
| 25 | + |
| 26 | +using namespace matter; |
| 27 | + |
| 28 | +namespace chip { |
| 29 | +namespace Credentials { |
| 30 | +namespace Trusty { |
| 31 | + |
| 32 | +CHIP_ERROR TrustyDACProvider::GetDeviceAttestationCert(MutableByteSpan & out_dac_buffer) |
| 33 | +{ |
| 34 | + size_t out_size = 0; |
| 35 | + int rc; |
| 36 | + |
| 37 | + rc = trusty_matter.ExportDACCert(out_dac_buffer.data(), out_dac_buffer.size(), out_size); |
| 38 | + if (rc == 0) { |
| 39 | + out_dac_buffer.reduce_size(out_size); |
| 40 | + return CHIP_NO_ERROR; |
| 41 | + } else |
| 42 | + return CHIP_ERROR_CERT_LOAD_FAILED; |
| 43 | +} |
| 44 | + |
| 45 | +CHIP_ERROR TrustyDACProvider::GetProductAttestationIntermediateCert(MutableByteSpan & out_pai_buffer) |
| 46 | +{ |
| 47 | + size_t out_size = 0; |
| 48 | + int rc; |
| 49 | + |
| 50 | + rc = trusty_matter.ExportPAICert(out_pai_buffer.data(), out_pai_buffer.size(), out_size); |
| 51 | + if (rc == 0) { |
| 52 | + out_pai_buffer.reduce_size(out_size); |
| 53 | + return CHIP_NO_ERROR; |
| 54 | + } else |
| 55 | + return CHIP_ERROR_CERT_LOAD_FAILED; |
| 56 | +} |
| 57 | + |
| 58 | +CHIP_ERROR TrustyDACProvider::GetCertificationDeclaration(MutableByteSpan & out_cd_buffer) |
| 59 | +{ |
| 60 | + size_t out_size = 0; |
| 61 | + int rc; |
| 62 | + |
| 63 | + rc = trusty_matter.ExportCDCert(out_cd_buffer.data(), out_cd_buffer.size(), out_size); |
| 64 | + if (rc == 0) { |
| 65 | + out_cd_buffer.reduce_size(out_size); |
| 66 | + return CHIP_NO_ERROR; |
| 67 | + } else |
| 68 | + return CHIP_ERROR_CERT_LOAD_FAILED; |
| 69 | +} |
| 70 | + |
| 71 | +CHIP_ERROR TrustyDACProvider::GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) |
| 72 | +{ |
| 73 | + // TODO: We need a real example FirmwareInformation to be populated. |
| 74 | + out_firmware_info_buffer.reduce_size(0); |
| 75 | + |
| 76 | + return CHIP_NO_ERROR; |
| 77 | +} |
| 78 | + |
| 79 | +CHIP_ERROR TrustyDACProvider::SignWithDeviceAttestationKey(const ByteSpan & message_to_sign, |
| 80 | + MutableByteSpan & out_signature_buffer) |
| 81 | +{ |
| 82 | + int rc = 0; |
| 83 | + size_t out_size = 0; |
| 84 | + |
| 85 | + VerifyOrReturnError(IsSpanUsable(out_signature_buffer), CHIP_ERROR_INVALID_ARGUMENT); |
| 86 | + VerifyOrReturnError(IsSpanUsable(message_to_sign), CHIP_ERROR_INVALID_ARGUMENT); |
| 87 | + |
| 88 | + rc = trusty_matter.SignWithDACKey(message_to_sign.data(), message_to_sign.size(), |
| 89 | + out_signature_buffer.data(), out_signature_buffer.size(), out_size); |
| 90 | + if (rc == 0) { |
| 91 | + out_signature_buffer.reduce_size(out_size); |
| 92 | + return CHIP_NO_ERROR; |
| 93 | + } else |
| 94 | + return CHIP_ERROR_CERT_LOAD_FAILED; |
| 95 | +} |
| 96 | + |
| 97 | +TrustyDACProvider & TrustyDACProvider::GetTrustyDACProvider() |
| 98 | +{ |
| 99 | + static TrustyDACProvider trusty_dac_provider; |
| 100 | + |
| 101 | + return trusty_dac_provider; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace Trusty |
| 105 | +} // namespace Credentials |
| 106 | +} // namespace chip |
0 commit comments