| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyauth |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "bytes" |
| 20 | "fmt" |
| 21 | "os" |
| 22 | "os/signal" |
| 23 | |
| 24 | "github.com/spf13/cobra" |
| 25 | "golang.org/x/term" |
| 26 | |
| 27 | caddycmd "github.com/caddyserver/caddy/v2/cmd" |
| 28 | |
| 29 | "github.com/caddyserver/caddy/v2" |
| 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | caddycmd.RegisterCommand(caddycmd.Command{ |
| 34 | Name: "hash-password", |
| 35 | Usage: "[--plaintext <password>] [--algorithm <argon2id|bcrypt>] [--bcrypt-cost <difficulty>] [--argon2id-time <iterations>] [--argon2id-memory <KiB>] [--argon2id-threads <n>] [--argon2id-keylen <bytes>]", |
| 36 | Short: "Hashes a password and writes base64", |
| 37 | Long: ` |
| 38 | Convenient way to hash a plaintext password. The resulting |
| 39 | hash is written to stdout as a base64 string. |
| 40 | |
| 41 | --plaintext |
| 42 | The password to hash. If omitted, it will be read from stdin. |
| 43 | If Caddy is attached to a controlling TTY, the input will not be echoed. |
| 44 | |
| 45 | --algorithm |
| 46 | Selects the hashing algorithm. Valid options are: |
| 47 | * 'argon2id' (recommended for modern security) |
| 48 | * 'bcrypt' (legacy, slower, configurable cost) |
| 49 | |
| 50 | bcrypt-specific parameters: |
| 51 | |
| 52 | --bcrypt-cost |
| 53 | Sets the bcrypt hashing difficulty. Higher values increase security by |
| 54 | making the hash computation slower and more CPU-intensive. |
| 55 | Must be within the valid range [bcrypt.MinCost, bcrypt.MaxCost]. |
| 56 | If omitted or invalid, the default cost is used. |
| 57 | |
| 58 | Argon2id-specific parameters: |
| 59 | |
| 60 | --argon2id-time |
| 61 | Number of iterations to perform. Increasing this makes |
| 62 | hashing slower and more resistant to brute-force attacks. |
| 63 | |
| 64 | --argon2id-memory |
| 65 | Amount of memory to use during hashing. |
| 66 | Larger values increase resistance to GPU/ASIC attacks. |
| 67 | |
| 68 | --argon2id-threads |
| 69 | Number of CPU threads to use. Increase for faster hashing |
| 70 | on multi-core systems. |
| 71 | |
| 72 | --argon2id-keylen |
| 73 | Length of the resulting hash in bytes. Longer keys increase |
| 74 | security but slightly increase storage size. |
| 75 | `, |
| 76 | CobraFunc: func(cmd *cobra.Command) { |
| 77 | cmd.Flags().StringP("plaintext", "p", "", "The plaintext password") |
| 78 | cmd.Flags().StringP("algorithm", "a", bcryptName, "Name of the hash algorithm") |
| 79 | cmd.Flags().Int("bcrypt-cost", defaultBcryptCost, "Bcrypt hashing cost (only used with 'bcrypt' algorithm)") |
| 80 | cmd.Flags().Uint32("argon2id-time", defaultArgon2idTime, "Number of iterations for Argon2id hashing. Increasing this makes the hash slower and more resistant to brute-force attacks.") |
| 81 | cmd.Flags().Uint32("argon2id-memory", defaultArgon2idMemory, "Memory to use in KiB for Argon2id hashing. Larger values increase resistance to GPU/ASIC attacks.") |
| 82 | cmd.Flags().Uint8("argon2id-threads", defaultArgon2idThreads, "Number of CPU threads to use for Argon2id hashing. Increase for faster hashing on multi-core systems.") |
| 83 | cmd.Flags().Uint32("argon2id-keylen", defaultArgon2idKeylen, "Length of the resulting Argon2id hash in bytes. Longer hashes increase security but slightly increase storage size.") |
| 84 | cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdHashPassword) |
| 85 | }, |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | func cmdHashPassword(fs caddycmd.Flags) (int, error) { |
| 90 | var err error |
| 91 | |
| 92 | algorithm := fs.String("algorithm") |
| 93 | plaintext := []byte(fs.String("plaintext")) |
| 94 | bcryptCost := fs.Int("bcrypt-cost") |
| 95 | |
| 96 | if len(plaintext) == 0 { |
| 97 | fd := int(os.Stdin.Fd()) |
| 98 | if term.IsTerminal(fd) { |
| 99 | // ensure the terminal state is restored on SIGINT |
| 100 | state, _ := term.GetState(fd) |
| 101 | c := make(chan os.Signal, 1) |
| 102 | signal.Notify(c, os.Interrupt) |
| 103 | go func() { |
| 104 | <-c |
| 105 | _ = term.Restore(fd, state) |
| 106 | os.Exit(caddy.ExitCodeFailedStartup) |
| 107 | }() |
| 108 | defer signal.Stop(c) |
| 109 | |
| 110 | fmt.Fprint(os.Stderr, "Enter password: ") |
| 111 | plaintext, err = term.ReadPassword(fd) |
| 112 | fmt.Fprintln(os.Stderr) |
| 113 | if err != nil { |
| 114 | return caddy.ExitCodeFailedStartup, err |
| 115 | } |
| 116 | |
| 117 | fmt.Fprint(os.Stderr, "Confirm password: ") |
| 118 | confirmation, err := term.ReadPassword(fd) |
| 119 | fmt.Fprintln(os.Stderr) |
| 120 | if err != nil { |
| 121 | return caddy.ExitCodeFailedStartup, err |
| 122 | } |
| 123 | |
| 124 | if !bytes.Equal(plaintext, confirmation) { |
| 125 | return caddy.ExitCodeFailedStartup, fmt.Errorf("password does not match") |
| 126 | } |
| 127 | } else { |
| 128 | rd := bufio.NewReader(os.Stdin) |
| 129 | plaintext, err = rd.ReadBytes('\n') |
| 130 | if err != nil { |
| 131 | return caddy.ExitCodeFailedStartup, err |
| 132 | } |
| 133 | |
| 134 | plaintext = plaintext[:len(plaintext)-1] // Trailing newline |
| 135 | } |
| 136 | |
| 137 | if len(plaintext) == 0 { |
| 138 | return caddy.ExitCodeFailedStartup, fmt.Errorf("plaintext is required") |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | var hash []byte |
| 143 | var hashString string |
| 144 | switch algorithm { |
| 145 | case bcryptName: |
| 146 | hash, err = BcryptHash{cost: bcryptCost}.Hash(plaintext) |
| 147 | hashString = string(hash) |
| 148 | case argon2idName: |
| 149 | time, err := fs.GetUint32("argon2id-time") |
| 150 | if err != nil { |
| 151 | return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to get argon2id time parameter: %w", err) |
| 152 | } |
| 153 | memory, err := fs.GetUint32("argon2id-memory") |
| 154 | if err != nil { |
| 155 | return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to get argon2id memory parameter: %w", err) |
| 156 | } |
| 157 | threads, err := fs.GetUint8("argon2id-threads") |
| 158 | if err != nil { |
| 159 | return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to get argon2id threads parameter: %w", err) |
| 160 | } |
| 161 | keyLen, err := fs.GetUint32("argon2id-keylen") |
| 162 | if err != nil { |
| 163 | return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to get argon2id keylen parameter: %w", err) |
| 164 | } |
| 165 | |
| 166 | hash, _ = Argon2idHash{ |
| 167 | time: time, |
| 168 | memory: memory, |
| 169 | threads: threads, |
| 170 | keyLen: keyLen, |
| 171 | }.Hash(plaintext) |
| 172 | |
| 173 | hashString = string(hash) |
| 174 | default: |
| 175 | return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm) |
| 176 | } |
| 177 | if err != nil { |
| 178 | return caddy.ExitCodeFailedStartup, err |
| 179 | } |
| 180 | |
| 181 | fmt.Println(hashString) |
| 182 | |
| 183 | return 0, nil |
| 184 | } |