cert.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package cli
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "zerotier/pkg/zerotier"
  20. )
  21. func Cert(basePath, authToken string, args []string, jsonOutput bool) int {
  22. if len(args) < 1 {
  23. Help()
  24. return 1
  25. }
  26. switch args[0] {
  27. case "newsid":
  28. if len(args) > 2 {
  29. Help()
  30. return 1
  31. }
  32. uniqueId, uniqueIdPrivate, err := zerotier.NewCertificateSubjectUniqueId(zerotier.CertificateUniqueIdTypeNistP384)
  33. if err != nil {
  34. fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
  35. return 1
  36. }
  37. sec, err := json.MarshalIndent(&zerotier.CertificateSubjectUniqueIDSecret{UniqueID: uniqueId, UniqueIDSecret: uniqueIdPrivate}, "", " ")
  38. if err != nil {
  39. fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
  40. return 1
  41. }
  42. if len(args) == 1 {
  43. fmt.Println(string(sec))
  44. } else {
  45. _ = ioutil.WriteFile(args[1], sec, 0600)
  46. }
  47. case "newcsr":
  48. if len(args) < 3 {
  49. Help()
  50. return 1
  51. }
  52. var cs zerotier.CertificateSubject
  53. csb, err := ioutil.ReadFile(args[1])
  54. if err != nil {
  55. fmt.Printf("ERROR: unable to read subject from %s: %s\n", args[1], err.Error())
  56. return 1
  57. }
  58. err = json.Unmarshal(csb, &cs)
  59. if err != nil {
  60. fmt.Printf("ERROR: unable to read subject from %s: %s\n", args[1], err.Error())
  61. return 1
  62. }
  63. var subj zerotier.CertificateSubjectUniqueIDSecret
  64. subjb, err := ioutil.ReadFile(args[2])
  65. if err != nil {
  66. fmt.Printf("ERROR: unable to read unique ID secret from %s: %s\n", args[2], err.Error())
  67. return 1
  68. }
  69. err = json.Unmarshal(subjb, &subj)
  70. if err != nil {
  71. fmt.Printf("ERROR: unable to read unique ID secret from %s: %s\n", args[2], err.Error())
  72. return 1
  73. }
  74. csr, err := zerotier.NewCertificateCSR(&cs, subj.UniqueID, subj.UniqueIDSecret)
  75. if err != nil {
  76. fmt.Printf("ERROR: problem creating CSR: %s\n", err.Error())
  77. return 1
  78. }
  79. if len(args) == 3 {
  80. _, _ = os.Stdout.Write(csr)
  81. } else {
  82. _ = ioutil.WriteFile(args[3], csr, 0644)
  83. }
  84. case "sign":
  85. case "verify":
  86. case "show":
  87. if len(args) != 1 {
  88. Help()
  89. return 1
  90. }
  91. case "import":
  92. case "restore":
  93. case "export":
  94. case "delete":
  95. }
  96. return 0
  97. }