| /* |
| * |
| * Copyright (c) 2021-2022 Project CHIP Authors |
| * Copyright (c) 2013-2017 Nest Labs, Inc. |
| * All rights reserved. |
| * |
| * 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. |
| */ |
| |
| /** |
| * @file |
| * This file implements the CHIP certificate (chip-cert) command line tool. |
| * |
| * The 'chip-cert' command line tool is used, primarily, for generating |
| * and manipulating CHIP certificate and private key material. |
| * |
| */ |
| |
| #include "chip-cert.h" |
| |
| namespace chip { |
| namespace Logging { |
| namespace Platform { |
| |
| void LogV(const char * module, uint8_t category, const char * msg, va_list v) {} |
| |
| } // namespace Platform |
| } // namespace Logging |
| } // namespace chip |
| |
| namespace { |
| |
| // clang-format off |
| const char * const sHelp = |
| "Usage: chip <command> [ <args...> ]\n" |
| "\n" |
| "Commands:\n" |
| "\n" |
| " gen-cert -- Generate a CHIP certificate.\n" |
| "\n" |
| " convert-cert -- Convert a certificate between CHIP and X509 form.\n" |
| "\n" |
| " convert-key -- Convert a private key between CHIP and PEM/DER form.\n" |
| "\n" |
| " resign-cert -- Resign a CHIP certificate using a new CA key.\n" |
| "\n" |
| " validate-cert -- Validate a CHIP certificate chain.\n" |
| "\n" |
| " print-cert -- Print a CHIP certificate.\n" |
| "\n" |
| " gen-att-cert -- Generate a CHIP attestation certificate.\n" |
| "\n" |
| " validate-att-cert -- Validate a CHIP attestation certificate chain.\n" |
| "\n" |
| " gen-cd -- Generate a CHIP certification declaration signed message.\n" |
| "\n" |
| " print-cd -- Print a CHIP certification declaration (CD) content.\n" |
| "\n" |
| " version -- Print the program version and exit.\n" |
| "\n"; |
| // clang-format on |
| |
| /** |
| * Print to standard output the program version information. |
| * |
| * @return Unconditionally returns true. |
| * |
| */ |
| bool PrintVersion() |
| { |
| printf("chip " CHIP_VERSION_STRING "\n" COPYRIGHT_STRING); |
| |
| return true; |
| } |
| |
| } // namespace |
| |
| int main(int argc, char * argv[]) |
| { |
| bool res = false; |
| |
| chip::Platform::MemoryInit(); |
| |
| if (argc == 1) |
| { |
| fprintf(stderr, "Please specify a command, or 'help' for help.\n"); |
| } |
| else if (strcasecmp(argv[1], "help") == 0 || strcasecmp(argv[1], "--help") == 0 || strcasecmp(argv[1], "-h") == 0) |
| { |
| res = (fputs(sHelp, stdout) != EOF); |
| } |
| else if (strcasecmp(argv[1], "version") == 0 || strcasecmp(argv[1], "--version") == 0 || strcasecmp(argv[1], "-v") == 0) |
| { |
| res = PrintVersion(); |
| } |
| else if (strcasecmp(argv[1], "gen-cert") == 0 || strcasecmp(argv[1], "gencert") == 0) |
| { |
| res = Cmd_GenCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "convert-cert") == 0 || strcasecmp(argv[1], "convertcert") == 0) |
| { |
| res = Cmd_ConvertCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "convert-key") == 0 || strcasecmp(argv[1], "convertkey") == 0) |
| { |
| res = Cmd_ConvertKey(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "resign-cert") == 0 || strcasecmp(argv[1], "resigncert") == 0) |
| { |
| res = Cmd_ResignCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "validate-cert") == 0 || strcasecmp(argv[1], "validatecert") == 0) |
| { |
| res = Cmd_ValidateCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "print-cert") == 0 || strcasecmp(argv[1], "printcert") == 0) |
| { |
| res = Cmd_PrintCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "gen-att-cert") == 0 || strcasecmp(argv[1], "genattcert") == 0) |
| { |
| res = Cmd_GenAttCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "validate-att-cert") == 0 || strcasecmp(argv[1], "validateattcert") == 0) |
| { |
| res = Cmd_ValidateAttCert(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "gen-cd") == 0 || strcasecmp(argv[1], "gencd") == 0) |
| { |
| res = Cmd_GenCD(argc - 1, argv + 1); |
| } |
| else if (strcasecmp(argv[1], "print-cd") == 0 || strcasecmp(argv[1], "printcd") == 0) |
| { |
| res = Cmd_PrintCD(argc - 1, argv + 1); |
| } |
| else |
| { |
| fprintf(stderr, "Unrecognized command: %s\n", argv[1]); |
| } |
| |
| return res ? 0 : -1; |
| } |