-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
122 lines (112 loc) · 3.78 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
// MIT License
//
// Copyright (c) 2023 TTBT Enterprises LLC
// Copyright (c) 2023 Robin Thellend <rthellend@rthellend.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//go:build wasm
// clientwasm implements TLS key generation and PKCS12 packaging in a browser
// so that the private key is never copied over the network.
package main
import (
"fmt"
"net/url"
"syscall/js"
"github.com/c2FmZQ/tlsproxy/proxy/internal/pki/clientwasm/impl"
)
var (
jsUint8Array = js.Global().Get("Uint8Array")
jsResponse = js.Global().Get("Response")
)
func main() {
pkiApp := js.Global().Get("pkiApp")
if pkiApp.Type() != js.TypeObject {
panic("pkiApp object not found")
}
ready := pkiApp.Get("pkiwasmIsReady")
if ready.Type() != js.TypeFunction {
panic("pkiApp.pkiwasmIsReady not found")
}
pkiApp.Set("makeCertificateRequest", js.FuncOf(makeCSR))
pkiApp.Set("makeResponse", js.FuncOf(makeResponse))
ready.Invoke()
<-make(chan struct{})
}
func makeCSR(this js.Value, args []js.Value) any {
// arg0: id (a unique ID to match makeCSR with makeP12)
// arg2: application/x-www-form-urlencoded arguments
if len(args) != 2 || args[0].Type() != js.TypeNumber || args[1].Type() != js.TypeString {
fmt.Println("makeCertificateRequest: unexpected arguments")
return js.Undefined()
}
form, err := url.ParseQuery(args[1].String())
if err != nil {
fmt.Printf("ParseQuery(%q): %v\n", args[1].String(), err)
return js.Undefined()
}
keyType := form.Get("keytype")
if keyType == "" {
keyType = "ecdsa-p256"
}
format := form.Get("format")
if format == "" {
format = "gpg"
}
password := form.Get("password")
if password == "" {
fmt.Println("password is missing")
return js.Undefined()
}
resp, err := impl.MakeCSR(args[0].Int(), keyType, format, form.Get("label"), form.Get("dnsname"), password)
if err != nil {
fmt.Println(err)
return js.Undefined()
}
return Uint8ArrayFromBytes(resp)
}
func makeResponse(this js.Value, args []js.Value) any {
// arg0: id (the same id used with makeCSR)
// arg1: the pem-encoded cert
if len(args) != 2 || args[0].Type() != js.TypeNumber || args[1].Type() != js.TypeString {
fmt.Println("makeResponse: unexpected argument")
return js.Undefined()
}
body, contentType, fileName, err := impl.MakeResponse(args[0].Int(), args[1].String())
if err != nil {
fmt.Println(err)
return js.Undefined()
}
return jsResponse.New(
Uint8ArrayFromBytes(body),
js.ValueOf(map[string]any{
"status": 200,
"statusText": "OK",
"headers": map[string]any{
"content-type": contentType,
"content-disposition": `attachment; filename="` + fileName + `"`,
"cache-control": "private, no-store",
},
}),
)
}
func Uint8ArrayFromBytes(in []byte) js.Value {
out := jsUint8Array.New(js.ValueOf(len(in)))
js.CopyBytesToJS(out, in)
return out
}