blob: 34760e3cacfa54ffc9ff760989d36a6c688cad11 [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
Alex Gaynor1d2901c2013-11-22 10:12:05 -080019import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070020
21import iso8601
22
23import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070024
Alex Gaynorde36e902013-10-31 10:10:44 -070025import six
26
Alex Gaynor38f34552013-10-31 14:50:00 -070027from cryptography.fernet import Fernet, InvalidToken
Alex Gaynorfae20712013-12-16 15:29:30 -080028from cryptography.hazmat.backends import default_backend
Paul Kehrer0abdf872014-01-09 22:21:14 -060029from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070030
Alex Stapletona39a3192014-03-14 20:03:12 +000031import cryptography_vectors
32
Alex Gaynor2b21b122013-10-31 09:39:25 -070033
Matthew Iversen68e77c72014-03-13 08:54:43 +110034def json_parametrize(keys, filename):
Alex Stapletona39a3192014-03-14 20:03:12 +000035 vector_file = cryptography_vectors.open_vector_file('fernet', filename)
36 with vector_file:
37 data = json.load(vector_file)
38 return pytest.mark.parametrize(keys, [
39 tuple([entry[k] for k in keys])
40 for entry in data
41 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070042
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070043
Paul Kehrer0abdf872014-01-09 22:21:14 -060044@pytest.mark.cipher
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070045class TestFernet(object):
Paul Kehrer0abdf872014-01-09 22:21:14 -060046 @pytest.mark.supported(
47 only_if=lambda backend: backend.cipher_supported(
48 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
49 ),
50 skip_message="Does not support AES CBC",
51 )
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070052 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070053 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070054 )
Alex Gaynorfae20712013-12-16 15:29:30 -080055 def test_generate(self, secret, now, iv, src, token, backend):
56 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070057 actual_token = f._encrypt_from_parts(
58 src.encode("ascii"),
59 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
60 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070061 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070062 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070063
Paul Kehrer0abdf872014-01-09 22:21:14 -060064 @pytest.mark.supported(
65 only_if=lambda backend: backend.cipher_supported(
66 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
67 ),
68 skip_message="Does not support AES CBC",
69 )
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070070 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070071 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070072 )
Alex Gaynorfae20712013-12-16 15:29:30 -080073 def test_verify(self, secret, now, src, ttl_sec, token, backend,
74 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)
78 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070079 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070080
Paul Kehrer0abdf872014-01-09 22:21:14 -060081 @pytest.mark.supported(
82 only_if=lambda backend: backend.cipher_supported(
83 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
84 ),
85 skip_message="Does not support AES CBC",
86 )
Alex Gaynor38f34552013-10-31 14:50:00 -070087 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080088 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
89 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070090 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080091 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070092 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080093 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070094
Paul Kehrer0abdf872014-01-09 22:21:14 -060095 @pytest.mark.supported(
96 only_if=lambda backend: backend.cipher_supported(
97 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
98 ),
99 skip_message="Does not support AES CBC",
100 )
Alex Gaynora8f0b632013-12-16 15:44:06 -0800101 def test_invalid_start_byte(self, backend):
102 f = Fernet(Fernet.generate_key(), backend=backend)
103 with pytest.raises(InvalidToken):
104 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
105
Paul Kehrer0abdf872014-01-09 22:21:14 -0600106 @pytest.mark.supported(
107 only_if=lambda backend: backend.cipher_supported(
108 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
109 ),
110 skip_message="Does not support AES CBC",
111 )
Alex Gaynore78960f2013-12-20 11:02:33 -0800112 def test_timestamp_too_short(self, backend):
113 f = Fernet(Fernet.generate_key(), backend=backend)
114 with pytest.raises(InvalidToken):
115 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
116
Paul Kehrer0abdf872014-01-09 22:21:14 -0600117 @pytest.mark.supported(
118 only_if=lambda backend: backend.cipher_supported(
119 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
120 ),
121 skip_message="Does not support AES CBC",
122 )
Alex Gaynorfae20712013-12-16 15:29:30 -0800123 def test_unicode(self, backend):
124 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700125 with pytest.raises(TypeError):
126 f.encrypt(six.u(""))
127 with pytest.raises(TypeError):
128 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700129
Paul Kehrer0abdf872014-01-09 22:21:14 -0600130 @pytest.mark.supported(
131 only_if=lambda backend: backend.cipher_supported(
132 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
133 ),
134 skip_message="Does not support AES CBC",
135 )
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700136 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800137 def test_roundtrips(self, message, backend):
138 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700139 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800140
141 def test_default_backend(self):
142 f = Fernet(Fernet.generate_key())
143 assert f._backend is default_backend()
Alex Gaynora8f0b632013-12-16 15:44:06 -0800144
Paul Kehrer0abdf872014-01-09 22:21:14 -0600145 @pytest.mark.supported(
146 only_if=lambda backend: backend.cipher_supported(
147 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
148 ),
149 skip_message="Does not support AES CBC",
150 )
Alex Gaynora8f0b632013-12-16 15:44:06 -0800151 def test_bad_key(self, backend):
152 with pytest.raises(ValueError):
153 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)