MCPcopy Create free account
hub / github.com/Binject/universal / machoCodeSign

Function machoCodeSign

codesign_darwin.go:276–363  ·  view source on GitHub ↗

machoCodeSign - sign the macho file This code has been borrowed from https://github.com/golang/go/blob/master/src/cmd/internal/codesign/codesign.go

(fname string)

Source from the content-addressed store, hash-verified

274// machoCodeSign - sign the macho file
275// This code has been borrowed from https://github.com/golang/go/blob/master/src/cmd/internal/codesign/codesign.go
276func machoCodeSign(fname string) error {
277 f, err := os.OpenFile(fname, os.O_RDWR, 0)
278 if err != nil {
279 return err
280 }
281 defer f.Close()
282
283 mf, err := macho.NewFile(f)
284 if err != nil {
285 return err
286 }
287 if mf.Magic != macho.Magic64 {
288 return fmt.Errorf("not 64-bit Mach-O file: %s", fname)
289 }
290
291 // Find existing LC_CODE_SIGNATURE and __LINKEDIT segment
292 var sigOff, sigSz, csCmdOff, linkeditOff int64
293 var linkeditSeg, textSeg *macho.Segment
294 loadOff := int64(machoHeaderSize64)
295 get32 := mf.ByteOrder.Uint32
296 for _, l := range mf.Loads {
297 data := l.Raw()
298 cmd, sz := get32(data), get32(data[4:])
299 if cmd == LC_CODE_SIGNATURE {
300 sigOff = int64(get32(data[8:]))
301 sigSz = int64(get32(data[12:]))
302 csCmdOff = loadOff
303 }
304 if seg, ok := l.(*macho.Segment); ok {
305 switch seg.Name {
306 case "__LINKEDIT":
307 linkeditSeg = seg
308 linkeditOff = loadOff
309 case "__TEXT":
310 textSeg = seg
311 }
312 }
313 loadOff += int64(sz)
314 }
315
316 if sigOff == 0 {
317 // The C linker doesn't generate a signed binary, for some reason.
318 // Skip.
319 return nil
320 }
321
322 fi, err := f.Stat()
323 if err != nil {
324 return err
325 }
326 if sigOff+sigSz != fi.Size() {
327 // We don't expect anything after the signature (this will invalidate
328 // the signature anyway.)
329 return fmt.Errorf("unexpected content after code signature")
330 }
331
332 sz := Size(sigOff, "a.out")
333 if sz != sigSz {

Callers 1

LoadLibraryDlopenFunction · 0.85

Calls 2

SizeFunction · 0.85
SignFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…