| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "log" |
| 8 | "os" |
| 9 | "runtime/debug" |
| 10 | |
| 11 | "filippo.io/age" |
| 12 | "filippo.io/age/internal/bech32" |
| 13 | "filippo.io/age/plugin" |
| 14 | ) |
| 15 | |
| 16 | const usage = `Usage: |
| 17 | age-plugin-pq -identity [-o OUTPUT] [INPUT] |
| 18 | |
| 19 | Options: |
| 20 | -identity Convert one or more native post-quantum identities from |
| 21 | INPUT or from standard input to plugin identities. |
| 22 | -o, --output OUTPUT Write the result to the file at path OUTPUT instead of |
| 23 | standard output. |
| 24 | |
| 25 | age-plugin-pq is an age plugin for post-quantum hybrid ML-KEM-768 + X25519 |
| 26 | recipients and identities. These are supported natively by age v1.3.0 and later, |
| 27 | but this plugin can be placed in $PATH to add support to any version and |
| 28 | implementation of age that supports plugins. |
| 29 | |
| 30 | Recipients work out of the box, while identities need to be converted to plugin |
| 31 | identities with -identity. If OUTPUT already exists, it is not overwritten.` |
| 32 | |
| 33 | // Version can be set at link time to override debug.BuildInfo.Main.Version when |
| 34 | // building manually without git history. It should look like "v1.2.3". |
| 35 | var Version string |
| 36 | |
| 37 | func main() { |
| 38 | log.SetFlags(0) |
| 39 | |
| 40 | p, err := plugin.New("pq") |
| 41 | if err != nil { |
| 42 | errorf("failed to create plugin: %v", err) |
| 43 | } |
| 44 | p.RegisterFlags(nil) |
| 45 | |
| 46 | flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } |
| 47 | |
| 48 | var outFlag string |
| 49 | var versionFlag, identityFlag bool |
| 50 | flag.BoolVar(&versionFlag, "version", false, "print the version") |
| 51 | flag.BoolVar(&identityFlag, "identity", false, "convert identities to plugin identities") |
| 52 | flag.StringVar(&outFlag, "o", "", "output to `FILE` (default stdout)") |
| 53 | flag.StringVar(&outFlag, "output", "", "output to `FILE` (default stdout)") |
| 54 | flag.Parse() |
| 55 | |
| 56 | if versionFlag { |
| 57 | if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { |
| 58 | Version = buildInfo.Main.Version |
| 59 | } |
| 60 | fmt.Println(Version) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | if identityFlag { |
| 65 | if len(flag.Args()) > 1 { |
| 66 | errorf("too many arguments") |
| 67 | } |
| 68 | |
| 69 | in := os.Stdin |
| 70 | if inFile := flag.Arg(0); inFile != "" && inFile != "-" { |
| 71 | f, err := os.Open(inFile) |
| 72 | if err != nil { |
| 73 | errorf("failed to open input file %q: %v", inFile, err) |
| 74 | } |
| 75 | defer f.Close() |
| 76 | in = f |
| 77 | } |
| 78 | converted := convert(in) |
| 79 | |
| 80 | out := os.Stdout |
| 81 | if outFlag != "" { |
| 82 | f, err := os.OpenFile(outFlag, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) |
| 83 | if err != nil { |
| 84 | errorf("failed to open output file %q: %v", outFlag, err) |
| 85 | } |
| 86 | defer func() { |
| 87 | if err := f.Close(); err != nil { |
| 88 | errorf("failed to close output file %q: %v", outFlag, err) |
| 89 | } |
| 90 | }() |
| 91 | out = f |
| 92 | } |
| 93 | if fi, err := out.Stat(); err == nil && fi.Mode().IsRegular() && fi.Mode().Perm()&0004 != 0 { |
| 94 | warning("writing secret key to a world-readable file") |
| 95 | } |
| 96 | if _, err := out.Write(converted); err != nil { |
| 97 | errorf("failed to write output: %v", err) |
| 98 | } |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | p.HandleRecipientEncoding(func(s string) (age.Recipient, error) { |
| 103 | return age.ParseHybridRecipient(s) |
| 104 | }) |
| 105 | p.HandleIdentity(func(data []byte) (age.Identity, error) { |
| 106 | // Convert from a AGE-PLUGIN-PQ-1... payload to a |
| 107 | // AGE-SECRET-KEY-PQ-1... identity encoding. |
| 108 | s, err := bech32.Encode("AGE-SECRET-KEY-PQ-", data) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | return age.ParseHybridIdentity(s) |
| 113 | }) |
| 114 | p.HandleIdentityAsRecipient(func(data []byte) (age.Recipient, error) { |
| 115 | s, err := bech32.Encode("AGE-SECRET-KEY-PQ-", data) |
| 116 | if err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | i, err := age.ParseHybridIdentity(s) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | return i.Recipient(), nil |
| 124 | }) |
| 125 | os.Exit(p.Main()) |
| 126 | } |
| 127 | |
| 128 | func convert(in io.Reader) []byte { |
| 129 | ids, err := age.ParseIdentities(in) |
| 130 | if err != nil { |
| 131 | errorf("failed to parse identities: %v", err) |
| 132 | } |
| 133 | var out []byte |
| 134 | for i, id := range ids { |
| 135 | hybridID, ok := id.(*age.HybridIdentity) |
| 136 | if !ok { |
| 137 | errorf("identity #%d is not a post-quantum hybrid identity", i+1) |
| 138 | } |
| 139 | _, data, err := bech32.Decode(hybridID.String()) |
| 140 | if err != nil { |
| 141 | errorf("failed to decode identity #%d: %v", i+1, err) |
| 142 | } |
| 143 | out = append(out, plugin.EncodeIdentity("pq", data)...) |
| 144 | out = append(out, '\n') |
| 145 | } |
| 146 | return out |
| 147 | } |
| 148 | |
| 149 | func errorf(format string, v ...any) { |
| 150 | log.Printf("age-plugin-pq: error: "+format, v...) |
| 151 | log.Fatalf("age-plugin-pq: report unexpected or unhelpful errors at https://filippo.io/age/report") |
| 152 | } |
| 153 | |
| 154 | func warning(msg string) { |
| 155 | log.Printf("age-plugin-pq: warning: %s", msg) |
| 156 | } |