Alex Gaynor | 8912d3a | 2013-11-02 14:04:19 -0700 | [diff] [blame] | 1 | # 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 Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 16 | import base64 |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 17 | import calendar |
| 18 | import json |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 19 | import time |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 20 | |
| 21 | import iso8601 |
| 22 | |
| 23 | import pytest |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 24 | |
Alex Gaynor | de36e90 | 2013-10-31 10:10:44 -0700 | [diff] [blame] | 25 | import six |
| 26 | |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 27 | from cryptography.fernet import Fernet, InvalidToken |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 28 | from cryptography.hazmat.backends import default_backend |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 29 | from cryptography.hazmat.primitives.ciphers import algorithms, modes |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 30 | |
| 31 | |
Matthew Iversen | 68e77c7 | 2014-03-13 08:54:43 +1100 | [diff] [blame^] | 32 | def json_parametrize(keys, filename): |
| 33 | from cryptography import vectors |
| 34 | vector_file = vectors.open_vector_file('fernet', filename) |
| 35 | data = json.load(vector_file) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 36 | return pytest.mark.parametrize(keys, [ |
| 37 | tuple([entry[k] for k in keys]) |
| 38 | for entry in data |
| 39 | ]) |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 40 | |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 41 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 42 | @pytest.mark.cipher |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 43 | class TestFernet(object): |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 44 | @pytest.mark.supported( |
| 45 | only_if=lambda backend: backend.cipher_supported( |
| 46 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 47 | ), |
| 48 | skip_message="Does not support AES CBC", |
| 49 | ) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 50 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 51 | ("secret", "now", "iv", "src", "token"), "generate.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 52 | ) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 53 | def test_generate(self, secret, now, iv, src, token, backend): |
| 54 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 55 | actual_token = f._encrypt_from_parts( |
| 56 | src.encode("ascii"), |
| 57 | calendar.timegm(iso8601.parse_date(now).utctimetuple()), |
| 58 | b"".join(map(six.int2byte, iv)) |
Alex Gaynor | 5e87dfd | 2013-10-31 09:46:03 -0700 | [diff] [blame] | 59 | ) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 60 | assert actual_token == token.encode("ascii") |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 61 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 62 | @pytest.mark.supported( |
| 63 | only_if=lambda backend: backend.cipher_supported( |
| 64 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 65 | ), |
| 66 | skip_message="Does not support AES CBC", |
| 67 | ) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 68 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 69 | ("secret", "now", "src", "ttl_sec", "token"), "verify.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 70 | ) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 71 | def test_verify(self, secret, now, src, ttl_sec, token, backend, |
| 72 | monkeypatch): |
| 73 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 74 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 75 | monkeypatch.setattr(time, "time", lambda: current_time) |
| 76 | payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 77 | assert payload == src.encode("ascii") |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 78 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 79 | @pytest.mark.supported( |
| 80 | only_if=lambda backend: backend.cipher_supported( |
| 81 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 82 | ), |
| 83 | skip_message="Does not support AES CBC", |
| 84 | ) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 85 | @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json") |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 86 | def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch): |
| 87 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 88 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 89 | monkeypatch.setattr(time, "time", lambda: current_time) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 90 | with pytest.raises(InvalidToken): |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 91 | f.decrypt(token.encode("ascii"), ttl=ttl_sec) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 92 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 93 | @pytest.mark.supported( |
| 94 | only_if=lambda backend: backend.cipher_supported( |
| 95 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 96 | ), |
| 97 | skip_message="Does not support AES CBC", |
| 98 | ) |
Alex Gaynor | a8f0b63 | 2013-12-16 15:44:06 -0800 | [diff] [blame] | 99 | def test_invalid_start_byte(self, backend): |
| 100 | f = Fernet(Fernet.generate_key(), backend=backend) |
| 101 | with pytest.raises(InvalidToken): |
| 102 | f.decrypt(base64.urlsafe_b64encode(b"\x81")) |
| 103 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 104 | @pytest.mark.supported( |
| 105 | only_if=lambda backend: backend.cipher_supported( |
| 106 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 107 | ), |
| 108 | skip_message="Does not support AES CBC", |
| 109 | ) |
Alex Gaynor | e78960f | 2013-12-20 11:02:33 -0800 | [diff] [blame] | 110 | def test_timestamp_too_short(self, backend): |
| 111 | f = Fernet(Fernet.generate_key(), backend=backend) |
| 112 | with pytest.raises(InvalidToken): |
| 113 | f.decrypt(base64.urlsafe_b64encode(b"\x80abc")) |
| 114 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 115 | @pytest.mark.supported( |
| 116 | only_if=lambda backend: backend.cipher_supported( |
| 117 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 118 | ), |
| 119 | skip_message="Does not support AES CBC", |
| 120 | ) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 121 | def test_unicode(self, backend): |
| 122 | f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 123 | with pytest.raises(TypeError): |
| 124 | f.encrypt(six.u("")) |
| 125 | with pytest.raises(TypeError): |
| 126 | f.decrypt(six.u("")) |
Alex Gaynor | ce8f9a4 | 2013-10-31 15:23:15 -0700 | [diff] [blame] | 127 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 128 | @pytest.mark.supported( |
| 129 | only_if=lambda backend: backend.cipher_supported( |
| 130 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 131 | ), |
| 132 | skip_message="Does not support AES CBC", |
| 133 | ) |
Alex Gaynor | ce8f9a4 | 2013-10-31 15:23:15 -0700 | [diff] [blame] | 134 | @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"]) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 135 | def test_roundtrips(self, message, backend): |
| 136 | f = Fernet(Fernet.generate_key(), backend=backend) |
Alex Gaynor | 6b9770b | 2013-10-31 16:07:35 -0700 | [diff] [blame] | 137 | assert f.decrypt(f.encrypt(message)) == message |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 138 | |
| 139 | def test_default_backend(self): |
| 140 | f = Fernet(Fernet.generate_key()) |
| 141 | assert f._backend is default_backend() |
Alex Gaynor | a8f0b63 | 2013-12-16 15:44:06 -0800 | [diff] [blame] | 142 | |
Paul Kehrer | 0abdf87 | 2014-01-09 22:21:14 -0600 | [diff] [blame] | 143 | @pytest.mark.supported( |
| 144 | only_if=lambda backend: backend.cipher_supported( |
| 145 | algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16) |
| 146 | ), |
| 147 | skip_message="Does not support AES CBC", |
| 148 | ) |
Alex Gaynor | a8f0b63 | 2013-12-16 15:44:06 -0800 | [diff] [blame] | 149 | def test_bad_key(self, backend): |
| 150 | with pytest.raises(ValueError): |
| 151 | Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend) |