-
Notifications
You must be signed in to change notification settings - Fork 1
/
signer.go
224 lines (192 loc) · 6.85 KB
/
signer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2023 WeFuzz Research and Development B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package signature
import (
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"github.com/ant4g0nist/chronometry/pkg/core/report"
)
// Signer creates digital signatures over a message using a specified key pair
type SignerBlob struct {
Report Report `json:"Report"`
PublicKey ed25519.PublicKey `json:"PublicKey"`
Signature []byte `json:"Signature"`
}
func (s *SignerBlob) SaveSignatureToFile(output string) bool {
// Save the signature to a file
signature := s.Signature
// write to file
err := os.WriteFile(output, []byte(signature), 0644)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (s *SignerBlob) GetBytes() []byte {
var blob []byte
blob = append(blob, []byte(s.Report.Version)...)
blob = append(blob, []byte(s.Report.Title)...)
blob = append(blob, []byte(s.Report.Description)...)
blob = append(blob, []byte(s.Report.Attributes)...)
blob = append(blob, []byte(s.Report.Author)...)
blob = append(blob, []byte(s.Report.Platform)...)
blob = append(blob, []byte(s.Report.Severity)...)
blob = append(blob, []byte(s.Report.Attachments)...)
blob = append(blob, []byte(s.PublicKey)...)
blob = append(blob, []byte(s.Signature)...)
return blob
}
func (s *SignerBlob) ReadBlobFromJSONFile(input string) bool {
// read SignerBlob from json file
signerBlob, err := os.ReadFile(input)
if err != nil {
log.Fatal(err)
return false
}
// unmarshal
err = json.Unmarshal(signerBlob, &s)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (s *SignerBlob) PrettyPrintJSON() string {
// Pretty print JSON
data, _ := json.MarshalIndent(s, "", " ")
return string(data)
}
func (s *SignerBlob) SaveBlobToFile(output string) bool {
// Save the blob to a file
data, _ := json.MarshalIndent(s, "", " ")
// write to file
err := os.WriteFile(output, data, 0644)
if err != nil {
log.Fatal(err)
return false
}
return true
}
// Signer creates digital signatures over a message using a specified key pair
func SignReport(report report.VulnerabilityReport, publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey, showAuthor bool) SignerBlob {
// Sign the message and generate a signed blob
authorDetailsHash := base64.URLEncoding.EncodeToString(report.Author.Hash())
author := "Anonymous"
if showAuthor {
author = report.Author.Name
}
authorHash := base64.URLEncoding.EncodeToString(Hash(author))
titleHash := base64.URLEncoding.EncodeToString(Hash(report.Title))
descriptionHash := base64.URLEncoding.EncodeToString(Hash(report.Description))
platformHash := base64.URLEncoding.EncodeToString(Hash(string(report.Platform)))
severityHash := base64.URLEncoding.EncodeToString(Hash(string(report.Severity)))
versionHash := base64.URLEncoding.EncodeToString(Hash(string(report.Version)))
attributeHash := sha256.New()
for _, attribute := range report.Attributes {
hash := attribute.Hash()
attributeHash.Write(hash)
}
attributeHashSum := base64.URLEncoding.EncodeToString(attributeHash.Sum(nil))
attachmentsHash := sha256.New()
for _, attachment := range report.Attachments {
hash := attachment.Hash()
attachmentsHash.Write(hash)
}
attachmentsHashSum := base64.URLEncoding.EncodeToString(attachmentsHash.Sum(nil))
// calculate hash of the entire report
reportHash := sha256.New()
reportHash.Write([]byte(versionHash))
reportHash.Write([]byte(authorHash))
reportHash.Write([]byte(authorDetailsHash))
reportHash.Write([]byte(titleHash))
reportHash.Write([]byte(descriptionHash))
reportHash.Write([]byte(platformHash))
reportHash.Write([]byte(attributeHashSum))
reportHash.Write([]byte(severityHash))
reportHash.Write([]byte(attachmentsHashSum))
// sign the report
signature := ed25519.Sign(privateKey, reportHash.Sum(nil))
// create a signed blob
signedBlob := SignerBlob{
Report: Report{
Version: versionHash,
Author: author,
AuthorDetailsHash: authorDetailsHash,
Title: titleHash,
Description: descriptionHash,
Platform: platformHash,
Attributes: attributeHashSum,
Severity: severityHash,
Attachments: attachmentsHashSum,
},
PublicKey: publicKey,
Signature: signature,
}
return signedBlob
}
// Hash calculates the hash of a byte array
func Hash(data string) []byte {
hash := sha256.Sum256([]byte(data))
return hash[:]
}
// Signer signs a Vulnerability Blob
func SignBlob(report Report, publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey) SignerBlob {
// Sign the message and generate a signed blob
fmt.Println("Signing the report for author: ", report.Author)
authorHash := base64.URLEncoding.EncodeToString(Hash(report.Author))
authorDetailsHash := base64.URLEncoding.EncodeToString(Hash(report.AuthorDetailsHash))
titleHash := base64.URLEncoding.EncodeToString(Hash(report.Title))
descriptionHash := base64.URLEncoding.EncodeToString(Hash(report.Description))
platformHash := base64.URLEncoding.EncodeToString(Hash(string(report.Platform)))
severityHash := base64.URLEncoding.EncodeToString(Hash(string(report.Severity)))
versionHash := base64.URLEncoding.EncodeToString(Hash(string(report.Version)))
attributeHash := base64.URLEncoding.EncodeToString(Hash(string(report.Attributes)))
attachmentsHash := base64.URLEncoding.EncodeToString(Hash(string(report.Attachments)))
// calculate hash of the entire report
reportHash := sha256.New()
reportHash.Write([]byte(versionHash))
reportHash.Write([]byte(authorHash))
reportHash.Write([]byte(authorDetailsHash))
reportHash.Write([]byte(titleHash))
reportHash.Write([]byte(descriptionHash))
reportHash.Write([]byte(platformHash))
reportHash.Write([]byte(attributeHash))
reportHash.Write([]byte(severityHash))
reportHash.Write([]byte(attachmentsHash))
// sign the report
signature := ed25519.Sign(privateKey, reportHash.Sum(nil))
// create a signed blob
signedBlob := SignerBlob{
Report: Report{
Version: versionHash,
Author: report.Author,
AuthorDetailsHash: authorDetailsHash,
Title: titleHash,
Description: descriptionHash,
Platform: platformHash,
Attributes: attributeHash,
Severity: severityHash,
Attachments: string(attachmentsHash),
},
PublicKey: publicKey,
Signature: signature,
}
return signedBlob
}