2014-06-20 20:00:00 +01:00
|
|
|
// Copyright (c) 2014, Google Inc.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-07-14 23:28:14 +01:00
|
|
|
"errors"
|
2014-06-20 20:00:00 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-16 19:37:51 +01:00
|
|
|
// ssl.h reserves values 1000 and above for error codes corresponding to
|
|
|
|
// alerts. If automatically assigned reason codes exceed this value, this script
|
|
|
|
// will error. This must be kept in sync with SSL_AD_REASON_OFFSET in ssl.h.
|
|
|
|
const reservedReasonCode = 1000
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
var resetFlag *bool = flag.Bool("reset", false, "If true, ignore current assignments and reassign from scratch")
|
|
|
|
|
|
|
|
func makeErrors(reset bool) error {
|
2015-02-06 19:52:16 +00:00
|
|
|
topLevelPath, err := findToplevel()
|
2014-06-20 20:00:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-06 19:52:16 +00:00
|
|
|
dirName, err := os.Getwd()
|
2014-07-14 23:28:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-06 19:52:16 +00:00
|
|
|
|
|
|
|
lib := filepath.Base(dirName)
|
|
|
|
headerPath := filepath.Join(topLevelPath, "include", "openssl", lib+".h")
|
|
|
|
errDir := filepath.Join(topLevelPath, "crypto", "err")
|
|
|
|
dataPath := filepath.Join(errDir, lib+".errordata")
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
headerFile, err := os.Open(headerPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2014-07-14 23:28:14 +01:00
|
|
|
return fmt.Errorf("No header %s. Run in the right directory or touch the file.", headerPath)
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := strings.ToUpper(lib)
|
2015-06-29 04:36:21 +01:00
|
|
|
reasons, err := parseHeader(prefix, headerFile)
|
2014-06-20 20:00:00 +01:00
|
|
|
headerFile.Close()
|
|
|
|
|
|
|
|
if reset {
|
|
|
|
err = nil
|
2014-07-16 19:37:51 +01:00
|
|
|
// Retain any reason codes above reservedReasonCode.
|
|
|
|
newReasons := make(map[string]int)
|
|
|
|
for key, value := range reasons {
|
|
|
|
if value >= reservedReasonCode {
|
|
|
|
newReasons[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reasons = newReasons
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := os.Open(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
filenames, err := dir.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range filenames {
|
2015-02-06 19:52:16 +00:00
|
|
|
if !strings.HasSuffix(name, ".c") {
|
2014-06-20 20:00:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
if err := addReasons(reasons, name, prefix); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:37:51 +01:00
|
|
|
assignNewValues(reasons, reservedReasonCode)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
headerFile, err = os.Open(headerPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer headerFile.Close()
|
|
|
|
|
2014-07-14 23:28:14 +01:00
|
|
|
newHeaderFile, err := os.OpenFile(headerPath+".tmp", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
2014-06-20 20:00:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer newHeaderFile.Close()
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
if err := writeHeaderFile(newHeaderFile, headerFile, prefix, reasons); err != nil {
|
2014-06-20 20:00:00 +01:00
|
|
|
return err
|
|
|
|
}
|
2014-07-14 23:28:14 +01:00
|
|
|
os.Rename(headerPath+".tmp", headerPath)
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-02-06 19:52:16 +00:00
|
|
|
dataFile, err := os.OpenFile(dataPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
2014-06-20 20:00:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
outputStrings(dataFile, lib, reasons)
|
2015-02-06 19:52:16 +00:00
|
|
|
dataFile.Close()
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-06 19:52:16 +00:00
|
|
|
func findToplevel() (path string, err error) {
|
|
|
|
path = ".."
|
|
|
|
buildingPath := filepath.Join(path, "BUILDING")
|
2014-07-14 23:28:14 +01:00
|
|
|
|
2015-02-06 19:52:16 +00:00
|
|
|
_, err = os.Stat(buildingPath)
|
2014-07-14 23:28:14 +01:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
2015-02-06 19:52:16 +00:00
|
|
|
path = filepath.Join("..", path)
|
|
|
|
buildingPath = filepath.Join(path, "BUILDING")
|
|
|
|
_, err = os.Stat(buildingPath)
|
2014-07-14 23:28:14 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-02-06 19:52:16 +00:00
|
|
|
return "", errors.New("Cannot find BUILDING file at the top-level")
|
2014-07-14 23:28:14 +01:00
|
|
|
}
|
2015-02-06 19:52:16 +00:00
|
|
|
return path, nil
|
2014-07-14 23:28:14 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
type assignment struct {
|
|
|
|
key string
|
|
|
|
value int
|
|
|
|
}
|
|
|
|
|
|
|
|
type assignmentsSlice []assignment
|
|
|
|
|
|
|
|
func (a assignmentsSlice) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a assignmentsSlice) Less(i, j int) bool {
|
|
|
|
return a[i].value < a[j].value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a assignmentsSlice) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputAssignments(w io.Writer, assignments map[string]int) {
|
|
|
|
var sorted assignmentsSlice
|
|
|
|
|
|
|
|
for key, value := range assignments {
|
|
|
|
sorted = append(sorted, assignment{key, value})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sorted)
|
|
|
|
|
|
|
|
for _, assignment := range sorted {
|
|
|
|
fmt.Fprintf(w, "#define %s %d\n", assignment.key, assignment.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
func parseDefineLine(line, lib string) (key string, value int, ok bool) {
|
2014-06-20 20:00:00 +01:00
|
|
|
if !strings.HasPrefix(line, "#define ") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) != 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
key = fields[1]
|
2015-06-29 04:36:21 +01:00
|
|
|
if !strings.HasPrefix(key, lib+"_R_") {
|
2014-06-20 20:00:00 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if value, err = strconv.Atoi(fields[2]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[string]int) error {
|
2014-06-20 20:00:00 +01:00
|
|
|
var last []byte
|
|
|
|
var haveLast, sawDefine bool
|
|
|
|
newLine := []byte("\n")
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(headerFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2015-06-29 04:36:21 +01:00
|
|
|
_, _, ok := parseDefineLine(line, lib)
|
2014-06-20 20:00:00 +01:00
|
|
|
if ok {
|
|
|
|
sawDefine = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if haveLast {
|
|
|
|
w.Write(last)
|
|
|
|
w.Write(newLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) > 0 || !sawDefine {
|
|
|
|
last = []byte(line)
|
|
|
|
haveLast = true
|
|
|
|
} else {
|
|
|
|
haveLast = false
|
|
|
|
}
|
|
|
|
sawDefine = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
outputAssignments(w, reasons)
|
|
|
|
w.Write(newLine)
|
|
|
|
|
|
|
|
if haveLast {
|
|
|
|
w.Write(last)
|
|
|
|
w.Write(newLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
func outputStrings(w io.Writer, lib string, assignments map[string]int) {
|
2014-06-20 20:00:00 +01:00
|
|
|
lib = strings.ToUpper(lib)
|
2015-06-29 04:36:21 +01:00
|
|
|
prefixLen := len(lib + "_R_")
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
keys := make([]string, 0, len(assignments))
|
|
|
|
for key := range assignments {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
2015-06-29 04:36:21 +01:00
|
|
|
fmt.Fprintf(w, "%s,%d,%s\n", lib, assignments[key], key[prefixLen:])
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:37:51 +01:00
|
|
|
func assignNewValues(assignments map[string]int, reserved int) {
|
2015-02-06 19:52:16 +00:00
|
|
|
// Needs to be in sync with the reason limit in
|
|
|
|
// |ERR_reason_error_string|.
|
2014-06-20 20:00:00 +01:00
|
|
|
max := 99
|
|
|
|
|
|
|
|
for _, value := range assignments {
|
2014-07-16 19:37:51 +01:00
|
|
|
if reserved >= 0 && value >= reserved {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
if value > max {
|
|
|
|
max = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max++
|
|
|
|
|
2015-02-10 02:30:53 +00:00
|
|
|
// Sort the keys, so this script is reproducible.
|
|
|
|
keys := make([]string, 0, len(assignments))
|
2014-06-20 20:00:00 +01:00
|
|
|
for key, value := range assignments {
|
|
|
|
if value == -1 {
|
2015-02-10 02:30:53 +00:00
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if reserved >= 0 && max >= reserved {
|
|
|
|
// If this happens, try passing -reset. Otherwise bump
|
|
|
|
// up reservedReasonCode.
|
|
|
|
panic("Automatically-assigned values exceeded limit!")
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-02-10 02:30:53 +00:00
|
|
|
assignments[key] = max
|
|
|
|
max++
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDeclareMacro(line, join, macroName string, m map[string]int) {
|
|
|
|
if i := strings.Index(line, macroName); i >= 0 {
|
2014-07-14 23:28:14 +01:00
|
|
|
contents := line[i+len(macroName):]
|
2014-06-20 20:00:00 +01:00
|
|
|
if i := strings.Index(contents, ")"); i >= 0 {
|
|
|
|
contents = contents[:i]
|
|
|
|
args := strings.Split(contents, ",")
|
|
|
|
for i := range args {
|
|
|
|
args[i] = strings.TrimSpace(args[i])
|
|
|
|
}
|
|
|
|
if len(args) != 2 {
|
|
|
|
panic("Bad macro line: " + line)
|
|
|
|
}
|
|
|
|
token := args[0] + join + args[1]
|
|
|
|
if _, ok := m[token]; !ok {
|
|
|
|
m[token] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
func addReasons(reasons map[string]int, filename, prefix string) error {
|
2014-06-20 20:00:00 +01:00
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2015-02-11 20:53:03 +00:00
|
|
|
reasonPrefix := prefix + "_R_"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
handleDeclareMacro(line, "_R_", "OPENSSL_DECLARE_ERROR_REASON(", reasons)
|
|
|
|
|
|
|
|
for len(line) > 0 {
|
2015-06-29 04:36:21 +01:00
|
|
|
i := strings.Index(line, prefix+"_")
|
2014-06-20 20:00:00 +01:00
|
|
|
if i == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
line = line[i:]
|
|
|
|
end := strings.IndexFunc(line, func(r rune) bool {
|
|
|
|
return !(r == '_' || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9'))
|
|
|
|
})
|
|
|
|
if end == -1 {
|
|
|
|
end = len(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
token, line = line[:end], line[end:]
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(token, reasonPrefix):
|
|
|
|
if _, ok := reasons[token]; !ok {
|
|
|
|
reasons[token] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scanner.Err()
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
func parseHeader(lib string, file io.Reader) (reasons map[string]int, err error) {
|
2014-06-20 20:00:00 +01:00
|
|
|
reasons = make(map[string]int)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
2015-06-29 04:36:21 +01:00
|
|
|
key, value, ok := parseDefineLine(scanner.Text(), lib)
|
2014-06-20 20:00:00 +01:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:36:21 +01:00
|
|
|
reasons[key] = value
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scanner.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if err := makeErrors(*resetFlag); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|