<?php
// Fixed by @ChiptuneXT, 2014
include('tools/producttypes.php');

$activation= (array_key_exists('activation-info-base64', $_POST) 
			  ? base64_decode($_POST['activation-info-base64']) 
			  : array_key_exists('activation-info', $_POST) ? $_POST['activation-info'] : '');
$guid = array_key_exists('guid', $_POST) ? $_POST["guid"] : '-';

if(!isset($activation) || empty($activation)) { exit('Only for iTunes access'); }

// load and decode activation info
$encodedrequest = new DOMDocument;
$encodedrequest->loadXML($activation);
$activationDecoded= base64_decode($encodedrequest->getElementsByTagName('data')->item(0)->nodeValue);
$fairPlayCertChain= $encodedrequest->getElementsByTagName('data')->item(1)->nodeValue;
#$fairPlaySignature= $encodedrequest->getElementsByTagName('data')->item(2)->nodeValue;

$decodedrequest = new DOMDocument;
$decodedrequest->loadXML($activationDecoded);
$nodes = $decodedrequest->getElementsByTagName('dict')->item(0)->getElementsByTagName('*');

for ($i = 0; $i < $nodes->length - 1; $i=$i+2)
{
	#${$nodes->item($i)->nodeValue} = preg_match('/(true|false)/', $nodes->item($i + 1)->nodeName) ? $nodes->item($i + 1)->nodeName : $nodes->item($i + 1)->nodeValue;

	switch ($nodes->item($i)->nodeValue)
	{
		case "ActivationRandomness": $activationRandomness = $nodes->item($i + 1)->nodeValue; break;
		case "DeviceCertRequest": $deviceCertRequest = base64_decode($nodes->item($i + 1)->nodeValue); break;
		case "DeviceClass": $deviceClass = $nodes->item($i + 1)->nodeValue; break;
		case "SerialNumber": $serialNumber = $nodes->item($i + 1)->nodeValue; break;
		case "UniqueDeviceID": $uniqueDiviceID = $nodes->item($i + 1)->nodeValue; break;
		case "InternationalMobileEquipmentIdentity": $imei = $nodes->item($i + 1)->nodeValue; break;
		case "InternationalMobileSubscriberIdentity": $imsi = $nodes->item($i + 1)->nodeValue; break;
		case "IntegratedCircuitCardIdentity": $iccid = $nodes->item($i + 1)->nodeValue; break;
		case "UniqueChipID": $ucid = $nodes->item($i + 1)->nodeValue; break;
		case "ProductType": $productType = $nodes->item($i + 1)->nodeValue; break;
		case "ActivationState": $activationState = $nodes->item($i + 1)->nodeValue; break;
		case "ProductVersion": $productVersion = $nodes->item($i + 1)->nodeValue; break;
	}
}

if ($activationState != 'Unactivated') #WildcardActivated, Activated, ??????
{
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.apple.com/itms/" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="iTunes Store" /><meta name="description" content="iTunes Store" /><title>iPhone Activation</title><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/shared/common-min.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/deviceservices/stylesheets/styles.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/pages/IPAJingleEndPointErrorPage-min.css" charset="utf-8" rel="stylesheet" /><script id="protocol" type="text/x-apple-plist">
		<plist version="1.0">
			<dict>
				<key>'.($deviceClass == "iPhone" ? 'iphone' : 'device').'-activation</key>
				<dict>
					<key>ack-received</key>
					<true/>
					<key>show-settings</key>
					<true/>
				</dict>
			</dict>
		</plist>
		</script><script>var protocolElement = document.getElementById("protocol");var protocolContent = protocolElement.innerText;iTunes.addProtocol(protocolContent);</script></head><body/></html>';
	exit;
}

# ----------------------------------- save request info ------------------------------------------
$devicefolder = 'devices/'.$deviceClass.'/'.$serialNumber.'/';

if (!file_exists('devices/'.$deviceClass.'/')) mkdir('devices/'.$deviceClass.'/', 0777, true);
if (!file_exists($devicefolder))  mkdir($devicefolder, 0777, true);

$encodedrequest->save($devicefolder.'device-request.xml');
$decodedrequest->save($devicefolder.'device-request-decoded.xml');
file_put_contents($devicefolder.'cert-request.csr', $deviceCertRequest);
file_put_contents($devicefolder.'fairPlayCertChain.crt', '-----BEGIN CERTIFICATE-----'.$fairPlayCertChain.'-----END CERTIFICATE-----');
#file_put_contents($devicefolder.'fairPlaySignature.key', '-----BEGIN RSA PUBLIC KEY-----'.$fairPlaySignature.'-----END RSA PUBLIC KEY-----');
file_put_contents($devicefolder.'cert-request-public.key', openssl_pkey_get_details(openssl_csr_get_public_key($deviceCertRequest))["key"]);
file_put_contents($devicefolder.'GUID.txt', $guid);
#file_put_contents($devicefolder.'serverCASigned.crt', $certout);

# -------------------------------------------------------------------------------------------------

# ---------------------------------- Sign device certificate request ------------------------------
$privkey = array(file_get_contents('certs/original/iPhoneDeviceCA_private.key'),"minacriss");
$devicecacert = file_get_contents('certs/original/iPhoneDeviceCA.crt');

#$config = array('digest_alg' => 'sha1');
$config = array('config'=>'C:/XAMPP/php/extras/openssl/openssl.cnf', 'digest_alg' => 'sha1');

$usercert = openssl_csr_sign($deviceCertRequest,$devicecacert,$privkey,1096, $config, '06');
openssl_x509_export($usercert,$certout);
$deviceCertificate=base64_encode($certout);
//write raw $certout to file
file_put_contents($devicefolder.'serverCASigned.crt', $certout);

$certs_path = 'certs/'; # certs/original/ - minacriss original
$days = 1096; # 3 years
$certs_pass = 'icloud'; # minacriss
$cert_sn = '0x02A590E676E2CEED3A99';
# serials:
# apple - 0x02A590E676E2CEED3A99
# doulci - 0x0285C3226FC00D7AE156
# 3gs apple - 0x03d6e624f6bd5df3df1f
# minacriss - 0x06 

# Device certificate request signing
exec('openssl x509 -req -sha1 -in '.$devicefolder.'cert-request.csr -CA '.$certs_path.'iPhoneDeviceCA.crt -CAkey '.$certs_path.'iPhoneDeviceCA_private.key -out '.$devicefolder.'serverCASigned.crt -days '.
		$days.' -extfile '.$certs_path.'extensions_device.cnf -extensions usr_cert -set_serial '.$cert_sn.' -passin pass:'.$certs_pass);

$deviceCertificate=base64_encode(file_get_contents($devicefolder."serverCASigned.crt"));
# -------------------------------------------------------------------------------------------------

# -------------------------------------------- Sign account token -----------------------------------------
#$accountToken = '{'."\n\t".'"ActivationRandomness" = "'.$activationRandomness.'";'."\n\t".'"UniqueDeviceID" = "'.$uniqueDiviceID.'";'."\n".'}';

$wildcardTicket=file_get_contents('certs/ext/wildcardticket.txt');
$accountToken=
 '{'.(isset($imei) ? "\n\t".'"InternationalMobileEquipmentIdentity" = "'.$imei.'";' : '').
	"\n\t".'"ActivityURL" = "https://albert.apple.com/deviceservices/activity";'.
	"\n\t".'"ActivationRandomness" = "'.$activationRandomness.'";'.
	"\n\t".'"UniqueDeviceID" = "'.$uniqueDiviceID.'";'.
	"\n\t".'"CertificateURL" = "https://albert.apple.com/deviceservices/certifyMe";'.
	"\n\t".'"PhoneNumberNotificationURL" = "https://albert.apple.com/deviceservices/phoneHome";'.
	"\n\t".'"WildcardTicket" = "'.$wildcardTicket.'";'.
	"\n".
 '}';
$accountTokenBase64=base64_encode($accountToken);
$pkeyid = openssl_pkey_get_private(file_get_contents("certs/signature_private.key"));

openssl_sign($accountTokenBase64, $signature, $pkeyid);
openssl_free_key($pkeyid);
# -------------------------------------------------------------------------------------------------

$accountTokenSignature= base64_encode($signature);
$accountTokenCertificateBase64 = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURaekNDQWsrZ0F3SUJBZ0lCQWpBTkJna3Foa2lHOXcwQkFRVUZBREI1TVFzd0NRWURWUVFHRXdKVlV6RVQKTUJFR0ExVUVDaE1LUVhCd2JHVWdTVzVqTGpFbU1DUUdBMVVFQ3hNZFFYQndiR1VnUTJWeWRHbG1hV05oZEdsdgpiaUJCZFhSb2IzSnBkSGt4TFRBckJnTlZCQU1USkVGd2NHeGxJR2xRYUc5dVpTQkRaWEowYVdacFkyRjBhVzl1CklFRjFkR2h2Y21sMGVUQWVGdzB3TnpBME1UWXlNalUxTURKYUZ3MHhOREEwTVRZeU1qVTFNREphTUZzeEN6QUoKQmdOVkJBWVRBbFZUTVJNd0VRWURWUVFLRXdwQmNIQnNaU0JKYm1NdU1SVXdFd1lEVlFRTEV3eEJjSEJzWlNCcApVR2h2Ym1VeElEQWVCZ05WQkFNVEYwRndjR3hsSUdsUWFHOXVaU0JCWTNScGRtRjBhVzl1TUlHZk1B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qUFpMdmpnMURyalZsUWU5K1FINGgrMElECi0tLS0tRU5EIENPTlRBSU5FUi0tLS0tCg==';
#$fairPlayKeyData = base64_encode(file_get_contents('certs/ext/FairPlayKeyData.pem'));
#$accountTokenCertificateBase64 = base64_encode(file_get_contents('certs/ext/AccountTokenCertificate.crt');
#$accountTokenCertificateBase64 = base64_encode(file_get_contents($certs_path.'iPhoneActivation.crt'));

file_put_contents($devicefolder.'account-token.txt', $accountToken);
file_put_contents($devicefolder.'signature.txt', $accountTokenSignature);

echo 
'<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="iTunes Store" /><meta name="description" content="iTunes Store" /><title>iPhone Activation</title><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/shared/common-min.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/deviceservices/stylesheets/styles.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/pages/IPAJingleEndPointErrorPage-min.css" charset="utf-8" rel="stylesheet" /><link href="resources/auth_styles.css" charset="utf-8" rel="stylesheet" /><script id="protocol" type="text/x-apple-plist">
<plist version="1.0">
	<dict>
		<key>'.($deviceClass == "iPhone" ? 'iphone' : 'device').'-activation</key>
		<dict>
			<key>activation-record</key>
			<dict>
				<key>FairPlayKeyData</key>
				<data>'.$fairPlayKeyData.'</data>
				<key>AccountTokenCertificate</key>
				<data>'.$accountTokenCertificateBase64.'</data>
				<key>DeviceCertificate</key>
				<data>'.$deviceCertificate.'</data>
				<key>AccountTokenSignature</key>
				<data>'.$accountTokenSignature.'</data>
				<key>AccountToken</key>
				<data>'.$accountTokenBase64.'</data>
			</dict>
			<key>unbrick</key>
			<true/>
		</dict>
	</dict>
</plist>
</script><script>var protocolElement = document.getElementById("protocol");var protocolContent = protocolElement.innerText;iTunes.addProtocol(protocolContent);</script></head>
<body>
<div class="page">
	<div class="content">
	<!-- Server problems fixed by @ChiptuneXT, 2014 year -->
		<section class="headline"><h1 class="title"><span class="title-text">Demo activation is done! Activation Info:</span></h1></section>
		<section class="message">
			<label class="message-title">'.$productNames[$productType].' details</label>
			<table class="message-text">
				<tr><td>iOSv:</td><td>'.$productVersion.'</td></tr>
				<tr><td>ARN:</td><td>'.$activationRandomness.'</td></tr>
				<tr><td>UDID:</td><td>'.$uniqueDiviceID.'</td></tr>
				<tr><td>ASN:</td><td>'.(isset($serialNumber) ? $serialNumber : "-").'</td></tr>
				<tr><td>UCID:</td><td>'.(isset($ucid) ? $ucid : "-").'</td></tr>								
				<tr><td>ICCID:</td><td>'.(isset($iccid) ? $iccid : "-").'</td></tr>
				<tr><td>IMEI:</td><td>'.(isset($imei) ? $imei : "-").'</td></tr>
				<tr><td>IMSI:</td><td>'.(isset($imsi) ? $imsi : "-").'</td></tr>
				<tr><td>GUID:</td><td>'.(isset($guid) ? $guid : "-").'</td></tr>
			</table>
		</section>
	</div>
	<img class="product-image" src="resources/auth_'.strtolower($deviceClass).'.png"/>
</div>
</body></html>';
?>