Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SOLVED: [UnityIAP / Android] Stripping problem: NullReferenceException when validating receipt

Discussion in 'Unity IAP' started by mh114, Mar 22, 2016.

Thread Status:
Not open for further replies.
  1. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    Hi,
    I was integrating Unity IAP to implement my one and only IAP purchase (ad removal) into the Android-version of my game. Testing the purchase however causes a NRE in the receipt validation, any ideas what might be wrong?

    My code is basically what is shown in the manual: http://docs.unity3d.com/Manual/UnityIAPValidatingReceipts.html

    Here's the stack trace I'm getting:
    Code (csharp):
    1. 03-22 11:09:41.025: I/Unity(14169): NullReferenceException: Object reference not set to an instance of an object
    2. 03-22 11:09:41.025: I/Unity(14169):   at Mono.Security.Cryptography.PKCS1.Encode_v15 (System.Security.Cryptography.HashAlgorithm hash, System.Byte[] hashValue, Int32 emLength) [0x00000] in <filename unknown>:0
    3. 03-22 11:09:41.025: I/Unity(14169):   at Mono.Security.Cryptography.PKCS1.Verify_v15 (System.Security.Cryptography.RSA rsa, System.Security.Cryptography.HashAlgorithm hash, System.Byte[] hashValue, System.Byte[] signature, Boolean tryNonStandardEncoding) [0x00000] in <filename unknown>:0
    4. 03-22 11:09:41.025: I/Unity(14169):   at Mono.Security.Cryptography.PKCS1.Verify_v15 (System.Security.Cryptography.RSA rsa, System.Security.Cryptography.HashAlgorithm hash, System.Byte[] hashValue, System.Byte[] signature) [0x00000] in <filename unknown>:0
    5. 03-22 11:09:41.025: I/Unity(14169):   at System.Security.Cryptography.RSACryptoServiceProvider.VerifyHash (System.Byte[] rgbHash, System.String str, System.Byte[] rgbSignature) [0x00000] in <filename unknown>:0
    6. 03-22 11:09:41.025: I/Unity(14169):   at UnityEngine.Purchasing.Security.RSAKey.Verify (System.Byte[] message, System.Byte[] signature) [0x00000] in <filen
     
  2. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    Interestingly, I tested various things like changing the API level from .Net 2.0 subset I was using to full .Net 2.0 without any difference. Then I decided to try with IL2CPP backend for fun (more like how the game generally works on it), and the receipt validation worked immediately! Does anybody have any idea what might be causing the NRE when using Mono?

    I'm using Unity 5.3.4f1.
     
  3. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    Ah, it's related to stripping. I was originally using Strip Bytecode, then I tried Strip Assemblies (still fails). When disabling all stripping it works. Now to find out what's getting stripped and how to preserve it... Probably some of those cryptography classes.

    EDIT: With this link.xml the problem is solved. Now to see if I can loosen it, probably, just takes time experimenting..
    Code (CSharp):
    1. <linker>
    2.        <assembly fullname="mscorlib">
    3.                <type fullname="System.Security.Cryptography.*" preserve="all"/>
    4.        </assembly>
    5. </linker>
     
    Last edited: Mar 22, 2016
    nicholasr likes this.
  4. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    I'm having the same problem with an iOS / Android build, but, it makes no difference if I use Net/subset or turn stripping on or off. What a hassle.
     
  5. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    API level doesn't matter, but are you sure you tried without stripping? For me it definitely works, also when preventing the cryptography classes from being stripped with link.xml I posted above. And for some reason IL2CPP was able to correctly strip things without this breaking, so this only matters to Mono.

    I don't have IAP in my iOS version, so cannot comment on that (except IL2CPP is used there so might work?).
     
  6. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Ah, as you told me on the other forum, it is

    using UnityEngine.Purchasing.Security;

    thanks a million!
     
    nicholasr likes this.
  7. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    had the same problem. The link.xml solved my problem right away. Thanks mh114 for saving me a lot of time! :)
     
    mh114 likes this.
  8. MaxB

    MaxB

    Joined:
    Jun 13, 2013
    Posts:
    13
    This issue still exists though it wasn't cryptography showing up as null for me, it was a class that I instantiated. Disabling stripping fixed it.
     
    gbelini and mh114 like this.
  9. Leonid

    Leonid

    Joined:
    Aug 20, 2013
    Posts:
    90
    Dear mh114, thank you so much! I've struggled with this issue for ages..
     
    mh114 likes this.
  10. nicholasr

    nicholasr

    Joined:
    Aug 15, 2015
    Posts:
    183
    @mh114 We're looking at adding this link.xml extension to Unity IAP 1.13.2 [to Assets/Plugins/UnityPurchasing/scripts/link.xml]. :)
     
    ap-unity likes this.
Thread Status:
Not open for further replies.