Ejemplo n.º 1
0
        private void btnSaveIPA_Click(object sender, EventArgs e)
        {
            string inputIPAPath           = txtInputIPA.Text;
            string mobileProvisionPath    = txtMobileProvision.Text;
            string signingCertificatePath = txtSigningCertificate.Text;
            string certificatePassword    = txtCertificatePassword.Text;

            FileStream ipaStream;

            byte[] mobileProvisionBytes = null;
            byte[] signingCertificateBytes;
            try
            {
                ipaStream = new FileStream(inputIPAPath, FileMode.Open, FileAccess.Read);
            }
            catch (IOException)
            {
                MessageBox.Show("Failed to read Input IPA file", "Error");
                return;
            }

            if (mobileProvisionPath != String.Empty)
            {
                try
                {
                    mobileProvisionBytes = File.ReadAllBytes(mobileProvisionPath);
                }
                catch (IOException)
                {
                    MessageBox.Show("Failed to read mobile provision file", "Error");
                    return;
                }
            }

            try
            {
                signingCertificateBytes = File.ReadAllBytes(signingCertificatePath);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Failed to read signing certificate file", "Error");
                return;
            }
            catch (IOException)
            {
                MessageBox.Show("Failed to read signing certificate file", "Error");
                return;
            }

            DialogResult result = saveIPADialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                string  outputIPAPath = saveIPADialog.FileName;
                IPAFile ipaFile       = new IPAFile(ipaStream);
                ResignIPA(ipaFile, mobileProvisionBytes, signingCertificateBytes, certificatePassword, outputIPAPath);
            }
        }
Ejemplo n.º 2
0
        private void btnTestIPA_Click(object sender, EventArgs e)
        {
            string     inputIPAPath           = txtInputIPA.Text;
            string     signingCertificatePath = txtSigningCertificate.Text;
            string     certificatePassword    = txtCertificatePassword.Text;
            FileStream ipaStream;

            byte[] signingCertificateBytes;

            try
            {
                ipaStream = new FileStream(inputIPAPath, FileMode.Open, FileAccess.Read);
            }
            catch (IOException)
            {
                MessageBox.Show("Failed to read Input IPA file", "Error");
                return;
            }

            try
            {
                signingCertificateBytes = File.ReadAllBytes(signingCertificatePath);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Failed to read signing certificate file", "Error");
                return;
            }
            catch (IOException)
            {
                MessageBox.Show("Failed to read signing certificate file", "Error");
                return;
            }

            IPAFile ipaFile = new IPAFile(ipaStream);
            var     msg     = ipaFile.ValidateIPA(signingCertificateBytes, certificatePassword);

            if (msg == "Success")
            {
                MessageBox.Show("Signature is valid", "Success");
            }
            else
            {
                MessageBox.Show(msg, "Error");
            }
        }
Ejemplo n.º 3
0
        private void ValidateIPA(IPAFile ipaFile, byte[] signingCertificateBytes, string certificatePassword)
        {
            AsymmetricKeyEntry privateKey;
            X509Certificate    signingCertificate = CertificateHelper.GetCertificateAndKeyFromBytes(signingCertificateBytes, certificatePassword, out privateKey);

            if (signingCertificate == null)
            {
                MessageBox.Show("Failed to parse the given signing certificate", "Error");
                return;
            }

            bool isValid;

            try
            {
                isValid = ipaFile.ValidateExecutableSignature(signingCertificate);
            }
            catch (Org.BouncyCastle.Security.Certificates.CertificateExpiredException)
            {
                MessageBox.Show("Certificate is outdated", "Error");
                return;
            }
            catch (Org.BouncyCastle.Security.Certificates.CertificateNotYetValidException)
            {
                MessageBox.Show("Certificate is outdated", "Error");
                return;
            }

            if (isValid)
            {
                MessageBox.Show("Signature is valid", "Success");
            }
            else
            {
                MessageBox.Show("Signature is invalid", "Error");
            }
        }
Ejemplo n.º 4
0
        private void ResignIPA(IPAFile ipaFile, byte[] mobileProvisionBytes, byte[] signingCertificateBytes, string certificatePassword, string outputIPAPath)
        {
            // Validate that the mobileprovision match the given certificate
            MobileProvisionFile mobileProvision;

            if (mobileProvisionBytes == null)
            {
                mobileProvision = ipaFile.GetMobileProvision();
            }
            else
            {
                mobileProvision = new MobileProvisionFile(mobileProvisionBytes);
            }

            List <byte[]> developerCertificates = mobileProvision.PList.DeveloperCertificates;

            if (developerCertificates.Count == 0)
            {
                MessageBox.Show("Mobile Provision does not contain developer certificate information", "Error");
                return;
            }

            AsymmetricKeyEntry privateKey;
            X509Certificate    signingCertificate = CertificateHelper.GetCertificateAndKeyFromBytes(signingCertificateBytes, certificatePassword, out privateKey);

            if (signingCertificate == null)
            {
                MessageBox.Show("Failed to parse the given signing certificate", "Error");
                return;
            }

            bool foundMatchingCertificate = false;

            for (int index = 0; index < developerCertificates.Count; index++)
            {
                X509Certificate provisionedCertificate = CertificateHelper.GetCertificatesFromBytes(developerCertificates[index]);
                if (provisionedCertificate.Equals(signingCertificate))
                {
                    foundMatchingCertificate = true;
                }
            }

            if (!foundMatchingCertificate)
            {
                MessageBox.Show("The signing certificate given does not match any specified in the Mobile Provision file", "Error");
                return;
            }

            List <X509Certificate> certificateStore;

            try
            {
                certificateStore = ReadCertificatesDirectory();
            }
            catch
            {
                MessageBox.Show("Failed to read certificate directory", "Error");
                return;
            }

            List <X509Certificate> certificateChain = CertificateHelper.BuildCertificateChain(signingCertificate, certificateStore);

            if (mobileProvisionBytes != null)
            {
                ipaFile.ReplaceMobileProvision(mobileProvisionBytes);
            }

            if (ipaFile.HasFrameworksFolder)
            {
                MessageBox.Show("Signing an IPA containing a framework is not supported", "Not supported");
                return;
            }

            ipaFile.ResignIPA(certificateChain, privateKey);
            try
            {
                ipaFile.Save(outputIPAPath);
            }
            catch (IOException ex)
            {
                MessageBox.Show("Failed to save output IPA: " + ex.Message, "Error");
                return;
            }

            MessageBox.Show("Done!");
        }
Ejemplo n.º 5
0
        private void ResignIPA(IPAFile ipaFile, byte[] mobileProvisionBytes, byte[] signingCertificateBytes, string certificatePassword, string outputIPAPath)
        {
            // Validate that the mobileprovision match the given certificate
            MobileProvisionFile mobileProvision;

            if (mobileProvisionBytes == null)
            {
                mobileProvision = ipaFile.GetMobileProvision();
            }
            else
            {
                mobileProvision = new MobileProvisionFile(mobileProvisionBytes);
            }

            if (mobileProvision.PList.DeveloperCertificates.Count == 0)
            {
                MessageBox.Show("Mobile Provision does not contain developer certificate information", "Error");
                return;
            }

            X509Certificate    provisionedCertificate = CertificateHelper.GetCertificatesFromBytes(mobileProvision.PList.DeveloperCertificates[0]);
            AsymmetricKeyEntry privateKey;
            X509Certificate    signingCertificate = CertificateHelper.GetCertificateAndKeyFromBytes(signingCertificateBytes, certificatePassword, out privateKey);

            if (signingCertificate == null)
            {
                MessageBox.Show("Failed to parse the given signing certificate", "Error");
                return;
            }

            if (!provisionedCertificate.Equals(signingCertificate))
            {
                MessageBox.Show("The signing certificate given does not match the one specified in the Mobile Provision file", "Error");
                return;
            }

            List <X509Certificate> certificateStore;

            try
            {
                certificateStore = ReadCertificatesDirectory();
            }
            catch
            {
                MessageBox.Show("Failed to read certificate directory", "Error");
                return;
            }

            List <X509Certificate> certificateChain = CertificateHelper.BuildCertificateChain(signingCertificate, certificateStore);

            if (mobileProvisionBytes != null)
            {
                ipaFile.ReplaceMobileProvision(mobileProvisionBytes);
            }

            ipaFile.ResignIPA(certificateChain, privateKey);
            try
            {
                ipaFile.Save(outputIPAPath);
            }
            catch (IOException ex)
            {
                MessageBox.Show("Failed to save output IPA: " + ex.Message, "Error");
                return;
            }

            MessageBox.Show("Done!");
        }