blob: 0b4e3e8712706fbd6df8adf6324cdad874859747 [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
Alex Gaynor732fbec2014-10-19 18:46:55 -070044def test_default_backend():
45 f = Fernet(Fernet.generate_key())
46 assert f._backend is default_backend()
47
48
Paul Kehrer0abdf872014-01-09 22:21:14 -060049@pytest.mark.cipher
Alex Gaynor732fbec2014-10-19 18:46:55 -070050@pytest.mark.supported(
51 only_if=lambda backend: backend.cipher_supported(
52 algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
53 ),
54 skip_message="Does not support AES CBC",
55)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070056class TestFernet(object):
57 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070058 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070059 )
Alex Gaynorfae20712013-12-16 15:29:30 -080060 def test_generate(self, secret, now, iv, src, token, backend):
61 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070062 actual_token = f._encrypt_from_parts(
63 src.encode("ascii"),
64 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
65 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070066 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070067 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070068
69 @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
80 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080081 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
82 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070083 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080084 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070085 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080086 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070087
Alex Gaynora8f0b632013-12-16 15:44:06 -080088 def test_invalid_start_byte(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070089 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynora8f0b632013-12-16 15:44:06 -080090 with pytest.raises(InvalidToken):
91 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
92
Alex Gaynore78960f2013-12-20 11:02:33 -080093 def test_timestamp_too_short(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070094 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynore78960f2013-12-20 11:02:33 -080095 with pytest.raises(InvalidToken):
96 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
97
Alex Gaynora1a21f42014-10-19 19:11:30 -070098 def test_non_base64_token(self, backend):
99 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
100 with pytest.raises(InvalidToken):
101 f.decrypt(b"\x00")
102
Alex Gaynorfae20712013-12-16 15:29:30 -0800103 def test_unicode(self, backend):
104 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700105 with pytest.raises(TypeError):
106 f.encrypt(six.u(""))
107 with pytest.raises(TypeError):
108 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700109
110 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800111 def test_roundtrips(self, message, backend):
112 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700113 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800114
Alex Gaynora8f0b632013-12-16 15:44:06 -0800115 def test_bad_key(self, backend):
116 with pytest.raises(ValueError):
117 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)