2001-09-05
|
An Introduction to OpenSSL, Part Two: Cryptographic Functions Continued
last updated September 5, 2001 |
|
This is the second article in a series on OpenSSL, a library written in the C programming language that provides routines for cryptographic primitives utilized in implementing the Secure Sockets Layer (SSL) protocol. In the first article in the series, we discussed some of the basics of cryptography. This article will cover acquiring and compiling OpenSSL and explore some commands that facilitate encryption and decryption. Acquiring and Compiling OpenSSL
If you are using a recent Debian package or RPM based Linux distribution, you can usually install
OpenSSL without compiling it. Debian users can probably type
Solaris users can browse to
http://www.sunfreeware.com
and download OpenSSL if it is available
for their architecture ( Readers who do not have the luxury of installing OpenSSL without compiling it, should download the OpenSSL source from openssl.org. At the time of publication of this article, the most recent version of OpenSSL was 0.9.6b. Using an earlier version is not recommended. If users have an earlier version, upgrading is highly recommended, as there were some security bugs in earlier versions that have since been fixed [OSSLupdate]. Readers who have a CryptoSwift, Compaq Atalla, or nCipher CHIL crypto device can download the engine source to experiment with the device. Note that the engine software is said to be at "a pretty experimental status and [have] almost no documentation" [ENGREADME].
Un-gzip and de-tar the source and change directory into the newly created OpenSSL source tree. One
possible command to accomplish this would be:
If you are compiling for a production system use:
For the values
If having multiple versions available is not a priority, readers can use:
If OpenSSL is being compiled for a development system in which SSL will be debugged at the
protocol level, omitting the command
Note that by default, shared objects are not created. If they are desired, users will have to append
the
At this point, users should now run
Now run Finally, add the OpenSSL binary directory to your path and you are good to go. If readers are lazy like me and like to have the OpenSSL commands available from the command line without having to prefix them with OpenSSL, they can change directory to the OpenSSL binary directory and use one of the following kludges to create symlinks: ./openssl -? 2>&1|perl -ne '/^$|[A-Z]/||push(@a,split());END{foreach(@a){do{symlink"openssl",$_;}unless-l$_}}'
Readers who don't have perl can use:
Note that there will be a symlink called passwd which generates crypt(3) passwords. Users should change its name to something else so that it doesn't collide with the system passwd binary in the user's path, delete the link, or place the OpenSSL directory after /bin or /usr/bin in the path. If the binary directory for OpenSSL is owned by a user other than you, this operation could require privileges.
OpenSSL can be run in command line mode or interactive mode. To begin interactive mode, run
If these operations were not being conducted as experiments, following reasonable security measures such as setting umask to a conservative value like 077 is necessary. Additionally, one should follow best security practices to prevent compromise, as cryptographic methods are only one link in the proverbial security chain. OpenSSL Commands Congratulations, you have successfully installed OpenSSL! Now, let's explore some of the commands that are now available.
Go to a temporary directory and type:
First we will experiment with a couple of OpenSSL's hash function commands so that we can use their output to
verify cryptographic operations later. If the reader has used the symlink kludges above, it is
unnecessary to preface the commands below with
Type
Repeat these steps for the Save the hashes for future reference. Symmetric Encryption Now we will explore symmetric encryption. First we need a couple of keys. Since we are testing, we'll use /dev/urandom. When encrypting data that really needs to be safe, using /dev/random is generally a better bet than /dev/urandom; this is because the latter produces randomness that is lower quality as it is generated by a PRNG once it runs out of the higher quality randomness that can also be fetch from /dev/random [Callas1996]. It is also possible that because of performance constraints or concern about Denial of Service, the users of the cryptography application would want to use /dev/urandom even though it is not as secure as /dev/random. Sticking to /dev/random is certainly the most conservative stance one can take, unless the user has a hardware-based random device. We can use the following commands: dd bs=16 count=1 if=/dev/urandom of=bf_key dd bs=21 count=1 if=/dev/urandom of=3des_key If you do not have a random device installed consult the randomness section of the previous article and then use the egc command that comes with [egd] as follows: egc.pl These keys will be used for blowfish and triple-des, respectively. Now encrypt the temporary file with blowfish: openssl bf -e -a -kfile bf_key -in gcc_man_temp -out gcc_man_temp_bf_enc
In order to verify that the data is now in ciphertext form, readers can use their favorite file
viewer to view
The data can then be decrypted with blowfish:
Now we run
Now repeat the above steps with Assymetric Cryptography Using OpenSSL Now that we have covered symmetric cryptography, let's take a brief look at asymmetric cryptography with OpenSSL's implementation of RSA.
First we need a seed for OpenSSL's pseudo-random number generator.
We can acquire this with the following command:
If the reader's OS lacks a random device, he or she can run the following until random_seed contains more than 4096 bits: egc.pl
This should take about three tries with egc. Readers can check to see how much is in the random_seed
file by using:
Next we need to generate our RSA keypair using:
If we were not testing it is to include the parameter
Next, extract the public key into a separate file using the command:
When using asymmetric key pairs on production systems, allowing access to the public key is necessary. However access to the private key should be tightly controlled and available only to the entities that need to utilize it.
We can examine the numbers that constitute the keys by using:
Now we need some material to use for encryption and signing tests. The material should be less than
4096 bits (512 bytes), as we are using a 4096 bit key. The mathematics of asymmetric ciphers do not
permit one to encrypt a set of data that is larger than the keysize of the asymmetric keypair. The
next few examples will use the result of:
We can encrypt to the private key with the command:
Decrypting is then accomplished with:
Now verify that the decryption operation was successful by using a hash function command on wc_man_temp_rsa_dec and wc_man_temp (or the appropriate files).
If readers have been using
Readers should now use RSA to sign and verify the signature of some data. Generate the signature
with:
We can then verify the signature with: openssl rsautl -pubin -inkey rsa_key_public -verify -in wc_man_temp_rsa_sign -out wc_man_temp_rsa_verify
Next, we can use a hash function to verify that
Readers who used That concludes the second article of our discussion of OpenSSL and wraps up the overview of cryptographic primitives. In the next installment of this series we will begin our discussion of OpenSSL and PKI. Holt Sorenson works for Counterpane Internet Security where he wrangles tuples of bits so his colleagues can get their work done. He's always serious and never jokes or laughs. When he is not surgically attached to computers he likes to hang out with his family and engage in frivolous pursuits that purportedly keep him out of trouble. To read An Introduction to OpenSSL, Part Three: PKI- Public Key Infrastructure, click here. |
|
|
