django-extensions/django-extensions — pull request #343 by heston (2013-06-18 23:52:28+00:00)
Title: Allow public-key-only encryption with encrypted fields

Currently, trying to instantiate an encrypted field using only public keys causes Keyczar to raise an exception:

keyczar.errors.KeyczarError: Unacceptable purpose: ENCRYPT

This is because Keyczar (version 0.71c) uses two different classes to do symmetric and asymmetric encryption/decryption. However, BaseEncryptedField always assumes the keys will be symmetric.

In production environments, it is desirable to distribute only public keys, since an attack that would compromise the database could also likely compromise the keys. By keeping the private keys off the production network, it is much more difficult (if not impossible) to decrypt compromised data.

This pull request adds a new Django setting, ENCRYPTED_FIELD_MODE, that allows BaseEncryptedField to use the correct Keyczar class. Behavior is unchanged if this setting is omitted, so this pull request is fully backwards compatible.

FWIW, the existing code seems to already have partial support for this:

if isinstance(self.crypt.primary_key, keyczar.keys.RsaPublicKey):