Skip to content

Commit

Permalink
chore: refactor codesign and trie into pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Jul 11, 2020
1 parent 74a705c commit f7c3a70
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 105 deletions.
8 changes: 4 additions & 4 deletions cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"
"unsafe"

ctypes "github.com/blacktop/go-macho/pkg/codesign/types"
"github.com/blacktop/go-macho/pkg/trie"
"github.com/blacktop/go-macho/types"
"github.com/blacktop/go-macho/types/codesign"
"github.com/blacktop/go-macho/types/trie"
)

// A Load represents any Mach-O load command.
Expand Down Expand Up @@ -628,8 +628,8 @@ type CodeSignature struct {
Size uint32
ID string
TeamID string
CodeDirectory codesign.CodeDirectory
Requirements []codesign.Requirement
CodeDirectory ctypes.CodeDirectory
Requirements []ctypes.Requirement
CMSSignature []byte
Entitlements string
}
Expand Down
5 changes: 3 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"strings"
"unsafe"

"github.com/blacktop/go-macho/pkg/codesign"
"github.com/blacktop/go-macho/pkg/trie"
"github.com/blacktop/go-macho/types"
"github.com/blacktop/go-macho/types/trie"
)

const (
Expand Down Expand Up @@ -647,7 +648,7 @@ func NewFile(r io.ReaderAt, loads ...types.LoadCmd) (*File, error) {
if _, err := r.ReadAt(csdat, int64(hdr.Offset)); err != nil {
return nil, err
}
cs, err := ParseCodeSignature(csdat)
cs, err := codesign.ParseCodeSignature(csdat)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/blacktop/go-macho/types"
)

// TODO refactor into a pkg
func (f *File) HasObjC() bool {
for _, s := range f.Segments() {
if strings.HasPrefix(s.Name, "__DATA") {
Expand Down
80 changes: 36 additions & 44 deletions codesign.go → pkg/codesign/codesign.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package macho
package codesign

import (
"bufio"
Expand All @@ -8,21 +8,21 @@ import (
"io"
"strings"

"github.com/blacktop/go-macho/types/codesign"
"github.com/blacktop/go-macho/pkg/codesign/types"
)

// ParseCodeSignature parses the LC_CODE_SIGNATURE data
func ParseCodeSignature(cmddat []byte) (*CodeSignature, error) {
func ParseCodeSignature(cmddat []byte) (*types.CodeSignature, error) {
r := bytes.NewReader(cmddat)

cs := &CodeSignature{}
cs := &types.CodeSignature{}

csBlob := codesign.SuperBlob{}
csBlob := types.SuperBlob{}
if err := binary.Read(r, binary.BigEndian, &csBlob); err != nil {
return nil, err
}

csIndex := make([]codesign.BlobIndex, csBlob.Count)
csIndex := make([]types.BlobIndex, csBlob.Count)
if err := binary.Read(r, binary.BigEndian, &csIndex); err != nil {
return nil, err
}
Expand All @@ -32,33 +32,33 @@ func ParseCodeSignature(cmddat []byte) (*CodeSignature, error) {
r.Seek(int64(index.Offset), io.SeekStart)

switch index.Type {
case codesign.CSSLOT_CODEDIRECTORY:
case types.CSSLOT_CODEDIRECTORY:
fallthrough
case codesign.CSSLOT_ALTERNATE_CODEDIRECTORIES:
case types.CSSLOT_ALTERNATE_CODEDIRECTORIES:
if err := binary.Read(r, binary.BigEndian, &cs.CodeDirectory); err != nil {
return nil, err
}
// TODO parse all the cdhashs
switch cs.CodeDirectory.Version {
case codesign.SUPPORTS_SCATTER:
case types.SUPPORTS_SCATTER:
if cs.CodeDirectory.ScatterOffset > 0 {
r.Seek(int64(index.Offset+cs.CodeDirectory.ScatterOffset), io.SeekStart)
scatter := codesign.Scatter{}
scatter := types.Scatter{}
if err := binary.Read(r, binary.BigEndian, &scatter); err != nil {
return nil, err
}
fmt.Printf("%#v\n", scatter)
}
case codesign.SUPPORTS_TEAMID:
case types.SUPPORTS_TEAMID:
r.Seek(int64(index.Offset+cs.CodeDirectory.TeamOffset), io.SeekStart)
teamID, err := bufio.NewReader(r).ReadString('\x00')
if err != nil {
return nil, fmt.Errorf("failed to read SUPPORTS_TEAMID at: %d: %v", index.Offset+cs.CodeDirectory.TeamOffset, err)
}
cs.TeamID = strings.Trim(teamID, "\x00")
case codesign.SUPPORTS_CODELIMIT64:
case types.SUPPORTS_CODELIMIT64:
// TODO 🤷‍♂️
case codesign.SUPPORTS_EXECSEG:
case types.SUPPORTS_EXECSEG:
// TODO 🤷‍♂️
default:
fmt.Printf("Unknown code directory version 0x%x, please notify author\n", cs.CodeDirectory.Version)
Expand All @@ -69,55 +69,47 @@ func ParseCodeSignature(cmddat []byte) (*CodeSignature, error) {
return nil, fmt.Errorf("failed to read CodeDirectory ID at: %d: %v", index.Offset+cs.CodeDirectory.IdentOffset, err)
}
cs.ID = id
case codesign.CSSLOT_REQUIREMENTS:
var err error
req := codesign.Requirement{}
csReqBlob := codesign.RequirementsBlob{}
if err := binary.Read(r, binary.BigEndian, &csReqBlob); err != nil {
case types.CSSLOT_REQUIREMENTS:
// TODO find out if there can be more than one requirement(s)
req := types.Requirement{}
if err := binary.Read(r, binary.BigEndian, &req.RequirementsBlob); err != nil {
return nil, err
}
req.RequirementsBlob = csReqBlob
reqData := make([]byte, int(csReqBlob.Length)-binary.Size(codesign.RequirementsBlob{}))
if err := binary.Read(r, binary.BigEndian, &reqData); err != nil {
return nil, err
}
rqr := bytes.NewReader(reqData)
var reqs codesign.Requirements
if rqr.Len() >= binary.Size(reqs) {
if err := binary.Read(rqr, binary.BigEndian, &reqs); err != nil {
datLen := int(req.RequirementsBlob.Length) - binary.Size(types.RequirementsBlob{})
if datLen > 0 {
reqData := make([]byte, datLen)
if err := binary.Read(r, binary.BigEndian, &reqData); err != nil {
return nil, err
}
req.Requirements = reqs
} else {
var reqType uint32
if err := binary.Read(rqr, binary.BigEndian, &reqType); err != nil {
// return nil, err
fmt.Printf("Got weird cs.Requirements: %#v\n", cs.Requirements)
rqr := bytes.NewReader(reqData)
if err := binary.Read(rqr, binary.BigEndian, &req.Requirements); err != nil {
return nil, err
}
req.Requirements.Type = codesign.RequirementType(reqType)
detail, err := types.ParseRequirements(rqr, req.Requirements)
if err != nil {
return nil, err
}
req.Detail = detail
} else {
req.Detail = "empty requirement set"
}
req.Detail, err = codesign.ParseRequirements(rqr, reqs)
if err != nil {
return nil, err
}
cs.Requirements = append(cs.Requirements, req)
case codesign.CSSLOT_ENTITLEMENTS:
entBlob := codesign.Blob{}
case types.CSSLOT_ENTITLEMENTS:
entBlob := types.Blob{}
if err := binary.Read(r, binary.BigEndian, &entBlob); err != nil {
return nil, err
}
plistData := make([]byte, entBlob.Length-8)
plistData := make([]byte, int(entBlob.Length)-binary.Size(entBlob))
if err := binary.Read(r, binary.BigEndian, &plistData); err != nil {
return nil, err
}
cs.Entitlements = string(plistData)
case codesign.CSSLOT_CMS_SIGNATURE:
cmsBlob := codesign.Blob{}
case types.CSSLOT_CMS_SIGNATURE:
cmsBlob := types.Blob{}
if err := binary.Read(r, binary.BigEndian, &cmsBlob); err != nil {
return nil, err
}
cmsData := make([]byte, cmsBlob.Length)
cmsData := make([]byte, int(cmsBlob.Length)-binary.Size(cmsBlob))
if err := binary.Read(r, binary.BigEndian, &cmsData); err != nil {
return nil, err
}
Expand Down
30 changes: 16 additions & 14 deletions types/codesign/directory.go → pkg/codesign/types/directory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codesign
package types

import "github.com/blacktop/go-macho/types"
import (
mtypes "github.com/blacktop/go-macho/types"
)

type hashType uint8

Expand All @@ -22,7 +24,7 @@ const (
HASH_MAX_SIZE = 48 /* max size of the hash we'll support */
)

var csHashTypeStrings = []types.IntName{
var csHashTypeStrings = []mtypes.IntName{
{uint32(HASHTYPE_NOHASH), "No Hash"},
{uint32(HASHTYPE_SHA1), "Sha1"},
{uint32(HASHTYPE_SHA256), "Sha256"},
Expand All @@ -31,8 +33,8 @@ var csHashTypeStrings = []types.IntName{
{uint32(HASHTYPE_SHA512), "Sha512"},
}

func (c hashType) String() string { return types.StringName(uint32(c), csHashTypeStrings, false) }
func (c hashType) GoString() string { return types.StringName(uint32(c), csHashTypeStrings, true) }
func (c hashType) String() string { return mtypes.StringName(uint32(c), csHashTypeStrings, false) }
func (c hashType) GoString() string { return mtypes.StringName(uint32(c), csHashTypeStrings, true) }

type cdVersion uint32

Expand All @@ -45,7 +47,7 @@ const (
SUPPORTS_LINKAGE cdVersion = 0x20600
)

var csVersionypeStrings = []types.IntName{
var csVersionypeStrings = []mtypes.IntName{
{uint32(SUPPORTS_SCATTER), "Scatter"},
{uint32(SUPPORTS_TEAMID), "TeamID"},
{uint32(SUPPORTS_CODELIMIT64), "Codelimit64"},
Expand All @@ -55,10 +57,10 @@ var csVersionypeStrings = []types.IntName{
}

func (v cdVersion) String() string {
return types.StringName(uint32(v), csVersionypeStrings, false)
return mtypes.StringName(uint32(v), csVersionypeStrings, false)
}
func (v cdVersion) GoString() string {
return types.StringName(uint32(v), csVersionypeStrings, true)
return mtypes.StringName(uint32(v), csVersionypeStrings, true)
}

type cdFlag uint32
Expand Down Expand Up @@ -106,7 +108,7 @@ const (
ENTITLEMENT_FLAGS cdFlag = (GET_TASK_ALLOW | INSTALLER | DATAVAULT_CONTROLLER | NVRAM_UNRESTRICTED)
)

var cdFlagStrings = []types.IntName{
var cdFlagStrings = []mtypes.IntName{
{uint32(NONE), "None"},
{uint32(VALID), "Valid"},
{uint32(ADHOC), "Adhoc"},
Expand Down Expand Up @@ -140,10 +142,10 @@ var cdFlagStrings = []types.IntName{
}

func (f cdFlag) String() string {
return types.StringName(uint32(f), cdFlagStrings, false)
return mtypes.StringName(uint32(f), cdFlagStrings, false)
}
func (f cdFlag) GoString() string {
return types.StringName(uint32(f), cdFlagStrings, true)
return mtypes.StringName(uint32(f), cdFlagStrings, true)
}

// C form of a CodeDirectory.
Expand Down Expand Up @@ -208,7 +210,7 @@ const (
EXECSEG_CAN_EXEC_CDHASH execSegFlag = 0x200 /* can execute blessed cdhash */
)

var execSegFlagStrings = []types.Int64Name{
var execSegFlagStrings = []mtypes.Int64Name{
{uint64(EXECSEG_MAIN_BINARY), "Main Binary"},
{uint64(EXECSEG_ALLOW_UNSIGNED), "Allow Unsigned"},
{uint64(EXECSEG_DEBUGGER), "Debugger"},
Expand All @@ -219,8 +221,8 @@ var execSegFlagStrings = []types.Int64Name{
}

func (f execSegFlag) String() string {
return types.StringName64(uint64(f), execSegFlagStrings, false)
return mtypes.StringName64(uint64(f), execSegFlagStrings, false)
}
func (f execSegFlag) GoString() string {
return types.StringName64(uint64(f), execSegFlagStrings, true)
return mtypes.StringName64(uint64(f), execSegFlagStrings, true)
}
Loading

0 comments on commit f7c3a70

Please sign in to comment.