Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename COSEType to COSETypes. #403

Merged
merged 1 commit into from Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cwt/__init__.py
Expand Up @@ -12,7 +12,7 @@
set_private_claim_names,
)
from .encrypted_cose_key import EncryptedCOSEKey
from .enums import COSEHeaders, COSEType
from .enums import COSEHeaders, COSETypes
from .exceptions import CWTError, DecodeError, EncodeError, VerifyError
from .helpers.hcert import load_pem_hcert_dsc
from .recipient import Recipient
Expand Down Expand Up @@ -40,7 +40,7 @@
"COSEKey",
"COSEMessage",
"COSESignature",
"COSEType",
"COSETypes",
"COSEHeaders",
"EncryptedCOSEKey",
"HPKECipherSuite",
Expand Down
30 changes: 15 additions & 15 deletions cwt/const.py
@@ -1,23 +1,23 @@
from .enums import COSEType
from .enums import COSETypes

COSE_TAG_TO_TYPE = {
16: COSEType.ENCRYPT0,
96: COSEType.ENCRYPT,
17: COSEType.MAC0,
97: COSEType.MAC,
18: COSEType.SIGN1,
98: COSEType.SIGN,
19: COSEType.COUNTERSIGNATURE,
16: COSETypes.ENCRYPT0,
96: COSETypes.ENCRYPT,
17: COSETypes.MAC0,
97: COSETypes.MAC,
18: COSETypes.SIGN1,
98: COSETypes.SIGN,
19: COSETypes.COUNTERSIGNATURE,
}

COSE_TYPE_TO_TAG = {
COSEType.ENCRYPT0: 16,
COSEType.ENCRYPT: 96,
COSEType.MAC0: 17,
COSEType.MAC: 97,
COSEType.SIGN1: 18,
COSEType.SIGN: 98,
COSEType.COUNTERSIGNATURE: 19,
COSETypes.ENCRYPT0: 16,
COSETypes.ENCRYPT: 96,
COSETypes.MAC0: 17,
COSETypes.MAC: 97,
COSETypes.SIGN1: 18,
COSETypes.SIGN: 98,
COSETypes.COUNTERSIGNATURE: 19,
}

# Registered CWT Claims
Expand Down
30 changes: 15 additions & 15 deletions cwt/cose_message.py
Expand Up @@ -5,7 +5,7 @@
from .cbor_processor import CBORProcessor
from .const import COSE_TAG_TO_TYPE, COSE_TYPE_TO_TAG
from .cose_key_interface import COSEKeyInterface
from .enums import COSEType
from .enums import COSETypes
from .signer import Signer

Self = TypeVar("Self", bound="COSEMessage")
Expand All @@ -16,7 +16,7 @@ class COSEMessage(CBORProcessor):
The COSE message.
"""

def __init__(self, type: COSEType, msg: List[Any]):
def __init__(self, type: COSETypes, msg: List[Any]):
"""
Constructor.

Expand All @@ -34,11 +34,11 @@ def __init__(self, type: COSEType, msg: List[Any]):
self._recipients: List[List[Any]] = []
self._signatures: List[List[Any]] = []

if self._type == COSEType.ENCRYPT0:
if self._type == COSETypes.ENCRYPT0:
if len(self._msg) != 3:
raise ValueError("Invalid COSE_Encrypt0 message.")

elif self._type == COSEType.ENCRYPT:
elif self._type == COSETypes.ENCRYPT:
if len(self._msg) != 4:
raise ValueError("Invalid COSE_Encrypt message.")
if not isinstance(self._msg[3], list):
Expand All @@ -47,14 +47,14 @@ def __init__(self, type: COSEType, msg: List[Any]):
self._validate_cose_message(recipient)
self._recipients = self._msg[3]

elif self._type == COSEType.MAC0:
elif self._type == COSETypes.MAC0:
if len(self._msg) != 4:
raise ValueError("Invalid COSE_Mac0 message.")
if not isinstance(self._msg[3], bytes):
raise ValueError("tag should be bytes.")
self._otther_fields = [self._msg[3]] # tag

elif self._type == COSEType.MAC:
elif self._type == COSETypes.MAC:
if len(self._msg) != 5:
raise ValueError("Invalid COSE_Mac message.")
if not isinstance(self._msg[3], bytes):
Expand All @@ -66,14 +66,14 @@ def __init__(self, type: COSEType, msg: List[Any]):
self._validate_cose_message(recipient)
self._recipients = self._msg[4]

elif self._type == COSEType.SIGN1:
elif self._type == COSETypes.SIGN1:
if len(self._msg) != 4:
raise ValueError("Invalid COSE_Sign1 message.")
if not isinstance(self._msg[3], bytes):
raise ValueError("The COSE signature should be bytes.")
self._otther_fields = [self._msg[3]]

elif self._type == COSEType.SIGN:
elif self._type == COSETypes.SIGN:
if len(self._msg) != 4:
raise ValueError("Invalid COSE_Sign message.")
if not isinstance(self._msg[3], list):
Expand All @@ -82,20 +82,20 @@ def __init__(self, type: COSEType, msg: List[Any]):
self._validate_cose_message(signature)
self._signatures = self._msg[3]

elif self._type == COSEType.COUNTERSIGNATURE:
elif self._type == COSETypes.COUNTERSIGNATURE:
if len(self._msg) != 3:
raise ValueError("Invalid COSE_Countersignature.")

elif self._type == COSEType.SIGNATURE:
elif self._type == COSETypes.SIGNATURE:
if len(self._msg) != 3:
raise ValueError("Invalid COSE_Signature.")

elif self._type == COSEType.RECIPIENT:
elif self._type == COSETypes.RECIPIENT:
if len(self._msg) != 3:
raise ValueError("Invalid COSE_Recipient.")

else:
raise ValueError(f"Invalid COSEType({type}) for COSE message.")
raise ValueError(f"Invalid COSETypes({type}) for COSE message.")
return

@classmethod
Expand All @@ -110,14 +110,14 @@ def loads(cls, msg: bytes):

@classmethod
def from_cose_signature(cls, signature: List[Any]):
return cls(COSEType.SIGNATURE, signature)
return cls(COSETypes.SIGNATURE, signature)

@classmethod
def from_cose_recipient(cls, recipient: List[Any]):
return cls(COSEType.RECIPIENT, recipient)
return cls(COSETypes.RECIPIENT, recipient)

@property
def type(self) -> COSEType:
def type(self) -> COSETypes:
"""
The identifier of the key type.
"""
Expand Down
2 changes: 1 addition & 1 deletion cwt/enums.py
@@ -1,7 +1,7 @@
import enum


class COSEType(enum.IntEnum):
class COSETypes(enum.IntEnum):
ENCRYPT0 = 1
ENCRYPT = 2
MAC0 = 3
Expand Down
56 changes: 28 additions & 28 deletions tests/test_cose_message.py
Expand Up @@ -10,7 +10,7 @@
import cbor2
import pytest

from cwt import COSE, COSEKey, COSEMessage, COSEType, Recipient, Signer, VerifyError
from cwt import COSE, COSEKey, COSEMessage, COSETypes, Recipient, Signer, VerifyError


class TestCOSEMessage:
Expand All @@ -23,7 +23,7 @@ def test_cose_message_constructor_with_mac0(self):
sender = COSE.new(alg_auto_inclusion=True, kid_auto_inclusion=True)
encoded = sender.encode(b"Hello world!", mac_key)
sig = COSEMessage.loads(encoded)
assert sig.type == COSEType.MAC0
assert sig.type == COSETypes.MAC0
assert sig.payload == b"Hello world!"
assert sig.signatures == []
assert sig.recipients == []
Expand Down Expand Up @@ -303,32 +303,32 @@ def test_cose_usage_examples_cose_mac_countersignature(self):
@pytest.mark.parametrize(
"type, msg, err_msg",
[
(COSEType.MAC0, [], "Invalid COSE message."),
(COSEType.MAC0, [{}, {}, b""], "The protected headers should be bytes."),
(COSEType.MAC0, [b"", b"", b""], "The unprotected headers should be Dict[int, Any]."),
(COSEType.MAC0, [b"", {}, {}], "The payload should be bytes."),
(COSEType.MAC0, [b"", {11: {}}, b""], "The countersignature should be array."),
(COSEType.MAC0, [b"", {11: []}, b""], "Invalid countersignature."),
(COSEType.MAC0, [b"", {11: [b""]}, b""], "Invalid COSE message."),
(COSEType.MAC0, [b"", {11: [""]}, b""], "Invalid countersignature."),
(COSEType.MAC0, [b"", {}, b""], "Invalid COSE_Mac0 message."),
(COSEType.MAC0, [b"", {}, b"", {}], "tag should be bytes."),
(COSEType.ENCRYPT0, [b"", {}, b"", {}], "Invalid COSE_Encrypt0 message."),
(COSEType.ENCRYPT, [b"", {}, b""], "Invalid COSE_Encrypt message."),
(COSEType.ENCRYPT, [b"", {}, b"", {}], "The COSE recipients should be array."),
(COSEType.MAC, [b"", {}, b"", {}], "Invalid COSE_Mac message."),
(COSEType.MAC, [b"", {}, b"", {}, {}], "The tag value should be bytes."),
(COSEType.MAC, [b"", {}, b"", b"", {}], "The COSE recipients should be array."),
(COSEType.MAC, [b"", {}, b"", b"", [[]]], "Invalid COSE message."),
(COSEType.SIGN1, [b"", {}, b""], "Invalid COSE_Sign1 message."),
(COSEType.SIGN1, [b"", {}, b"", {}], "The COSE signature should be bytes."),
(COSEType.SIGN, [b"", {}, b""], "Invalid COSE_Sign message."),
(COSEType.SIGN, [b"", {}, b"", {}], "The COSE signatures should be array."),
(COSEType.SIGN, [b"", {}, b"", [[]]], "Invalid COSE message."),
(COSEType.COUNTERSIGNATURE, [b"", {}, b"", {}], "Invalid COSE_Countersignature."),
(COSEType.SIGNATURE, [b"", {}, b"", {}], "Invalid COSE_Signature."),
(COSEType.RECIPIENT, [b"", {}, b"", {}], "Invalid COSE_Recipient."),
(-1, [b"", {}, b""], "Invalid COSEType(-1) for COSE message."),
(COSETypes.MAC0, [], "Invalid COSE message."),
(COSETypes.MAC0, [{}, {}, b""], "The protected headers should be bytes."),
(COSETypes.MAC0, [b"", b"", b""], "The unprotected headers should be Dict[int, Any]."),
(COSETypes.MAC0, [b"", {}, {}], "The payload should be bytes."),
(COSETypes.MAC0, [b"", {11: {}}, b""], "The countersignature should be array."),
(COSETypes.MAC0, [b"", {11: []}, b""], "Invalid countersignature."),
(COSETypes.MAC0, [b"", {11: [b""]}, b""], "Invalid COSE message."),
(COSETypes.MAC0, [b"", {11: [""]}, b""], "Invalid countersignature."),
(COSETypes.MAC0, [b"", {}, b""], "Invalid COSE_Mac0 message."),
(COSETypes.MAC0, [b"", {}, b"", {}], "tag should be bytes."),
(COSETypes.ENCRYPT0, [b"", {}, b"", {}], "Invalid COSE_Encrypt0 message."),
(COSETypes.ENCRYPT, [b"", {}, b""], "Invalid COSE_Encrypt message."),
(COSETypes.ENCRYPT, [b"", {}, b"", {}], "The COSE recipients should be array."),
(COSETypes.MAC, [b"", {}, b"", {}], "Invalid COSE_Mac message."),
(COSETypes.MAC, [b"", {}, b"", {}, {}], "The tag value should be bytes."),
(COSETypes.MAC, [b"", {}, b"", b"", {}], "The COSE recipients should be array."),
(COSETypes.MAC, [b"", {}, b"", b"", [[]]], "Invalid COSE message."),
(COSETypes.SIGN1, [b"", {}, b""], "Invalid COSE_Sign1 message."),
(COSETypes.SIGN1, [b"", {}, b"", {}], "The COSE signature should be bytes."),
(COSETypes.SIGN, [b"", {}, b""], "Invalid COSE_Sign message."),
(COSETypes.SIGN, [b"", {}, b"", {}], "The COSE signatures should be array."),
(COSETypes.SIGN, [b"", {}, b"", [[]]], "Invalid COSE message."),
(COSETypes.COUNTERSIGNATURE, [b"", {}, b"", {}], "Invalid COSE_Countersignature."),
(COSETypes.SIGNATURE, [b"", {}, b"", {}], "Invalid COSE_Signature."),
(COSETypes.RECIPIENT, [b"", {}, b"", {}], "Invalid COSE_Recipient."),
(-1, [b"", {}, b""], "Invalid COSETypes(-1) for COSE message."),
],
)
def test_cose_message_constructor_with_invalid_args(self, type, msg, err_msg):
Expand Down