2013-11-02 21:04:19 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
|
# implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2014-03-08 16:32:56 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
2013-10-31 16:39:25 +00:00
|
|
|
import base64
|
2013-10-31 21:16:24 +00:00
|
|
|
import calendar
|
|
|
|
|
import json
|
2013-11-22 18:12:05 +00:00
|
|
|
import time
|
2013-10-31 21:16:24 +00:00
|
|
|
|
|
|
|
|
import iso8601
|
|
|
|
|
|
|
|
|
|
import pytest
|
2013-10-31 16:39:25 +00:00
|
|
|
|
2013-10-31 17:10:44 +00:00
|
|
|
import six
|
|
|
|
|
|
2014-10-20 02:09:44 +00:00
|
|
|
from cryptography.fernet import Fernet, InvalidToken, MultiFernet
|
2013-12-16 23:29:30 +00:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
2014-01-10 04:21:14 +00:00
|
|
|
from cryptography.hazmat.primitives.ciphers import algorithms, modes
|
2013-10-31 16:39:25 +00:00
|
|
|
|
2014-03-14 20:03:12 +00:00
|
|
|
import cryptography_vectors
|
|
|
|
|
|
2013-10-31 16:39:25 +00:00
|
|
|
|
2014-03-12 21:54:43 +00:00
|
|
|
def json_parametrize(keys, filename):
|
2014-03-14 20:03:12 +00:00
|
|
|
vector_file = cryptography_vectors.open_vector_file('fernet', filename)
|
|
|
|
|
with vector_file:
|
|
|
|
|
data = json.load(vector_file)
|
|
|
|
|
return pytest.mark.parametrize(keys, [
|
|
|
|
|
tuple([entry[k] for k in keys])
|
|
|
|
|
for entry in data
|
|
|
|
|
])
|
2013-10-31 21:16:24 +00:00
|
|
|
|
|
|
|
|
|
2014-10-20 01:46:55 +00:00
|
|
|
def test_default_backend():
|
|
|
|
|
f = Fernet(Fernet.generate_key())
|
|
|
|
|
assert f._backend is default_backend()
|
|
|
|
|
|
|
|
|
|
|
2014-01-10 04:21:14 +00:00
|
|
|
@pytest.mark.cipher
|
2014-10-20 01:46:55 +00:00
|
|
|
@pytest.mark.supported(
|
|
|
|
|
only_if=lambda backend: backend.cipher_supported(
|
|
|
|
|
algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
|
|
|
|
|
),
|
|
|
|
|
skip_message="Does not support AES CBC",
|
|
|
|
|
)
|
2013-10-31 16:39:25 +00:00
|
|
|
class TestFernet(object):
|
2013-10-31 21:16:24 +00:00
|
|
|
@json_parametrize(
|
2013-10-31 21:50:00 +00:00
|
|
|
("secret", "now", "iv", "src", "token"), "generate.json",
|
2013-10-31 21:16:24 +00:00
|
|
|
)
|
2013-12-16 23:29:30 +00:00
|
|
|
def test_generate(self, secret, now, iv, src, token, backend):
|
|
|
|
|
f = Fernet(secret.encode("ascii"), backend=backend)
|
2013-10-31 21:16:24 +00:00
|
|
|
actual_token = f._encrypt_from_parts(
|
|
|
|
|
src.encode("ascii"),
|
|
|
|
|
calendar.timegm(iso8601.parse_date(now).utctimetuple()),
|
|
|
|
|
b"".join(map(six.int2byte, iv))
|
2013-10-31 16:39:25 +00:00
|
|
|
)
|
2013-10-31 23:29:18 +00:00
|
|
|
assert actual_token == token.encode("ascii")
|
2013-10-31 16:39:25 +00:00
|
|
|
|
2013-10-31 21:16:24 +00:00
|
|
|
@json_parametrize(
|
2013-10-31 21:50:00 +00:00
|
|
|
("secret", "now", "src", "ttl_sec", "token"), "verify.json",
|
2013-10-31 21:16:24 +00:00
|
|
|
)
|
2013-12-16 23:29:30 +00:00
|
|
|
def test_verify(self, secret, now, src, ttl_sec, token, backend,
|
|
|
|
|
monkeypatch):
|
|
|
|
|
f = Fernet(secret.encode("ascii"), backend=backend)
|
2013-10-31 22:03:53 +00:00
|
|
|
current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
|
2013-11-22 18:12:05 +00:00
|
|
|
monkeypatch.setattr(time, "time", lambda: current_time)
|
|
|
|
|
payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
|
2013-10-31 23:29:18 +00:00
|
|
|
assert payload == src.encode("ascii")
|
2013-10-31 21:50:00 +00:00
|
|
|
|
|
|
|
|
@json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
|
2013-12-16 23:29:30 +00:00
|
|
|
def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
|
|
|
|
|
f = Fernet(secret.encode("ascii"), backend=backend)
|
2013-10-31 22:03:53 +00:00
|
|
|
current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
|
2013-11-22 18:12:05 +00:00
|
|
|
monkeypatch.setattr(time, "time", lambda: current_time)
|
2013-10-31 21:50:00 +00:00
|
|
|
with pytest.raises(InvalidToken):
|
2013-11-22 18:12:05 +00:00
|
|
|
f.decrypt(token.encode("ascii"), ttl=ttl_sec)
|
2013-10-31 21:50:00 +00:00
|
|
|
|
2013-12-16 23:44:06 +00:00
|
|
|
def test_invalid_start_byte(self, backend):
|
2014-10-20 02:11:30 +00:00
|
|
|
f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
2013-12-16 23:44:06 +00:00
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
|
f.decrypt(base64.urlsafe_b64encode(b"\x81"))
|
|
|
|
|
|
2013-12-20 19:02:33 +00:00
|
|
|
def test_timestamp_too_short(self, backend):
|
2014-10-20 02:11:30 +00:00
|
|
|
f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
2013-12-20 19:02:33 +00:00
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
|
f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))
|
|
|
|
|
|
2014-10-20 02:11:30 +00:00
|
|
|
def test_non_base64_token(self, backend):
|
|
|
|
|
f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
|
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
|
f.decrypt(b"\x00")
|
|
|
|
|
|
2013-12-16 23:29:30 +00:00
|
|
|
def test_unicode(self, backend):
|
|
|
|
|
f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
2013-10-31 21:50:00 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
f.encrypt(six.u(""))
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
f.decrypt(six.u(""))
|
2013-10-31 22:23:15 +00:00
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
|
2013-12-16 23:29:30 +00:00
|
|
|
def test_roundtrips(self, message, backend):
|
|
|
|
|
f = Fernet(Fernet.generate_key(), backend=backend)
|
2013-10-31 23:07:35 +00:00
|
|
|
assert f.decrypt(f.encrypt(message)) == message
|
2013-12-16 23:29:30 +00:00
|
|
|
|
2013-12-16 23:44:06 +00:00
|
|
|
def test_bad_key(self, backend):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)
|
2014-10-20 02:09:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.supported(
|
|
|
|
|
only_if=lambda backend: backend.cipher_supported(
|
|
|
|
|
algorithms.AES("\x00" * 32), modes.CBC("\x00" * 16)
|
|
|
|
|
),
|
|
|
|
|
skip_message="Does not support AES CBC",
|
|
|
|
|
)
|
|
|
|
|
class TestMultiFernet(object):
|
|
|
|
|
def test_encrypt(self, backend):
|
|
|
|
|
single_f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
|
|
|
|
f = MultiFernet([
|
|
|
|
|
single_f,
|
|
|
|
|
Fernet(base64.urlsafe_b64encode(b"\x01" * 32), backend=backend)
|
|
|
|
|
])
|
|
|
|
|
assert single_f.decrypt(f.encrypt(b"abc")) == b"abc"
|
|
|
|
|
|
|
|
|
|
def test_decrypt(self, backend):
|
|
|
|
|
f1 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
|
|
|
|
f2 = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
|
|
|
|
|
f = MultiFernet([f1, f2])
|
|
|
|
|
|
|
|
|
|
assert f.decrypt(f1.encrypt(b"abc")) == b"abc"
|
|
|
|
|
assert f.decrypt(f2.encrypt(b"abc")) == b"abc"
|
|
|
|
|
|
|
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
|
f.decrypt(b"\x00" * 16)
|
|
|
|
|
|
|
|
|
|
def test_no_fernets(self, backend):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
MultiFernet([])
|
2014-10-20 18:30:57 +00:00
|
|
|
|
|
|
|
|
def test_non_iterable_argument(self, backend):
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
MultiFernet(None)
|