title: How to decrypt an encrypted Apple iTunes iPhone backup?
body: <p>I've been asked by a number of unfortunate iPhone users to help them restore data from their iTunes backups. This is easy when they are unencrypted, but not when they are encrypted, whether or not the password is known.</p>
  
  <p>As such, I'm trying to figure out the encryption scheme used on mddata and mdinfo files when encrypted. I have no problems reading these files otherwise, and have built some robust C# libraries for doing so. (If you're able to help, I don't care which language you use. It's the principle I'm after here!)</p>
  
  <p>The Apple "iPhone OS Enterprise Deployment Guide" states that "Device backups can be stored in encrypted format by selecting the Encrypt iPhone
  Backup option in the device summary pane of iTunes. Files are encrypted using AES128 with a 256-bit key. The key is stored securely in the iPhone keychain."</p>
  
  <p>That's a pretty good clue, and there's some good info here on Stackoverflow on iPhone <a href="https://stackoverflow.com/questions/538435/aes-interoperability-between-net-and-iphone">AES/Rijndael interoperability</a> suggesting a keysize of 128 and CBC mode may be used.</p>
  
  <p>Aside from any other obfuscation, a key and initialisation vector (IV)/salt are required.</p>
  
  <p>One might assume that the key is a manipulation  of the "backup password" that users are prompted to enter by iTunes and passed to "<a href="http://keepmurmuring.blogspot.com/2009/06/iphone-backup-informationnote.html" rel="noreferrer">AppleMobileBackup.exe</a>", padded in a fashion dictated by CBC. However, given the reference to the iPhone keychain, I wonder whether the "backup password" might not be used as a password on an X509 certificate or symmetric private key, and that the certificate or private key itself might be used as the key. (<a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" rel="noreferrer">AES</a> and the iTunes encrypt/decrypt process is symmetric.)</p>
  
  <p>The IV is another matter, and it could be a few things. Perhaps it's one of the keys <a href="http://hcsoftware.sourceforge.net/jason-rohrer/itms4all/" rel="noreferrer">hard-coded</a> into iTunes, or into the <a href="http://wikee.iphwn.org/s5l8900:encryption_keys" rel="noreferrer">devices themselves</a>.</p>
  
  <p>Although Apple's comment above suggests the key is present on the device's keychain, I think this isn't that important. One can restore an encrypted backup to a <i>different</i> device, which suggests all information relevant to the decryption is present in the backup and iTunes configuration, and that anything solely on the device is irrelevant and replacable in this context. So where might be the key be?</p>
  
  <p>I've listed paths below from a Windows machine but it's much of a muchness whichever OS we use.</p>
  
  <p>The "\appdata\Roaming\Apple Computer\iTunes\itunesprefs.xml" contains a PList with a "Keychain" dict entry in it. The "\programdata\apple\Lockdown\09037027da8f4bdefdea97d706703ca034c88bab.plist" contains a PList with "DeviceCertificate", "HostCertificate", and "RootCertificate", all of which appear to be valid X509 certs. The same file also appears to contain asymmetric keys "RootPrivateKey" and "HostPrivateKey" (my reading suggests these might be PKCS #7-enveloped). Also, within each backup there are "AuthSignature" and "AuthData" values in the Manifest.plist file, although these appear to be rotated as each file gets incrementally backed up, suggested they're not that useful as a key, unless something really quite involved is being done.</p>
  
  <p>There's a lot of misleading stuff out there suggesting getting data from encrypted backups is easy. It's not, and to my knowledge it hasn't been done. <a href="http://iphoneinsecurity.com/" rel="noreferrer">Bypassing or disabling</a> the backup encryption is another matter entirely, and is not what I'm looking to do.</p>
  
  <p>This isn't about hacking apart the iPhone or anything like that. All I'm after here is a means to extract data (photos, contacts, etc.) from encrypted iTunes backups as I can unencrypted ones. I've tried all sorts of permutations with the information I've put down above but got nowhere. I'd appreciate any thoughts or techniques I might have missed.</p>