| 1 | package csblob |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/x509" |
| 6 | "encoding/asn1" |
| 7 | "encoding/binary" |
| 8 | ) |
| 9 | |
| 10 | type reqBuilder struct { |
| 11 | out bytes.Buffer |
| 12 | err error |
| 13 | } |
| 14 | |
| 15 | func DefaultRequirement(identifier string, certs []*x509.Certificate) ([]byte, error) { |
| 16 | leaf := certs[0] |
| 17 | b := new(reqBuilder) |
| 18 | b.putUint32(1) |
| 19 | items := []func(){ |
| 20 | b.identifier(identifier), |
| 21 | b.anchorAppleGeneric(), |
| 22 | b.certFieldEqual(0, "subject.CN", leaf.Subject.CommonName), |
| 23 | } |
| 24 | // find intermediate cert |
| 25 | for _, cert := range certs[1:] { |
| 26 | if !bytes.Equal(cert.RawSubject, leaf.RawIssuer) { |
| 27 | continue |
| 28 | } |
| 29 | // look for endorsement for specific signature role |
| 30 | for _, ext := range cert.Extensions { |
| 31 | if hasPrefix(ext.Id, Intermediate) { |
| 32 | items = append(items, b.certExtensionExists(1, ext.Id)) |
| 33 | break |
| 34 | } |
| 35 | } |
| 36 | break |
| 37 | } |
| 38 | // build requirement from criteria |
| 39 | b.and(items...)() |
| 40 | if b.err != nil { |
| 41 | return nil, b.err |
| 42 | } |
| 43 | i := newSuperItem(csRequirement, b.out.Bytes()) |
| 44 | i.itype = uint32(DesignatedRequirement) |
| 45 | return marshalSuperBlob(csRequirements, []superItem{i}), nil |
| 46 | } |
| 47 | |
| 48 | func (b *reqBuilder) putUint32(v uint32) { |
| 49 | var d [4]byte |
| 50 | binary.BigEndian.PutUint32(d[:], v) |
| 51 | b.out.Write(d[:]) |
| 52 | } |
| 53 | |
| 54 | func (b *reqBuilder) putData(v []byte) { |
| 55 | b.putUint32(uint32(len(v))) |
| 56 | b.out.Write(v) |
| 57 | n := len(v) |
| 58 | for n%4 != 0 { |
| 59 | b.out.WriteByte(0) |
| 60 | n++ |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (b *reqBuilder) putOID(oid asn1.ObjectIdentifier) { |
| 65 | // pack first two digits together |
| 66 | packed := append(asn1.ObjectIdentifier{oid[0]*40 + oid[1]}, oid[2:]...) |
| 67 | var out []byte |
| 68 | for _, v := range packed { |
| 69 | if v < 0x80 { |
| 70 | // simple case |
| 71 | out = append(out, byte(v)) |
| 72 | continue |
| 73 | } |
| 74 | // build starting from least-significant word |
| 75 | var outv []byte |
| 76 | for { |
| 77 | outv = append(outv, byte(v&0x7f)) |
| 78 | if v >= 0x80 { |
| 79 | v >>= 7 |
| 80 | } else { |
| 81 | break |
| 82 | } |
| 83 | } |
| 84 | // reverse and set MSB on all but the last word |
| 85 | for i := len(outv) - 1; i >= 0; i-- { |
| 86 | vv := outv[i] |
| 87 | if i != 0 { |
| 88 | vv |= 0x80 |
| 89 | } |
| 90 | out = append(out, vv) |
| 91 | } |
| 92 | } |
| 93 | b.putData(out) |
| 94 | } |
| 95 | |
| 96 | func (b *reqBuilder) and(items ...func()) func() { |
| 97 | return func() { |
| 98 | for len(items) > 1 { |
| 99 | b.putUint32(opAnd) |
| 100 | items[0]() |
| 101 | items = items[1:] |
| 102 | } |
| 103 | items[0]() |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func (b *reqBuilder) identifier(v string) func() { |
| 108 | return func() { |
| 109 | b.putUint32(opIdent) |
| 110 | b.putData([]byte(v)) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func (b *reqBuilder) anchorAppleGeneric() func() { |
| 115 | return func() { |
| 116 | b.putUint32(opAppleGenericAnchor) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (b *reqBuilder) certFieldEqual(slot int32, field, value string) func() { |
| 121 | return func() { |
| 122 | b.putUint32(opCertField) |
| 123 | b.putUint32(uint32(slot)) |
| 124 | b.putData([]byte(field)) |
| 125 | b.putUint32(uint32(matchEqual)) |
| 126 | b.putData([]byte(value)) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (b *reqBuilder) certExtensionExists(slot int32, oid asn1.ObjectIdentifier) func() { |
| 131 | return func() { |
| 132 | b.putUint32(opCertGeneric) |
| 133 | b.putUint32(uint32(slot)) |
| 134 | b.putOID(oid) |
| 135 | b.putUint32(uint32(matchExists)) |
| 136 | } |
| 137 | } |