

#include <openssl/asn1t.h>
#include <openssl/pem.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/cms.h>
#include "internal/nelem.h"
#include "crypto/x509.h"
#include "cms_local.h"



#define CMS_ATTR_F_SIGNED         0x01

#define CMS_ATTR_F_UNSIGNED       0x02

#define CMS_ATTR_F_REQUIRED_COND  0x10

#define CMS_ATTR_F_ONLY_ONE       0x20

#define CMS_ATTR_F_ONE_ATTR_VALUE 0x40


static const struct {
    int nid;   
    int flags;
} cms_attribute_properties[] = {
    
    { NID_pkcs9_contentType, CMS_ATTR_F_SIGNED
                             | CMS_ATTR_F_ONLY_ONE
                             | CMS_ATTR_F_ONE_ATTR_VALUE
                             | CMS_ATTR_F_REQUIRED_COND },
    { NID_pkcs9_messageDigest, CMS_ATTR_F_SIGNED
                               | CMS_ATTR_F_ONLY_ONE
                               | CMS_ATTR_F_ONE_ATTR_VALUE
                               | CMS_ATTR_F_REQUIRED_COND },
    { NID_pkcs9_signingTime, CMS_ATTR_F_SIGNED
                             | CMS_ATTR_F_ONLY_ONE
                             | CMS_ATTR_F_ONE_ATTR_VALUE },
    { NID_pkcs9_countersignature, CMS_ATTR_F_UNSIGNED },
    
    { NID_id_smime_aa_signingCertificate, CMS_ATTR_F_SIGNED
                                          | CMS_ATTR_F_ONLY_ONE
                                          | CMS_ATTR_F_ONE_ATTR_VALUE },
    { NID_id_smime_aa_signingCertificateV2, CMS_ATTR_F_SIGNED
                                            | CMS_ATTR_F_ONLY_ONE
                                            | CMS_ATTR_F_ONE_ATTR_VALUE },
    { NID_id_smime_aa_receiptRequest, CMS_ATTR_F_SIGNED
                                      | CMS_ATTR_F_ONLY_ONE
                                      | CMS_ATTR_F_ONE_ATTR_VALUE }
};



int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
{
    return X509at_get_attr_count(si->signedAttrs);
}

int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
{
    return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
}

int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
                               int lastpos)
{
    return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
}

X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
{
    return X509at_get_attr(si->signedAttrs, loc);
}

X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
{
    return X509at_delete_attr(si->signedAttrs, loc);
}

int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
{
    if (ossl_x509at_add1_attr(&si->signedAttrs, attr))
        return 1;
    return 0;
}

int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
                                const ASN1_OBJECT *obj, int type,
                                const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
        return 1;
    return 0;
}

int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
                                int nid, int type, const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
        return 1;
    return 0;
}

int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
                                const char *attrname, int type,
                                const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes,
                                     len))
        return 1;
    return 0;
}

void *CMS_signed_get0_data_by_OBJ(const CMS_SignerInfo *si,
                                  const ASN1_OBJECT *oid,
                                  int lastpos, int type)
{
    return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
}

int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
{
    return X509at_get_attr_count(si->unsignedAttrs);
}

int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
                                 int lastpos)
{
    return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
}

int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
                                 const ASN1_OBJECT *obj, int lastpos)
{
    return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
}

X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
{
    return X509at_get_attr(si->unsignedAttrs, loc);
}

X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
{
    return X509at_delete_attr(si->unsignedAttrs, loc);
}

int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
{
    if (ossl_x509at_add1_attr(&si->unsignedAttrs, attr))
        return 1;
    return 0;
}

int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
                                  const ASN1_OBJECT *obj, int type,
                                  const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
        return 1;
    return 0;
}

int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
                                  int nid, int type,
                                  const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
        return 1;
    return 0;
}

int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
                                  const char *attrname, int type,
                                  const void *bytes, int len)
{
    if (ossl_x509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
                                     type, bytes, len))
        return 1;
    return 0;
}

void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
                                    int lastpos, int type)
{
    return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
}


static X509_ATTRIBUTE *cms_attrib_get(int nid,
                                      const STACK_OF(X509_ATTRIBUTE) *attrs,
                                      int *lastpos)
{
    X509_ATTRIBUTE *at;
    int loc;

    loc = X509at_get_attr_by_NID(attrs, nid, *lastpos);
    if (loc < 0)
        return NULL;

    at = X509at_get_attr(attrs, loc);
    *lastpos = loc;
    return at;
}

static int cms_check_attribute(int nid, int flags, int type,
                               const STACK_OF(X509_ATTRIBUTE) *attrs,
                               int have_attrs)
{
    int lastpos = -1;
    X509_ATTRIBUTE *at = cms_attrib_get(nid, attrs, &lastpos);

    if (at != NULL) {
        int count = X509_ATTRIBUTE_count(at);

        
        if (((flags & type) == 0)
            
            || (((flags & CMS_ATTR_F_ONLY_ONE) != 0)
                && cms_attrib_get(nid, attrs, &lastpos) != NULL)
            
            || (((flags & CMS_ATTR_F_ONE_ATTR_VALUE) != 0)
                && count != 1)
            
            || count == 0)
        return 0;
    } else {
        
        if (have_attrs
            && ((flags & CMS_ATTR_F_REQUIRED_COND) != 0)
            && (flags & type) != 0)
            return 0;
    }
    return 1;
}


int ossl_cms_si_check_attributes(const CMS_SignerInfo *si)
{
    int i;
    int have_signed_attrs = (CMS_signed_get_attr_count(si) > 0);
    int have_unsigned_attrs = (CMS_unsigned_get_attr_count(si) > 0);

    for (i = 0; i < (int)OSSL_NELEM(cms_attribute_properties); ++i) {
        int nid = cms_attribute_properties[i].nid;
        int flags = cms_attribute_properties[i].flags;

        if (!cms_check_attribute(nid, flags, CMS_ATTR_F_SIGNED,
                                 si->signedAttrs, have_signed_attrs)
            || !cms_check_attribute(nid, flags, CMS_ATTR_F_UNSIGNED,
                                    si->unsignedAttrs, have_unsigned_attrs)) {
            ERR_raise(ERR_LIB_CMS, CMS_R_ATTRIBUTE_ERROR);
            return 0;
        }
    }
    return 1;
}
