Пример #1
0
 def __init__(self, p_header=CoseAttrs(), u_header=CoseAttrs(), ciphertext=b'', rcpt_key=None):
     super(CoseRecipient, self).__init__(
         deepcopy(p_header),
         deepcopy(u_header),
     )
     self._ciphertext = ciphertext
     self._rcpt_key = rcpt_key
Пример #2
0
 def __init__(self, p_header=CoseAttrs(), u_header=CoseAttrs(), payload=None, key=None):
     super(Mac0Message, self).__init__(
         p_header=copy.deepcopy(p_header),
         u_header=copy.deepcopy(u_header),
         payload=payload,
         key=key
     )
Пример #3
0
 def __init__(self,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=None,
              key=None):
     super(EncCommon, self).__init__(p_header, u_header, payload)
     self._key = key
Пример #4
0
 def __init__(self, p_header=CoseAttrs(), u_header=CoseAttrs(), payload=b'', signature=b'', key=b''):
     super(Sign1Message, self).__init__(
         copy.deepcopy(p_header),
         copy.deepcopy(u_header),
         payload)
     self._key = key
     self._signature = signature
Пример #5
0
 def __init__(self,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=b'',
              external_aad=b''):
     super(CoseMessage, self).__init__(p_header, u_header)
     self._payload = payload
     self._external_aad = external_aad
Пример #6
0
 def add_signature_to_signers(self, signer, signature):
     if signer in self._signers:
         self._signers[signer][2] = signature
     else:
         # make new container
         container = [CoseAttrs(), CoseAttrs(), b'']
         # add the attribute
         container[2] = signature
         self._signers[signer] = container
Пример #7
0
 def __init__(self,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=None,
              auth_tag=None,
              key=None):
     super(MacCommon, self).__init__(p_header, u_header, payload)
     self._auth_tag = auth_tag
     self._key = key
Пример #8
0
 def __init__(self,
              alg,
              nonce,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=b'',
              rcpt_key=None):
     super(CoseRecipient, self).__init__()
     self.alg = alg
     self.nonce = nonce
Пример #9
0
 def __init__(self,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=b'',
              signers=CoseAttrs(),
              key=b''):
     super(SignMessage, self).__init__(copy.deepcopy(p_header),
                                       copy.deepcopy(u_header), payload)
     self._key = key
     self._signers = self._convert_to_coseattrs(copy.deepcopy(signers))
Пример #10
0
 def __init__(self,
              alg,
              nonce,
              p_header=CoseAttrs(),
              u_header=CoseAttrs(),
              payload=b'',
              rcpt_key=None):
     super(CoseEncRecipient, self).__init__(alg, nonce, deepcopy(p_header),
                                            deepcopy(u_header), payload,
                                            rcpt_key)
Пример #11
0
    def __init__(self,
                 p_header=CoseAttrs(),
                 u_header=CoseAttrs(),
                 payload=None,
                 key=None,
                 recipients=[]):
        super(MacMessage, self).__init__(deepcopy(p_header),
                                         deepcopy(u_header), payload, key)

        for rcpt in recipients:
            if not isinstance(rcpt, CoseMacRecipient):
                raise ValueError()
        self._recipients = deepcopy(recipients)
        self._encoded_recipients = self.__encode_recipients()
Пример #12
0
    def add_to_signers(self, signer, label, value, where):
        if signer in self._signers:
            if where == "PROTECTED":
                self._signers[signer][0][label] = value

            if where == "UNPROTECTED":
                self._signers[signer][1][label] = value

        else:
            # make new recipient
            signature = [CoseAttrs(), CoseAttrs(), b'']
            # add the attribute
            if where == "PROTECTED":
                signature[0][label] = value

            if where == "UNPROTECTED":
                signature[1][label] = value

            self._signers[signer] = signature
Пример #13
0
 def _convert_to_coseattrs(dictionary):
     if isinstance(dictionary, dict):
         new_coseattrs = CoseAttrs()
         for key in dictionary:
             new_coseattrs[key] = dictionary[key]
         return new_coseattrs
     elif isinstance(dictionary, CoseAttrs):
         return dictionary
     else:
         raise ValueError(
             "Can only accept objects of the type dict or CoseAttrs.")
Пример #14
0
    def signers(self, new_value):
        new_value = copy.deepcopy(new_value)
        if new_value is not None and len(new_value) != 0:
            self._signers = CoseAttrs()
            key = 1
            for sig in new_value:
                self._signers[key] = sig
                key += 1

            # cbor decode the byte string protected header, so that we can do lookups

            for sig in self._signers:
                if len(self._signers[sig][0]) != 0:
                    protected_signature = CoseAttrs()
                    new_value = cbor.loads(self._signers[sig][0])
                    for key, value in new_value.items():
                        protected_signature[key] = value

                    self._signers[sig][0] = copy.deepcopy(protected_signature)
                else:
                    self._signers[sig][0] = CoseAttrs()

            for sig in self._signers:
                if len(self._signers[sig][1]) != 0:
                    unprotected_signature = CoseAttrs()
                    for key, value in self._signers[sig][1].items():
                        unprotected_signature[key] = value

                    self._signers[sig][1] = copy.deepcopy(
                        unprotected_signature)
                else:
                    self._signers[sig][1] = CoseAttrs()
        else:
            self._signers = CoseAttrs()
Пример #15
0
    def from_cose_obj(cls, cose_obj):
        """Returns an initialized COSE message object."""
        protected_header = CoseAttrs()
        unprotected_header = CoseAttrs()

        try:
            decoded_protected_header = cbor.loads(cose_obj.pop(0))
        except ValueError:
            decoded_protected_header = {}

        try:
            decoded_unprotected_header = cose_obj.pop(0)
        except ValueError:
            decoded_unprotected_header = {}

        for k1 in decoded_protected_header:
            protected_header[k1] = decoded_protected_header[k1]

        for k2 in decoded_unprotected_header:
            unprotected_header[k2] = decoded_unprotected_header[k2]

        payload = cose_obj.pop(0)

        return cls(protected_header, unprotected_header, payload)
Пример #16
0
 def __init__(self, p_header=CoseAttrs(), u_header=CoseAttrs()):
     self._protected_header = p_header
     self._encoded_protected_header = self.__encode_protected_header()
     self._unprotected_header = u_header
     self._encoded_unprotected_header = self.__encode_unprotected_header()
Пример #17
0
 def test2_creation(self):
     for name_test, (a, b, c) in self.test2_params.items():
         with self.subTest(name=name_test):
             new_coseattr = CoseAttrs()
             new_coseattr[a] = b
             self.assertEqual(new_coseattr, c)
Пример #18
0
 def __init__(self, p_header=CoseAttrs(), u_header=CoseAttrs):
     self._protected_header = self._convert_to_coseattrs(p_header)
     self._encoded_protected_header = self._encode_protected_header()
     self._unprotected_header = self._convert_to_coseattrs(u_header)
Пример #19
0
 def test1_reinstantiation(self):
     new_coseattr = CoseAttrs()
     new_coseattr['alg'] = "HS256/64"
     new_coseattr = CoseAttrs()
     # test for empty dictionary
     self.assertEqual(bool(new_coseattr), False)