main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2019, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package main
  20. import (
  21. "flag"
  22. "fmt"
  23. "os"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  25. )
  26. func main() {
  27. var publicKey string
  28. flag.StringVar(&publicKey, "public-key", "", "server entry signing public key")
  29. var privateKey string
  30. flag.StringVar(&privateKey, "private-key", "", "server entry signing private key")
  31. var encodedServerEntry string
  32. flag.StringVar(&encodedServerEntry, "server-entry", "", "encoded server entry")
  33. flag.Usage = func() {
  34. fmt.Fprintf(os.Stderr,
  35. "Usage:\n\n"+
  36. "%s <flags> generate generates and outputs a signing key pair\n"+
  37. "%s <flags> sign signs a specified server entry with a specified key pair\n\n",
  38. os.Args[0], os.Args[0])
  39. flag.PrintDefaults()
  40. }
  41. flag.Parse()
  42. args := flag.Args()
  43. var command string
  44. if len(args) >= 1 {
  45. command = args[0]
  46. }
  47. envPublicKey := os.Getenv("SIGNER_PUBLIC_KEY")
  48. if envPublicKey != "" {
  49. publicKey = envPublicKey
  50. }
  51. envPrivateKey := os.Getenv("SIGNER_PRIVATE_KEY")
  52. if envPrivateKey != "" {
  53. privateKey = envPrivateKey
  54. }
  55. envEncodedServerEntry := os.Getenv("SIGNER_SERVER_ENTRY")
  56. if envEncodedServerEntry != "" {
  57. encodedServerEntry = envEncodedServerEntry
  58. }
  59. var err error
  60. switch command {
  61. case "generate":
  62. err = generate()
  63. case "sign":
  64. if publicKey == "" || privateKey == "" || encodedServerEntry == "" {
  65. flag.Usage()
  66. os.Exit(1)
  67. }
  68. err = sign(publicKey, privateKey, encodedServerEntry)
  69. default:
  70. flag.Usage()
  71. os.Exit(1)
  72. }
  73. if err != nil {
  74. fmt.Printf("%s\n", err)
  75. os.Exit(1)
  76. }
  77. }
  78. func generate() error {
  79. publicKey, privateKey, err := protocol.NewServerEntrySignatureKeyPair()
  80. if err != nil {
  81. return fmt.Errorf("generate key pair failed: %s", err)
  82. }
  83. fmt.Printf("public-key: %s\nprivate-key: %s\n\n", publicKey, privateKey)
  84. return nil
  85. }
  86. func sign(publicKey, privateKey, encodedServerEntry string) error {
  87. serverEntryFields, err := protocol.DecodeServerEntryFields(encodedServerEntry, "", "")
  88. if err != nil {
  89. return fmt.Errorf("decode server entry failed: %s", err)
  90. }
  91. err = serverEntryFields.AddSignature(publicKey, privateKey)
  92. if err != nil {
  93. return fmt.Errorf("add signature failed: %s", err)
  94. }
  95. encodedSignedServerEntry, err := protocol.EncodeServerEntryFields(serverEntryFields)
  96. if err != nil {
  97. return fmt.Errorf("encode server entry failed: %s", err)
  98. }
  99. fmt.Printf("%s\n\n", encodedSignedServerEntry)
  100. return nil
  101. }