blob: bd4d90a58aa2c2938131181ccf0615dfc2d8c770 [file] [log] [blame]
Alex Gaynor8912d3a2013-11-02 14:04:19 -07001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
Alex Gaynor2b21b122013-10-31 09:39:25 -070014import base64
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070015import calendar
16import json
17import os
Alex Gaynor1d2901c2013-11-22 10:12:05 -080018import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070019
20import iso8601
21
22import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070023
Alex Gaynorde36e902013-10-31 10:10:44 -070024import six
25
Alex Gaynor38f34552013-10-31 14:50:00 -070026from cryptography.fernet import Fernet, InvalidToken
Alex Gaynorfae20712013-12-16 15:29:30 -080027from cryptography.hazmat.backends import default_backend
Paul Kehrer0abdf872014-01-09 22:21:14 -060028from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070029
30
Alex Gaynor38f34552013-10-31 14:50:00 -070031def json_parametrize(keys, fname):
32 path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070033 with open(path) as f:
34 data = json.load(f)
35 return pytest.mark.parametrize(keys, [
36 tuple([entry[k] for k in keys])
37 for entry in data
38 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070039
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070040
Paul Kehrer0abdf872014-01-09 22:21:14 -060041@pytest.mark.cipher
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070042class TestFernet(object):
Paul Kehrer0abdf872014-01-09 22:21:14 -060043 @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 -070049 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070050 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070051 )
Alex Gaynorfae20712013-12-16 15:29:30 -080052 def test_generate(self, secret, now, iv, src, token, backend):
53 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070054 actual_token = f._encrypt_from_parts(
55 src.encode("ascii"),
56 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
57 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070058 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070059 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070060
Paul Kehrer0abdf872014-01-09 22:21:14 -060061 @pytest.mark.supported(
62 only_if=lambda backend: backend.cipher_supported(
63 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
64 ),
65 skip_message="Does not support AES CBC",
66 )
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070067 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070068 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070069 )
Alex Gaynorfae20712013-12-16 15:29:30 -080070 def test_verify(self, secret, now, src, ttl_sec, token, backend,
71 monkeypatch):
72 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070073 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080074 monkeypatch.setattr(time, "time", lambda: current_time)
75 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070076 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070077
Paul Kehrer0abdf872014-01-09 22:21:14 -060078 @pytest.mark.supported(
79 only_if=lambda backend: backend.cipher_supported(
80 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
81 ),
82 skip_message="Does not support AES CBC",
83 )
Alex Gaynor38f34552013-10-31 14:50:00 -070084 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080085 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
86 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070087 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080088 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070089 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080090 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070091
Paul Kehrer0abdf872014-01-09 22:21:14 -060092 @pytest.mark.supported(
93 only_if=lambda backend: backend.cipher_supported(
94 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
95 ),
96 skip_message="Does not support AES CBC",
97 )
Alex Gaynora8f0b632013-12-16 15:44:06 -080098 def test_invalid_start_byte(self, backend):
99 f = Fernet(Fernet.generate_key(), backend=backend)
100 with pytest.raises(InvalidToken):
101 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
102
Paul Kehrer0abdf872014-01-09 22:21:14 -0600103 @pytest.mark.supported(
104 only_if=lambda backend: backend.cipher_supported(
105 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
106 ),
107 skip_message="Does not support AES CBC",
108 )
Alex Gaynore78960f2013-12-20 11:02:33 -0800109 def test_timestamp_too_short(self, backend):
110 f = Fernet(Fernet.generate_key(), backend=backend)
111 with pytest.raises(InvalidToken):
112 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
113
Paul Kehrer0abdf872014-01-09 22:21:14 -0600114 @pytest.mark.supported(
115 only_if=lambda backend: backend.cipher_supported(
116 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
117 ),
118 skip_message="Does not support AES CBC",
119 )
Alex Gaynorfae20712013-12-16 15:29:30 -0800120 def test_unicode(self, backend):
121 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700122 with pytest.raises(TypeError):
123 f.encrypt(six.u(""))
124 with pytest.raises(TypeError):
125 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700126
Paul Kehrer0abdf872014-01-09 22:21:14 -0600127 @pytest.mark.supported(
128 only_if=lambda backend: backend.cipher_supported(
129 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
130 ),
131 skip_message="Does not support AES CBC",
132 )
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700133 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800134 def test_roundtrips(self, message, backend):
135 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700136 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800137
138 def test_default_backend(self):
139 f = Fernet(Fernet.generate_key())
140 assert f._backend is default_backend()
Alex Gaynora8f0b632013-12-16 15:44:06 -0800141
Paul Kehrer0abdf872014-01-09 22:21:14 -0600142 @pytest.mark.supported(
143 only_if=lambda backend: backend.cipher_supported(
144 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
145 ),
146 skip_message="Does not support AES CBC",
147 )
Alex Gaynora8f0b632013-12-16 15:44:06 -0800148 def test_bad_key(self, backend):
149 with pytest.raises(ValueError):
150 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)