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