identity.go 2.2 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/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. fmt.Println(readIdentity(args[1]).String())
  52. os.Exit(0)
  53. }
  54. case "validate":
  55. if len(args) == 2 {
  56. if readIdentity(args[1]).LocallyValidate() {
  57. fmt.Println("OK")
  58. os.Exit(0)
  59. }
  60. fmt.Println("FAILED")
  61. os.Exit(1)
  62. }
  63. case "sign", "verify":
  64. if len(args) > 2 {
  65. id := readIdentity(args[1])
  66. msg, err := ioutil.ReadFile(args[2])
  67. if err != nil {
  68. fmt.Printf("ERROR: unable to read input file: %s\n", err.Error())
  69. os.Exit(1)
  70. }
  71. if args[0] == "verify" {
  72. if len(args) == 4 {
  73. sig, err := hex.DecodeString(strings.TrimSpace(args[3]))
  74. if err != nil {
  75. fmt.Println("FAILED")
  76. os.Exit(1)
  77. }
  78. if id.Verify(msg, sig) {
  79. fmt.Println("OK")
  80. os.Exit(0)
  81. }
  82. }
  83. fmt.Println("FAILED")
  84. os.Exit(1)
  85. } else {
  86. sig, err := id.Sign(msg)
  87. if err != nil {
  88. fmt.Printf("ERROR: internal error signing message: %s\n", err.Error())
  89. os.Exit(1)
  90. }
  91. fmt.Println(hex.EncodeToString(sig))
  92. os.Exit(0)
  93. }
  94. }
  95. case "makeroot":
  96. if len(args) >= 2 {
  97. }
  98. }
  99. }
  100. Help()
  101. os.Exit(1)
  102. }