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