@@ -629,71 +629,44 @@ def get_signed_corim_report(self) -> bytes:
629629 """Returns the signed CoRIM report (COSE-Sign1)."""
630630 return self .signed_corim_report
631631
632- def _sign_corim_report_internal (self , signer ) -> bool :
633- """Sign the CoRIM report using COSE-Sign1 with the cwt library .
632+ def _sign_corim_report_internal (self , cose_key ) -> bool :
633+ """Sign the CoRIM report as COSE_Sign1 (CBOR tag 18) using cwt.COSE .
634634
635- Uses the cwt (CBOR Web Token) library for better COSE compatibility.
636- do not call directly, use either sign_corim_report_pem() or sign_corim_report_azure()
637- which provide appropriate signer modules.
638- """
639- # Get CoRIM payload as claims (cwt expects claims, not raw payload)
640- corim_cbor = self .get_report_as_corim_cbor ()
635+ Uses cwt.COSE.encode() to produce a proper COSE_Sign1 envelope,
636+ as required by the CoRIM spec:
637+ signed-corim = #6.18(COSE_Sign1_Tagged<corim-map>)
641638
642- # For COSE signing, we need to create claims structure
643- # The CoRIM data becomes the payload claim
644- claims = {
645- # Use a custom claim number for CoRIM data
646- - 65537 : corim_cbor # Custom claim for CoRIM payload
647- }
639+ The raw CoRIM CBOR is used directly as the COSE_Sign1 payload
640+ (no CWT claims wrapping).
648641
649- # Sign using cwt library with the signer
650- signed_corim_report = cwt .encode_and_sign (
651- claims = claims ,
652- signers = [signer ],
653- tagged = True , # Use CBOR tag for COSE_Sign1
654- )
642+ cose_key: A cwt.COSEKeyInterface (e.g. from cwt.COSEKey.from_pem()).
655643
656- self .signed_corim_report = signed_corim_report
644+ Do not call directly; use sign_corim_report_pem() or
645+ sign_corim_report_azure().
646+ """
647+ corim_cbor = self .get_report_as_corim_cbor ()
648+ cose = cwt .COSE (alg_auto_inclusion = True , kid_auto_inclusion = True )
649+ self .signed_corim_report = cose .encode (corim_cbor , cose_key )
657650 return True
658651
659652 def sign_corim_report_pem (self , priv_key : bytes , algo : str , kid : str ) -> bool :
660- """Sign the CoRIM report using COSE-Sign1 with the cwt library .
653+ """Sign the CoRIM report as COSE_Sign1 (tag 18) using a PEM private key .
661654
662- Uses the cwt (CBOR Web Token) library for better COSE compatibility.
655+ Uses cwt.COSE to produce a proper COSE_Sign1 envelope.
656+
657+ priv_key: PEM-encoded private key bytes.
658+ algo: Algorithm name (ES384, ES512, PS384, PS512).
659+ kid: Key identifier string for the unprotected header.
663660 """
664661 try :
665- # Load private key using cryptography
666- pem = serialization .load_pem_private_key (
667- priv_key , None , backend = default_backend ()
668- )
669-
670- # Map algorithm to COSE algorithm identifier
671- cose_alg = None
672- if (
673- algo == "ES512"
674- and isinstance (pem , EllipticCurvePrivateKey )
675- and pem .curve .name == "secp521r1"
676- ):
677- cose_alg = - 36 # ES512
678- elif (
679- algo == "ES384"
680- and isinstance (pem , EllipticCurvePrivateKey )
681- and pem .curve .name == "secp384r1"
682- ):
683- cose_alg = - 35 # ES384
684- elif algo == "PS512" and isinstance (pem , RSAPrivateKey ):
685- cose_alg = - 38 # PS512
686- elif algo == "PS384" and isinstance (pem , RSAPrivateKey ):
687- cose_alg = - 37 # PS384
688- else :
689- print (f"Unsupported algorithm/key combination: { algo } with { type (pem )} " )
662+ algo_map = {"ES384" : - 35 , "ES512" : - 36 , "PS384" : - 37 , "PS512" : - 38 }
663+ cose_alg = algo_map .get (algo )
664+ if cose_alg is None :
665+ print (f"Unsupported algorithm: { algo } " )
690666 return False
691667
692- # Create Signer using cwt library
693- signer = cwt .Signer .from_pem (priv_key , alg = cose_alg , kid = kid )
694-
695- # sign and return result
696- return self ._sign_corim_report_internal (signer )
668+ cose_key = cwt .COSEKey .from_pem (priv_key , alg = cose_alg , kid = kid )
669+ return self ._sign_corim_report_internal (cose_key )
697670
698671 except Exception as e :
699672 print (f"Error signing CoRIM with cwt: { e } " )
@@ -767,20 +740,24 @@ def verify_signed_json_report(
767740 return decoded
768741
769742 def verify_signed_corim_report (
770- self , signed_corim_report : bytes , pub_key : bytes , kid : str
743+ self , signed_corim_report : bytes , pub_key : bytes , kid : str ,
744+ algo : int = - 36 ,
771745 ) -> bool :
772- """Verify the signed report using the provided public key .
746+ """Verify a signed CoRIM report (COSE_Sign1, tag 18) .
773747
774- signed_corim_report: A bytes object containing the signed report as a CoRIM CBOR object.
775- pub_key: A bytes object containing the public key used to verify the signed report, which corresponds to the SRP's 'kid'.
748+ signed_corim_report: Signed COSE_Sign1 bytes.
749+ pub_key: PEM-encoded public key bytes.
750+ kid: Key identifier string.
751+ algo: COSE algorithm identifier
752+ (default: -36 / ES512 for backward compatibility;
753+ use -35 for ES384).
776754
777- Returns a dictionary containing the decoded short-form report payload .
755+ Returns True on success, raises Exception on failure .
778756 """
779757 try :
780- cwt .decode (
781- data = signed_corim_report ,
782- keys = cwt .COSEKey .from_pem (pub_key , alg = - 36 , kid = kid ),
783- ) # alg -36 is ES512
758+ cose_key = cwt .COSEKey .from_pem (pub_key , alg = algo , kid = kid )
759+ cose = cwt .COSE ()
760+ cose .decode (signed_corim_report , cose_key )
784761 return True
785762
786763 except Exception as e :
0 commit comments