bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

Cannot generate Apple SKADNetwork Signature

Open riccardo91 opened this issue 4 years ago • 0 comments

Following apple SKADNetwork I've generated private key and public key with this command

openssl ecparam -name prime192v1 -genkey -noout -out private_key.pem
openssl ec -in private_key.pem -pubout -out public_key.pem

i'm able to generate a valid signature with python following this project https://github.com/singular-labs/Singular-SKAdNetwork-App/tree/master/skadnetwork-server

But when i'm trying to generate signature via c# bc library i have different results if i use same data.


        public static AsymmetricCipherKeyPair ReadAsymmetricPrivateKeyParameter(string pemFilename)
        {
            var fileStream = System.IO.File.OpenText(pemFilename);
            var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream);
            var KeyParameter = (AsymmetricCipherKeyPair)pemReader.ReadObject();
            return (AsymmetricCipherKeyPair)KeyParameter;
        }

        static void Main(string[] args)
        {
            AsymmetricCipherKeyPair pkey = ReadAsymmetricPrivateKeyParameter("private_key.pem");

            string pars = getParamsSignature("2.0", "xxxxxxx.skadnetwork", "10", "302584613", "0a97ad57-87d1-49e7-b166-9152c708251b", "1613749507007", "0");

            Sign(pkey, "", pars);
            Console.Read();
        }

        static string getParamsSignature(string version, string ad_network_id, string campaign_id, string target_app_id, string nonce, string timestamp, string source_app_id)
        {
            string[] p = new string[] {
            version,
            ad_network_id,
            campaign_id,
            target_app_id,
            nonce,
            source_app_id,
            timestamp
            };
            return string.Join("\u2063", p);
        }

        public static bool Sign(AsymmetricCipherKeyPair pubKey,  string msg)
        {
            try
            {

                ECDomainParameters aa;
                byte[] msgBytes = Encoding.UTF8.GetBytes(msg);

                ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");

                signer.Init(true, pubKey.Private);

                signer.BlockUpdate(msgBytes, 0, msgBytes.Length);

                byte[] sigBsdytes = signer.GenerateSignature();

               
                var signature = Convert.ToBase64String(sigBsdytes);



                //MDUCGQDgqw1YQN/vvHTxXXTpovNYUnACzkFrXJwCGCXAnr3TUbbqIUr6cBamymrypcQET5RR7Q==

    
            }
            catch (Exception exc)
            {
                Console.WriteLine("Verification failed with the error: " + exc.ToString());
                return false;
            }
            return false;
        }


any suggestion?

riccardo91 avatar Feb 19 '21 17:02 riccardo91