fullsailor / pkcs7

Implements a subset of PKCS#7/Crytpographic Message Syntax (rfc2315, rfc5652)
MIT License
123 stars 201 forks source link

Question about sorting #43

Closed doshmajhan closed 4 years ago

doshmajhan commented 4 years ago

Looking at the code I notice when you prepare the attribute set for marshalling the attributes themselves get sorted based on the marshalled value of the attribute itself:

func (attrs *attributes) ForMarshaling() ([]attribute, error) {
    sortables := make(attributeSet, len(attrs.types))
    for i := range sortables {
        attrType := attrs.types[i]
        attrValue := attrs.values[i]
        asn1Value, err := asn1.Marshal(attrValue)
        if err != nil {
            return nil, err
        }
        attr := attribute{
            Type:  attrType,
            Value: asn1.RawValue{Tag: 17, IsCompound: true, Bytes: asn1Value}, // 17 == SET tag
        }
        encoded, err := asn1.Marshal(attr)
        if err != nil {
            return nil, err
        }
        sortables[i] = sortableAttribute{
            SortKey:   encoded,
            Attribute: attr,
        }
    }
    sort.Sort(sortables)
    return sortables.Attributes(), nil
}

From reading the CMS RFC I can't see why the attributes need to be sorted, and when changing the code to not have them sorted, validation begins to fail. Wondering if you can provide some insight into why the sorting needs to occur.