Command /usr/bin/codesign failed with exit code 1

When signing an ios app via ANT cmd

<exec executable="/usr/bin/open">
<arg value="${env.WORKSPACE}/provisioning_profiles/${provisioning_profile}.mobileprovision"/>
</exec>
<echo>
Sleep 10 secs
</echo>
<exec executable="/bin/sleep">
<arg value="10"/>
</exec>
<echo>
/usr/bin/osascript -e tell application "Xcode" to quit
</echo>
<exec executable="/usr/bin/osascript">
<arg value="-e"/>
<arg value='tell application "Xcode" to quit'/>
</exec>
<echo>
Sleep 10 secs
</echo>
<exec executable="/bin/sleep">
<arg value="10"/>
</exec>
<echo>
/usr/bin/security unlock-keychain -p password ${keychain}
</echo>
<exec executable="/usr/bin/security" failonerror="true">
<arg value="unlock-keychain"/>
<arg value="-p"/>
<arg value="password"/>
<arg value="${keychain}"/>
</exec>
<echo>iPhone-build starting</echo>
<echo>xcodebuild clean -project MyCompany.xcodeproj -target Aggregate -configuration ${buildTarget} CODE_SIGN_IDENTITY=${codeSignIdentity} OTHER_CODE_SIGN_FLAGS="--keychain ${keychain}" PROVISIONING_PROFILE=${mopro_key}</echo>
<exec executable="xcodebuild" failonerror="true">
<arg value="clean"/>
<arg value="-project"/>
<arg value="MyCompany.xcodeproj"/>
<arg value="-target"/>
<arg value="Aggregate"/>
<arg value="-configuration"/>
<arg value="${buildTarget}"/>
<arg value="CODE_SIGN_IDENTITY=${codeSignIdentity}"/>
<arg line="OTHER_CODE_SIGN_FLAGS=&quot;--keychain ${keychain}&quot;"/>
<arg value="PROVISIONING_PROFILE=${mopro_key}"/>
</exec>
<echo>xcodebuild -project MyCompany.xcodeproj -target Aggregate -configuration ${buildTarget} CODE_SIGN_IDENTITY=${codeSignIdentity} OTHER_CODE_SIGN_FLAGS="--keychain ${keychain}" PROVISIONING_PROFILE=${mopro_key} GCC_PREPROCESSOR_DEFINITIONS='${preprocdefs}'</echo>
<echo>
SVN_INFO_URL = ${svn.info.url}
SVN_INFO_REV ="${svn.info.rev}
</echo>
<exec executable="xcodebuild" failonerror="true">
<arg value="-project"/>
<arg value="MyCompany.xcodeproj"/>
<arg value="-target"/>
<arg value="Aggregate"/>
<arg value="-configuration"/>
<arg value="${buildTarget}"/>
<arg value="CODE_SIGN_IDENTITY=${codeSignIdentity}"/>
<arg line="OTHER_CODE_SIGN_FLAGS=&quot;--keychain ${keychain}&quot;"/>
<arg value="PROVISIONING_PROFILE=${mopro_key}"/>
<arg value="GCC_PREPROCESSOR_DEFINITIONS='${preprocdefs}'"/>
</exec>

I see the error:

     [exec] CodeSign build/AdHoc-iphoneos/myapp.app
     [exec]     cd /opt/jenkins/workspace/iOS_build_3.4/client/iOS
     [exec]     setenv CODESIGN_ALLOCATE /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
     [exec]     setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin"
     [exec]     /usr/bin/codesign --force --sign "iPhone Distribution: My Company" --resource-rules=/opt/jenkins/workspace/iOS_build_3.4/client/iOS/build/AdHoc-iphoneos/myapp.app/ResourceRules.plist --keychain /Users/cmuser/Library/Keychains/login.keychain --entitlements /opt/jenkins/workspace/iOS_build_3.4/client/iOS/build/company.build/AdHoc-iphoneos/iPhoneClient.build/myapp.xcent /opt/jenkins/workspace/iOS_build_3.4/client/iOS/build/AdHoc-iphoneos/myapp.app
     [exec] /opt/jenkins/workspace/iOS_build_3.4/client/iOS/build/AdHoc-iphoneos/myapp.app: User interaction is not allowed.
     [exec] Command /usr/bin/codesign failed with exit code 1
     [exec] 
     [exec] ** BUILD FAILED **
     [exec] 
     [exec] 
     [exec] The following build commands failed:
     [exec] 	CodeSign build/AdHoc-iphoneos/myapp.app
     [exec] (1 failure)
     [exec] 

BUILD FAILED
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:136: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:141: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:147: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:157: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:283: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/clientBuildPackager/build_osx_CM.xml:518: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/client/iOS/build_CM.xml:580: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/client/iOS/build_CM.xml:523: The following error occurred while executing this line:
/opt/jenkins/workspace/iOS_build_3.4/client/iOS/build_CM.xml:397: exec returned: 65

Note I am already unlocking the keychain. Solution: The Key used by ${codeSignIdentity} “iPhone Distribution: My Company” Certificate does not have Access Control granted to codesign

  1. Open your keychain in Keychain Access
  2. Select Certificates category
  3. Maximize so you can see the Key under the Cert
  4. Double click the key then select “Access Control”
  5. Ensure codesign is listed in the “Always allow access by these applications:”

— If it is not:

  1. Click the +
  2. HitG & in the popup window, enter /usr/bin then click Go
  3. Select codesign & click Add
  4. Click Save Changes then enter your password

Resign ios app

  1. CD to the dir which contains your ipa file.

    cd ~/Downloads/my_app
    
  2. Unzip the app

    unzip my_app.ipa
    
  3. Delete the Mobile Provision in the app.

    rm Payload/my_app.app/embedded.mobileprovision
    
  4. Copy your new provisioning profile into the app.

    cp ~/Downloads/ProvisionAdHoc.mobileprovision Payload/my_app.app/embedded.mobileprovision
    
  5. Export required system vars

    export EMBEDDED_PROFILE_NAME=embedded.mobileprovision
    export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    
  6. Resign your code

    codesign -f -vv -s "iPhone Distribution: Joe Smith" Payload/my_app.app
    
  7. Recreate your ipa file.

    zip -r Payload Payload
    mv Payload.zip my_app.ipa