示例#1
0
def CMS(data, format="PEM"):
    """
    Factory function to create CMS objects from received messages.
    
    Parses CMS data and returns either SignedData or EnvelopedData
    object. format argument can be either "PEM" or "DER".

    It determines object type from the contents of received CMS
    structure.
    """
    bio = Membio(data)
    if format == "PEM":
        ptr = libcrypto.PEM_read_bio_CMS(bio.bio, None, None, None)
    else:
        ptr = libcrypto.d2i_CMS_bio(bio.bio, None)
    if ptr is None:
        raise CMSError("Error parsing CMS data")
    typeoid = Oid(libcrypto.OBJ_obj2nid(libcrypto.CMS_get0_type(ptr)))
    if typeoid.shortname() == "pkcs7-signedData":
        return SignedData(ptr)
    elif typeoid.shortname() == "pkcs7-envelopedData":
        return EnvelopedData(ptr)
    elif typeoid.shortname() == "pkcs7-encryptedData":
        return EncryptedData(ptr)
    else:
        raise NotImplementedError("cannot handle " + typeoid.shortname())
示例#2
0
 def fromobj(obj):
     """
     Creates an OID object from the pointer to ASN1_OBJECT c structure.
     This method intended for internal use for submodules which deal
     with libcrypto ASN1 parsing functions, such as x509 or CMS
     """
     nid = libcrypto.OBJ_obj2nid(obj)
     if nid == 0:
         buf = create_string_buffer(80)
         dotted_len = libcrypto.OBJ_obj2txt(buf, 80, obj, 1)
         dotted = buf[:dotted_len]
         oid = create(dotted, dotted, dotted)
     else:
         oid = Oid(nid)
     return oid
示例#3
0
def CMS(data, format="PEM"):
    """
    Parses CMS data and returns either SignedData or EnvelopedData
    object
    """
    bio = Membio(data)
    if format == "PEM":
        ptr = libcrypto.PEM_read_bio_CMS(bio.bio, None, None, None)
    else:
        ptr = libcrypto.d2i_CMS_bio(bio.bio, None)
    if ptr is None:
        raise CMSError("Error parsing CMS data")
    typeoid = Oid(libcrypto.OBJ_obj2nid(libcrypto.CMS_get0_type(ptr)))
    if typeoid.shortname() == "pkcs7-signedData":
        return SignedData(ptr)
    elif typeoid.shortname() == "pkcs7-envelopedData":
        return EnvelopedData(ptr)
    elif typeoid.shortname() == "pkcs7-encryptedData":
        return EncryptedData(ptr)
    else:
        raise NotImplementedError("cannot handle " + typeoid.shortname())