dlitz/pycrypto — pull request #32 by Legrandin (2013-01-21 17:16:40+00:00)
Title: Support for PKCS#8 encoding of private keys

Hi Dwayne,

The main goal of this set of patches is to introduce support for password-based PKCS#8 encoding of private keys (only RSA for now, but PKCS#8 is also applicable to DSA, etc). Today, the only way to encrypt a private key relies on the old PEM encryption, which is not very widespread, it uses weak ciphers, and it is in general under-specified.

With these changes, one can do:

rsaKey.exportKey('DER', 'my_password', pkcs=8)

or:

rsaKey.exportKey('PEM', 'my_password', pkcs=8, 'PBKDF2WithHMAC-SHA1AndAES128-CBC')

And obtain a solid PBKDF2-based PKCS#8 container. Ideally, it would have been nice to have PKCS#8 encryption as the new default over PEM encryption, but in this way the interface remains backward compatible. The following password based encryption schemes are supported:
* PBKDF2WithHMAC-SHA1AndAES128-CBC
* PBKDF2WithHMAC-SHA1AndAES192-CBC
* PBKDF2WithHMAC-SHA1AndAES256-CBC
* PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC
* pbeWithMD5AndRC2-CBC (import only)
* pbeWithMD5AndDES-CBC (import only)
* pbeWithSHA1AndRC2-CBC (import only)
* pbeWithSHA1AndDES-CBC (import only)

The first group of patches are actually a refactoring of the asn1 module. Now I think it is much easier to write code that parses DER, plus I added the relevant documentation.

Finally, by having separate modules for PEM and PKCS8, the code in RSA.py becomes much more compact,