title: iOS IPA Re-packagin/Re-signing of an AppStore app
body: <h2>Problem description</h2>
  
  <p>I need to control an arbitrary application on an iOS device, my plan is to inject an executable to the IPA ( where remote control logic is implemented ) and then re-package it.</p>
  
  <p>Since the application should run in a controlled environment ( a specific device ), I plan on using my provisioning profile with my development certificate for re-packaging/signing.</p>
  
  <p>To begin with, I am trying to re-package the 3rd party app w/o Injecting any code, this is done is the following manner:</p>
  
  <pre><code> 1. Unzip the existing IPA
   2. Copy the provisioning profile to %APP_NAME%.app/embedded.mobileprovisioning
   3. export CODESIGN_ALLOCATE="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate"
   4. signcode --force --deep -s "%Dev Cert Name%" "%Path/To/APP_NAME%.app"
   5. zip the re-signed code back together
  </code></pre>
  
  <p>The Above is working great for an applications I manually build using Xcode, however, when using IPAs downloaded from the AppStore this doesn't work with the following device log error:</p>
  
  <pre><code>&lt;Debug&gt;: AppleFairplayTextCrypterSession::fairplayOpen() failed, error -42112
  </code></pre>
  
  <p>Inspecting the 'Mach-O' Executable of the application, I have verified that the "Code Signature" section of the relevant architecture was fully changed ( by the 'signcode' tool ).<br />
  <br /></p>
  
  <h2>Questions</h2>
  
  <ul>
  <li>Why can't I re-package the app I have downloaded from the AppStore while the an app I manually create w/ Xcode is successfully re-packaged/signed?</li>
  <li>How can I re-package/sign an AppStore app using my development certificate &amp; provisioning profile?</li>
  <li>How does FairPaly distinguish between an app I manually produce ( using Xcode ) to an app downloaded from the appstore? what residuals does an appstore app has that a manually made app does not ?</li>
  </ul>
  
  <h2>References</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/25737711/ios-undocumented-api-using-uiautomation-framework-on-ios7">https://stackoverflow.com/questions/25737711/ios-undocumented-api-using-uiautomation-framework-on-ios7</a></li>
  <li><a href="http://macsecurity.net/view/55/" rel="nofollow noreferrer">http://macsecurity.net/view/55/</a></li>
  <li><a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/codesign.1.html" rel="nofollow noreferrer">https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/codesign.1.html</a></li>
  </ul>