| 1 | package csblob |
| 2 | |
| 3 | import ( |
| 4 | "crypto" |
| 5 | "crypto/hmac" |
| 6 | "crypto/x509/pkix" |
| 7 | "encoding/asn1" |
| 8 | "fmt" |
| 9 | |
| 10 | "howett.net/plist" |
| 11 | |
| 12 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 13 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 14 | ) |
| 15 | |
| 16 | func checkCDHashes(si *pkcs7.SignerInfo, computed map[crypto.Hash][]byte) error { |
| 17 | var cdHashes []cdHashAttrib |
| 18 | if err := si.AuthenticatedAttributes.GetAll(AttrCodeDirHashes, &cdHashes); err != nil { |
| 19 | if _, ok := err.(pkcs7.ErrNoAttribute); ok { |
| 20 | return nil |
| 21 | } |
| 22 | return err |
| 23 | } |
| 24 | for _, cd := range cdHashes { |
| 25 | hash, err := x509tools.PkixDigestToHashE(pkix.AlgorithmIdentifier{Algorithm: cd.Algorithm}) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | hc := computed[hash] |
| 30 | if hc == nil { |
| 31 | return fmt.Errorf("missing hash with algorithm %s", hash) |
| 32 | } |
| 33 | if !hmac.Equal(hc, cd.Digest) { |
| 34 | return fmt.Errorf("digest mismatch: expected %x, got %x", cd.Digest, hc) |
| 35 | } |
| 36 | } |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | func addCSHashes(builder *pkcs7.SignatureBuilder, hashes []cdHashAttrib) error { |
| 41 | for _, h := range hashes { |
| 42 | if err := builder.AddAuthenticatedAttribute(AttrCodeDirHashes, h); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | func checkPlistHashes(dirs []*CodeDirectory, si *pkcs7.SignerInfo, computed map[crypto.Hash][]byte) error { |
| 50 | var computedList [][]byte |
| 51 | for _, dir := range dirs { |
| 52 | computedList = append(computedList, computed[dir.HashFunc][:20]) |
| 53 | } |
| 54 | var plistText []byte |
| 55 | if err := si.AuthenticatedAttributes.GetOne(AttrCodeDirHashPlist, &plistText); err != nil { |
| 56 | if _, ok := err.(pkcs7.ErrNoAttribute); ok { |
| 57 | return nil |
| 58 | } |
| 59 | return err |
| 60 | } |
| 61 | var parsed cdHashPlist |
| 62 | if _, err := plist.Unmarshal(plistText, &parsed); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | if len(parsed.CDHashes) != len(computedList) { |
| 66 | return fmt.Errorf("expected %d hashes but got %d", len(parsed.CDHashes), len(computedList)) |
| 67 | } |
| 68 | for i, expected := range parsed.CDHashes { |
| 69 | actual := computedList[i] |
| 70 | if !hmac.Equal(expected, actual) { |
| 71 | return fmt.Errorf("digest mismatch: expected %x, got %x", expected, actual) |
| 72 | } |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func addPlistHashes(builder *pkcs7.SignatureBuilder, pl cdHashPlist) error { |
| 78 | blob, err := plist.MarshalIndent(pl, plist.XMLFormat, " ") |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | return builder.AddAuthenticatedAttribute(AttrCodeDirHashPlist, blob) |
| 83 | } |
| 84 | |
| 85 | type cdHashAttrib struct { |
| 86 | Algorithm asn1.ObjectIdentifier |
| 87 | Digest []byte |
| 88 | } |
| 89 | |
| 90 | type cdHashPlist struct { |
| 91 | CDHashes [][]byte `plist:"cdhashes"` |
| 92 | } |