Ok I've done this quest but first I want to say that it's much easier to obtain LetsEncrypt free cert for https what i did later.
For this solution you need openssl to be installed.
Lets write views for our ajaxes
Getting public key. If we don't have one in project directory then generate the pair.
def public_key(req):
if not os.path.isfile(os.path.join(settings.BASE_DIR, 'form_key.pem')) or not os.path.isfile(os.path.join( settings.BASE_DIR,'form_key_pub.pem')):
check_call(['openssl', 'genrsa', '-out', os.path.join(settings.BASE_DIR,'form_key.pem'), '4096'])
check_call(['openssl', 'rsa', '-pubout', '-in', os.path.join(settings.BASE_DIR,'form_key.pem'), '-out', os.path.join(settings.BASE_DIR,'form_key_pub.pem')])
f = open(os.path.join(settings.BASE_DIR,'form_key_pub.pem'))
key = f.read()
f.close()
return JsonResponse({"publickey": key})
Ok, and the handshake. To CSRF protect this view we need to patch the jCryption javascript library what i didn't.
I save AES key in session storage here.
@csrf_exempt
def handshake(req):
if req.method == 'POST':
encb64key = req.POST['key']
encb64key = re.sub(r'[^a-zA-Z0-9/=+]', '', encb64key)
enckey = b64decode(encb64key)
openssl = Popen(['openssl', 'rsautl', '-decrypt', '-inkey', os.path.join(settings.BASE_DIR,'form_key.pem')], stdin = PIPE, stdout=PIPE, stderr=PIPE)
key, stderr = openssl.communicate(enckey)
print stderr
key = re.sub(r'[^a-zA-Z0-9]', '', key)
req.session['form_key'] = key
openssl = Popen(['openssl', 'enc', '-aes-256-cbc', '-pass', 'pass:'+key, '-a', '-e'], stdin = PIPE, stdout = PIPE, stderr = PIPE)
enckey , stderr = openssl.communicate(key)
print stderr
enckey = re.sub('[^a-zA-Z0-9/+=]', '' , enckey)
return JsonResponse({'challenge': enckey})
raise Http404()
Lets choose urls for the views in urls.py
url('^pubkey', public_key, name = 'publickey'),
url('^handshake', handshake, name = 'handshake'),
And the most tricky part. Our own middleware. You need to add it to MIDDLEWARE_CLASSES in settings.py . Something like 'myapp.views.JCryptionMiddleware' if you place it in your myapp's views.py file.
The trick is that we send the wrong POST data with only 'jCryption' attr. The middleware decrypts apropriate data in this attr and rewrites the POST data in the request object with it.
Read about middlewares in Django documentation.
class JCryptionMiddleware(object):
def process_view(self, request, callback, callback_args, callback_kwargs):
jcryptedb64 = request.POST.get('jCryption', '')
if jcryptedb64:
try:
jcrypted = b64decode(jcryptedb64)
p = Popen(['openssl', 'enc', '-aes-256-cbc', '-pass', 'pass:'+request.session['form_key'], '-d'], stdin = PIPE, stdout = PIPE, stderr = PIPE)
qstr, stderr = p.communicate(jcrypted)
print stderr
wasmutable = request.POST._mutable
request.POST._mutable = True
request.POST.__init__(qstr)
request.POST._mutable = wasmutable
except Exception as e:
print e
return None
And the client code in the page with the form template.
<script src="{{ STATIC_URL }}js/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/jcryption.js"></script>
<script>
$(function() {
$('form').jCryption({"getKeysURL": "/pubkey", "handshakeURL": "/handshake"});
});
</script>
See the urls from our urls.py .
For example you can encrypt your admin login form. Copy login.html from django contrib admin to templates/admin/login.html and add this javascript code to the template.
ta da!
Don't use this, use HTTPS.