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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package main
import (
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"unicode/utf8"
"github.com/c0mm4nd/go-ripemd"
"github.com/go-piv/piv-go/v2/piv"
"github.com/martinlindhe/gogost/gost34112012256"
"github.com/tjfoc/gmsm/sm3"
)
// Ed25519 constants (EXACTLY from yubisigner.go:95-99)
const (
Ed25519SignatureSize = 64
Ed25519PublicKeySize = 32
Ed25519CombinedSize = Ed25519SignatureSize + Ed25519PublicKeySize
)
// Algorithm constant (EXACTLY from yubisigner.go:69-73)
const (
AlgorithmED25519 = "ED25519"
)
// normalizeToCRLF converts any line ending to RFC-compliant CRLF (EXACTLY from yubisigner.go:538-544)
func normalizeToCRLF(data []byte) []byte {
s := string(data)
s = strings.ReplaceAll(s, "\r\n", "\n")
s = strings.ReplaceAll(s, "\r", "\n")
s = strings.ReplaceAll(s, "\n", "\r\n")
return []byte(s)
}
// ensureUTF8 ensures the string is valid UTF-8 (EXACTLY from yubisigner.go:547-552)
func ensureUTF8(s string) string {
if utf8.ValidString(s) {
return s
}
return strings.ToValidUTF8(s, " ")
}
// calculateHashesRAM calculates all hashes for files that fit in RAM (EXACTLY from yubisigner.go:1621-1640)
func calculateHashesRAM(data []byte) map[string]string {
hashes := make(map[string]string)
gostHasher := gost34112012256.New()
gostHasher.Write(data)
hashes["Streebog-256"] = hex.EncodeToString(gostHasher.Sum(nil))
ripemdHasher := ripemd.New256()
ripemdHasher.Write(data)
hashes["RIPEMD-256"] = hex.EncodeToString(ripemdHasher.Sum(nil))
sha256Hash := sha256.Sum256(data)
hashes["SHA-256"] = hex.EncodeToString(sha256Hash[:])
sm3Hasher := sm3.New()
sm3Hasher.Write(data)
hashes["SM3"] = hex.EncodeToString(sm3Hasher.Sum(nil))
return hashes
}
// formatHashes formats hashes with right-aligned names (EXACTLY from yubisigner.go:1746-1766)
func formatHashes(hashes map[string]string) string {
keys := make([]string, 0, len(hashes))
for k := range hashes {
keys = append(keys, k)
}
sort.Strings(keys)
maxLen := 0
for _, k := range keys {
if len(k) > maxLen {
maxLen = len(k)
}
}
var result strings.Builder
for _, k := range keys {
paddedKey := fmt.Sprintf("%*s", maxLen, k)
result.WriteString(fmt.Sprintf("%s: %s\r\n", paddedKey, hashes[k]))
}
return result.String()
}
// formatSignatureRFC formats signature in RFC-compliant format with line breaks (EXACTLY from yubisigner.go:1996-2007)
func formatSignatureRFC(sig string) string {
var result strings.Builder
for i := 0; i < len(sig); i += 64 {
end := i + 64
if end > len(sig) {
end = len(sig)
}
result.WriteString(sig[i:end])
result.WriteString("\r\n")
}
return result.String()
}
// openYubiKey opens YubiKey at specified index (EXACTLY from yubisigner.go:2073-2092)
func openYubiKey(index int) (*piv.YubiKey, error) {
cards, err := piv.Cards()
if err != nil {
return nil, fmt.Errorf("failed to list cards: %v", err)
}
if len(cards) == 0 {
return nil, fmt.Errorf("no smart card found")
}
count := 0
for _, card := range cards {
if strings.Contains(strings.ToLower(card), "yubikey") {
if count == index {
return piv.Open(card)
}
count++
}
}
return nil, fmt.Errorf("no YubiKey found at index %d", index)
}
// signEd25519Data handles Ed25519 signing (EXACTLY from yubisigner.go:1843-1862)
func signEd25519Data(pin string, hash []byte, pubKey ed25519.PublicKey, yk *piv.YubiKey) (string, string, error) {
auth := piv.KeyAuth{PIN: pin}
priv, err := yk.PrivateKey(piv.SlotSignature, pubKey, auth)
if err != nil {
return "", "", fmt.Errorf("failed to get private key: %v", err)
}
signer, ok := priv.(crypto.Signer)
if !ok {
return "", "", fmt.Errorf("key does not implement crypto.Signer")
}
signature, err := signer.Sign(rand.Reader, hash, crypto.Hash(0))
if err != nil {
return "", "", fmt.Errorf("Ed25519 signing failed: %v", err)
}
combined := append(pubKey, signature...)
return hex.EncodeToString(combined), AlgorithmED25519, nil
}
// signDataInternal performs the actual signing operation with YubiKey (EXACTLY from yubisigner.go:1769-1840, Ed25519 only)
func signDataInternal(pin, data []byte) (string, string, error) {
yk, err := openYubiKey(0)
if err != nil {
return "", "", err
}
defer yk.Close()
cert, err := yk.Certificate(piv.SlotSignature)
if err != nil {
return "", "", fmt.Errorf("failed to get certificate from signature slot: %v", err)
}
ed25519PubKey, ok := cert.PublicKey.(ed25519.PublicKey)
if !ok {
return "", "", fmt.Errorf("public key is not Ed25519 (this CLI tool only supports Ed25519)")
}
hash := sha256.Sum256(data)
return signEd25519Data(string(pin), hash[:], ed25519PubKey, yk)
}
func main() {
var (
inputFile string
author string
email string
url string
telefax string
comment string
pin string
outputFile string
)
flag.StringVar(&inputFile, "input", "", "File to sign (required)")
flag.StringVar(&author, "author", "", "Author name (required)")
flag.StringVar(&email, "email", "n/a", "Email address (optional)")
flag.StringVar(&url, "url", "n/a", "URL (optional)")
flag.StringVar(&telefax, "telefax", "n/a", "Telefax (optional)")
flag.StringVar(&comment, "comment", "n/a", "Comment (optional)")
flag.StringVar(&pin, "pin", "", "YubiKey PIN (will prompt if not provided)")
flag.StringVar(&outputFile, "output", "", "Output signature file (default: <input>.sig)")
flag.Parse()
// Validate required fields
if inputFile == "" {
fmt.Fprintln(os.Stderr, "Error: --input is required")
flag.Usage()
os.Exit(1)
}
if author == "" {
fmt.Fprintln(os.Stderr, "Error: --author is required")
flag.Usage()
os.Exit(1)
}
// Ensure UTF-8 (EXACTLY from yubisigner.go:1010, 1120)
author = ensureUTF8(author)
if comment != "n/a" {
comment = ensureUTF8(comment)
}
// Read input file
data, err := os.ReadFile(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err)
os.Exit(1)
}
// Get file info
fileInfo, err := os.Stat(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting file info: %v\n", err)
os.Exit(1)
}
fileSize := fileInfo.Size()
// Calculate hashes (EXACTLY from yubisigner.go:1103)
fmt.Println("Calculating hashes...")
hashes := calculateHashesRAM(data)
// Prepare metadata (EXACTLY from yubisigner.go:1124-1132)
metadata := fmt.Sprintf("Author: %s\r\n", author)
metadata += fmt.Sprintf("Signed at: %s\r\n", time.Now().UTC().Format("2006-01-02 15:04:05 +0000"))
metadata += fmt.Sprintf("Filename: %s\r\n", filepath.Base(inputFile))
metadata += fmt.Sprintf("File size: %d bytes\r\n", fileSize)
metadata += fmt.Sprintf("Email: %s\r\n", email)
metadata += fmt.Sprintf("Telefax: %s\r\n", telefax)
metadata += fmt.Sprintf("URL: %s\r\n", url)
metadata += fmt.Sprintf("Comment: %s\r\n", comment)
metadata += formatHashes(hashes)
// Get PIN if not provided
if pin == "" {
fmt.Print("Enter YubiKey PIN: ")
fmt.Scanln(&pin)
}
// Sign metadata (EXACTLY from yubisigner.go:1138)
fmt.Println("Signing with YubiKey (touch required)...")
sig, algo, err := signDataInternal([]byte(pin), []byte(metadata))
if err != nil {
fmt.Fprintf(os.Stderr, "Signing failed: %v\n", err)
os.Exit(1)
}
// Build result (EXACTLY from yubisigner.go:1144-1148)
result := metadata
result += "-----BEGIN YUBISIGNER " + algo + " SIGNATURE-----\r\n"
result += formatSignatureRFC(sig)
result += "-----END YUBISIGNER " + algo + " SIGNATURE-----\r\n"
// Determine output file
if outputFile == "" {
outputFile = inputFile + ".sig"
}
// Write signature file
err = os.WriteFile(outputFile, []byte(result), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing signature: %v\n", err)
os.Exit(1)
}
fmt.Printf("✓ File signed successfully: %s\n", outputFile)
}
|