美文网首页
如何使用命令行签名

如何使用命令行签名

作者: _我和你一样 | 来源:发表于2019-01-01 17:46 被阅读0次

一般而言,作为一个iOS或者MacOS的开发人员,目前已经不用怎么操心签名的事了,因为现在的Xcode版本已经集成了自动管理签名的功能。

但是有些时候,当出现签名问题的时候,我们还是需要能分析出来具体签名哪里出现问题了。也就是说,我们可以不必管签名的事,但是有问题的时候我们应当有解决问题的能力。那么如何查看签名信息,又如何使用命令行进行签名呢?

对于签名的管理,我们使用的命名是codesign

The codesign command is used to create, check, and display code signatures, as well as inquire into the dynamic status of signed code in the system.

codesign就是创建和管理证书的。下面列举一些基本的操作使用。

查看签名

比如我们看一下xcode的签名,我们使用 -d -v 参数,

-d 是display展示签名信息的意思,-v 是verbose的意思,越多的verbose显示信息越多,通常3个就已经足够了。

codesign -d -vvvv /Applications/Xcode.app
Executable=/Applications/Xcode.app/Contents/MacOS/Xcode
Identifier=com.apple.dt.Xcode
Format=app bundle with Mach-O thin (x86_64)
CodeDirectory v=20200 size=434 flags=0x2200(kill,library-validation) hashes=6+5 location=embedded
VersionPlatform=1
VersionMin=658688
VersionSDK=658944
Hash type=sha256 size=32
CandidateCDHash sha256=d60c658e651113198d9296d88528c5b1ee8e3031
Hash choices=sha256
Page size=4096
CDHash=d60c658e651113198d9296d88528c5b1ee8e3031
Signature size=4555
Authority=Apple Mac OS Application Signing
Authority=Apple Worldwide Developer Relations Certification Authority
Authority=Apple Root CA
Info.plist entries=39
TeamIdentifier=59GAB85EFG
Sealed Resources version=2 rules=13 files=253327
Internal requirements count=1 size=108

上面的签名中没有显示一些额外的信息,比如授权机制 entitements信息 。那么如何显示证书中的其他信息呢,在查看时使用 --entitlements 选项可以查看,比如:

codesign -d --entitlements - /Applications/Xcode.app 
Executable=/Applications/Xcode.app/Contents/MacOS/Xcode
??qq?<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.authkit.client.private</key>
    <true/>
    <key>com.apple.dt.simulator.client</key>
    <true/>
    <key>com.apple.private.tcc.allow</key>
    <array>
        <string>kTCCServiceAppleEvents</string>
    </array>
    <key>com.apple.private.coreservices.definesExtensionPoint</key>
    <true/>
    <key>com.apple.sysmond.client</key>
    <true/>
    <key>com.apple.developer.aps-environment</key>
    <string>production</string>
    <key>com.apple.PairingManager.Write</key>
    <true/>
    <key>com.apple.application-identifier</key>
    <string>59GAB85EFG.com.apple.dt.Xcode</string>
    <key>com.apple.developer.maps</key>
    <true/>
    <key>com.apple.PairingManager.Read</key>
    <true/>
    <key>com.apple.PairingManager.RemovePeer</key>
    <true/>
</dict>
</plist>                      

签名

签名使用codesign -s 命令,s就是签名sign的意思

签名需要一个证书,我们可以打开 钥匙串应用 查看我们拥有的证书,或者通过命令行使用security命令访问钥匙串,查找可以签名的证书,

security find-identity -v -p codesigning
  1) EB88A7CC8131ECABD0288D9E21F3BD2D9B22CA66 "iPhone Developer: 家树 黄 (F6MJ39N69F)"
     1 valid identities found

我的钥匙串中就有这么一个证书:"iPhone Developer: 家树 黄 (F6MJ39N69F)"

为了方便起见,我就创建一个my.app文件。创建文件可以使用 touch 命令

touch my.app

我们看下没有签名的样子

codesign -d -vvvv my.app
my.app: code object is not signed at all

然后签名的时候,指定此证书进行签名就可以了

codesign -s "iPhone Developer: 家树 黄 (F6MJ39N69F)" my.app

签名之后没有输出,说明签名正确。如果有错误,会输出错误的。

如果想要重新签名 那么需要加上-f参数,-f的意思是force的意思,如果没有这个参数,签名不会不替换,签名操作会失败。

codesign -f -s "iPhone Developer: 家树 黄 (F6MJ39N69F)" my.app 
my.app: replacing existing signature

修改签名参数

  • 修改Identifier

上述查看xcode的签名,我们看到苹果的Identifier是com.apple.dt.Xcode,我们签名时,也是可以指定标识符的。那么我们需要使用-i参数,

我们先看一下刚才的签名信息:

codesign -vvvv -d my.app 
Executable=/private/tmp/my.app
Identifier=my
Format=generic
CodeDirectory v=20200 size=130 flags=0x0(none) hashes=0+2 location=embedded
Hash type=sha256 size=32
CandidateCDHash sha1=76a18f48dafd3f1f4b8d7284ae0a84c1f7bc594f
CandidateCDHash sha256=c79a6b24c624b76d0c459ca7217fdf8507ca0714
Hash choices=sha1,sha256
Page size=none
CDHash=c79a6b24c624b76d0c459ca7217fdf8507ca0714
Signature size=4793
Authority=iPhone Developer: 家树 黄 (F6MJ39N69F)
Authority=Apple Worldwide Developer Relations Certification Authority
Authority=Apple Root CA
Signed Time=Jan 1, 2019 at 5:31:48 PM
Info.plist=not bound
TeamIdentifier=85GDEX4F59
Sealed Resources=none
Internal requirements count=1 size=164

修改Identifier参数

codesign -f -i com.hjs.my -s "iPhone Developer: 家树 黄 (F6MJ39N69F)" my.app
my.app: replacing existing signature

查看一下,标示符已经变更了

codesign --verbose=4 -d my.app 
Executable=/private/tmp/my.app
Identifier=com.hjs.my
Format=generic
CodeDirectory v=20200 size=138 flags=0x0(none) hashes=0+2 location=embedded
Hash type=sha256 size=32
CandidateCDHash sha1=96f563024ef9fed0e92efbd801308740b0eb7e36
CandidateCDHash sha256=d6beb44d3b54c234fcaeaca5337ebc590d472183
Hash choices=sha1,sha256
Page size=none
CDHash=d6beb44d3b54c234fcaeaca5337ebc590d472183
Signature size=4793
Authority=iPhone Developer: 家树 黄 (F6MJ39N69F)
Authority=Apple Worldwide Developer Relations Certification Authority
Authority=Apple Root CA
Signed Time=Jan 1, 2019 at 5:35:34 PM
Info.plist=not bound
TeamIdentifier=85GDEX4F59
Sealed Resources=none
Internal requirements count=1 size=172
  • 修改flag

    我们看到我们的flags=0x0(none),Xcode的flag是flags=0x2200

    那么如何修改flag呢?使用-o参数

    codesign -f -o 0x2200 -s  "iPhone Developer: 家树 黄 (F6MJ39N69F)" my.app
    my.app: replacing existing signature
    

    查看flag

    codesign -vvvv -d my.app 
    Executable=/private/tmp/my.app
    Identifier=my
    Format=generic
    CodeDirectory v=20200 size=130 flags=0x2200(kill,library-validation) hashes=0+2 location=embedded
    Hash type=sha256 size=32
    CandidateCDHash sha1=2fb37cbcdd9d95939ca5de6608b506f3adc37403
    CandidateCDHash sha256=f001e1c70f67c2ee7cdf4acaa3854e466150be3d
    Hash choices=sha1,sha256
    Page size=none
    CDHash=f001e1c70f67c2ee7cdf4acaa3854e466150be3d
    Signature size=4793
    Authority=iPhone Developer: 家树 黄 (F6MJ39N69F)
    Authority=Apple Worldwide Developer Relations Certification Authority
    Authority=Apple Root CA
    Signed Time=Jan 1, 2019 at 5:40:21 PM
    Info.plist=not bound
    TeamIdentifier=85GDEX4F59
    Sealed Resources=none
    Internal requirements count=1 size=164
    
  • 修改entitlements

签名时使用--entitlements参数即可。此处没有准备entitlements文件,不做示范了。使用方法参考上述例子。

相关文章

网友评论

      本文标题:如何使用命令行签名

      本文链接:https://www.haomeiwen.com/subject/rfznlqtx.html