• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The BoringSSL Authors
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
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//go:build ignore
16
17package main
18
19import (
20	"bufio"
21	"bytes"
22	"errors"
23	"fmt"
24	"os"
25	"os/exec"
26	"sort"
27	"strconv"
28	"strings"
29)
30
31func sanitizeName(in string) string {
32	in = strings.Replace(in, "-", "_", -1)
33	in = strings.Replace(in, ".", "_", -1)
34	in = strings.Replace(in, " ", "_", -1)
35	return in
36}
37
38type object struct {
39	name string
40	// shortName and longName are the short and long names, respectively. If
41	// one is missing, it takes the value of the other, but the
42	// corresponding SN_foo or LN_foo macro is not defined.
43	shortName, longName       string
44	hasShortName, hasLongName bool
45	oid                       []int
46	encoded                   []byte
47}
48
49type objects struct {
50	// byNID is the list of all objects, indexed by nid.
51	byNID []object
52	// nameToNID is a map from object name to nid.
53	nameToNID map[string]int
54}
55
56func readNumbers(path string) (nameToNID map[string]int, numNIDs int, err error) {
57	in, err := os.Open(path)
58	if err != nil {
59		return nil, 0, err
60	}
61	defer in.Close()
62
63	nameToNID = make(map[string]int)
64	nidsSeen := make(map[int]struct{})
65
66	// Reserve NID 0 for NID_undef.
67	numNIDs = 1
68	nameToNID["undef"] = 0
69	nidsSeen[0] = struct{}{}
70
71	var lineNo int
72	scanner := bufio.NewScanner(in)
73	for scanner.Scan() {
74		line := scanner.Text()
75		lineNo++
76		withLine := func(err error) error {
77			return fmt.Errorf("%s:%d: %s", path, lineNo, err)
78		}
79
80		fields := strings.Fields(line)
81		if len(fields) == 0 {
82			// Skip blank lines.
83			continue
84		}
85
86		// Each line is a name and a nid, separated by space.
87		if len(fields) != 2 {
88			return nil, 0, withLine(errors.New("syntax error"))
89		}
90		name := fields[0]
91		nid, err := strconv.Atoi(fields[1])
92		if err != nil {
93			return nil, 0, withLine(err)
94		}
95		if nid < 0 {
96			return nil, 0, withLine(errors.New("invalid NID"))
97		}
98
99		// NID_undef is implicitly defined.
100		if name == "undef" && nid == 0 {
101			continue
102		}
103
104		// Forbid duplicates.
105		if _, ok := nameToNID[name]; ok {
106			return nil, 0, withLine(fmt.Errorf("duplicate name %q", name))
107		}
108		if _, ok := nidsSeen[nid]; ok {
109			return nil, 0, withLine(fmt.Errorf("duplicate NID %d", nid))
110		}
111
112		nameToNID[name] = nid
113		nidsSeen[nid] = struct{}{}
114
115		if nid >= numNIDs {
116			numNIDs = nid + 1
117		}
118	}
119	if err := scanner.Err(); err != nil {
120		return nil, 0, fmt.Errorf("error reading %s: %s", path, err)
121	}
122
123	return nameToNID, numNIDs, nil
124}
125
126func parseOID(aliases map[string][]int, in []string) (oid []int, err error) {
127	if len(in) == 0 {
128		return
129	}
130
131	// The first entry may be a reference to a previous alias.
132	if alias, ok := aliases[sanitizeName(in[0])]; ok {
133		in = in[1:]
134		oid = append(oid, alias...)
135	}
136
137	for _, c := range in {
138		val, err := strconv.Atoi(c)
139		if err != nil {
140			return nil, err
141		}
142		if val < 0 {
143			return nil, fmt.Errorf("negative component")
144		}
145		oid = append(oid, val)
146	}
147	return
148}
149
150func appendBase128(dst []byte, value int) []byte {
151	// Zero is encoded with one, not zero bytes.
152	if value == 0 {
153		return append(dst, 0)
154	}
155
156	// Count how many bytes are needed.
157	var l int
158	for n := value; n != 0; n >>= 7 {
159		l++
160	}
161	for ; l > 0; l-- {
162		b := byte(value>>uint(7*(l-1))) & 0x7f
163		if l > 1 {
164			b |= 0x80
165		}
166		dst = append(dst, b)
167	}
168	return dst
169}
170
171func encodeOID(oid []int) []byte {
172	if len(oid) < 2 {
173		return nil
174	}
175
176	var der []byte
177	der = appendBase128(der, 40*oid[0]+oid[1])
178	for _, value := range oid[2:] {
179		der = appendBase128(der, value)
180	}
181	return der
182}
183
184func readObjects(numPath, objectsPath string) (*objects, error) {
185	nameToNID, numNIDs, err := readNumbers(numPath)
186	if err != nil {
187		return nil, err
188	}
189
190	in, err := os.Open(objectsPath)
191	if err != nil {
192		return nil, err
193	}
194	defer in.Close()
195
196	// Implicitly define NID_undef.
197	objs := &objects{
198		byNID:     make([]object, numNIDs),
199		nameToNID: make(map[string]int),
200	}
201
202	objs.byNID[0] = object{
203		name:         "undef",
204		shortName:    "UNDEF",
205		longName:     "undefined",
206		hasShortName: true,
207		hasLongName:  true,
208	}
209	objs.nameToNID["undef"] = 0
210
211	var module, nextName string
212	var lineNo int
213	longNamesSeen := make(map[string]struct{})
214	shortNamesSeen := make(map[string]struct{})
215	aliases := make(map[string][]int)
216	scanner := bufio.NewScanner(in)
217	for scanner.Scan() {
218		line := scanner.Text()
219		lineNo++
220		withLine := func(err error) error {
221			return fmt.Errorf("%s:%d: %s", objectsPath, lineNo, err)
222		}
223
224		// Remove comments.
225		idx := strings.IndexRune(line, '#')
226		if idx >= 0 {
227			line = line[:idx]
228		}
229
230		// Skip empty lines.
231		line = strings.TrimSpace(line)
232		if len(line) == 0 {
233			continue
234		}
235
236		if line[0] == '!' {
237			args := strings.Fields(line)
238			switch args[0] {
239			case "!module":
240				if len(args) != 2 {
241					return nil, withLine(errors.New("too many arguments"))
242				}
243				module = sanitizeName(args[1]) + "_"
244			case "!global":
245				module = ""
246			case "!Cname":
247				// !Cname directives override the name for the
248				// next object.
249				if len(args) != 2 {
250					return nil, withLine(errors.New("too many arguments"))
251				}
252				nextName = sanitizeName(args[1])
253			case "!Alias":
254				// !Alias directives define an alias for an OID
255				// without emitting an object.
256				if len(nextName) != 0 {
257					return nil, withLine(errors.New("!Cname directives may not modify !Alias directives."))
258				}
259				if len(args) < 3 {
260					return nil, withLine(errors.New("not enough arguments"))
261				}
262				aliasName := module + sanitizeName(args[1])
263				oid, err := parseOID(aliases, args[2:])
264				if err != nil {
265					return nil, withLine(err)
266				}
267				if _, ok := aliases[aliasName]; ok {
268					return nil, withLine(fmt.Errorf("duplicate name '%s'", aliasName))
269				}
270				aliases[aliasName] = oid
271			default:
272				return nil, withLine(fmt.Errorf("unknown directive '%s'", args[0]))
273			}
274			continue
275		}
276
277		fields := strings.Split(line, ":")
278		if len(fields) < 2 || len(fields) > 3 {
279			return nil, withLine(errors.New("invalid field count"))
280		}
281
282		obj := object{name: nextName}
283		nextName = ""
284
285		var err error
286		obj.oid, err = parseOID(aliases, strings.Fields(fields[0]))
287		if err != nil {
288			return nil, withLine(err)
289		}
290		obj.encoded = encodeOID(obj.oid)
291
292		obj.shortName = strings.TrimSpace(fields[1])
293		if len(fields) == 3 {
294			obj.longName = strings.TrimSpace(fields[2])
295		}
296
297		// Long and short names default to each other if missing.
298		if len(obj.shortName) == 0 {
299			obj.shortName = obj.longName
300		} else {
301			obj.hasShortName = true
302		}
303		if len(obj.longName) == 0 {
304			obj.longName = obj.shortName
305		} else {
306			obj.hasLongName = true
307		}
308		if len(obj.shortName) == 0 || len(obj.longName) == 0 {
309			return nil, withLine(errors.New("object with no name"))
310		}
311
312		// If not already specified, prefer the long name if it has no
313		// spaces, otherwise the short name.
314		if len(obj.name) == 0 && strings.IndexRune(obj.longName, ' ') < 0 {
315			obj.name = sanitizeName(obj.longName)
316		}
317		if len(obj.name) == 0 {
318			obj.name = sanitizeName(obj.shortName)
319		}
320		obj.name = module + obj.name
321
322		// Check for duplicate names.
323		if _, ok := aliases[obj.name]; ok {
324			return nil, withLine(fmt.Errorf("duplicate name '%s'", obj.name))
325		}
326		if _, ok := shortNamesSeen[obj.shortName]; ok && len(obj.shortName) > 0 {
327			return nil, withLine(fmt.Errorf("duplicate short name '%s'", obj.shortName))
328		}
329		if _, ok := longNamesSeen[obj.longName]; ok && len(obj.longName) > 0 {
330			return nil, withLine(fmt.Errorf("duplicate long name '%s'", obj.longName))
331		}
332
333		// Allocate a NID.
334		nid, ok := nameToNID[obj.name]
335		if !ok {
336			nid = len(objs.byNID)
337			objs.byNID = append(objs.byNID, object{})
338		}
339
340		objs.byNID[nid] = obj
341		objs.nameToNID[obj.name] = nid
342
343		longNamesSeen[obj.longName] = struct{}{}
344		shortNamesSeen[obj.shortName] = struct{}{}
345		aliases[obj.name] = obj.oid
346	}
347	if err := scanner.Err(); err != nil {
348		return nil, err
349	}
350
351	// The kNIDsIn*Order constants assume each NID fits in a uint16_t.
352	if len(objs.byNID) > 0xffff {
353		return nil, errors.New("too many NIDs allocated")
354	}
355
356	return objs, nil
357}
358
359func writeNumbers(path string, objs *objects) error {
360	out, err := os.Create(path)
361	if err != nil {
362		return err
363	}
364	defer out.Close()
365
366	for nid, obj := range objs.byNID {
367		if len(obj.name) == 0 {
368			continue
369		}
370		if _, err := fmt.Fprintf(out, "%s\t\t%d\n", obj.name, nid); err != nil {
371			return err
372		}
373	}
374	return nil
375}
376
377func clangFormat(input string) (string, error) {
378	var b bytes.Buffer
379	cmd := exec.Command("clang-format")
380	cmd.Stdin = strings.NewReader(input)
381	cmd.Stdout = &b
382	cmd.Stderr = os.Stderr
383	if err := cmd.Run(); err != nil {
384		return "", err
385	}
386	return b.String(), nil
387}
388
389func writeHeader(path string, objs *objects) error {
390	var b bytes.Buffer
391	fmt.Fprintf(&b, `/*
392 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
393 *
394 * Licensed under the OpenSSL license (the "License").  You may not use
395 * this file except in compliance with the License.  You can obtain a copy
396 * in the file LICENSE in the source distribution or at
397 * https://www.openssl.org/source/license.html
398 */
399
400/* This file is generated by crypto/obj/objects.go. */
401
402#ifndef OPENSSL_HEADER_NID_H
403#define OPENSSL_HEADER_NID_H
404
405#include <openssl/base.h>
406
407#if defined(__cplusplus)
408extern "C" {
409#endif
410
411
412/* The nid library provides numbered values for ASN.1 object identifiers and
413 * other symbols. These values are used by other libraries to identify
414 * cryptographic primitives.
415 *
416 * A separate objects library, obj.h, provides functions for converting between
417 * nids and object identifiers. However it depends on large internal tables with
418 * the encodings of every nid defined. Consumers concerned with binary size
419 * should instead embed the encodings of the few consumed OIDs and compare
420 * against those.
421 *
422 * These values should not be used outside of a single process; they are not
423 * stable identifiers. */
424
425
426`)
427
428	for nid, obj := range objs.byNID {
429		if len(obj.name) == 0 {
430			continue
431		}
432
433		if obj.hasShortName {
434			fmt.Fprintf(&b, "#define SN_%s \"%s\"\n", obj.name, obj.shortName)
435		}
436		if obj.hasLongName {
437			fmt.Fprintf(&b, "#define LN_%s \"%s\"\n", obj.name, obj.longName)
438		}
439		fmt.Fprintf(&b, "#define NID_%s %d\n", obj.name, nid)
440
441		// Although NID_undef does not have an OID, OpenSSL emits
442		// OBJ_undef as if it were zero.
443		oid := obj.oid
444		if nid == 0 {
445			oid = []int{0}
446		}
447		if len(oid) != 0 {
448			var oidStr string
449			for _, val := range oid {
450				if len(oidStr) != 0 {
451					oidStr += ","
452				}
453				oidStr += fmt.Sprintf("%dL", val)
454			}
455
456			fmt.Fprintf(&b, "#define OBJ_%s %s\n", obj.name, oidStr)
457		}
458
459		fmt.Fprintf(&b, "\n")
460	}
461
462	fmt.Fprintf(&b, `
463#if defined(__cplusplus)
464}  /* extern C */
465#endif
466
467#endif  /* OPENSSL_HEADER_NID_H */
468`)
469
470	formatted, err := clangFormat(b.String())
471	if err != nil {
472		return err
473	}
474
475	return os.WriteFile(path, []byte(formatted), 0666)
476}
477
478func sortNIDs(nids []int, objs *objects, cmp func(a, b object) bool) {
479	sort.Slice(nids, func(i, j int) bool { return cmp(objs.byNID[nids[i]], objs.byNID[nids[j]]) })
480}
481
482func writeData(path string, objs *objects) error {
483	var b bytes.Buffer
484	fmt.Fprintf(&b, `/*
485 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
486 *
487 * Licensed under the OpenSSL license (the "License").  You may not use
488 * this file except in compliance with the License.  You can obtain a copy
489 * in the file LICENSE in the source distribution or at
490 * https://www.openssl.org/source/license.html
491 */
492
493/* This file is generated by crypto/obj/objects.go. */
494
495
496`)
497
498	fmt.Fprintf(&b, "#define NUM_NID %d\n", len(objs.byNID))
499
500	// Emit each object's DER encoding, concatenated, and save the offsets.
501	fmt.Fprintf(&b, "\nstatic const uint8_t kObjectData[] = {\n")
502	offsets := make([]int, len(objs.byNID))
503	var nextOffset int
504	for nid, obj := range objs.byNID {
505		if len(obj.name) == 0 || len(obj.encoded) == 0 {
506			offsets[nid] = -1
507			continue
508		}
509
510		offsets[nid] = nextOffset
511		nextOffset += len(obj.encoded)
512		fmt.Fprintf(&b, "/* NID_%s */\n", obj.name)
513		for _, val := range obj.encoded {
514			fmt.Fprintf(&b, "0x%02x, ", val)
515		}
516		fmt.Fprintf(&b, "\n")
517	}
518	fmt.Fprintf(&b, "};\n")
519
520	// Emit an ASN1_OBJECT for each object.
521	fmt.Fprintf(&b, "\nstatic const ASN1_OBJECT kObjects[NUM_NID] = {\n")
522	for nid, obj := range objs.byNID {
523		// Skip the entry for NID_undef. It is stored separately, so that
524		// OBJ_get_undef avoids pulling in the table.
525		if nid == 0 {
526			continue
527		}
528
529		if len(obj.name) == 0 {
530			fmt.Fprintf(&b, "{NULL, NULL, NID_undef, 0, NULL, 0},\n")
531			continue
532		}
533
534		fmt.Fprintf(&b, "{\"%s\", \"%s\", NID_%s, ", obj.shortName, obj.longName, obj.name)
535		if offset := offsets[nid]; offset >= 0 {
536			fmt.Fprintf(&b, "%d, &kObjectData[%d], 0},\n", len(obj.encoded), offset)
537		} else {
538			fmt.Fprintf(&b, "0, NULL, 0},\n")
539		}
540	}
541	fmt.Fprintf(&b, "};\n")
542
543	// Emit a list of NIDs sorted by short name.
544	var nids []int
545	for nid, obj := range objs.byNID {
546		if len(obj.name) == 0 || len(obj.shortName) == 0 {
547			continue
548		}
549		nids = append(nids, nid)
550	}
551	sortNIDs(nids, objs, func(a, b object) bool { return a.shortName < b.shortName })
552
553	fmt.Fprintf(&b, "\nstatic const uint16_t kNIDsInShortNameOrder[] = {\n")
554	for _, nid := range nids {
555		// Including NID_undef in the table does not do anything. Whether OBJ_sn2nid
556		// finds the object or not, it will return NID_undef.
557		if nid != 0 {
558			fmt.Fprintf(&b, "%d /* %s */,\n", nid, objs.byNID[nid].shortName)
559		}
560	}
561	fmt.Fprintf(&b, "};\n")
562
563	// Emit a list of NIDs sorted by long name.
564	nids = nil
565	for nid, obj := range objs.byNID {
566		if len(obj.name) == 0 || len(obj.longName) == 0 {
567			continue
568		}
569		nids = append(nids, nid)
570	}
571	sortNIDs(nids, objs, func(a, b object) bool { return a.longName < b.longName })
572
573	fmt.Fprintf(&b, "\nstatic const uint16_t kNIDsInLongNameOrder[] = {\n")
574	for _, nid := range nids {
575		// Including NID_undef in the table does not do anything. Whether OBJ_ln2nid
576		// finds the object or not, it will return NID_undef.
577		if nid != 0 {
578			fmt.Fprintf(&b, "%d /* %s */,\n", nid, objs.byNID[nid].longName)
579		}
580	}
581	fmt.Fprintf(&b, "};\n")
582
583	// Emit a list of NIDs sorted by OID.
584	nids = nil
585	for nid, obj := range objs.byNID {
586		if len(obj.name) == 0 || len(obj.encoded) == 0 {
587			continue
588		}
589		nids = append(nids, nid)
590	}
591	sortNIDs(nids, objs, func(a, b object) bool {
592		// This comparison must match the definition of |obj_cmp|.
593		if len(a.encoded) < len(b.encoded) {
594			return true
595		}
596		if len(a.encoded) > len(b.encoded) {
597			return false
598		}
599		return bytes.Compare(a.encoded, b.encoded) < 0
600	})
601
602	fmt.Fprintf(&b, "\nstatic const uint16_t kNIDsInOIDOrder[] = {\n")
603	for _, nid := range nids {
604		obj := objs.byNID[nid]
605		fmt.Fprintf(&b, "%d /* ", nid)
606		for i, c := range obj.oid {
607			if i > 0 {
608				fmt.Fprintf(&b, ".")
609			}
610			fmt.Fprintf(&b, "%d", c)
611		}
612		fmt.Fprintf(&b, " (OBJ_%s) */,\n", obj.name)
613	}
614	fmt.Fprintf(&b, "};\n")
615
616	formatted, err := clangFormat(b.String())
617	if err != nil {
618		return err
619	}
620
621	return os.WriteFile(path, []byte(formatted), 0666)
622}
623
624func main() {
625	objs, err := readObjects("obj_mac.num", "objects.txt")
626	if err != nil {
627		fmt.Fprintf(os.Stderr, "Error reading objects: %s\n", err)
628		os.Exit(1)
629	}
630
631	if err := writeNumbers("obj_mac.num", objs); err != nil {
632		fmt.Fprintf(os.Stderr, "Error writing numbers: %s\n", err)
633		os.Exit(1)
634	}
635
636	if err := writeHeader("../../include/openssl/nid.h", objs); err != nil {
637		fmt.Fprintf(os.Stderr, "Error writing header: %s\n", err)
638		os.Exit(1)
639	}
640
641	if err := writeData("obj_dat.h", objs); err != nil {
642		fmt.Fprintf(os.Stderr, "Error writing data: %s\n", err)
643		os.Exit(1)
644	}
645}
646