How to check apk signature at runtime xamarin.forms

733 views Asked by At

Hello guys I am trying to implement anti tamper protection and verify app signature in xamarin forms android app. Currently I am using this code:

var context = Android.App.Application.Context;
Signature sigs = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Signatures).Signatures[0];

DisplayAlert("sigs.ToString()", sigs.ToString(), "ok");   //1331014879
DisplayAlert(" sigs.GetHashCode().ToString()", sigs.GetHashCode().ToString(), "ok");  //  android.content.pm.Signature@4f55acdf                         

sigs.GetHashCode().ToString() returns 1331014879
sigs.ToString() returns android.content.pm.Signature@4f55acdf

But I think I may be doing it wrong. Does this is correct way to get verify android app signature at runtime ? Else please provide me code and guidance. Thanks.

2

There are 2 answers

0
Leon On BEST ANSWER

If API28 or higher you should check for multipleSigners as well like this thread. How to use PackageInfo.GET_SIGNING_CERTIFICATES in API 28?

Here is xamarin.android code.

 public string Sig_Hash()
        {
            var Context = Android.App.Application.Context;

                foreach (Android.Content.PM.Signature signature in Context.PackageManager.GetPackageInfo(Context.PackageName, PackageInfoFlags.Signatures  ).Signatures)
                {
                    using (SHA1Managed sha1 = new SHA1Managed())
                    {
                        var hash = sha1.ComputeHash(signature.ToByteArray());
                        var sb = new StringBuilder(hash.Length * 2);
                        foreach (byte b in hash)
                        {
                            sb.Append(b.ToString("X2"));
                        }
                        return sb.ToString();
                    }

                }
                return "";
        }
0
G Clovs On

Thx @Leon Lu Just a little update on this:

        public string GetSha1()
        {
            var Context = Android.App.Application.Context;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
            {
                PackageInfo packageInfo = Context.PackageManager.GetPackageInfo(Context.PackageName, PackageInfoFlags.SigningCertificates);
                if (packageInfo == null || packageInfo.SigningInfo == null)
                    return string.Empty;

                var signature = packageInfo.SigningInfo.GetSigningCertificateHistory().FirstOrDefault();
                if (signature != null)
                {
                    return SignatureDigest(signature);
                }

            }
            else
            {
                PackageInfo packageInfo = Context.PackageManager.GetPackageInfo(Context.PackageName, PackageInfoFlags.Signatures);
                if (packageInfo == null || packageInfo.Signatures == null)
                    return string.Empty;

                var signature = Context.PackageManager.GetPackageInfo(Context.PackageName, PackageInfoFlags.Signatures).Signatures.FirstOrDefault();
                if (signature != null)
                    return SignatureDigest(signature);
            }
            return string.Empty;
        }

        private static string SignatureHexa(Android.Content.PM.Signature signature)
        {
            using (SHA1Managed sha1 = new SHA1Managed())
            {
                var hash = sha1.ComputeHash(signature.ToByteArray());
                var sb = new StringBuilder(hash.Length * 2);
                foreach (byte b in hash)
                {
                    sb.Append(b.ToString("X2"));
                }
                return sb.ToString();
            }
        }

For me My App is Sign by GooglePlay, so I don't need multiple signatures

But if you need to check multiple signers

if (packageInfo.SigningInfo.HasMultipleSigners)
   {
     foreach (Signature signature in packageInfo.SigningInfo.GetApkContentsSigners())
        {
            //Dostuff
            SignatureDigest(signature);
        }
   }