blob: 36e872974adcc289258e058578dd613714fb8e9e [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 Gaynorc37feed2014-03-08 08:32:56 -080014from __future__ import absolute_import, division, print_function
15
Alex Gaynor2b21b122013-10-31 09:39:25 -070016import base64
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070017import calendar
18import json
19import os
Alex Gaynor1d2901c2013-11-22 10:12:05 -080020import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070021
22import iso8601
23
24import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070025
Alex Gaynorde36e902013-10-31 10:10:44 -070026import six
27
Alex Gaynor38f34552013-10-31 14:50:00 -070028from cryptography.fernet import Fernet, InvalidToken
Alex Gaynorfae20712013-12-16 15:29:30 -080029from cryptography.hazmat.backends import default_backend
Paul Kehrer0abdf872014-01-09 22:21:14 -060030from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070031
32
Alex Gaynor38f34552013-10-31 14:50:00 -070033def json_parametrize(keys, fname):
34 path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070035 with open(path) as f:
36 data = json.load(f)
37 return pytest.mark.parametrize(keys, [
38 tuple([entry[k] for k in keys])
39 for entry in data
40 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070041
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070042
Paul Kehrer0abdf872014-01-09 22:21:14 -060043@pytest.mark.cipher
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070044class TestFernet(object):
Paul Kehrer0abdf872014-01-09 22:21:14 -060045 @pytest.mark.supported(
46 only_if=lambda backend: backend.cipher_supported(
47 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
48 ),
49 skip_message="Does not support AES CBC",
50 )
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070051 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070052 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070053 )
Alex Gaynorfae20712013-12-16 15:29:30 -080054 def test_generate(self, secret, now, iv, src, token, backend):
55 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070056 actual_token = f._encrypt_from_parts(
57 src.encode("ascii"),
58 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
59 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070060 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070061 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070062
Paul Kehrer0abdf872014-01-09 22:21:14 -060063 @pytest.mark.supported(
64 only_if=lambda backend: backend.cipher_supported(
65 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
66 ),
67 skip_message="Does not support AES CBC",
68 )
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070069 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070070 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070071 )
Alex Gaynorfae20712013-12-16 15:29:30 -080072 def test_verify(self, secret, now, src, ttl_sec, token, backend,
73 monkeypatch):
74 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070075 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080076 monkeypatch.setattr(time, "time", lambda: current_time)
77 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070078 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070079
Paul Kehrer0abdf872014-01-09 22:21:14 -060080 @pytest.mark.supported(
81 only_if=lambda backend: backend.cipher_supported(
82 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
83 ),
84 skip_message="Does not support AES CBC",
85 )
Alex Gaynor38f34552013-10-31 14:50:00 -070086 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080087 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
88 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070089 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080090 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070091 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080092 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070093
Paul Kehrer0abdf872014-01-09 22:21:14 -060094 @pytest.mark.supported(
95 only_if=lambda backend: backend.cipher_supported(
96 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
97 ),
98 skip_message="Does not support AES CBC",
99 )
Alex Gaynora8f0b632013-12-16 15:44:06 -0800100 def test_invalid_start_byte(self, backend):
101 f = Fernet(Fernet.generate_key(), backend=backend)
102 with pytest.raises(InvalidToken):
103 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
104
Paul Kehrer0abdf872014-01-09 22:21:14 -0600105 @pytest.mark.supported(
106 only_if=lambda backend: backend.cipher_supported(
107 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
108 ),
109 skip_message="Does not support AES CBC",
110 )
Alex Gaynore78960f2013-12-20 11:02:33 -0800111 def test_timestamp_too_short(self, backend):
112 f = Fernet(Fernet.generate_key(), backend=backend)
113 with pytest.raises(InvalidToken):
114 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
115
Paul Kehrer0abdf872014-01-09 22:21:14 -0600116 @pytest.mark.supported(
117 only_if=lambda backend: backend.cipher_supported(
118 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
119 ),
120 skip_message="Does not support AES CBC",
121 )
Alex Gaynorfae20712013-12-16 15:29:30 -0800122 def test_unicode(self, backend):
123 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700124 with pytest.raises(TypeError):
125 f.encrypt(six.u(""))
126 with pytest.raises(TypeError):
127 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700128
Paul Kehrer0abdf872014-01-09 22:21:14 -0600129 @pytest.mark.supported(
130 only_if=lambda backend: backend.cipher_supported(
131 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
132 ),
133 skip_message="Does not support AES CBC",
134 )
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700135 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800136 def test_roundtrips(self, message, backend):
137 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700138 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800139
140 def test_default_backend(self):
141 f = Fernet(Fernet.generate_key())
142 assert f._backend is default_backend()
Alex Gaynora8f0b632013-12-16 15:44:06 -0800143
Paul Kehrer0abdf872014-01-09 22:21:14 -0600144 @pytest.mark.supported(
145 only_if=lambda backend: backend.cipher_supported(
146 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
147 ),
148 skip_message="Does not support AES CBC",
149 )
Alex Gaynora8f0b632013-12-16 15:44:06 -0800150 def test_bad_key(self, backend):
151 with pytest.raises(ValueError):
152 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)