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