tags:

views:

113

answers:

3

I have created a C++ CLI wrapper for native C++ code, which in turn I reference in my C# application. Is it possible to somehow protect this assembly so that it may only be used in my application without the possibility of someone else using it?

I'm a Microsoft technology developer, I'm all about selfishness :)

+3  A: 

I have used this technique with success.

Basically, it's about protecting your assembly from being loaded by placing a link-time security demand and using the assembly strong name's public key as evidence. Only assemblies signed with your private key will be able to load it.

CesarGon
Possible duplicate: http://stackoverflow.com/questions/1929080/net-how-to-make-a-class-such-that-only-one-other-specific-class-can-instantiat :)
BlueRaja - Danny Pflughoeft
@BlueRaja: The post you're referring to asks about instantiating a class; here it's about using a whole assembly. The technique in my answer can be applied to either, but other techniques may be only valid for one of them. Therefore I think it's not a duplicate. :-)
CesarGon
A: 

Yes, but this will be a losing battle against a determined attacker.

For example, you could provide an encrypted version that only accepts a particular key, as some people have suggested. But then you'll need to decrypt it on your clients, who may not have encrypted memory stores, so a determined attacker could simply read the appropriate block of memory and deserialize the result to get your original assembly.

John Feminella
A: 

Not perfectly. LinkDemand is only enforced in a partial trust environment. Code running with full trust can also use reflection to access private types, and generally bypass any protection you care to discuss.

Maybe you want your C++/CLI code to call Assembly::GetEntryAssembly and check it against a whitelist of approved apps for which the library is intended.

Of course, you want to minimize the amount of managed code (and attributes) involved in the checks since managed code is really easy to decompile.

Actually, mixing the occasional check into the core logic of the C++ code is the only hope you have that it won't be bypassed.

Ben Voigt