| 1 | // Copyright 2023 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 plugin |
| 6 | |
| 7 | import ( |
| 8 | "crypto/ecdh" |
| 9 | "crypto/mlkem" |
| 10 | "fmt" |
| 11 | "strings" |
| 12 | |
| 13 | "filippo.io/age/internal/bech32" |
| 14 | "filippo.io/hpke" |
| 15 | ) |
| 16 | |
| 17 | // EncodeIdentity encodes a plugin identity string for a plugin with the given |
| 18 | // name. If the name is invalid, it returns an empty string. |
| 19 | func EncodeIdentity(name string, data []byte) string { |
| 20 | if !validPluginName(name) { |
| 21 | return "" |
| 22 | } |
| 23 | s, _ := bech32.Encode("AGE-PLUGIN-"+strings.ToUpper(name)+"-", data) |
| 24 | return s |
| 25 | } |
| 26 | |
| 27 | // ParseIdentity decodes a plugin identity string. It returns the plugin name |
| 28 | // in lowercase and the encoded data. |
| 29 | func ParseIdentity(s string) (name string, data []byte, err error) { |
| 30 | hrp, data, err := bech32.Decode(s) |
| 31 | if err != nil { |
| 32 | return "", nil, fmt.Errorf("invalid identity encoding: %v", err) |
| 33 | } |
| 34 | if !strings.HasPrefix(hrp, "AGE-PLUGIN-") || !strings.HasSuffix(hrp, "-") { |
| 35 | return "", nil, fmt.Errorf("not a plugin identity: %v", err) |
| 36 | } |
| 37 | name = strings.TrimSuffix(strings.TrimPrefix(hrp, "AGE-PLUGIN-"), "-") |
| 38 | name = strings.ToLower(name) |
| 39 | if !validPluginName(name) { |
| 40 | return "", nil, fmt.Errorf("invalid plugin name: %q", name) |
| 41 | } |
| 42 | return name, data, nil |
| 43 | } |
| 44 | |
| 45 | // EncodeRecipient encodes a plugin recipient string for a plugin with the given |
| 46 | // name. If the name is invalid, it returns an empty string. |
| 47 | func EncodeRecipient(name string, data []byte) string { |
| 48 | if !validPluginName(name) { |
| 49 | return "" |
| 50 | } |
| 51 | s, _ := bech32.Encode("age1"+strings.ToLower(name), data) |
| 52 | return s |
| 53 | } |
| 54 | |
| 55 | // ParseRecipient decodes a plugin recipient string. It returns the plugin name |
| 56 | // in lowercase and the encoded data. |
| 57 | func ParseRecipient(s string) (name string, data []byte, err error) { |
| 58 | hrp, data, err := bech32.Decode(s) |
| 59 | if err != nil { |
| 60 | return "", nil, fmt.Errorf("invalid recipient encoding: %v", err) |
| 61 | } |
| 62 | if !strings.HasPrefix(hrp, "age1") { |
| 63 | return "", nil, fmt.Errorf("not a plugin recipient: %v", err) |
| 64 | } |
| 65 | name = strings.TrimPrefix(hrp, "age1") |
| 66 | if !validPluginName(name) { |
| 67 | return "", nil, fmt.Errorf("invalid plugin name: %q", name) |
| 68 | } |
| 69 | return name, data, nil |
| 70 | } |
| 71 | |
| 72 | func validPluginName(name string) bool { |
| 73 | if name == "" { |
| 74 | return false |
| 75 | } |
| 76 | allowed := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-._" |
| 77 | for _, r := range name { |
| 78 | if !strings.ContainsRune(allowed, r) { |
| 79 | return false |
| 80 | } |
| 81 | } |
| 82 | return true |
| 83 | } |
| 84 | |
| 85 | // EncodeX25519Recipient encodes a native X25519 recipient from a |
| 86 | // [crypto/ecdh.X25519] public key. It's meant for plugins that implement |
| 87 | // identities that are compatible with native recipients. |
| 88 | func EncodeX25519Recipient(pk *ecdh.PublicKey) (string, error) { |
| 89 | if pk.Curve() != ecdh.X25519() { |
| 90 | return "", fmt.Errorf("wrong ecdh Curve") |
| 91 | } |
| 92 | return bech32.Encode("age", pk.Bytes()) |
| 93 | } |
| 94 | |
| 95 | // EncodeHybridRecipient encodes a native MLKEM768-X25519 recipient from a |
| 96 | // [crypto/mlkem.EncapsulationKey768] and a [crypto/ecdh.X25519] public key. |
| 97 | // It's meant for plugins that implement identities that are compatible with |
| 98 | // native recipients. |
| 99 | func EncodeHybridRecipient(pq *mlkem.EncapsulationKey768, t *ecdh.PublicKey) (string, error) { |
| 100 | if t.Curve() != ecdh.X25519() { |
| 101 | return "", fmt.Errorf("wrong ecdh Curve") |
| 102 | } |
| 103 | pk, err := hpke.NewHybridPublicKey(pq, t) |
| 104 | if err != nil { |
| 105 | return "", fmt.Errorf("failed to create hybrid public key: %v", err) |
| 106 | } |
| 107 | return bech32.Encode("age1pq", pk.Bytes()) |
| 108 | } |