-
Notifications
You must be signed in to change notification settings - Fork 8
/
sign.go
177 lines (146 loc) · 3.41 KB
/
sign.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2021 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpsig
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type sigImpl struct {
w io.Writer
sign func() []byte
}
type sigHolder struct {
alg string
signer func() sigImpl
}
type signer struct {
headers []string
keys map[string]sigHolder
// For testing
nowFunc func() time.Time
}
func (s *signer) Sign(msg *message) (http.Header, error) {
var b bytes.Buffer
var items []string
// canonicalize headers
for _, h := range s.headers {
// Skip unset headers
if len(h) > 0 && h[0] != '@' && len(msg.Header.Values(h)) == 0 {
continue
}
// handle specialty components, section 2.3
var err error
switch h {
case "@method":
err = canonicalizeMethod(&b, msg.Method)
case "@path":
err = canonicalizePath(&b, msg.URL.Path)
case "@query":
err = canonicalizeQuery(&b, msg.URL.RawQuery)
case "@authority":
err = canonicalizeAuthority(&b, msg.Authority)
default:
// handle default (header) components
err = canonicalizeHeader(&b, h, msg.Header)
}
if err != nil {
return nil, err
}
items = append(items, h)
}
now := s.nowFunc()
sps := make(map[string]string)
sigs := make(map[string]string)
i := 1 // 1 indexed icky
for k, si := range s.keys {
sp := &signatureParams{
items: items,
keyID: k,
created: now,
alg: si.alg,
}
sps[fmt.Sprintf("sig%d", i)] = sp.canonicalize()
signer := si.signer()
if _, err := signer.w.Write(b.Bytes()); err != nil {
return nil, err
}
if err := canonicalizeSignatureParams(signer.w, sp); err != nil {
return nil, err
}
sigs[fmt.Sprintf("sig%d", i)] = base64.StdEncoding.EncodeToString(signer.sign())
i++
}
// for each configured key id,
// canonicalize signing options appended to byte slice
// create signature
// add new headers with params for all key ids and signatures
// TODO: make this stable
var parts []string
var sigparts []string
for k, v := range sps {
parts = append(parts, fmt.Sprintf("%s=%s", k, v))
sigparts = append(sigparts, fmt.Sprintf("%s=:%s:", k, sigs[k]))
}
hdr := make(http.Header)
hdr.Set("signature-input", strings.Join(parts, ", "))
hdr.Set("signature", strings.Join(sigparts, ", "))
return hdr, nil
}
func signRsaPssSha512(pk *rsa.PrivateKey) sigHolder {
return sigHolder{
alg: "rsa-pss-sha512",
signer: func() sigImpl {
h := sha256.New()
return sigImpl{
w: h,
sign: func() []byte {
b := h.Sum(nil)
// TODO: might have to deal with this error :)
sig, _ := rsa.SignPSS(rand.Reader, pk, crypto.SHA512, b, nil)
return sig
},
}
},
}
}
func signEccP256(pk *ecdsa.PrivateKey) sigHolder {
return sigHolder{
alg: "ecdsa-p256-sha256",
signer: func() sigImpl {
h := sha256.New()
return sigImpl{
w: h,
sign: func() []byte {
b := h.Sum(nil)
// TODO: might have to deal with this error :)
sig, _ := ecdsa.SignASN1(rand.Reader, pk, b)
return sig
},
}
},
}
}
func signHmacSha256(secret []byte) sigHolder {
// TODO: add alg description
return sigHolder{
signer: func() sigImpl {
h := hmac.New(sha256.New, secret)
return sigImpl{
w: h,
sign: func() []byte { return h.Sum(nil) },
}
},
}
}