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