Skip to content

Commit ba67de8

Browse files
committed
tag/internal/age-plugin-tagtest: add plugin for testing tag recipients
1 parent 7fa810b commit ba67de8

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Command age-plugin-tagtest is a that decrypts files encrypted to fixed
2+
// age1tag1... or age1tagpq1... recipients for testing purposes.
3+
//
4+
// It can be used with the "-j" flag:
5+
//
6+
// go install ./tag/internal/age-plugin-tagtest
7+
// age -d -j tagtest file.age
8+
package main
9+
10+
import (
11+
"errors"
12+
"fmt"
13+
"log"
14+
"os"
15+
16+
"filippo.io/age"
17+
"filippo.io/age/plugin"
18+
"filippo.io/age/tag/internal/tagtest"
19+
)
20+
21+
const classicRecipient = "age1tag1qwe0kafsjrar4txm6heqnhpfuggzr0gvznz7fvygxrlq90u5mq2pysxtw6h"
22+
23+
const hybridRecipient = "age1tagpq14h4z7cks9sxftfc8tq4xektt4854ur9rv76tvujdvtzk2fmyywkvh9z2emz3x4epvhz7qdt2v7uksyyq2cdzf3k04ny0g5sc3u4heqh3r9v4cnwhfjw0a2azpgmnk9xk02wvywt5szcq6q3jvwsjxvkn3tsk52vqjczdcvc398ym4j6cvqas4w99gkgt7ur3fmt4g873phr23tgxw3f7wgsz9zxz7m8cp27vpq3h5vc8nssjemtr2etmtmqkg4fzn2u9x9zvtysuya5yrytgx482ftx9864h8a6pprarxd3d0qe8nw2at5ekg3tsahtef7kawasxjamyckw2ans6v933vuypcfrra32f89r2v72mka9hhc55s49xe2khfsq7w9r2zynuzfx4fg6v7jjncsc87rw2yy8qp8hr27edus6zw5xd6m3hax2nxhl2dys9792z3wp5c034sfkrxe86guj7pfdh7sytzrufl9euhuhyf9w6c7z2nwf7v5v8f2s4gplgvfx4jj4le22k2qn242qkqkcwx7llfyrct7jm2wcv0ytypeh6h93ezgtd7q6zr428qze3dec5jxlc5xxjetyephp42fljft3s02p0570kjwyfeyjcnks2vglkvyus5g9l4z6m0gf8wu22ygfm40028txwjlxvvgnn7c36z783c6tmc9k5nef8nucj6u3ustff5vhtnzhnscsvsz79wzrkv3sujtntx4wezucy6lp49flmnyydn3khk2xsesw0ekn4u44nzqw2g2rjyrrl7crshlzttgpe0jvqycjzp9kmtz23t3yu0w9j4n344nnrf88k2jqqfpjxte38pcn0epr879pqsuvajxrkmsas89pvfrzwcewneujn08guj5pvvrtn5hzzg2y6u4wwjqqxx4x8w65yc4dchf750dft8kgcttt2f6j0j5v8s7tkaua78tte7artdfar544vl0rau79h95mc4ghp887z82s6rq93txpkvan86n963kagqkldngnkjcn28zdrh38vdxj002zqs9mx7zjvg3ynzdfhfakkynt9fyqpaxpsdrsqrycuhw5ykwgjz6wldef7xtu6p689234hstxe7v8e5422f2dy8ystn57z3fvy9yfrm4t3lye6ejk5n6x8zqexmql7lx965xcxuuy38xzyt8j9qprnwgfqgx54l4tnjdpzdde6xgmwtnpkfwvyr7rkgnavvjn6a3e56wtvjx3evmhjjxvukpq5zqrj0s4sntkz3yeszs5dty8q0q6m7dgp6mjpvaer0c4343g72eycfqzkjupeaemh0n8e935hqs8fh3jgk7fzyxctzuqlx6d2q9jaf8r9wu4sjxj5w6ppw7m9c3hxrzpcv2uek3kxnndgf2hd99q9v2ux8pjkv29ntslvnvhy09dvcy9578rt89gf4cj4cu79zjxtlj3dpct6rjme02zj3qspsade96njkkufu9zuq2lk3qwvddpxjkqm2hnpqwck54zug7ctvkgvk325lwkg4q5rf73zkgys5e9y8jqc96ntdyl4r78lgtw4k5uljk5ttf46s3gc0rq0jwmddnxt875twwq92505zh3zkse5ag2dhjjxyfzkn7xv3j0kv9r3jzpvgep8fq6z8mar509u4fvnhvthp2ah0r45lsyq0mm6fwkcs30v8k9wzvgt6uvcty6qsjvarjs3htym69zu43m4jd3k4tllrr8c05v6p6spuhup4hkk2p9fp9lxafe3pntcn4nk83gzhjjpcjwyg7jcyz5uancu0fakgz27up7ymzp2xv3sqyqewkkqynskw9qkvysrncxj0cy7dt6q8dsseuwmc2urfmcvkykf82wfa54t85hqx8gywhmhzunm2x0d66a4pwl0xl78fhkces5dpq8pfnp35m5a3u8vdam64zx5s5x9cmnrx3zr066f4f8hlecqnq2fd5quw79ljg3q5nvs6ggmm4gkc"
24+
25+
func init() {
26+
c := tagtest.NewClassicIdentity("age-plugin-tagtest").Recipient().String()
27+
if c != classicRecipient {
28+
log.Fatalf("unexpected classic recipient: %s", c)
29+
}
30+
h := tagtest.NewHybridIdentity("age-plugin-tagtest").Recipient().String()
31+
if h != hybridRecipient {
32+
log.Fatalf("unexpected hybrid recipient: %s", h)
33+
}
34+
}
35+
36+
func main() {
37+
p, err := plugin.New("tagtest")
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
p.HandleIdentity(func(b []byte) (age.Identity, error) {
42+
if len(b) != 0 {
43+
return nil, fmt.Errorf("unexpected identity data")
44+
}
45+
return &tagtestIdentity{}, nil
46+
})
47+
os.Exit(p.Main())
48+
}
49+
50+
type tagtestIdentity struct{}
51+
52+
func (i *tagtestIdentity) Unwrap(ss []*age.Stanza) ([]byte, error) {
53+
classic := tagtest.NewClassicIdentity("age-plugin-tagtest")
54+
if key, err := classic.Unwrap(ss); err == nil {
55+
return key, nil
56+
} else if !errors.Is(err, age.ErrIncorrectIdentity) {
57+
return nil, err
58+
}
59+
hybrid := tagtest.NewHybridIdentity("age-plugin-tagtest")
60+
return hybrid.Unwrap(ss)
61+
}

tag/internal/tagtest/tagtest.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"crypto/ecdh"
99
"crypto/subtle"
1010
"fmt"
11-
"testing"
1211

1312
"filippo.io/age"
1413
"filippo.io/age/internal/format"
@@ -18,16 +17,15 @@ import (
1817
)
1918

2019
type ClassicIdentity struct {
21-
t *testing.T
2220
k hpke.PrivateKey
2321
}
2422

2523
var _ age.Identity = &ClassicIdentity{}
2624

27-
func NewClassicIdentity(t *testing.T) *ClassicIdentity {
28-
k, err := hpke.DHKEM(ecdh.P256()).GenerateKey()
25+
func NewClassicIdentity(seed string) *ClassicIdentity {
26+
k, err := hpke.DHKEM(ecdh.P256()).DeriveKeyPair([]byte(seed))
2927
if err != nil {
30-
t.Fatalf("failed to generate key: %v", err)
28+
panic(fmt.Sprintf("failed to generate key: %v", err))
3129
}
3230
return &ClassicIdentity{k: k}
3331
}
@@ -36,11 +34,11 @@ func (i *ClassicIdentity) Recipient() *tag.Recipient {
3634
uncompressed := i.k.PublicKey().Bytes()
3735
p, err := nistec.NewP256Point().SetBytes(uncompressed)
3836
if err != nil {
39-
i.t.Fatalf("failed to parse public key: %v", err)
37+
panic(fmt.Sprintf("failed to parse public key: %v", err))
4038
}
4139
r, err := tag.NewClassicRecipient(p.BytesCompressed())
4240
if err != nil {
43-
i.t.Fatalf("failed to create recipient: %v", err)
41+
panic(fmt.Sprintf("failed to create recipient: %v", err))
4442
}
4543
return r
4644
}
@@ -89,24 +87,23 @@ func (i *ClassicIdentity) Unwrap(ss []*age.Stanza) ([]byte, error) {
8987
}
9088

9189
type HybridIdentity struct {
92-
t *testing.T
9390
k hpke.PrivateKey
9491
}
9592

9693
var _ age.Identity = &HybridIdentity{}
9794

98-
func NewHybridIdentity(t *testing.T) *HybridIdentity {
99-
k, err := hpke.MLKEM768P256().GenerateKey()
95+
func NewHybridIdentity(seed string) *HybridIdentity {
96+
k, err := hpke.MLKEM768P256().DeriveKeyPair([]byte(seed))
10097
if err != nil {
101-
t.Fatalf("failed to generate key: %v", err)
98+
panic(fmt.Sprintf("failed to generate key: %v", err))
10299
}
103100
return &HybridIdentity{k: k}
104101
}
105102

106103
func (i *HybridIdentity) Recipient() *tag.Recipient {
107104
r, err := tag.NewHybridRecipient(i.k.PublicKey().Bytes())
108105
if err != nil {
109-
i.t.Fatalf("failed to create recipient: %v", err)
106+
panic(fmt.Sprintf("failed to create recipient: %v", err))
110107
}
111108
return r
112109
}

tag/tag_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func TestClassicRoundTrip(t *testing.T) {
18-
i := tagtest.NewClassicIdentity(t)
18+
i := tagtest.NewClassicIdentity("test")
1919
r := i.Recipient()
2020

2121
if r.Hybrid() {
@@ -62,7 +62,7 @@ func TestClassicRoundTrip(t *testing.T) {
6262
}
6363

6464
func TestHybridRoundTrip(t *testing.T) {
65-
i := tagtest.NewHybridIdentity(t)
65+
i := tagtest.NewHybridIdentity("test")
6666
r := i.Recipient()
6767

6868
if !r.Hybrid() {
@@ -113,7 +113,7 @@ func TestTagHybridMixingRestrictions(t *testing.T) {
113113
if err != nil {
114114
t.Fatal(err)
115115
}
116-
tagHybrid := tagtest.NewHybridIdentity(t).Recipient()
116+
tagHybrid := tagtest.NewHybridIdentity("test").Recipient()
117117

118118
// Hybrid tag recipients can be used together with hybrid recipients.
119119
hybrid, err := age.GenerateHybridIdentity()
@@ -133,7 +133,7 @@ func TestTagHybridMixingRestrictions(t *testing.T) {
133133
}
134134

135135
// Classic tag and X25519 recipients can be mixed (both are non-PQ).
136-
tagClassic := tagtest.NewClassicIdentity(t).Recipient()
136+
tagClassic := tagtest.NewClassicIdentity("test").Recipient()
137137
if _, err := age.Encrypt(io.Discard, tagClassic, x25519.Recipient()); err != nil {
138138
t.Errorf("expected classic tag + X25519 to work, got %v", err)
139139
}

0 commit comments

Comments
 (0)