diff --git a/ctypescrypto/cms.py b/ctypescrypto/cms.py index 6061cb2..88d1e78 100644 --- a/ctypescrypto/cms.py +++ b/ctypescrypto/cms.py @@ -104,6 +104,15 @@ def __str__(self): raise CMSError("writing CMS to DER") return str(bio) + def __bytes__(self): + """ + Serialize in DER format. Return bytes + """ + bio = Membio() + if not libcrypto.i2d_CMS_bio(bio.bio, self.ptr): + raise CMSError("writing CMS to DER") + return bytes(bio) + def pem(self): """ Serialize in PEM format @@ -312,7 +321,11 @@ def decrypt(self, pkey, cert, flags=0): bio.bio, flags) if res <= 0: raise CMSError("decrypting CMS") - return str(bio) + + if flags & Flags.BINARY != 0: ## If we are expecting binary data + return bytes(bio) # we return bytes + else: + return str(bio) # else we return a string class EncryptedData(CMSBase): """