From 5cf9d283ab49f81db8d2379af71ba16cb0746baa Mon Sep 17 00:00:00 2001 From: Evgeny Margolis Date: Wed, 20 Jul 2022 12:50:23 -0700 Subject: [PATCH] Added Negative Test Cases for Invalid CATs in NOC. (#20998) --- src/credentials/tests/BUILD.gn | 4 +- .../tests/CHIPCert_error_test_vectors.cpp | 86 +++++++++++++++++++ .../tests/CHIPCert_error_test_vectors.h | 44 ++++++++++ src/credentials/tests/TestChipCert.cpp | 19 +++- src/tools/chip-cert/Cmd_GenCert.cpp | 1 - src/tools/chip-cert/Cmd_PrintCert.cpp | 2 +- 6 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 src/credentials/tests/CHIPCert_error_test_vectors.cpp create mode 100644 src/credentials/tests/CHIPCert_error_test_vectors.h diff --git a/src/credentials/tests/BUILD.gn b/src/credentials/tests/BUILD.gn index f4fb7845271ed8..87c694bb8aa364 100644 --- a/src/credentials/tests/BUILD.gn +++ b/src/credentials/tests/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Project CHIP Authors +# Copyright (c) 2020-2022 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -26,6 +26,8 @@ static_library("cert_test_vectors") { sources = [ "CHIPAttCert_test_vectors.cpp", "CHIPAttCert_test_vectors.h", + "CHIPCert_error_test_vectors.cpp", + "CHIPCert_error_test_vectors.h", "CHIPCert_test_vectors.cpp", "CHIPCert_test_vectors.h", "CHIPCert_unit_test_vectors.cpp", diff --git a/src/credentials/tests/CHIPCert_error_test_vectors.cpp b/src/credentials/tests/CHIPCert_error_test_vectors.cpp new file mode 100644 index 00000000000000..c1b44945a0c33d --- /dev/null +++ b/src/credentials/tests/CHIPCert_error_test_vectors.cpp @@ -0,0 +1,86 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CHIPCert_error_test_vectors.h" + +namespace chip { +namespace TestCerts { + +const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_Cert_Array[275] = { + 0x15, 0x30, 0x01, 0x08, 0x3b, 0x75, 0x5b, 0xc1, 0x54, 0xbe, 0x8b, 0x60, 0x24, 0x02, 0x01, 0x37, 0x03, 0x27, 0x13, 0x03, + 0x00, 0x00, 0x00, 0xca, 0xca, 0xca, 0xca, 0x18, 0x26, 0x04, 0xef, 0x17, 0x1b, 0x27, 0x26, 0x05, 0x6e, 0xb5, 0xb9, 0x4c, + 0x37, 0x06, 0x27, 0x11, 0x01, 0x00, 0xee, 0xee, 0xde, 0xde, 0xde, 0xde, 0x27, 0x15, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xb0, 0xfa, 0x26, 0x16, 0x00, 0x00, 0xcd, 0xab, 0x18, 0x24, 0x07, 0x01, 0x24, 0x08, 0x01, 0x30, 0x09, 0x41, 0x04, 0xff, + 0x23, 0x5d, 0x91, 0xf3, 0x3c, 0x04, 0x7f, 0x82, 0xf0, 0x2c, 0x86, 0x70, 0xbb, 0xd2, 0xb4, 0x34, 0x42, 0x44, 0x6b, 0xc2, + 0xb8, 0x38, 0x6a, 0x36, 0x90, 0x92, 0x36, 0xed, 0x5b, 0x4e, 0x63, 0x81, 0xe6, 0x6b, 0x30, 0x0f, 0x45, 0x75, 0xab, 0x88, + 0xba, 0xcb, 0xad, 0x4e, 0x5f, 0xfe, 0x7e, 0x83, 0x5f, 0xb3, 0xf1, 0xb3, 0x3a, 0x5d, 0xa7, 0x83, 0xf5, 0x07, 0x0e, 0xcf, + 0x91, 0xfb, 0x2b, 0x37, 0x0a, 0x35, 0x01, 0x28, 0x01, 0x18, 0x24, 0x02, 0x01, 0x36, 0x03, 0x04, 0x02, 0x04, 0x01, 0x18, + 0x30, 0x04, 0x14, 0x1b, 0xc5, 0xf6, 0x12, 0xf9, 0x6a, 0x92, 0xa1, 0x14, 0xbf, 0x2c, 0x77, 0x2f, 0x13, 0xcd, 0xd7, 0x14, + 0x2f, 0x14, 0xc8, 0x30, 0x05, 0x14, 0x34, 0x3d, 0xbd, 0x99, 0xff, 0xaf, 0xb3, 0x1e, 0xeb, 0x4f, 0x81, 0x4f, 0x6f, 0x9a, + 0x14, 0x74, 0xf1, 0xd4, 0x4f, 0xeb, 0x18, 0x30, 0x0b, 0x40, 0xb9, 0x80, 0xee, 0x56, 0xf7, 0x61, 0xd6, 0x85, 0xf8, 0x55, + 0x1a, 0x57, 0x07, 0xc1, 0x25, 0xbd, 0x6a, 0xc7, 0x80, 0x46, 0x85, 0x5b, 0x80, 0x28, 0x2e, 0x35, 0xf0, 0x60, 0x72, 0x68, + 0xf5, 0xe7, 0xd9, 0x9f, 0x3e, 0x27, 0xef, 0x29, 0x8b, 0xbf, 0x73, 0x05, 0xa2, 0x56, 0x73, 0x6e, 0xac, 0x8c, 0xd9, 0xf2, + 0xca, 0x59, 0x6e, 0xb1, 0x75, 0xf5, 0x74, 0xa9, 0x2e, 0x27, 0x1b, 0x7a, 0xba, 0x20, 0x18, +}; +const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_PublicKey_Array[65] = { + 0x04, 0xff, 0x23, 0x5d, 0x91, 0xf3, 0x3c, 0x04, 0x7f, 0x82, 0xf0, 0x2c, 0x86, 0x70, 0xbb, 0xd2, 0xb4, + 0x34, 0x42, 0x44, 0x6b, 0xc2, 0xb8, 0x38, 0x6a, 0x36, 0x90, 0x92, 0x36, 0xed, 0x5b, 0x4e, 0x63, 0x81, + 0xe6, 0x6b, 0x30, 0x0f, 0x45, 0x75, 0xab, 0x88, 0xba, 0xcb, 0xad, 0x4e, 0x5f, 0xfe, 0x7e, 0x83, 0x5f, + 0xb3, 0xf1, 0xb3, 0x3a, 0x5d, 0xa7, 0x83, 0xf5, 0x07, 0x0e, 0xcf, 0x91, 0xfb, 0x2b, +}; +const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_PrivateKey_Array[32] = { + 0x95, 0xa3, 0xa7, 0x23, 0x79, 0x8a, 0xf7, 0x44, 0x17, 0xe2, 0xa5, 0xa4, 0x4d, 0x18, 0xb5, 0x32, + 0x3e, 0x24, 0x83, 0x60, 0xcf, 0xdc, 0x5a, 0x13, 0xd1, 0xa8, 0xb4, 0x0a, 0x65, 0xe4, 0x0f, 0xbc, +}; + +ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_Cert = ByteSpan(kTestErrorCert_NOC_0001_InvCATVerZero_Cert_Array); +ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_PrivateKey = ByteSpan(kTestErrorCert_NOC_0001_InvCATVerZero_PrivateKey_Array); +ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_PublicKey = ByteSpan(kTestErrorCert_NOC_0001_InvCATVerZero_PublicKey_Array); + +const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_Cert_Array[287] = { + 0x15, 0x30, 0x01, 0x08, 0x32, 0xf6, 0xd3, 0x7c, 0xfa, 0xd4, 0x71, 0xe2, 0x24, 0x02, 0x01, 0x37, 0x03, 0x27, 0x13, 0x03, 0x00, + 0x00, 0x00, 0xca, 0xca, 0xca, 0xca, 0x18, 0x26, 0x04, 0xef, 0x17, 0x1b, 0x27, 0x26, 0x05, 0x6e, 0xb5, 0xb9, 0x4c, 0x37, 0x06, + 0x26, 0x16, 0x04, 0x00, 0xcd, 0xab, 0x26, 0x16, 0x10, 0x00, 0x22, 0x22, 0x26, 0x16, 0x02, 0x00, 0xcd, 0xab, 0x27, 0x11, 0x02, + 0x00, 0xee, 0xee, 0xde, 0xde, 0xde, 0xde, 0x27, 0x15, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xfa, 0x18, 0x24, 0x07, 0x01, + 0x24, 0x08, 0x01, 0x30, 0x09, 0x41, 0x04, 0x0e, 0x40, 0x23, 0x8b, 0xf6, 0x6e, 0xb5, 0x26, 0x01, 0xc4, 0xac, 0x57, 0x97, 0xdd, + 0xc3, 0x9a, 0x1a, 0x01, 0x85, 0xf4, 0x38, 0x34, 0x0d, 0xd5, 0x00, 0x5b, 0x8e, 0x23, 0x26, 0x18, 0x47, 0x3c, 0x47, 0x71, 0xb6, + 0x05, 0xe2, 0xab, 0x73, 0xd7, 0x95, 0xbf, 0xfc, 0x55, 0xdf, 0x31, 0x51, 0x40, 0xb0, 0xdd, 0x16, 0x47, 0x58, 0xca, 0xfd, 0x3c, + 0x65, 0x00, 0xa9, 0x0a, 0x9a, 0x85, 0xec, 0x72, 0x37, 0x0a, 0x35, 0x01, 0x28, 0x01, 0x18, 0x24, 0x02, 0x01, 0x36, 0x03, 0x04, + 0x02, 0x04, 0x01, 0x18, 0x30, 0x04, 0x14, 0x19, 0xf2, 0x66, 0x00, 0x88, 0xac, 0x5a, 0xf5, 0xd0, 0xac, 0xa3, 0xf8, 0xac, 0xec, + 0xb8, 0x6e, 0x3f, 0xe2, 0x77, 0x4f, 0x30, 0x05, 0x14, 0x34, 0x3d, 0xbd, 0x99, 0xff, 0xaf, 0xb3, 0x1e, 0xeb, 0x4f, 0x81, 0x4f, + 0x6f, 0x9a, 0x14, 0x74, 0xf1, 0xd4, 0x4f, 0xeb, 0x18, 0x30, 0x0b, 0x40, 0xba, 0xa7, 0xaf, 0x18, 0x14, 0x52, 0xe6, 0x06, 0x2d, + 0xd1, 0x0a, 0x9a, 0x6d, 0xd0, 0x5d, 0xdb, 0xc2, 0x2c, 0xd3, 0x17, 0x94, 0x75, 0x9e, 0x1c, 0x2b, 0x57, 0x4e, 0xbb, 0xd6, 0xa4, + 0x58, 0x52, 0x5a, 0x80, 0x60, 0xf2, 0x43, 0x21, 0x4b, 0xba, 0x9d, 0xd2, 0xc9, 0x2a, 0x3b, 0xe6, 0xab, 0x22, 0xe9, 0xe2, 0x29, + 0xc2, 0x13, 0xac, 0xf0, 0xf2, 0x4e, 0x6c, 0x63, 0xea, 0xfa, 0xb8, 0xaa, 0xc3, 0x18, +}; +const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_PublicKey_Array[65] = { + 0x04, 0x0e, 0x40, 0x23, 0x8b, 0xf6, 0x6e, 0xb5, 0x26, 0x01, 0xc4, 0xac, 0x57, 0x97, 0xdd, 0xc3, 0x9a, + 0x1a, 0x01, 0x85, 0xf4, 0x38, 0x34, 0x0d, 0xd5, 0x00, 0x5b, 0x8e, 0x23, 0x26, 0x18, 0x47, 0x3c, 0x47, + 0x71, 0xb6, 0x05, 0xe2, 0xab, 0x73, 0xd7, 0x95, 0xbf, 0xfc, 0x55, 0xdf, 0x31, 0x51, 0x40, 0xb0, 0xdd, + 0x16, 0x47, 0x58, 0xca, 0xfd, 0x3c, 0x65, 0x00, 0xa9, 0x0a, 0x9a, 0x85, 0xec, 0x72, +}; +const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_PrivateKey_Array[32] = { + 0x06, 0x1b, 0x1e, 0xfd, 0xac, 0x86, 0x9b, 0xec, 0xd3, 0x82, 0xc9, 0x8e, 0x63, 0x63, 0x2a, 0x35, + 0xc2, 0xef, 0x2a, 0x8e, 0x2d, 0xb1, 0x7b, 0x72, 0x55, 0x8c, 0x1c, 0xc7, 0x83, 0x8e, 0xaa, 0xee, +}; + +ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_Cert = ByteSpan(kTestErrorCert_NOC_0002_InvCATMulVers_Cert_Array); +ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_PrivateKey = ByteSpan(kTestErrorCert_NOC_0002_InvCATMulVers_PrivateKey_Array); +ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_PublicKey = ByteSpan(kTestErrorCert_NOC_0002_InvCATMulVers_PublicKey_Array); + +} // namespace TestCerts +} // namespace chip diff --git a/src/credentials/tests/CHIPCert_error_test_vectors.h b/src/credentials/tests/CHIPCert_error_test_vectors.h new file mode 100644 index 00000000000000..49f46f5121622f --- /dev/null +++ b/src/credentials/tests/CHIPCert_error_test_vectors.h @@ -0,0 +1,44 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include + +namespace chip { +namespace TestCerts { + +extern const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_Cert_Array[275]; +extern const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_PublicKey_Array[65]; +extern const uint8_t kTestErrorCert_NOC_0001_InvCATVerZero_PrivateKey_Array[32]; + +extern ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_Cert; +extern ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_PrivateKey; +extern ByteSpan kTestErrorCert_NOC_0001_InvCATVerZero_PublicKey; + +extern const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_Cert_Array[287]; +extern const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_PublicKey_Array[65]; +extern const uint8_t kTestErrorCert_NOC_0002_InvCATMulVers_PrivateKey_Array[32]; + +extern ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_Cert; +extern ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_PrivateKey; +extern ByteSpan kTestErrorCert_NOC_0002_InvCATMulVers_PublicKey; + +} // namespace TestCerts +} // namespace chip diff --git a/src/credentials/tests/TestChipCert.cpp b/src/credentials/tests/TestChipCert.cpp index afdf04caf166ee..97c00d7ca555bd 100644 --- a/src/credentials/tests/TestChipCert.cpp +++ b/src/credentials/tests/TestChipCert.cpp @@ -37,6 +37,7 @@ #include +#include "CHIPCert_error_test_vectors.h" #include "CHIPCert_test_vectors.h" using namespace chip; @@ -1765,7 +1766,7 @@ static void TestChipCert_ExtractOperationalDiscoveryId(nlTestSuite * inSuite, vo } } -static void TestChipCert_ExtractCATsFromOpCert(nlTestSuite * inSuite, void * inContext) +static void TestChipCert_ExtractAndValidateCATsFromOpCert(nlTestSuite * inSuite, void * inContext) { struct TestCase { @@ -1835,6 +1836,20 @@ static void TestChipCert_ExtractCATsFromOpCert(nlTestSuite * inSuite, void * inC certSet.Release(); } + + // Error case: NOC with invalid CAT version. + { + CATValues cats; + CHIP_ERROR err = ExtractCATsFromOpCert(kTestErrorCert_NOC_0001_InvCATVerZero_Cert, cats); + NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT); + } + + // Error case: NOC with multiple versions of the same CAT tag. + { + CATValues cats; + CHIP_ERROR err = ExtractCATsFromOpCert(kTestErrorCert_NOC_0002_InvCATMulVers_Cert, cats); + NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_WRONG_CERT_DN); + } } static void TestChipCert_ExtractSubjectDNFromChipCert(nlTestSuite * inSuite, void * inContext) @@ -2007,7 +2022,7 @@ static const nlTest sTests[] = { NL_TEST_DEF("Test CHIP Verify Generated Cert Chain No ICA", TestChipCert_VerifyGeneratedCertsNoICA), NL_TEST_DEF("Test extracting Node ID and Fabric ID from node certificate", TestChipCert_ExtractNodeIdFabricId), NL_TEST_DEF("Test extracting Operational Discovery ID from node and root certificate", TestChipCert_ExtractOperationalDiscoveryId), - NL_TEST_DEF("Test extracting CASE Authenticated Tags from node certificate", TestChipCert_ExtractCATsFromOpCert), + NL_TEST_DEF("Test extracting and validating CASE Authenticated Tags from NOC", TestChipCert_ExtractAndValidateCATsFromOpCert), NL_TEST_DEF("Test extracting Subject DN from chip certificate", TestChipCert_ExtractSubjectDNFromChipCert), NL_TEST_DEF("Test extracting PublicKey and SKID from chip certificate", TestChipCert_ExtractPublicKeyAndSKID), NL_TEST_SENTINEL() diff --git a/src/tools/chip-cert/Cmd_GenCert.cpp b/src/tools/chip-cert/Cmd_GenCert.cpp index 75153464084414..baed859b89143b 100644 --- a/src/tools/chip-cert/Cmd_GenCert.cpp +++ b/src/tools/chip-cert/Cmd_GenCert.cpp @@ -141,7 +141,6 @@ const char * const gCmdOptionHelp = " x509-der - X.509 DER raw format\n" " x509-hex - X.509 DER hex encoded format\n" " chip - raw CHIP TLV format\n" - " chip-hex - hex encoded CHIP TLV format\n" " chip-b64 - base-64 encoded CHIP TLV format (default)\n" " chip-hex - hex encoded CHIP TLV format\n" "\n" diff --git a/src/tools/chip-cert/Cmd_PrintCert.cpp b/src/tools/chip-cert/Cmd_PrintCert.cpp index 298606136660cd..2bef2ed868c2a2 100644 --- a/src/tools/chip-cert/Cmd_PrintCert.cpp +++ b/src/tools/chip-cert/Cmd_PrintCert.cpp @@ -84,7 +84,7 @@ OptionSet *gCmdOptionSets[] = // clang-format on const char * gInFileName = nullptr; -const char * gOutFileName = nullptr; +const char * gOutFileName = "-"; bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg) {