| 1 | // Copyright 2019 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 main |
| 6 | |
| 7 | import ( |
| 8 | "flag" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "log" |
| 12 | "os" |
| 13 | "runtime/debug" |
| 14 | "time" |
| 15 | |
| 16 | "filippo.io/age" |
| 17 | "golang.org/x/term" |
| 18 | ) |
| 19 | |
| 20 | const usage = `Usage: |
| 21 | age-keygen [-pq] [-o OUTPUT] |
| 22 | age-keygen -y [-o OUTPUT] [INPUT] |
| 23 | |
| 24 | Options: |
| 25 | -pq Generate a post-quantum hybrid ML-KEM-768 + X25519 key pair. |
| 26 | (This might become the default in the future.) |
| 27 | -o, --output OUTPUT Write the result to the file at path OUTPUT. |
| 28 | -y Convert an identity file to a recipients file. |
| 29 | |
| 30 | age-keygen generates a new native X25519 or, with the -pq flag, post-quantum |
| 31 | hybrid ML-KEM-768 + X25519 key pair, and outputs it to standard output or to |
| 32 | the OUTPUT file. |
| 33 | |
| 34 | When generating a key, if the output is not going to a terminal, the public key |
| 35 | is printed to standard error. If OUTPUT already exists, it is not overwritten. |
| 36 | |
| 37 | In -y mode, age-keygen reads an identity file from INPUT or from standard |
| 38 | input and writes the corresponding recipient(s) to OUTPUT or to standard |
| 39 | output, one per line, with no comments. |
| 40 | |
| 41 | Examples: |
| 42 | |
| 43 | $ age-keygen |
| 44 | # created: 2021-01-02T15:30:45+01:00 |
| 45 | # public key: age1lvyvwawkr0mcnnnncaghunadrqkmuf9e6507x9y920xxpp866cnql7dp2z |
| 46 | AGE-SECRET-KEY-1N9JEPW6DWJ0ZQUDX63F5A03GX8QUW7PXDE39N8UYF82VZ9PC8UFS3M7XA9 |
| 47 | |
| 48 | $ age-keygen -pq |
| 49 | # created: 2025-11-17T12:15:17+01:00 |
| 50 | # public key: age1pq1pd[... 1950 more characters ...] |
| 51 | AGE-SECRET-KEY-PQ-1XXC4XS9DXHZ6TREKQTT3XECY8VNNU7GJ83C3Y49D0GZ3ZUME4JWS6QC3EF |
| 52 | |
| 53 | $ age-keygen -o key.txt |
| 54 | Public key: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p |
| 55 | |
| 56 | $ age-keygen -y key.txt |
| 57 | age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p` |
| 58 | |
| 59 | // Version can be set at link time to override debug.BuildInfo.Main.Version when |
| 60 | // building manually without git history. It should look like "v1.2.3". |
| 61 | var Version string |
| 62 | |
| 63 | func main() { |
| 64 | log.SetFlags(0) |
| 65 | flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } |
| 66 | |
| 67 | var outFlag string |
| 68 | var pqFlag, versionFlag, convertFlag bool |
| 69 | |
| 70 | flag.BoolVar(&versionFlag, "version", false, "print the version") |
| 71 | flag.BoolVar(&pqFlag, "pq", false, "generate a post-quantum key pair") |
| 72 | flag.BoolVar(&convertFlag, "y", false, "convert identities to recipients") |
| 73 | flag.StringVar(&outFlag, "o", "", "output to `FILE` (default stdout)") |
| 74 | flag.StringVar(&outFlag, "output", "", "output to `FILE` (default stdout)") |
| 75 | flag.Parse() |
| 76 | |
| 77 | if versionFlag { |
| 78 | if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { |
| 79 | Version = buildInfo.Main.Version |
| 80 | } |
| 81 | fmt.Println(Version) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | if len(flag.Args()) != 0 && !convertFlag { |
| 86 | errorf("too many arguments") |
| 87 | } |
| 88 | if len(flag.Args()) > 1 && convertFlag { |
| 89 | errorf("too many arguments") |
| 90 | } |
| 91 | if pqFlag && convertFlag { |
| 92 | errorf("-pq cannot be used with -y") |
| 93 | } |
| 94 | |
| 95 | var converted []byte |
| 96 | if convertFlag { |
| 97 | in := os.Stdin |
| 98 | if inFile := flag.Arg(0); inFile != "" && inFile != "-" { |
| 99 | f, err := os.Open(inFile) |
| 100 | if err != nil { |
| 101 | errorf("failed to open input file %q: %v", inFile, err) |
| 102 | } |
| 103 | defer f.Close() |
| 104 | in = f |
| 105 | } |
| 106 | converted = convert(in) |
| 107 | } |
| 108 | |
| 109 | out := os.Stdout |
| 110 | if outFlag != "" { |
| 111 | f, err := os.OpenFile(outFlag, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) |
| 112 | if err != nil { |
| 113 | errorf("failed to open output file %q: %v", outFlag, err) |
| 114 | } |
| 115 | defer func() { |
| 116 | if err := f.Close(); err != nil { |
| 117 | errorf("failed to close output file %q: %v", outFlag, err) |
| 118 | } |
| 119 | }() |
| 120 | out = f |
| 121 | } |
| 122 | |
| 123 | if convertFlag { |
| 124 | writef(out, "%s", converted) |
| 125 | } else { |
| 126 | if fi, err := out.Stat(); err == nil && fi.Mode().IsRegular() && fi.Mode().Perm()&0004 != 0 { |
| 127 | warning("writing secret key to a world-readable file") |
| 128 | } |
| 129 | generate(out, pqFlag) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func generate(out *os.File, pq bool) { |
| 134 | var i age.Identity |
| 135 | var r age.Recipient |
| 136 | if pq { |
| 137 | k, err := age.GenerateHybridIdentity() |
| 138 | if err != nil { |
| 139 | errorf("internal error: %v", err) |
| 140 | } |
| 141 | i = k |
| 142 | r = k.Recipient() |
| 143 | } else { |
| 144 | k, err := age.GenerateX25519Identity() |
| 145 | if err != nil { |
| 146 | errorf("internal error: %v", err) |
| 147 | } |
| 148 | i = k |
| 149 | r = k.Recipient() |
| 150 | } |
| 151 | |
| 152 | if !term.IsTerminal(int(out.Fd())) { |
| 153 | fmt.Fprintf(os.Stderr, "Public key: %s\n", r) |
| 154 | } |
| 155 | |
| 156 | writef(out, "# created: %s\n", time.Now().Format(time.RFC3339)) |
| 157 | writef(out, "# public key: %s\n", r) |
| 158 | writef(out, "%s\n", i) |
| 159 | } |
| 160 | |
| 161 | func writef(out io.Writer, format string, v ...any) { |
| 162 | if _, err := fmt.Fprintf(out, format, v...); err != nil { |
| 163 | errorf("failed to write output: %v", err) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func convert(in io.Reader) []byte { |
| 168 | ids, err := age.ParseIdentities(in) |
| 169 | if err != nil { |
| 170 | errorf("failed to parse input: %v", err) |
| 171 | } |
| 172 | if len(ids) == 0 { |
| 173 | errorf("no identities found in the input") |
| 174 | } |
| 175 | var out []byte |
| 176 | for _, id := range ids { |
| 177 | switch id := id.(type) { |
| 178 | case *age.X25519Identity: |
| 179 | out = append(out, id.Recipient().String()...) |
| 180 | case *age.HybridIdentity: |
| 181 | out = append(out, id.Recipient().String()...) |
| 182 | default: |
| 183 | errorf("internal error: unexpected identity type: %T", id) |
| 184 | } |
| 185 | out = append(out, '\n') |
| 186 | } |
| 187 | return out |
| 188 | } |
| 189 | |
| 190 | func errorf(format string, v ...any) { |
| 191 | log.Printf("age-keygen: error: "+format, v...) |
| 192 | log.Fatalf("age-keygen: report unexpected or unhelpful errors at https://filippo.io/age/report") |
| 193 | } |
| 194 | |
| 195 | func warning(msg string) { |
| 196 | log.Printf("age-keygen: warning: %s", msg) |
| 197 | } |