cert.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: 2025-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. "zerotier/pkg/zerotier"
  19. )
  20. func Cert(basePath, authToken string, args []string, jsonOutput bool) int {
  21. if len(args) < 1 {
  22. Help()
  23. return 1
  24. }
  25. switch args[0] {
  26. case "newsid":
  27. if len(args) > 2 {
  28. Help()
  29. return 1
  30. }
  31. uniqueId, uniqueIdPrivate, err := zerotier.NewCertificateSubjectUniqueId(zerotier.CertificateUniqueIdTypeNistP384)
  32. if err != nil {
  33. fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
  34. return 1
  35. }
  36. sec, err := json.MarshalIndent(&zerotier.CertificateSubjectUniqueIDSecret{UniqueID: uniqueId, UniqueIDSecret: uniqueIdPrivate}, "", " ")
  37. if err != nil {
  38. fmt.Printf("ERROR: unable to create unique ID and private key: %s\n", err.Error())
  39. return 1
  40. }
  41. if len(args) == 1 {
  42. fmt.Println(string(sec))
  43. } else {
  44. _ = ioutil.WriteFile(args[1], sec, 0600)
  45. }
  46. case "newcsr":
  47. if len(args) != 4 {
  48. Help()
  49. return 1
  50. }
  51. var cs zerotier.CertificateSubject
  52. err := readJSONFile(args[1], &cs)
  53. if err != nil {
  54. fmt.Printf("ERROR: unable to read subject from %s: %s\n", args[1], err.Error())
  55. return 1
  56. }
  57. var subj zerotier.CertificateSubjectUniqueIDSecret
  58. err = readJSONFile(args[2], &subj)
  59. if err != nil {
  60. fmt.Printf("ERROR: unable to read unique ID secret from %s: %s\n", args[2], err.Error())
  61. return 1
  62. }
  63. csr, err := zerotier.NewCertificateCSR(&cs, subj.UniqueID, subj.UniqueIDSecret)
  64. if err != nil {
  65. fmt.Printf("ERROR: problem creating CSR: %s\n", err.Error())
  66. return 1
  67. }
  68. err = ioutil.WriteFile(args[3], csr, 0644)
  69. if err == nil {
  70. fmt.Printf("Wrote CSR to %s\n", args[3])
  71. } else {
  72. fmt.Printf("ERROR: unable to write CSR to %s: %s\n", args[3], err.Error())
  73. return 1
  74. }
  75. case "sign":
  76. if len(args) != 4 {
  77. Help()
  78. return 1
  79. }
  80. var csr zerotier.Certificate
  81. csrBytes, err := ioutil.ReadFile(args[1])
  82. if err != nil {
  83. fmt.Printf("ERROR: unable to read CSR from %s: %s\n", args[1], err.Error())
  84. return 1
  85. }
  86. c, err := zerotier.NewCertificateFromBytes(csrBytes, false)
  87. if err != nil {
  88. fmt.Printf("ERROR: CSR in %s is invalid: %s\n", args[1], err.Error())
  89. return 1
  90. }
  91. id := readIdentity(args[2])
  92. if id == nil {
  93. fmt.Printf("ERROR: unable to read identity from %s\n", args[2])
  94. return 1
  95. }
  96. if !id.HasPrivate() {
  97. fmt.Printf("ERROR: signing identity in %s lacks private key\n", args[2])
  98. return 1
  99. }
  100. c, err = csr.Sign(id)
  101. if err != nil {
  102. fmt.Printf("ERROR: error signing CSR or generating certificate: %s\n", err.Error())
  103. return 1
  104. }
  105. cb, err := c.Marshal()
  106. if err != nil {
  107. fmt.Printf("ERROR: error marshaling signed certificate: %s\n", err.Error())
  108. return 1
  109. }
  110. err = ioutil.WriteFile(args[3], cb, 0644)
  111. if err == nil {
  112. fmt.Printf("Wrote signed certificate to %s\n", args[3])
  113. } else {
  114. fmt.Printf("ERROR: unable to write signed certificate to %s: %s\n", args[3], err.Error())
  115. return 1
  116. }
  117. case "verify":
  118. case "show":
  119. if len(args) != 1 {
  120. Help()
  121. return 1
  122. }
  123. case "import":
  124. case "restore":
  125. case "export":
  126. case "delete":
  127. }
  128. return 0
  129. }