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
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 )
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
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
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
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
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
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
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))
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)
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()
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
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.")
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()
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)
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()
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)
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)
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)