Skip to content

Commit 144fd6b

Browse files
committed
core identifiers, structures, and helpers
1 parent 1d3c1bc commit 144fd6b

File tree

11 files changed

+673
-0
lines changed

11 files changed

+673
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
""""""
14+
from dynamodb_encryption_sdk.encrypted.item import (
15+
decrypt_dynamodb_item, decrypt_python_item,
16+
encrypt_dynamodb_item, encrypt_python_item
17+
)
18+
19+
# encrypt_item
20+
# encrypt_raw_item
21+
# decrypt_item
22+
# decrypt_raw_item
23+
# EncryptedTable
24+
# EncryptedResource
25+
# EncryptedClient
26+
27+
# TableConfiguration
28+
# MaterialDescription
29+
# ItemConfiguration
30+
31+
__all__ = (
32+
'decrypt_dynamodb_item', 'decrypt_python_item',
33+
'encrypt_dynamodb_item', 'encrypt_python_item'
34+
)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
14+
15+
class DynamodbEncryptionSdkError(Exception):
16+
"""Base class for all custom exceptions."""
17+
18+
19+
class SerializationError(DynamodbEncryptionSdkError):
20+
"""Otherwise undifferentiated errors encountered while serializing data."""
21+
22+
23+
class DeserializationError(DynamodbEncryptionSdkError):
24+
"""Otherwise undifferentiated errors encountered while deserializing data."""
25+
26+
27+
class InvalidMaterialsetError(DeserializationError):
28+
"""Raised when errors are encountered processing a material description."""
29+
# TODO: MaterialDescription, not Materialset...
30+
31+
32+
class InvalidMaterialsetVersionError(DeserializationError):
33+
"""Raised when a material description is encountered with an invalid version."""
34+
# TODO: MaterialDescription, not Materialset...
35+
36+
37+
class InvalidAlgorithmError(DynamodbEncryptionSdkError):
38+
"""Raised when an invalid algorithm identifier is encountered."""
39+
40+
41+
class JceTransformationError(DynamodbEncryptionSdkError):
42+
""""""
43+
44+
45+
class DelegatedKeyError(DynamodbEncryptionSdkError):
46+
""""""
47+
48+
49+
class DelegatedKeyEncryptionError(DelegatedKeyError):
50+
""""""
51+
52+
53+
class DelegatedKeyDecryptionError(DelegatedKeyError):
54+
""""""
55+
56+
57+
class AwsKmsMaterialsProviderError(DynamodbEncryptionSdkError):
58+
""""""
59+
60+
61+
class UnknownRegionError(AwsKmsMaterialsProviderError):
62+
""""""
63+
64+
65+
class DecryptionError(DynamodbEncryptionSdkError):
66+
""""""
67+
68+
69+
class UnwrappingError(DynamodbEncryptionSdkError):
70+
""""""
71+
72+
73+
class EncryptionError(DynamodbEncryptionSdkError):
74+
""""""
75+
76+
77+
class WrappingError(DynamodbEncryptionSdkError):
78+
""""""
79+
80+
81+
class SigningError(DynamodbEncryptionSdkError):
82+
""""""
83+
84+
85+
class SignatureVerificationError(DynamodbEncryptionSdkError):
86+
""""""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
from enum import Enum
14+
15+
__version__ = '0.0.0'
16+
17+
LOGGER_NAME = 'dynamodb_encryption_sdk'
18+
19+
20+
class ItemAction(Enum):
21+
"""Possible actions to take on an item attribute."""
22+
DO_NOTHING = 0
23+
SIGN_ONLY = 1
24+
ENCRYPT_AND_SIGN = 2
25+
26+
def __lt__(self, other):
27+
return self.value < other.value
28+
29+
def __eq__(self, other):
30+
return self.value == other.value
31+
32+
33+
class EncryptionKeyTypes(Enum):
34+
"""Supported types of encryption keys."""
35+
SYMMETRIC = 0
36+
PRIVATE = 1
37+
PUBLIC = 2
38+
39+
40+
class KeyEncodingType(Enum):
41+
"""Supported key encoding schemes."""
42+
RAW = 0
43+
DER = 1
44+
PEM = 2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Internal implementation details.
14+
15+
.. warning::
16+
No guarantee is provided on the modules and APIs within this
17+
namespace staying consistent. Directly reference at your own risk.
18+
"""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
""""""
14+
15+
ENCODING = 'utf-8'
16+
LOGGING_NAME = 'dynamodb_encryption_sdk'
17+
MATERIAL_DESCRIPTION_VERSION = b'\00' * 4
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
""""""
14+
from enum import Enum
15+
16+
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
17+
from typing import Any, ByteString, Dict, List, Text, Union # pylint: disable=unused-import
18+
except ImportError: # pragma: no cover
19+
# We only actually need these imports when running the mypy checks
20+
pass
21+
22+
23+
class ReservedAttributes(Enum):
24+
"""Item attributes reserved for use by DynamoDBEncryptionClient"""
25+
MATERIAL_DESCRIPTION = '*amzn-ddb-map-desc*'
26+
SIGNATURE = '*amzn-ddb-map-sig*'
27+
28+
29+
class Tag(Enum):
30+
"""Attribute data type identifiers used for serialization and deserialization of attributes."""
31+
32+
BINARY = (b'b', 'B')
33+
BINARY_SET = (b'B', 'BS', b'b')
34+
NUMBER = (b'n', 'N')
35+
NUMBER_SET = (b'N', 'NS', b'n')
36+
STRING = (b's', 'S')
37+
STRING_SET = (b'S', 'SS', b's')
38+
BOOLEAN = (b'?', 'BOOL')
39+
NULL = (b'\x00', 'NULL')
40+
LIST = (b'L', 'L')
41+
MAP = (b'M', 'M')
42+
43+
def __init__(self, tag, dynamodb_tag, element_tag=None):
44+
# type: (bytes, Text, Optional[bytes]) -> None
45+
"""Sets up new Tag object.
46+
47+
:param bytes tag: DynamoDB Encryption SDK tag
48+
:param bytes dynamodb_tag: DynamoDB tag
49+
:param bytes element_tag: The type of tag contained within attributes of this type
50+
"""
51+
self.tag = tag
52+
self.dynamodb_tag = dynamodb_tag
53+
self.element_tag = element_tag
54+
55+
56+
class TagValues(Enum):
57+
"""Static values to use when serializing attribute values."""
58+
FALSE = b'\x00'
59+
TRUE = b'\x01'
60+
61+
62+
class SignatureValues(Enum):
63+
"""Values used when building the string to sign.
64+
65+
.. note::
66+
67+
The only time we actually use these values, we use the SHA256 hash of the value, so
68+
we pre-compute these hashes here.
69+
"""
70+
ENCRYPTED = (
71+
b'ENCRYPTED',
72+
b"9A\x15\xacN\xb0\x9a\xa4\x94)4\x88\x16\xb2\x03\x81'\xb0\xf9\xe3\xa5 7*\xe1\x00\xca\x19\xfb\x08\xfdP"
73+
)
74+
PLAINTEXT = (
75+
b'PLAINTEXT',
76+
b'\xcb@\xe7\xda\xdc\x86\x16\x1b\x97\x98\xdeHQ/3-!\xc1A\xfc\xc1\xe2\x8a\x08o\xdeJ3u\xaa\xb1\xb5'
77+
)
78+
79+
def __init__(self, raw, sha256):
80+
# type: (bytes, bytes) -> None
81+
"""Set up a new SignatureValues object.
82+
83+
:param bytes raw: Raw value
84+
:param bytes sha256: SHA256 hash of raw value
85+
"""
86+
self.raw = raw
87+
self.sha256 = sha256
88+
89+
90+
class MaterialDescriptionKeys(Enum):
91+
"""Static keys for use when building and reading material descriptions."""
92+
ATTRIBUTE_ENCRYPTION_MODE = 'amzn-ddb-map-sym-mode'
93+
SIGNING_KEY_ALGORITHM = 'amzn-ddb-map-signingAlg'
94+
WRAPPED_DATA_KEY = 'amzn-ddb-env-key'
95+
CONTENT_ENCRYPTION_ALGORITHM = 'amzn-ddb-env-alg'
96+
CONTENT_KEY_WRAPPING_ALGORITHM = 'amzn-ddb-wrap-alg'
97+
ITEM_SIGNATURE_ALGORITHM = 'amzn-ddb-sig-alg'
98+
99+
100+
class MaterialDescriptionValues(Enum):
101+
"""Static default values for use when building material descriptions."""
102+
CBC_PKCS5_ATTRIBUTE_ENCRYPTION = '/CBC/PKCS5Padding'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Helper functions for consistently obtaining str and bytes objects in both Python2 and Python3."""
14+
import codecs
15+
16+
import six
17+
18+
19+
def to_str(data):
20+
"""Takes an input str or bytes object and returns an equivalent str object.
21+
22+
:param data: Input data
23+
:type data: str or bytes
24+
:returns: Data normalized to str
25+
:rtype: str
26+
"""
27+
if isinstance(data, bytes):
28+
return codecs.decode(data, 'utf-8')
29+
return data
30+
31+
32+
def to_bytes(data):
33+
"""Takes an input str or bytes object and returns an equivalent bytes object.
34+
35+
:param data: Input data
36+
:type data: str or bytes
37+
:returns: Data normalized to bytes
38+
:rtype: bytes
39+
"""
40+
if isinstance(data, six.string_types) and not isinstance(data, bytes):
41+
return codecs.encode(data, 'utf-8')
42+
return data

0 commit comments

Comments
 (0)