blob: dbce44fbea8adfe684f1f182a5cfaf72cd8241b0 [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
Paul Kehrerfdae0702014-11-27 07:50:46 -100010import os
Alex Gaynor1d2901c2013-11-22 10:12:05 -080011import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070012
13import iso8601
14
15import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070016
Alex Gaynorde36e902013-10-31 10:10:44 -070017import six
18
Alex Gaynor7b593e12014-10-19 19:09:44 -070019from cryptography.fernet import Fernet, InvalidToken, MultiFernet
Alex Gaynorfae20712013-12-16 15:29:30 -080020from cryptography.hazmat.backends import default_backend
Alex Gaynore6ac6022014-10-24 07:50:30 -070021from cryptography.hazmat.backends.interfaces import CipherBackend, HMACBackend
Paul Kehrer0abdf872014-01-09 22:21:14 -060022from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070023
Alex Stapletona39a3192014-03-14 20:03:12 +000024import cryptography_vectors
25
Alex Gaynor2b21b122013-10-31 09:39:25 -070026
Matthew Iversen68e77c72014-03-13 08:54:43 +110027def json_parametrize(keys, filename):
Paul Kehrerfdae0702014-11-27 07:50:46 -100028 vector_file = cryptography_vectors.open_vector_file(
29 os.path.join('fernet', filename), "r"
30 )
Alex Stapletona39a3192014-03-14 20:03:12 +000031 with vector_file:
32 data = json.load(vector_file)
33 return pytest.mark.parametrize(keys, [
34 tuple([entry[k] for k in keys])
35 for entry in data
36 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070037
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070038
Alex Gaynor732fbec2014-10-19 18:46:55 -070039def test_default_backend():
40 f = Fernet(Fernet.generate_key())
41 assert f._backend is default_backend()
42
43
Alex Gaynor7aab8b42014-10-23 11:01:25 -070044@pytest.mark.requires_backend_interface(interface=CipherBackend)
Alex Gaynore6ac6022014-10-24 07:50:30 -070045@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor732fbec2014-10-19 18:46:55 -070046@pytest.mark.supported(
47 only_if=lambda backend: backend.cipher_supported(
Paul Kehrer4fc597d2016-03-07 08:41:51 -043048 algorithms.AES(b"\x00" * 32), modes.CBC(b"\x00" * 16)
Alex Gaynor732fbec2014-10-19 18:46:55 -070049 ),
50 skip_message="Does not support AES CBC",
51)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070052class TestFernet(object):
53 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070054 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070055 )
Alex Gaynorfae20712013-12-16 15:29:30 -080056 def test_generate(self, secret, now, iv, src, token, backend):
57 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070058 actual_token = f._encrypt_from_parts(
59 src.encode("ascii"),
60 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
61 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070062 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070063 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070064
65 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070066 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070067 )
Alex Gaynorfae20712013-12-16 15:29:30 -080068 def test_verify(self, secret, now, src, ttl_sec, token, backend,
69 monkeypatch):
70 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070071 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080072 monkeypatch.setattr(time, "time", lambda: current_time)
73 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070074 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070075
76 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080077 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
78 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070079 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080080 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070081 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080082 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070083
Alex Gaynora8f0b632013-12-16 15:44:06 -080084 def test_invalid_start_byte(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070085 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynora8f0b632013-12-16 15:44:06 -080086 with pytest.raises(InvalidToken):
87 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
88
Alex Gaynore78960f2013-12-20 11:02:33 -080089 def test_timestamp_too_short(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070090 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynore78960f2013-12-20 11:02:33 -080091 with pytest.raises(InvalidToken):
92 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
93
Alex Gaynora1a21f42014-10-19 19:11:30 -070094 def test_non_base64_token(self, backend):
95 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
96 with pytest.raises(InvalidToken):
97 f.decrypt(b"\x00")
98
Alex Gaynorfae20712013-12-16 15:29:30 -080099 def test_unicode(self, backend):
100 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700101 with pytest.raises(TypeError):
Eeshan Gargf1234152015-04-29 18:41:00 +0530102 f.encrypt(u"")
Alex Gaynor38f34552013-10-31 14:50:00 -0700103 with pytest.raises(TypeError):
Eeshan Gargf1234152015-04-29 18:41:00 +0530104 f.decrypt(u"")
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700105
Paul Kehrera418e962016-01-21 08:54:59 -0600106 def test_timestamp_ignored_no_ttl(self, monkeypatch, backend):
107 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
108 pt = b"encrypt me"
109 token = f.encrypt(pt)
110 ts = "1985-10-26T01:20:01-07:00"
111 current_time = calendar.timegm(iso8601.parse_date(ts).utctimetuple())
112 monkeypatch.setattr(time, "time", lambda: current_time)
113 assert f.decrypt(token, ttl=None) == pt
114
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700115 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800116 def test_roundtrips(self, message, backend):
117 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700118 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800119
Alex Gaynora8f0b632013-12-16 15:44:06 -0800120 def test_bad_key(self, backend):
121 with pytest.raises(ValueError):
122 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700123
124
Alex Gaynore6ac6022014-10-24 07:50:30 -0700125@pytest.mark.requires_backend_interface(interface=CipherBackend)
126@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700127@pytest.mark.supported(
128 only_if=lambda backend: backend.cipher_supported(
Paul Kehrer4fc597d2016-03-07 08:41:51 -0430129 algorithms.AES(b"\x00" * 32), modes.CBC(b"\x00" * 16)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700130 ),
131 skip_message="Does not support AES CBC",
132)
133class TestMultiFernet(object):
134 def test_encrypt(self, backend):
Alex Gaynor41b33b72014-10-20 14:34:35 -0700135 f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
136 f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
137 f = MultiFernet([f1, f2])
138
139 assert f1.decrypt(f.encrypt(b"abc")) == b"abc"
Alex Gaynor7b593e12014-10-19 19:09:44 -0700140
141 def test_decrypt(self, backend):
142 f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor41b33b72014-10-20 14:34:35 -0700143 f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700144 f = MultiFernet([f1, f2])
145
146 assert f.decrypt(f1.encrypt(b"abc")) == b"abc"
147 assert f.decrypt(f2.encrypt(b"abc")) == b"abc"
148
149 with pytest.raises(InvalidToken):
150 f.decrypt(b"\x00" * 16)
151
152 def test_no_fernets(self, backend):
153 with pytest.raises(ValueError):
154 MultiFernet([])
Alex Gaynor4f286ce2014-10-20 11:30:57 -0700155
156 def test_non_iterable_argument(self, backend):
157 with pytest.raises(TypeError):
158 MultiFernet(None)