asn1_builder_der.mx raw

   1  // Regression: the ASN.1 builder path of cryptobyte must emit real DER, byte for
   2  // byte.
   3  //
   4  // The vendored builder's AddASN1* helpers were the last capturing closures in
   5  // the tree (10 of them): each was `b.AddASN1(tag, func(c *Builder) {...})`, and
   6  // a Moxie function value carries no environment, so they were rewritten to the
   7  // callback-free BeginASN1/EndASN1 the tree already had. Nothing in the suite
   8  // called a single AddASN1* helper - x509 only READS with cryptobyte - so the
   9  // rewrite had no behavioural coverage at all. This is that coverage: every
  10  // helper is encoded and compared against the DER the encoding rules require.
  11  //
  12  // Fixed in: src/vendor/golang.org/x/crypto/cryptobyte/asn1.mx and builder.mx.
  13  package main
  14  
  15  import (
  16  	asn1enc "encoding/asn1"
  17  	"encoding/hex"
  18  	"golang.org/x/crypto/cryptobyte"
  19  	"golang.org/x/crypto/cryptobyte/asn1"
  20  	"math/big"
  21  	"time"
  22  )
  23  
  24  func check(name string, b *cryptobyte.Builder, want string) {
  25  	got, err := b.Bytes()
  26  	if err != nil {
  27  		panic(name | ": Bytes: " | err.Error())
  28  	}
  29  	hexed := hex.EncodeToString(got)
  30  	if hexed != want {
  31  		panic(name | ": got " | hexed | " want " | want)
  32  	}
  33  	println("ok " | name | " " | hexed)
  34  }
  35  
  36  func main() {
  37  	// Termination: a batch program must still be stoppable, so the entry
  38  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  39  	select {
  40  	case <-shutdown:
  41  		return
  42  	default:
  43  	}
  44  
  45  	// INTEGER: minimal two's-complement, padded when the top bit is set.
  46  	b := cryptobyte.NewBuilder(nil)
  47  	b.AddASN1Int64(0)
  48  	check("int64_0", b, "020100")
  49  
  50  	b = cryptobyte.NewBuilder(nil)
  51  	b.AddASN1Int64(-1)
  52  	check("int64_-1", b, "0201ff")
  53  
  54  	b = cryptobyte.NewBuilder(nil)
  55  	b.AddASN1Int64(128)
  56  	check("int64_128", b, "02020080")
  57  
  58  	b = cryptobyte.NewBuilder(nil)
  59  	b.AddASN1Int64WithTag(-129, asn1.INTEGER)
  60  	check("int64_-129", b, "0202ff7f")
  61  
  62  	b = cryptobyte.NewBuilder(nil)
  63  	b.AddASN1Uint64(65535)
  64  	check("uint64_65535", b, "020300ffff")
  65  
  66  	b = cryptobyte.NewBuilder(nil)
  67  	b.AddASN1Enum(1)
  68  	check("enum_1", b, "0a0101")
  69  
  70  	// BOOLEAN, OCTET STRING, BIT STRING.
  71  	b = cryptobyte.NewBuilder(nil)
  72  	b.AddASN1Boolean(true)
  73  	check("bool_true", b, "0101ff")
  74  
  75  	b = cryptobyte.NewBuilder(nil)
  76  	b.AddASN1Boolean(false)
  77  	check("bool_false", b, "010100")
  78  
  79  	b = cryptobyte.NewBuilder(nil)
  80  	b.AddASN1OctetString("hi")
  81  	check("octets", b, "04026869")
  82  
  83  	b = cryptobyte.NewBuilder(nil)
  84  	b.AddASN1BitString([]byte{0xab})
  85  	check("bitstring", b, "030200ab")
  86  
  87  	// OBJECT IDENTIFIER: 1.2.840.113549 -> 2a 86 48 86 f7 0d
  88  	b = cryptobyte.NewBuilder(nil)
  89  	b.AddASN1ObjectIdentifier(asn1enc.ObjectIdentifier{1, 2, 840, 113549})
  90  	check("oid", b, "06062a864886f70d")
  91  
  92  	// OBJECT IDENTIFIER is refused when it is not a valid one.
  93  	b = cryptobyte.NewBuilder(nil)
  94  	b.AddASN1ObjectIdentifier(asn1enc.ObjectIdentifier{3, 1})
  95  	if _, err := b.Bytes(); err == nil {
  96  		panic("invalid OID accepted")
  97  	}
  98  	println("ok oid_invalid refused")
  99  
 100  	// BIG INTEGER: negative, zero and a value whose top bit needs padding.
 101  	b = cryptobyte.NewBuilder(nil)
 102  	b.AddASN1BigInt(big.NewInt(-1))
 103  	check("bigint_-1", b, "0201ff")
 104  
 105  	b = cryptobyte.NewBuilder(nil)
 106  	b.AddASN1BigInt(big.NewInt(0))
 107  	check("bigint_0", b, "020100")
 108  
 109  	b = cryptobyte.NewBuilder(nil)
 110  	b.AddASN1BigInt(big.NewInt(128))
 111  	check("bigint_128", b, "02020080")
 112  
 113  	// Times.
 114  	utc := time.Date(2024, time.January, 2, 3, 4, 5, 0, time.UTC())
 115  	b = cryptobyte.NewBuilder(nil)
 116  	b.AddASN1UTCTime(utc)
 117  	check("utctime", b, "170d3234303130323033303430355a")
 118  
 119  	b = cryptobyte.NewBuilder(nil)
 120  	b.AddASN1GeneralizedTime(utc)
 121  	check("gentime", b, "180f32303234303130323033303430355a")
 122  
 123  	// Nesting: a SEQUENCE built through BeginSequence must wrap the contents.
 124  	b = cryptobyte.NewBuilder(nil)
 125  	b.BeginSequence()
 126  	b.AddASN1Int64(1)
 127  	b.AddASN1OctetString("ab")
 128  	b.EndSequence()
 129  	check("nested", b, "300702010104026162")
 130  
 131  	// AddASN1 itself is NOT exercised here. Handing a function value to a
 132  	// named func-typed parameter (BuilderContinuation) and calling it inside
 133  	// cryptobyte segfaults at runtime, with a plain named function as much as
 134  	// with a func literal - see the work log, round 140, "AddASN1's
 135  	// continuation SEGVs". The inline BeginASN1/EndASN1 path every in-tree
 136  	// caller now uses is what the checks above cover.
 137  
 138  	println("PASS: asn1 builder der")
 139  }
 140