Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions Apkifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace Emulamer.Utils
{
public class Apkifier : IDisposable
public class Apkifier : IDisposable, IApkFileIO
{
private SHA1 _sha = SHA1Managed.Create();
private UTF8Encoding _encoding = new UTF8Encoding(false);
Expand Down Expand Up @@ -53,7 +53,11 @@ public string PemData
return _pemData;
}
}

public bool IsReadOnly
{
get
{ return _readOnly; }
}

/// <summary>
/// Creates a new instance of Apkifier
Expand All @@ -66,7 +70,7 @@ public Apkifier(string filename, bool sign = true, string pemCertificateData = n
_filename = filename;
_sign = sign;
_readOnly = readOnly;

if (pemCertificateData != null)
{
_pemData = pemCertificateData;
Expand All @@ -82,9 +86,10 @@ public Apkifier(string filename, bool sign = true, string pemCertificateData = n
_archive = ZipFile.OpenRead(_filename);
else
_archive = ZipFile.Open(_filename, ZipArchiveMode.Update);

}



public void LoadCert(byte[] certData)
{
Expand Down Expand Up @@ -139,10 +144,17 @@ public virtual void Write(Stream fileData, string targetPath, bool overwrite = f
public virtual void Write(string inputFileName, string targetPath, bool overwrite = false, bool compress = true)
{
_hasChanges = true;
using (FileStream fs = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
if (overwrite )
{
Write(fs, targetPath, overwrite, compress);
}
var entry = _archive.GetEntry(targetPath);
if (entry != null)
entry.Delete();
}
_archive.CreateEntryFromFile(inputFileName, targetPath, compress ? CompressionLevel.Optimal : CompressionLevel.NoCompression);
//using (FileStream fs = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
//{
// Write(fs, targetPath, overwrite, compress);
//}
}

/// <summary>
Expand Down Expand Up @@ -287,7 +299,7 @@ public void Sign()


//write the SF to memory then copy it out to the actual file- contents will be needed later to use for signing, don't want to hit the zip stream twice

byte[] sigFileBytes = null;

using (StreamWriter swSignatureFile = GetSW(msSigFile))
Expand Down Expand Up @@ -320,10 +332,10 @@ public void Sign()
x.Delete();
});
}

//write the 3 files
msManifestFile.Seek(0, SeekOrigin.Begin);

var manifestEntry = _archive.CreateEntry("META-INF/MANIFEST.MF");
using (Stream s = manifestEntry.Open())
{
Expand Down Expand Up @@ -385,7 +397,7 @@ private void WriteEntryHashes(ZipArchiveEntry sourceFile, Stream manifestFileStr
{
swSFFile.WriteLine($"Name: {sourceFile.FullName}");
swSFFile.WriteLine($"SHA1-Digest: {hashOfMFSection}");
swSFFile.WriteLine();
swSFFile.WriteLine();
}

msSection.Seek(0, SeekOrigin.Begin);
Expand Down Expand Up @@ -422,7 +434,7 @@ public string GenerateNewCertificatePEM()
using (var writer = new StringWriter())
{
var pemWriter = new OpenSsl.PemWriter(writer);

pemWriter.WriteObject(new PemObject("CERTIFICATE", cert.GetEncoded()));
pemWriter.WriteObject(subjectKeyPair.Private);
return writer.ToString();
Expand Down Expand Up @@ -465,7 +477,7 @@ private static X509Certificate LoadCert(string pemData, out AsymmetricKeyParamet
private byte[] SignIt(byte[] sfFileData)
{
AsymmetricKeyParameter privateKey = null;

var cert = LoadCert(_pemData, out privateKey);

//create things needed to make the CmsSignedDataGenerator work
Expand Down
26 changes: 26 additions & 0 deletions IApkFileIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace Emulamer.Utils
{
public interface IApkFileIO : IDisposable
{
bool HasChanges { get; }
bool IsReadOnly { get; }

void CopyFileInto(string sourceFilePath, string destEntryPath);
void Delete(string pattern);
void Dispose();
bool FileExists(string targetPath);
IEnumerable<string> FindFiles(string pattern);
string GenerateNewCertificatePEM();
long GetFileSize(string filename);
Stream GetWriteStream(string targetPath, bool overwrite = false, bool compress = true);
void LoadCert(byte[] certData);
byte[] Read(string targetPath);
void Sign();
void Write(Stream fileData, string targetPath, bool overwrite = false, bool compress = true);
void Write(string inputFileName, string targetPath, bool overwrite = false, bool compress = true);
}
}