identity.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c)2019 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: 2023-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/hex"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "zerotier/pkg/zerotier"
  21. )
  22. // Identity command
  23. func Identity(args []string) {
  24. if len(args) > 0 {
  25. switch args[0] {
  26. case "new":
  27. idType := zerotier.IdentityTypeC25519
  28. if len(args) > 1 {
  29. if len(args) > 2 {
  30. Help()
  31. os.Exit(1)
  32. }
  33. switch args[1] {
  34. case "c25519":
  35. case "p384":
  36. idType = zerotier.IdentityTypeP384
  37. default:
  38. Help()
  39. os.Exit(1)
  40. }
  41. }
  42. id, err := zerotier.NewIdentity(idType)
  43. if err != nil {
  44. fmt.Printf("ERROR: internal error generating identity: %s\n", err.Error())
  45. os.Exit(1)
  46. }
  47. fmt.Println(id.PrivateKeyString())
  48. os.Exit(0)
  49. case "getpublic":
  50. if len(args) == 2 {
  51. idData, err := ioutil.ReadFile(args[1])
  52. if err != nil {
  53. fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
  54. os.Exit(1)
  55. }
  56. id, err := zerotier.NewIdentityFromString(string(idData))
  57. if err != nil {
  58. fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
  59. os.Exit(1)
  60. }
  61. fmt.Println(id.String())
  62. os.Exit(0)
  63. }
  64. case "validate":
  65. if len(args) == 2 {
  66. idData, err := ioutil.ReadFile(args[1])
  67. if err != nil {
  68. fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
  69. os.Exit(1)
  70. }
  71. id, err := zerotier.NewIdentityFromString(string(idData))
  72. if err != nil {
  73. fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
  74. os.Exit(1)
  75. }
  76. if id.LocallyValidate() {
  77. fmt.Println("OK")
  78. os.Exit(0)
  79. }
  80. fmt.Println("FAILED")
  81. os.Exit(1)
  82. }
  83. case "sign", "verify":
  84. if len(args) > 2 {
  85. idData, err := ioutil.ReadFile(args[1])
  86. if err != nil {
  87. fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
  88. os.Exit(1)
  89. }
  90. id, err := zerotier.NewIdentityFromString(string(idData))
  91. if err != nil {
  92. fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
  93. os.Exit(1)
  94. }
  95. msg, err := ioutil.ReadFile(args[2])
  96. if err != nil {
  97. fmt.Printf("ERROR: unable to read input file: %s\n", err.Error())
  98. os.Exit(1)
  99. }
  100. if args[0] == "verify" {
  101. if len(args) == 4 {
  102. sig, err := hex.DecodeString(strings.TrimSpace(args[3]))
  103. if err != nil {
  104. fmt.Println("FAILED")
  105. os.Exit(1)
  106. }
  107. if id.Verify(msg, sig) {
  108. fmt.Println("OK")
  109. os.Exit(0)
  110. }
  111. }
  112. fmt.Println("FAILED")
  113. os.Exit(1)
  114. } else {
  115. sig, err := id.Sign(msg)
  116. if err != nil {
  117. fmt.Printf("ERROR: internal error signing message: %s\n", err.Error())
  118. os.Exit(1)
  119. }
  120. fmt.Println(hex.EncodeToString(sig))
  121. os.Exit(0)
  122. }
  123. }
  124. }
  125. }
  126. Help()
  127. os.Exit(1)
  128. }