| 1 | // Copyright 2025 The age Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package tag_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "io" |
| 10 | "testing" |
| 11 | |
| 12 | "filippo.io/age" |
| 13 | "filippo.io/age/internal/format" |
| 14 | "filippo.io/age/tag" |
| 15 | "filippo.io/age/tag/internal/tagtest" |
| 16 | ) |
| 17 | |
| 18 | func TestClassicRoundTrip(t *testing.T) { |
| 19 | i := tagtest.NewClassicIdentity("test") |
| 20 | r := i.Recipient() |
| 21 | |
| 22 | if r.Hybrid() { |
| 23 | t.Error("classic recipient incorrectly reports as hybrid") |
| 24 | } |
| 25 | |
| 26 | r1, err := tag.ParseRecipient(r.String()) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if r1.String() != r.String() { |
| 31 | t.Errorf("recipient did not round-trip through parsing: got %q, want %q", r1.String(), r.String()) |
| 32 | } |
| 33 | if r1.Hybrid() { |
| 34 | t.Error("parsed classic recipient incorrectly reports as hybrid") |
| 35 | } |
| 36 | |
| 37 | plaintext := []byte("hello world") |
| 38 | |
| 39 | encrypted := &bytes.Buffer{} |
| 40 | w, err := age.Encrypt(encrypted, r) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if _, err := w.Write(plaintext); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err := w.Close(); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | |
| 51 | decrypted, err := age.Decrypt(encrypted, i) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | out, err := io.ReadAll(decrypted) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | if !bytes.Equal(plaintext, out) { |
| 61 | t.Errorf("invalid output: %q, expected %q", out, plaintext) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestHybridRoundTrip(t *testing.T) { |
| 66 | i := tagtest.NewHybridIdentity("test") |
| 67 | r := i.Recipient() |
| 68 | |
| 69 | if !r.Hybrid() { |
| 70 | t.Error("hybrid recipient incorrectly reports as classic") |
| 71 | } |
| 72 | |
| 73 | r1, err := tag.ParseRecipient(r.String()) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if r1.String() != r.String() { |
| 78 | t.Errorf("recipient did not round-trip through parsing: got %q, want %q", r1.String(), r.String()) |
| 79 | } |
| 80 | if !r1.Hybrid() { |
| 81 | t.Error("parsed hybrid recipient incorrectly reports as classic") |
| 82 | } |
| 83 | |
| 84 | plaintext := []byte("hello world") |
| 85 | |
| 86 | encrypted := &bytes.Buffer{} |
| 87 | w, err := age.Encrypt(encrypted, r) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | if _, err := w.Write(plaintext); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if err := w.Close(); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | |
| 98 | decrypted, err := age.Decrypt(encrypted, i) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | out, err := io.ReadAll(decrypted) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | |
| 107 | if !bytes.Equal(plaintext, out) { |
| 108 | t.Errorf("invalid output: %q, expected %q", out, plaintext) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestTagCollision(t *testing.T) { |
| 113 | tests := []struct { |
| 114 | name string |
| 115 | identity age.Identity |
| 116 | recipient *tag.Recipient |
| 117 | other *tag.Recipient |
| 118 | }{ |
| 119 | { |
| 120 | name: "classic", |
| 121 | identity: tagtest.NewClassicIdentity("target"), |
| 122 | recipient: tagtest.NewClassicIdentity("target").Recipient(), |
| 123 | other: tagtest.NewClassicIdentity("other").Recipient(), |
| 124 | }, |
| 125 | { |
| 126 | name: "hybrid", |
| 127 | identity: tagtest.NewHybridIdentity("target"), |
| 128 | recipient: tagtest.NewHybridIdentity("target").Recipient(), |
| 129 | other: tagtest.NewHybridIdentity("other").Recipient(), |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for _, test := range tests { |
| 134 | t.Run(test.name, func(t *testing.T) { |
| 135 | fileKey := bytes.Repeat([]byte{1}, 16) |
| 136 | stanzas, err := test.recipient.Wrap(fileKey) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | unrelated, err := test.other.Wrap(make([]byte, 16)) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | |
| 145 | collision := &age.Stanza{ |
| 146 | Type: unrelated[0].Type, |
| 147 | Args: append([]string(nil), unrelated[0].Args...), |
| 148 | Body: append([]byte(nil), unrelated[0].Body...), |
| 149 | } |
| 150 | enc, err := format.DecodeString(collision.Args[1]) |
| 151 | if err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | collisionTag, err := test.recipient.Tag(enc) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | collision.Args[0] = format.EncodeToString(collisionTag) |
| 159 | |
| 160 | allStanzas := append(unrelated, collision) |
| 161 | allStanzas = append(allStanzas, stanzas...) |
| 162 | out, err := test.identity.Unwrap(allStanzas) |
| 163 | if err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if !bytes.Equal(fileKey, out) { |
| 167 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 168 | } |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestTagHybridMixingRestrictions(t *testing.T) { |
| 174 | x25519, err := age.GenerateX25519Identity() |
| 175 | if err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | tagHybrid := tagtest.NewHybridIdentity("test").Recipient() |
| 179 | |
| 180 | // Hybrid tag recipients can be used together with hybrid recipients. |
| 181 | hybrid, err := age.GenerateHybridIdentity() |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | if _, err := age.Encrypt(io.Discard, tagHybrid, hybrid.Recipient()); err != nil { |
| 186 | t.Errorf("expected hybrid tag + hybrid to work, got %v", err) |
| 187 | } |
| 188 | |
| 189 | // Hybrid tag and X25519 recipients cannot be mixed. |
| 190 | if _, err := age.Encrypt(io.Discard, tagHybrid, x25519.Recipient()); err == nil { |
| 191 | t.Error("expected hybrid tag mixed with X25519 to fail") |
| 192 | } |
| 193 | if _, err := age.Encrypt(io.Discard, x25519.Recipient(), tagHybrid); err == nil { |
| 194 | t.Error("expected X25519 mixed with hybrid tag to fail") |
| 195 | } |
| 196 | |
| 197 | // Classic tag and X25519 recipients can be mixed (both are non-PQ). |
| 198 | tagClassic := tagtest.NewClassicIdentity("test").Recipient() |
| 199 | if _, err := age.Encrypt(io.Discard, tagClassic, x25519.Recipient()); err != nil { |
| 200 | t.Errorf("expected classic tag + X25519 to work, got %v", err) |
| 201 | } |
| 202 | } |