Skip to content

Commit 2041313

Browse files
emargolispull[bot]
authored andcommitted
Added CD Generation Option to the chip-cert Tool. (#10475)
-- added gen-cd command -- Used new tool command to generate Test CD samples, which can be found in credentials/test/certification-declaration/ -- added script that was used to generate Test CD samples: credentials/test/gen-test-cds.sh -- added new functions to parse uint16_t and uint8_t integers from string -- minor chip-cert tool code cleanups
1 parent d4927bb commit 2041313

24 files changed

+589
-62
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBszCCAVqgAwIBAgIIRdrzneR6oI8wCgYIKoZIzj0EAwIwKzEpMCcGA1UEAwwg
3+
TWF0dGVyIFRlc3QgQ0QgU2lnbmluZyBBdXRob3JpdHkwIBcNMjEwNjI4MTQyMzQz
4+
WhgPOTk5OTEyMzEyMzU5NTlaMCsxKTAnBgNVBAMMIE1hdHRlciBUZXN0IENEIFNp
5+
Z25pbmcgQXV0aG9yaXR5MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPDmJIkUr
6+
VcrzicJb0bykZWlSzLkOiGkkmthHRlMBTL+V1oeWXgNrUhxRA35rjO3vyh60QEZp
7+
T6CIgu7WUZ3suqNmMGQwEgYDVR0TAQH/BAgwBgEB/wIBATAOBgNVHQ8BAf8EBAMC
8+
AQYwHQYDVR0OBBYEFGL6gjNZrPqplj4c+hQK3fUE83FgMB8GA1UdIwQYMBaAFGL6
9+
gjNZrPqplj4c+hQK3fUE83FgMAoGCCqGSM49BAMCA0cAMEQCICxUXOTkV9im8NnZ
10+
u+vW7OHd/n+MbZps83UyH8b6xxOEAiBUB3jodDlyUn7t669YaGIgtUB48s1OYqdq
11+
58u5L/VMiw==
12+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEIK7zSEEW6UgexXvgRy30G/SZBk5QJK2GnspeiJgC1IB1oAoGCCqGSM49
3+
AwEHoUQDQgAEPDmJIkUrVcrzicJb0bykZWlSzLkOiGkkmthHRlMBTL+V1oeWXgNr
4+
UhxRA35rjO3vyh60QEZpT6CIgu7WUZ3sug==
5+
-----END EC PRIVATE KEY-----

credentials/test/gen-test-cds.sh

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Copyright (c) 2021 Project CHIP Authors
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
# Script that can be used to generate Certification Declaration (CD)
20+
# for testing purposes.
21+
# The script expects the path to the chip-cert tool binary as an input argument.
22+
#
23+
# Usage example when the script is run from the CHIP SDK root directory:
24+
# ./credentials/test/gen-test-cds.sh ./out/debug/standalone/chip-cert
25+
#
26+
# The result will be stored in:
27+
# credentials/test/certification-declaration
28+
#
29+
# If the intention is to re-generate a new set of CDs that replace the already
30+
# present ones then it is recommended to clear the folder:
31+
# rm credentials/test/certification-declaration/*
32+
#
33+
34+
set -e
35+
36+
here=${0%/*}
37+
38+
dest_dir="$here/certification-declaration"
39+
40+
mkdir -p "$dest_dir"
41+
42+
if [ $# == 1 ]; then
43+
chip_cert_tool=$1
44+
else
45+
echo "Error: Please specify exactly one input argument; the path to the chip-cert tool binary"
46+
exit
47+
fi
48+
49+
cert_valid_from="2021-06-28 14:23:43"
50+
cert_lifetime=4294967295
51+
52+
format_version=1
53+
vids=(FFF2 FFF3)
54+
pid0=8001
55+
pid1=8002
56+
device_type_id=0x1234
57+
certificate_id0="ZIG20141ZB330001-24"
58+
certificate_id1="ZIG20142ZB330002-24"
59+
security_level=0
60+
security_info=0
61+
version_num=9876
62+
certification_type=0
63+
dac_origin_vendor_id=0xFFF1
64+
dac_origin_product_id=0x8000
65+
66+
cd_signing_key="$dest_dir/Chip-Test-CD-Signing-Key.pem"
67+
cd_signing_cert="$dest_dir/Chip-Test-CD-Signing-Cert.pem"
68+
69+
# Using gen-att-cert command to generate CD Signing Cert/Key:
70+
"$chip_cert_tool" gen-att-cert --type a --subject-cn "Matter Test CD Signing Authority" --valid-from "$cert_valid_from" --lifetime "$cert_lifetime" --out-key "$cd_signing_key" --out "$cd_signing_cert"
71+
72+
for vid in "${vids[@]}"; do
73+
74+
"$chip_cert_tool" gen-cd --key "$cd_signing_key" --cert "$cd_signing_cert" --out "$dest_dir/Chip-Test-CD-$vid-$pid0.der" --format-version "$format_version" --vendor-id "0x$vid" --product-id "0x$pid0" --device-type-id "$device_type_id" --certificate-id "$certificate_id0" --security-level "$security_level" --security-info "$security_info" --version-number "$version_num" --certification-type "$certification_type"
75+
76+
"$chip_cert_tool" gen-cd --key "$cd_signing_key" --cert "$cd_signing_cert" --out "$dest_dir/Chip-Test-CD-$vid-$pid1.der" --format-version "$format_version" --vendor-id "0x$vid" --product-id "0x$pid1" --device-type-id "$device_type_id" --certificate-id "$certificate_id1" --security-level "$security_level" --security-info "$security_info" --version-number "$version_num" --certification-type "$certification_type"
77+
78+
"$chip_cert_tool" gen-cd --key "$cd_signing_key" --cert "$cd_signing_cert" --out "$dest_dir/Chip-Test-CD-$vid-$pid1-WithDACOrigin.der" --format-version "$format_version" --vendor-id "0x$vid" --product-id "0x$pid1" --device-type-id "$device_type_id" --certificate-id "$certificate_id1" --security-level "$security_level" --security-info "$security_info" --version-number "$version_num" --certification-type "$certification_type" --dac-origin-vendor-id "$dac_origin_vendor_id" --dac-origin-product-id "$dac_origin_product_id"
79+
80+
"$chip_cert_tool" gen-cd --key "$cd_signing_key" --cert "$cd_signing_cert" --out "$dest_dir/Chip-Test-CD-$vid-$pid0-$pid1.der" --format-version "$format_version" --vendor-id "0x$vid" --product-id "0x$pid0" --product-id "0x$pid1" --device-type-id "$device_type_id" --certificate-id "$certificate_id1" --security-level "$security_level" --security-info "$security_info" --version-number "$version_num" --certification-type "$certification_type"
81+
82+
"$chip_cert_tool" gen-cd --key "$cd_signing_key" --cert "$cd_signing_cert" --out "$dest_dir/Chip-Test-CD-$vid-$pid0-$pid1-WithDACOrigin.der" --format-version "$format_version" --vendor-id "0x$vid" --product-id "0x$pid0" --product-id "0x$pid1" --device-type-id "$device_type_id" --certificate-id "$certificate_id1" --security-level "$security_level" --security-info "$security_info" --version-number "$version_num" --certification-type "$certification_type" --dac-origin-vendor-id "$dac_origin_vendor_id" --dac-origin-product-id "$dac_origin_product_id"
83+
84+
done

src/lib/support/CHIPArgParser.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,64 @@ bool ParseInt(const char * str, int32_t & output, int base)
753753
return parseEnd > str && *parseEnd == 0 && ((v != LONG_MIN && v != LONG_MAX) || errno == 0);
754754
}
755755

756+
/**
757+
* Parse and attempt to convert a string to a 16-bit unsigned integer,
758+
* applying the appropriate interpretation based on the base parameter.
759+
*
760+
* @param[in] str A pointer to a NULL-terminated C string representing
761+
* the integer to parse.
762+
* @param[out] output A reference to storage for a 16-bit unsigned integer
763+
* to which the parsed value will be stored on success.
764+
* @param[in] base The base according to which the string should be
765+
* interpreted and parsed. If 0 or 16, the string may
766+
* be hexadecimal and prefixed with "0x". Otherwise, a 0
767+
* is implied as 10 unless a leading 0 is encountered in
768+
* which 8 is implied.
769+
*
770+
* @return true on success; otherwise, false on failure.
771+
*/
772+
bool ParseInt(const char * str, uint16_t & output, int base)
773+
{
774+
uint32_t v;
775+
776+
if (!ParseInt(str, v, base) || !CanCastTo<uint16_t>(v))
777+
{
778+
return false;
779+
}
780+
output = static_cast<uint16_t>(v);
781+
782+
return true;
783+
}
784+
785+
/**
786+
* Parse and attempt to convert a string to a 8-bit unsigned integer,
787+
* applying the appropriate interpretation based on the base parameter.
788+
*
789+
* @param[in] str A pointer to a NULL-terminated C string representing
790+
* the integer to parse.
791+
* @param[out] output A reference to storage for a 8-bit unsigned integer
792+
* to which the parsed value will be stored on success.
793+
* @param[in] base The base according to which the string should be
794+
* interpreted and parsed. If 0 or 16, the string may
795+
* be hexadecimal and prefixed with "0x". Otherwise, a 0
796+
* is implied as 10 unless a leading 0 is encountered in
797+
* which 8 is implied.
798+
*
799+
* @return true on success; otherwise, false on failure.
800+
*/
801+
bool ParseInt(const char * str, uint8_t & output, int base)
802+
{
803+
uint32_t v;
804+
805+
if (!ParseInt(str, v, base) || !CanCastTo<uint8_t>(v))
806+
{
807+
return false;
808+
}
809+
output = static_cast<uint8_t>(v);
810+
811+
return true;
812+
}
813+
756814
/**
757815
* Parse and attempt to convert a string interpreted as a decimal
758816
* value to a 64-bit unsigned integer, applying the appropriate

src/lib/support/CHIPArgParser.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ bool ParseInt(const char * str, uint16_t & output);
123123
bool ParseInt(const char * str, int32_t & output);
124124
bool ParseInt(const char * str, uint32_t & output);
125125
bool ParseInt(const char * str, uint64_t & output);
126+
bool ParseInt(const char * str, uint8_t & output, int base);
127+
bool ParseInt(const char * str, uint16_t & output, int base);
126128
bool ParseInt(const char * str, int32_t & output, int base);
127129
bool ParseInt(const char * str, uint32_t & output, int base);
128130
bool ParseInt(const char * str, uint64_t & output, int base);

src/tools/chip-cert/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ executable("chip-cert") {
2424
"Cmd_ConvertCert.cpp",
2525
"Cmd_ConvertKey.cpp",
2626
"Cmd_GenAttCert.cpp",
27+
"Cmd_GenCD.cpp",
2728
"Cmd_GenCert.cpp",
2829
"Cmd_PrintCert.cpp",
2930
"Cmd_ResignCert.cpp",

src/tools/chip-cert/CertUtils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace {
166166
CertFormat DetectCertFormat(uint8_t * cert, uint32_t certLen)
167167
{
168168
static const uint8_t chipRawPrefix[] = { 0x15, 0x30, 0x01 };
169-
static const char * chipB64Prefix = "FTABC";
169+
static const char * chipB64Prefix = "FTAB";
170170
static const size_t chipB64PrefixLen = strlen(chipB64Prefix);
171171
static const char * pemMarker = "-----BEGIN CERTIFICATE-----";
172172

src/tools/chip-cert/Cmd_GenAttCert.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ struct tm gValidFrom;
150150

151151
bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg)
152152
{
153-
uint64_t chip64bitAttr;
154-
155153
switch (id)
156154
{
157155
case 't':
@@ -181,20 +179,18 @@ bool HandleOption(const char * progName, OptionSet * optSet, int id, const char
181179
gSubjectCN = arg;
182180
break;
183181
case 'V':
184-
if (!ParseChip64bitAttr(arg, chip64bitAttr) || !chip::CanCastTo<uint16_t>(chip64bitAttr))
182+
if (!ParseInt(arg, gSubjectVID, 16))
185183
{
186184
PrintArgError("%s: Invalid value specified for the subject VID attribute: %s\n", progName, arg);
187185
return false;
188186
}
189-
gSubjectVID = static_cast<uint16_t>(chip64bitAttr);
190187
break;
191188
case 'P':
192-
if (!ParseChip64bitAttr(arg, chip64bitAttr) || !chip::CanCastTo<uint16_t>(chip64bitAttr))
189+
if (!ParseInt(arg, gSubjectPID, 16))
193190
{
194191
PrintArgError("%s: Invalid value specified for the subject PID attribute: %s\n", progName, arg);
195192
return false;
196193
}
197-
gSubjectPID = static_cast<uint16_t>(chip64bitAttr);
198194
break;
199195
case 'k':
200196
gInKeyFileName = arg;

0 commit comments

Comments
 (0)