Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014, Google Inc. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "errors" |
| 20 | "flag" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "sort" |
| 26 | "strconv" |
| 27 | "strings" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // ssl.h reserves values 1000 and above for error codes corresponding to |
| 31 | // alerts. If automatically assigned reason codes exceed this value, this script |
| 32 | // will error. This must be kept in sync with SSL_AD_REASON_OFFSET in ssl.h. |
| 33 | const reservedReasonCode = 1000 |
| 34 | |
| 35 | var resetFlag *bool = flag.Bool("reset", false, "If true, ignore current assignments and reassign from scratch") |
| 36 | |
| 37 | func makeErrors(reset bool) error { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 38 | topLevelPath, err := findToplevel() |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 43 | dirName, err := os.Getwd() |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | lib := filepath.Base(dirName) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 49 | headerPath := filepath.Join(topLevelPath, "include", "openssl", lib+".h") |
| 50 | errDir := filepath.Join(topLevelPath, "crypto", "err") |
| 51 | dataPath := filepath.Join(errDir, lib+".errordata") |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 52 | |
| 53 | headerFile, err := os.Open(headerPath) |
| 54 | if err != nil { |
| 55 | if os.IsNotExist(err) { |
| 56 | return fmt.Errorf("No header %s. Run in the right directory or touch the file.", headerPath) |
| 57 | } |
| 58 | |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | prefix := strings.ToUpper(lib) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 63 | reasons, err := parseHeader(prefix, headerFile) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | headerFile.Close() |
| 65 | |
| 66 | if reset { |
| 67 | err = nil |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 68 | // Retain any reason codes above reservedReasonCode. |
| 69 | newReasons := make(map[string]int) |
| 70 | for key, value := range reasons { |
| 71 | if value >= reservedReasonCode { |
| 72 | newReasons[key] = value |
| 73 | } |
| 74 | } |
| 75 | reasons = newReasons |
| 76 | } |
| 77 | |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | dir, err := os.Open(".") |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | defer dir.Close() |
| 87 | |
| 88 | filenames, err := dir.Readdirnames(-1) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 93 | if filepath.Base(filepath.Dir(dirName)) == "fipsmodule" { |
| 94 | // Search the non-FIPS half of library for error codes as well. |
| 95 | extraPath := filepath.Join(topLevelPath, "crypto", lib+"_extra") |
| 96 | extraDir, err := os.Open(extraPath) |
| 97 | if err != nil && !os.IsNotExist(err) { |
| 98 | return err |
| 99 | } |
| 100 | if err == nil { |
| 101 | defer extraDir.Close() |
| 102 | extraFilenames, err := extraDir.Readdirnames(-1) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | for _, extraFilename := range extraFilenames { |
| 107 | filenames = append(filenames, filepath.Join(extraPath, extraFilename)) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | for _, name := range filenames { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 113 | if !strings.HasSuffix(name, ".c") { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | continue |
| 115 | } |
| 116 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 117 | if err := addReasons(reasons, name, prefix); err != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | return err |
| 119 | } |
| 120 | } |
| 121 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 122 | assignNewValues(reasons, reservedReasonCode) |
| 123 | |
| 124 | headerFile, err = os.Open(headerPath) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | defer headerFile.Close() |
| 129 | |
| 130 | newHeaderFile, err := os.OpenFile(headerPath+".tmp", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | defer newHeaderFile.Close() |
| 135 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 136 | if err := writeHeaderFile(newHeaderFile, headerFile, prefix, reasons); err != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 137 | return err |
| 138 | } |
| 139 | os.Rename(headerPath+".tmp", headerPath) |
| 140 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 141 | dataFile, err := os.OpenFile(dataPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 142 | if err != nil { |
| 143 | return err |
| 144 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 145 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 146 | outputStrings(dataFile, lib, reasons) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 147 | dataFile.Close() |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 148 | |
| 149 | return nil |
| 150 | } |
| 151 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 152 | func findToplevel() (path string, err error) { |
| 153 | path = ".." |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 154 | buildingPath := filepath.Join(path, "BUILDING.md") |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 156 | _, err = os.Stat(buildingPath) |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 157 | for i := 0; i < 2 && err != nil && os.IsNotExist(err); i++ { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 158 | path = filepath.Join("..", path) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 159 | buildingPath = filepath.Join(path, "BUILDING.md") |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 160 | _, err = os.Stat(buildingPath) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 161 | } |
| 162 | if err != nil { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 163 | return "", errors.New("Cannot find BUILDING.md file at the top-level") |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 165 | return path, nil |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | type assignment struct { |
| 169 | key string |
| 170 | value int |
| 171 | } |
| 172 | |
| 173 | type assignmentsSlice []assignment |
| 174 | |
| 175 | func (a assignmentsSlice) Len() int { |
| 176 | return len(a) |
| 177 | } |
| 178 | |
| 179 | func (a assignmentsSlice) Less(i, j int) bool { |
| 180 | return a[i].value < a[j].value |
| 181 | } |
| 182 | |
| 183 | func (a assignmentsSlice) Swap(i, j int) { |
| 184 | a[i], a[j] = a[j], a[i] |
| 185 | } |
| 186 | |
| 187 | func outputAssignments(w io.Writer, assignments map[string]int) { |
| 188 | var sorted assignmentsSlice |
| 189 | |
| 190 | for key, value := range assignments { |
| 191 | sorted = append(sorted, assignment{key, value}) |
| 192 | } |
| 193 | |
| 194 | sort.Sort(sorted) |
| 195 | |
| 196 | for _, assignment := range sorted { |
| 197 | fmt.Fprintf(w, "#define %s %d\n", assignment.key, assignment.value) |
| 198 | } |
| 199 | } |
| 200 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 201 | func parseDefineLine(line, lib string) (key string, value int, ok bool) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | if !strings.HasPrefix(line, "#define ") { |
| 203 | return |
| 204 | } |
| 205 | |
| 206 | fields := strings.Fields(line) |
| 207 | if len(fields) != 3 { |
| 208 | return |
| 209 | } |
| 210 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 211 | key = fields[1] |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 212 | if !strings.HasPrefix(key, lib+"_R_") { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | return |
| 214 | } |
| 215 | |
| 216 | var err error |
| 217 | if value, err = strconv.Atoi(fields[2]); err != nil { |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | ok = true |
| 222 | return |
| 223 | } |
| 224 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 225 | func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[string]int) error { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | var last []byte |
| 227 | var haveLast, sawDefine bool |
| 228 | newLine := []byte("\n") |
| 229 | |
| 230 | scanner := bufio.NewScanner(headerFile) |
| 231 | for scanner.Scan() { |
| 232 | line := scanner.Text() |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 233 | _, _, ok := parseDefineLine(line, lib) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 234 | if ok { |
| 235 | sawDefine = true |
| 236 | continue |
| 237 | } |
| 238 | |
| 239 | if haveLast { |
| 240 | w.Write(last) |
| 241 | w.Write(newLine) |
| 242 | } |
| 243 | |
| 244 | if len(line) > 0 || !sawDefine { |
| 245 | last = []byte(line) |
| 246 | haveLast = true |
| 247 | } else { |
| 248 | haveLast = false |
| 249 | } |
| 250 | sawDefine = false |
| 251 | } |
| 252 | |
| 253 | if err := scanner.Err(); err != nil { |
| 254 | return err |
| 255 | } |
| 256 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | outputAssignments(w, reasons) |
| 258 | w.Write(newLine) |
| 259 | |
| 260 | if haveLast { |
| 261 | w.Write(last) |
| 262 | w.Write(newLine) |
| 263 | } |
| 264 | |
| 265 | return nil |
| 266 | } |
| 267 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 268 | func outputStrings(w io.Writer, lib string, assignments map[string]int) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | lib = strings.ToUpper(lib) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 270 | prefixLen := len(lib + "_R_") |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 271 | |
| 272 | keys := make([]string, 0, len(assignments)) |
| 273 | for key := range assignments { |
| 274 | keys = append(keys, key) |
| 275 | } |
| 276 | sort.Strings(keys) |
| 277 | |
| 278 | for _, key := range keys { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 279 | fmt.Fprintf(w, "%s,%d,%s\n", lib, assignments[key], key[prefixLen:]) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | func assignNewValues(assignments map[string]int, reserved int) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 284 | // Needs to be in sync with the reason limit in |
| 285 | // |ERR_reason_error_string|. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 286 | max := 99 |
| 287 | |
| 288 | for _, value := range assignments { |
| 289 | if reserved >= 0 && value >= reserved { |
| 290 | continue |
| 291 | } |
| 292 | if value > max { |
| 293 | max = value |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | max++ |
| 298 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 299 | // Sort the keys, so this script is reproducible. |
| 300 | keys := make([]string, 0, len(assignments)) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | for key, value := range assignments { |
| 302 | if value == -1 { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 303 | keys = append(keys, key) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 304 | } |
| 305 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 306 | sort.Strings(keys) |
| 307 | |
| 308 | for _, key := range keys { |
| 309 | if reserved >= 0 && max >= reserved { |
| 310 | // If this happens, try passing -reset. Otherwise bump |
| 311 | // up reservedReasonCode. |
| 312 | panic("Automatically-assigned values exceeded limit!") |
| 313 | } |
| 314 | assignments[key] = max |
| 315 | max++ |
| 316 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | func handleDeclareMacro(line, join, macroName string, m map[string]int) { |
| 320 | if i := strings.Index(line, macroName); i >= 0 { |
| 321 | contents := line[i+len(macroName):] |
| 322 | if i := strings.Index(contents, ")"); i >= 0 { |
| 323 | contents = contents[:i] |
| 324 | args := strings.Split(contents, ",") |
| 325 | for i := range args { |
| 326 | args[i] = strings.TrimSpace(args[i]) |
| 327 | } |
| 328 | if len(args) != 2 { |
| 329 | panic("Bad macro line: " + line) |
| 330 | } |
| 331 | token := args[0] + join + args[1] |
| 332 | if _, ok := m[token]; !ok { |
| 333 | m[token] = -1 |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 339 | func addReasons(reasons map[string]int, filename, prefix string) error { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 340 | file, err := os.Open(filename) |
| 341 | if err != nil { |
| 342 | return err |
| 343 | } |
| 344 | defer file.Close() |
| 345 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 346 | reasonPrefix := prefix + "_R_" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | |
| 348 | scanner := bufio.NewScanner(file) |
| 349 | for scanner.Scan() { |
| 350 | line := scanner.Text() |
| 351 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | handleDeclareMacro(line, "_R_", "OPENSSL_DECLARE_ERROR_REASON(", reasons) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 353 | |
| 354 | for len(line) > 0 { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 355 | i := strings.Index(line, prefix+"_") |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 356 | if i == -1 { |
| 357 | break |
| 358 | } |
| 359 | |
| 360 | line = line[i:] |
| 361 | end := strings.IndexFunc(line, func(r rune) bool { |
| 362 | return !(r == '_' || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9')) |
| 363 | }) |
| 364 | if end == -1 { |
| 365 | end = len(line) |
| 366 | } |
| 367 | |
| 368 | var token string |
| 369 | token, line = line[:end], line[end:] |
| 370 | |
| 371 | switch { |
| 372 | case strings.HasPrefix(token, reasonPrefix): |
| 373 | if _, ok := reasons[token]; !ok { |
| 374 | reasons[token] = -1 |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return scanner.Err() |
| 381 | } |
| 382 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 383 | func parseHeader(lib string, file io.Reader) (reasons map[string]int, err error) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 384 | reasons = make(map[string]int) |
| 385 | |
| 386 | scanner := bufio.NewScanner(file) |
| 387 | for scanner.Scan() { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 388 | key, value, ok := parseDefineLine(scanner.Text(), lib) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | if !ok { |
| 390 | continue |
| 391 | } |
| 392 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 393 | reasons[key] = value |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | err = scanner.Err() |
| 397 | return |
| 398 | } |
| 399 | |
| 400 | func main() { |
| 401 | flag.Parse() |
| 402 | |
| 403 | if err := makeErrors(*resetFlag); err != nil { |
| 404 | fmt.Fprintf(os.Stderr, "%s\n", err) |
| 405 | os.Exit(1) |
| 406 | } |
| 407 | } |