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

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: Unity - Manual: Receipt validation

Here’s the stack trace I’m getting:

03-22 11:09:41.025: I/Unity(14169): NullReferenceException: Object reference not set to an instance of an object
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
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
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
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
03-22 11:09:41.025: I/Unity(14169):   at UnityEngine.Purchasing.Security.RSAKey.Verify (System.Byte[] message, System.Byte[] signature) [0x00000] in <filen

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.

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…

<linker>
       <assembly fullname="mscorlib">
               <type fullname="System.Security.Cryptography.*" preserve="all"/>
       </assembly>
</linker>
1 Like

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.

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?).

Ah, as you told me on the other forum, it is

using UnityEngine.Purchasing.Security;

thanks a million!

1 Like

had the same problem. The link.xml solved my problem right away. Thanks mh114 for saving me a lot of time! :slight_smile:

1 Like

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.

2 Likes

Dear mh114, thank you so much! I’ve struggled with this issue for ages…

1 Like

@mh114 We’re looking at adding this link.xml extension to Unity IAP 1.13.2 [to Assets/Plugins/UnityPurchasing/scripts/link.xml]. :slight_smile:

1 Like