identity.go 3.5 KB

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