| 1 | package dmg |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto" |
| 7 | "encoding/binary" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | |
| 12 | "github.com/sassoftware/relic/v7/lib/binpatch" |
| 13 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 14 | "github.com/sassoftware/relic/v7/lib/fruit/csblob" |
| 15 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 16 | ) |
| 17 | |
| 18 | type SignatureParams struct { |
| 19 | HashFunc crypto.Hash |
| 20 | Requirements []byte // requirements to embed in signature |
| 21 | SigningIdentity string |
| 22 | TeamIdentifier string |
| 23 | } |
| 24 | |
| 25 | func Sign(ctx context.Context, rsfBytes []byte, r io.Reader, cert *certloader.Certificate, params *SignatureParams) (*binpatch.PatchSet, *pkcs9.TimestampedSignature, error) { |
| 26 | // parse UDIF header |
| 27 | var rsf udifResourceFile |
| 28 | if err := binary.Read(bytes.NewReader(rsfBytes), binary.BigEndian, &rsf); err != nil { |
| 29 | return nil, nil, fmt.Errorf("udif header: %w", err) |
| 30 | } |
| 31 | nr := &counter{r: r} |
| 32 | bundleSize := rsf.XMLOffset + rsf.XMLLength |
| 33 | oldOffset, oldLength := rsf.SignatureOffset, rsf.SignatureLength |
| 34 | rsf.SignatureOffset = bundleSize |
| 35 | blobParams := &csblob.SignatureParams{ |
| 36 | HashFunc: params.HashFunc, |
| 37 | Requirements: params.Requirements, |
| 38 | SigningIdentity: params.SigningIdentity, |
| 39 | TeamIdentifier: params.TeamIdentifier, |
| 40 | Pages: io.LimitReader(nr, bundleSize), |
| 41 | RepSpecific: rsf.ForHashing(), |
| 42 | } |
| 43 | if oldOffset != 0 { |
| 44 | // provide old signature to copy requirements and flags |
| 45 | if oldOffset != bundleSize { |
| 46 | return nil, nil, errors.New("overlap or gap between bundle and signature") |
| 47 | } |
| 48 | blobParams.OldSignature = io.LimitReader(nr, oldLength) |
| 49 | } |
| 50 | blob, tsig, err := csblob.Sign(ctx, cert, blobParams) |
| 51 | if err != nil { |
| 52 | return nil, nil, err |
| 53 | } |
| 54 | // drain rest of input file |
| 55 | if _, err := io.Copy(io.Discard, nr); err != nil { |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | oldSize := nr.n |
| 59 | // generate patch |
| 60 | rsf.SignatureLength = int64(len(blob)) |
| 61 | var b bytes.Buffer |
| 62 | _, _ = b.Write(blob) |
| 63 | _ = binary.Write(&b, binary.BigEndian, rsf) |
| 64 | patch := binpatch.New() |
| 65 | patch.Add(rsf.SignatureOffset, oldSize-rsf.SignatureOffset, b.Bytes()) |
| 66 | // copy values back to params |
| 67 | params.Requirements = blobParams.Requirements |
| 68 | params.SigningIdentity = blobParams.SigningIdentity |
| 69 | params.TeamIdentifier = blobParams.TeamIdentifier |
| 70 | return patch, tsig, nil |
| 71 | } |
| 72 | |
| 73 | type counter struct { |
| 74 | r io.Reader |
| 75 | n int64 |
| 76 | } |
| 77 | |
| 78 | func (c *counter) Read(d []byte) (n int, err error) { |
| 79 | n, err = c.r.Read(d) |
| 80 | if n > 0 { |
| 81 | c.n += int64(n) |
| 82 | } |
| 83 | return |
| 84 | } |