blob: 5b7a9f98964a1e883a9986e40060e1f81d43d3c8 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# 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 Gaynor8912d3a2013-11-02 14:04:19 -07004
Alex Gaynorc37feed2014-03-08 08:32:56 -08005from __future__ import absolute_import, division, print_function
6
Alex Gaynor2b21b122013-10-31 09:39:25 -07007import base64
Alex Gaynorfb8adfc2013-10-31 14:16:24 -07008import calendar
9import json
Paul Kehrerfdae0702014-11-27 07:50:46 -100010import os
Alex Gaynor1d2901c2013-11-22 10:12:05 -080011import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070012
13import iso8601
14
15import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070016
Alex Gaynorde36e902013-10-31 10:10:44 -070017import six
18
Alex Gaynor7b593e12014-10-19 19:09:44 -070019from cryptography.fernet import Fernet, InvalidToken, MultiFernet
Alex Gaynorfae20712013-12-16 15:29:30 -080020from cryptography.hazmat.backends import default_backend
Alex Gaynore6ac6022014-10-24 07:50:30 -070021from cryptography.hazmat.backends.interfaces import CipherBackend, HMACBackend
Paul Kehrer0abdf872014-01-09 22:21:14 -060022from cryptography.hazmat.primitives.ciphers import algorithms, modes
Alex Gaynor2b21b122013-10-31 09:39:25 -070023
Alex Stapletona39a3192014-03-14 20:03:12 +000024import cryptography_vectors
25
Alex Gaynor2b21b122013-10-31 09:39:25 -070026
Matthew Iversen68e77c72014-03-13 08:54:43 +110027def json_parametrize(keys, filename):
Paul Kehrerfdae0702014-11-27 07:50:46 -100028 vector_file = cryptography_vectors.open_vector_file(
29 os.path.join('fernet', filename), "r"
30 )
Alex Stapletona39a3192014-03-14 20:03:12 +000031 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 Gaynor2b21b122013-10-31 09:39:25 -070037
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070038
Alex Gaynor732fbec2014-10-19 18:46:55 -070039def test_default_backend():
40 f = Fernet(Fernet.generate_key())
41 assert f._backend is default_backend()
42
43
Alex Gaynor7aab8b42014-10-23 11:01:25 -070044@pytest.mark.requires_backend_interface(interface=CipherBackend)
Alex Gaynore6ac6022014-10-24 07:50:30 -070045@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor732fbec2014-10-19 18:46:55 -070046@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 Gaynorfb8adfc2013-10-31 14:16:24 -070052class TestFernet(object):
53 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070054 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070055 )
Alex Gaynorfae20712013-12-16 15:29:30 -080056 def test_generate(self, secret, now, iv, src, token, backend):
57 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070058 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 Gaynor5e87dfd2013-10-31 09:46:03 -070062 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070063 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070064
65 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070066 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070067 )
Alex Gaynorfae20712013-12-16 15:29:30 -080068 def test_verify(self, secret, now, src, ttl_sec, token, backend,
69 monkeypatch):
70 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070071 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080072 monkeypatch.setattr(time, "time", lambda: current_time)
73 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070074 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070075
76 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080077 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
78 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070079 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080080 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070081 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080082 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070083
Alex Gaynora8f0b632013-12-16 15:44:06 -080084 def test_invalid_start_byte(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070085 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynora8f0b632013-12-16 15:44:06 -080086 with pytest.raises(InvalidToken):
87 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
88
Alex Gaynore78960f2013-12-20 11:02:33 -080089 def test_timestamp_too_short(self, backend):
Alex Gaynora1a21f42014-10-19 19:11:30 -070090 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynore78960f2013-12-20 11:02:33 -080091 with pytest.raises(InvalidToken):
92 f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
93
Alex Gaynora1a21f42014-10-19 19:11:30 -070094 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 Gaynorfae20712013-12-16 15:29:30 -080099 def test_unicode(self, backend):
100 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -0700101 with pytest.raises(TypeError):
102 f.encrypt(six.u(""))
103 with pytest.raises(TypeError):
104 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -0700105
106 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -0800107 def test_roundtrips(self, message, backend):
108 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -0700109 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -0800110
Alex Gaynora8f0b632013-12-16 15:44:06 -0800111 def test_bad_key(self, backend):
112 with pytest.raises(ValueError):
113 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700114
115
Alex Gaynore6ac6022014-10-24 07:50:30 -0700116@pytest.mark.requires_backend_interface(interface=CipherBackend)
117@pytest.mark.requires_backend_interface(interface=HMACBackend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700118@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)
124class TestMultiFernet(object):
125 def test_encrypt(self, backend):
Alex Gaynor41b33b72014-10-20 14:34:35 -0700126 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 Gaynor7b593e12014-10-19 19:09:44 -0700131
132 def test_decrypt(self, backend):
133 f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor41b33b72014-10-20 14:34:35 -0700134 f2 = Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
Alex Gaynor7b593e12014-10-19 19:09:44 -0700135 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 Gaynor4f286ce2014-10-20 11:30:57 -0700146
147 def test_non_iterable_argument(self, backend):
148 with pytest.raises(TypeError):
149 MultiFernet(None)