If you try to make some money out of your programs, you'll need some sort of registration key to license the software. You have all seen keys like VP9VV-VJW7Q-MHY6W-JK47R-M2KGJ used to activate windows and other microsoft programs. There are many ways to generate license keys but the main principles are:
Generating keys isn't easy for the average windows programmer. It is a separate skill especially when considered as part of the total protection against piracy, reverse engineering etc. There are products you can buy to add protection and licensing to your code (e.g. WinLicense, Armadillo etc), or you can roll out your own key generator like I did with xplorer².
Why reinvent the wheel? Believe it or not sometimes off the shelf protection solutions are easier to crack because so many programs use them, there are automated tools to strip their protection! They also use some obscure tricks to protect the software from debuggers and other tools used by the pirates, which may cause antivirus programs to mistake your software for a virus. That's not going to help your revenues. Finally from what I could tell, these licensing tools do not support secure public/private key RSA algorithms (or their customer support wasn't tech savvy), so you risk being keygenned.
The worst case scenario for your licensing is your key generator scheme being discovered by some mad dog pirate, who then goes on to distribute keygens. People who get hold of these keygens can offer themselves free license keys for your software and your business is losing money.
I used an amateur license key scheme in xplorer²; it took a couple of years before the first keygens appeared back in 2006(?). These first crack attempts were not very good so it was easy for me to detect legitimate from keygenned keys. Over the years the pirates became more sophisticated as they could read the code I used to fight against them so they improved their keygens. To this date this cat and mouse story continues, but I believe that soon a perfect keygen will emerge. (PS. none of the existing keygens work properly). So I'm looking for a more secure alternative.
RSA cryptography key generators
Using cryptographic techniques it is possible to come up with license keys that are 100% proof against illegal key generators. RSA is a heavy duty asymmetric encryption algorithm using some fancy mathematics and private/public keys. The idea is the same as digital signatures used in emails or authenticode:
Although the RSA algorithm is well known, its strength lies in the separation of the encryption and decryption keys (unlike symmetric encryption algorithms like blowfish or AES). The pirate can only see the public key in your software. The private key is safely kept on your own webserver that sends out the legitimate registration codes. As the private encryption key is a huge prime number, it is computationally infeasible (or let's say very hard indeed) to derive the private key knowing just the public key. So your license key generator is uncrackable.
Having a bullet proof key generator doesn't mean you are saved from software piracy. You will be cracked in other ways. Hackers can decompile your executable, find where you do the signature check and disable it with a patch. But this is hard work for them, every time you release a software update, they will have to reapply the patch. An unauthorized key generator is much more convenient for the pirates.
Practical RSA software licensing
RSA encryption boils down to large number arithmetic (exponentiation and modulo division of numbers having in excess of 512 bits). There are many open source big-num libraries, usually as part of crypto libraries (crypto++, openssl, mozilla's NSS et al). For windows programming there is also MS CryptoAPI, present on almost all windows boxes (even in windows 95). There also is a selection of algorithms, RSA vs DSA or elliptic curves.
I did not make an exhaustive enumeration of pros and cons of each alternative. I didn't like CryptoAPI much because it is too complicated and the keys are too opaque for interoperability with other packages. I chose openssl RSA simply because it is as good as the next crypto library and there are a few samples floating around. More importantly openssl is also usable via PHP scripts; the scripts that will issue the license keys will be most probably running on your webserver and PHP is handy for this task.
My starting point for the RSA key generator code was this article. It is a very good introduction with sample code. But the devil is always in the details. Which padding scheme do you use? Do you go for MD5 or SHA-1 hashing? Are we big or little endian? And how do you make sure that the keys signed by your PHP scripts will be validated from your C/C++ code? All will be revealed below.
OpenSSL windows development platform
To develop and test all aspects of the key generator on windows you will need two downloads. For the C/C++ part you need the openssl windows headers and binaries that will allow you to compile and link against openssl. For PHP development you need a WAMP distribution that conveniently packages the Apache server, MySQL (not necessary for us) and PHP (including the scriptable version of openssl).
I downloaded the full version of the win32 binaries (Win32 OpenSSL v0.9.8o) and you may also need the Visual C++ 2008 Redistributables if you find that the openssl command line tools return funny errors (openssl.exe complained about missing configuration files). After installation make sure you add the openssl folder to the PATH environmental variable so that ssleay32.dll and libeay32.dll can be found. Also add the openssl INCLUDE and LIB folders to the build environment. I didn't manage to link statically to the openssl libraries with VS6 but dynamic linking works fine.
To run PHP first you need to run Apache. The WAMP distribution mentioned above is very convenient installing all the components, in a win32 friendly way: everything ran smoothly without messing about with configuration files that linux types are so fond of — we have better things to do in our lives with windows <g>. The only problem I had was that Apache service wouldn't start unless I was logged with administrator credentials on a virtual XP machine. The wampserver icon would be 1/3 red otherwise and localhost was inaccessible.
The openssl C library on the other hand is poorly documented. There are few explanations, weird concepts like BIO buffers, and very few actual code samples mostly focusing on the secure sockets layer part, not the digital signatures. In the end I found an online book with a lot of information. But hopefully all will become clear in the code examples further down.
Step 1. Generate your encryption keys
The most important parameter is the bit length of the encryption. I have opted for 512 bits which means that the signature (with padding) will be approximately 512/8=64 bytes long. Longer bit lengths result in stronger encryption but remember that we are not building anything that will affect national security, and we don't want to end up with too long software codes (registration keys).
Step 2. Create and sign a registration key
When somebody buys your software, the ecommerce provider that handles the credit card payments (e.g. Plimus) will do a HTTP POST (or GET) request to your webserver with the customer details expecting a registration key in response. The idea is to bundle the order parameters (date, customer email, number of licenses and what have you) in a text string which will be the 'message' to be signed, e.g:
We calculate the message digest (20 bytes for SHA-1) and RSA sign it using the default PKCS1 padding scheme. The details are explained in this tutorial. The resulting signature will be a very long number (~512 bits) which for convenience is base64 encoded so that it can be included in an email as text. Here's the PHP code that does the signing:
All pretty standard PHP stuff using some openssl functions to sign the text with our private key. We can use the PEM text file verbatim to initialize the RSA key variable. It goes without saying that the private key should be kept for your eyes only — don't publicise it in your blog <g>. This PHP script resides in the webserver and (I hope) cannot be seen directly by cheeky pirates. For extra safety only allow this script to run from known IP addresses (eg your ecommerce provider only).
The registration key you email to the customer is 2 text lines, the original order details and the base64 encoded signature thereof as such:
It looks a bit funny and it is quite long but it is bullet proof! If the key information is tampered the signature will no longer match. As only you know the private encryption key, nobody can patch the registration code. You are in keygen heaven!
If you are concerned with the size of the key, you can use a weaker encryption key pair (eg 256 bits) but as CPU power increases chances are that pirates may brute-force their way to find your private key and the game is lost. I don't think people will need to type this key anyway, they can just copy/paste the text from the registration email. For extra safety you can calculate a simple checksum and tack it on at the end of the key (unlock code).
Step 3. Validate a registration key
The customer has received the unlock code and somehow pasted it in the program. But is the key any good? Here is the validation procedure:
Here is a sample C++ program that links to openssl to perform these checks:
This C++ demonstrates how awkward and unintuitive the low-level openssl can get. I haven't got a clue what these BIO containers in unbase64() function do or if handles and memory is leaked. It works validating RSA signatures, that's for sure. Note that the hash (digest) and padding of the verification stage match exactly that of signing earlier.
You must be careful not to reject valid registration codes because of formatting errors. You must remove extra whitespace and newlines otherwise the hash won't match. Encryption purists may also get upset from the apparent lack of security, but see this link for adding blinding, whatever this may be. Again remember that you are not dealing with credit cards or national security here, so you can relax your high standards <g>
Isn't this magic? With a mere 100 lines of code you have yourself the bare essentials of a potentially uncrackable software key scheme! We don't care that anybody can potentially read our public key as they cannot infer the secret private key from it — not without a cluster of cray supercomputers anyway.
The only improvement I am considering is to ditch the C part of openssl verification for something more reasonable and less mysterious like this bigdigits library. It is going to be fun extracting the prime numbers and exponents from the PEM private key files, and do the RSA modulo arithmetic manually, but more on that in a future blog post!
|© 2002—2010 Nikos Bozinis, all rights reserved|