blob: 455d985cab51e8548d46d42b72c48f1f5271f10d [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
Alex Gaynor8912d3a2013-11-02 14:04:19 -07004
Alex Gaynorc37feed2014-03-08 08:32:56 -08005from __future__ import absolute_import, division, print_function
6
Alex Gaynor2b21b122013-10-31 09:39:25 -07007import base64
Alex Gaynorfb8adfc2013-10-31 14:16:24 -07008import calendar
9import json
Alex Gaynor1d2901c2013-11-22 10:12:05 -080010import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070011
12import iso8601
13
14import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070015
Alex Gaynorde36e902013-10-31 10:10:44 -070016import six
17
Alex Gaynor7b593e12014-10-19 19:09:44 -070018from cryptography.fernet import Fernet, InvalidToken, MultiFernet
Alex Gaynorfae20712013-12-16 15:29:30 -080019from cryptography.hazmat.backends import default_backend
Alex Gaynore6ac6022014-10-24 07:50:30 -070020from cryptography.hazmat.backends.interfaces import CipherBackend, HMACBackend
Paul Kehrer0abdf872014-01-09 22:21:14 -060021from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070022
Alex Stapletona39a3192014-03-14 20:03:12 +000023import cryptography_vectors
24
Alex Gaynor2b21b122013-10-31 09:39:25 -070025
Matthew Iversen68e77c72014-03-13 08:54:43 +110026def json_parametrize(keys, filename):
Alex Stapletona39a3192014-03-14 20:03:12 +000027 vector_file = cryptography_vectors.open_vector_file('fernet', filename)
28 with vector_file:
29 data = json.load(vector_file)
30 return pytest.mark.parametrize(keys, [
31 tuple([entry[k] for k in keys])
32 for entry in data
33 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070034
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070035
Alex Gaynor732fbec2014-10-19 18:46:55 -070036def test_default_backend():
37 f = Fernet(Fernet.generate_key())
38 assert f._backend is default_backend()
39
40
Alex Gaynor7aab8b42014-10-23 11:01:25 -070041@pytest.mark.requires_backend_interface(interface=CipherBackend)
Alex Gaynore6ac6022014-10-24 07:50:30 -070042@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor732fbec2014-10-19 18:46:55 -070043@pytest.mark.supported(
44 only_if=lambda backend: backend.cipher_supported(
45 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
46 ),
47 skip_message="Does not support AES CBC",
48)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070049class TestFernet(object):
50 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070051 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070052 )
Alex Gaynorfae20712013-12-16 15:29:30 -080053 def test_generate(self, secret, now, iv, src, token, backend):
54 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070055 actual_token = f._encrypt_from_parts(
56 src.encode("ascii"),
57 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
58 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070059 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070060 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070061
62 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070063 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070064 )
Alex Gaynorfae20712013-12-16 15:29:30 -080065 def test_verify(self, secret, now, src, ttl_sec, token, backend,
66 monkeypatch):
67 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070068 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080069 monkeypatch.setattr(time, "time", lambda: current_time)
70 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070071 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070072
73 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080074 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
75 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070076 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080077 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070078 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080079 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070080
Alex Gaynora8f0b632013-12-16 15:44:06 -080081 def test_invalid_start_byte(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070082 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynora8f0b632013-12-16 15:44:06 -080083 with pytest.raises(InvalidToken):
84 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
85
Alex Gaynore78960f2013-12-20 11:02:33 -080086 def test_timestamp_too_short(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070087 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynore78960f2013-12-20 11:02:33 -080088 with pytest.raises(InvalidToken):
89 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
90
Alex Gaynora1a21f42014-10-19 19:11:30 -070091 def test_non_base64_token(self, backend):
92 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
93 with pytest.raises(InvalidToken):
94 f.decrypt(b"\x00")
95
Alex Gaynorfae20712013-12-16 15:29:30 -080096 def test_unicode(self, backend):
97 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -070098 with pytest.raises(TypeError):
99 f.encrypt(six.u(""))
100 with pytest.raises(TypeError):
101 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700102
103 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800104 def test_roundtrips(self, message, backend):
105 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700106 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800107
Alex Gaynora8f0b632013-12-16 15:44:06 -0800108 def test_bad_key(self, backend):
109 with pytest.raises(ValueError):
110 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700111
112
Alex Gaynore6ac6022014-10-24 07:50:30 -0700113@pytest.mark.requires_backend_interface(interface=CipherBackend)
114@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700115@pytest.mark.supported(
116 only_if=lambda backend: backend.cipher_supported(
117 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
118 ),
119 skip_message="Does not support AES CBC",
120)
121class TestMultiFernet(object):
122 def test_encrypt(self, backend):
Alex Gaynor41b33b72014-10-20 14:34:35 -0700123 f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
124 f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
125 f = MultiFernet([f1, f2])
126
127 assert f1.decrypt(f.encrypt(b"abc")) == b"abc"
Alex Gaynor7b593e12014-10-19 19:09:44 -0700128
129 def test_decrypt(self, backend):
130 f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor41b33b72014-10-20 14:34:35 -0700131 f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700132 f = MultiFernet([f1, f2])
133
134 assert f.decrypt(f1.encrypt(b"abc")) == b"abc"
135 assert f.decrypt(f2.encrypt(b"abc")) == b"abc"
136
137 with pytest.raises(InvalidToken):
138 f.decrypt(b"\x00" * 16)
139
140 def test_no_fernets(self, backend):
141 with pytest.raises(ValueError):
142 MultiFernet([])
Alex Gaynor4f286ce2014-10-20 11:30:57 -0700143
144 def test_non_iterable_argument(self, backend):
145 with pytest.raises(TypeError):
146 MultiFernet(None)