Skip to content

Commit 2327802

Browse files
committed
refactor(est): build the certs-only message with internal/cms
The degenerate SignedData encoder moves into internal/cms, shared with SCEP and RFC 3161. CertsOnlyPKCS7 and ParseCertsOnlyPKCS7 keep their signatures and behaviour, and the output is byte-for-byte what it was. Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com>
1 parent 1f9821c commit 2327802

2 files changed

Lines changed: 11 additions & 114 deletions

File tree

‎internal/est/doc.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
//
1616
// Dependencies are stdlib only. The one thing EST needs that the standard
1717
// library does not provide is a CMS writer, and it needs exactly one shape of
18-
// it: the degenerate certs-only SignedData in pkcs7.go, which is a fixed
19-
// ASN.1 envelope with a certificate set and no signers.
18+
// it: the degenerate certs-only SignedData, a fixed ASN.1 envelope with a
19+
// certificate set and no signers. pkcs7.go builds it with internal/cms, the
20+
// hand-written CMS package EST shares with SCEP and RFC 3161.
2021
//
2122
// # Authentication, and what it does and does not prove
2223
//

‎internal/est/pkcs7.go‎

Lines changed: 8 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,17 @@ limitations under the License.
1919
*/
2020

2121
import (
22-
"encoding/asn1"
2322
"errors"
2423
"fmt"
25-
)
2624

27-
// PKCS#7 / CMS object identifiers (RFC 5652 section 3 and 4).
28-
var (
29-
oidSignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
30-
oidData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
25+
"github.com/CryptOS-PKI/cryptos/internal/cms"
3126
)
3227

33-
// contentInfo is the RFC 5652 section 3 outer wrapper. Content is [0]
34-
// EXPLICIT, which is written by hand as a constructed context tag rather than
35-
// with an `explicit` struct tag: encoding/asn1 ignores the tag options on a
36-
// RawValue and emits it verbatim, so the wrapper would silently go missing.
37-
type contentInfo struct {
38-
ContentType asn1.ObjectIdentifier
39-
Content asn1.RawValue
40-
}
41-
42-
// encapsulatedContentInfo is the RFC 5652 section 5.2 eContent wrapper. For a
43-
// certs-only message the eContent itself is absent, so only the type remains.
44-
type encapsulatedContentInfo struct {
45-
EContentType asn1.ObjectIdentifier
46-
}
47-
48-
// signedData is the RFC 5652 section 5.1 SignedData, restricted to the
49-
// degenerate certs-only shape: no digest algorithms, no encapsulated content,
50-
// and no signers. Only the certificate set carries anything.
51-
type signedData struct {
52-
Version int
53-
DigestAlgorithms []asn1.RawValue `asn1:"set"`
54-
EncapContentInfo encapsulatedContentInfo
55-
Certificates asn1.RawValue
56-
SignerInfos []asn1.RawValue `asn1:"set"`
57-
}
58-
5928
// CertsOnlyPKCS7 builds the degenerate "certs-only" SignedData that EST uses
6029
// to carry certificates (RFC 7030 sections 4.1.3 and 4.2.3; the structure is
6130
// RFC 5652 section 5.2, which explicitly blesses a SignedData with no signers
62-
// as the way to transport a certificate set).
63-
//
64-
// The Go standard library has no CMS writer, and this is the whole of what EST
65-
// needs from CMS: an ASN.1 envelope with a certificate set and nothing else.
66-
// Pulling in a PKCS#7 library to emit forty bytes of fixed structure would put
67-
// a third-party ASN.1 parser next to the CA for no benefit.
31+
// as the way to transport a certificate set). The encoding lives in
32+
// internal/cms, shared with SCEP and RFC 3161.
6833
//
6934
// certs are DER certificates in the order they should appear, conventionally
7035
// leaf first. At least one is required: an empty certs-only message is legal
@@ -73,51 +38,9 @@ func CertsOnlyPKCS7(certs [][]byte) ([]byte, error) {
7338
if len(certs) == 0 {
7439
return nil, errors.New("est: CertsOnlyPKCS7: at least one certificate is required")
7540
}
76-
77-
// CertificateSet is [0] IMPLICIT SET OF CertificateChoices. DER wants a
78-
// SET's members sorted, but RFC 5652 section 10.2.3 defines this as a SET
79-
// OF whose order is not significant, and every implementation reads it as
80-
// a sequence of certificates in the order written -- which is what lets
81-
// the chain arrive leaf-first.
82-
var certBytes []byte
83-
for i, der := range certs {
84-
if len(der) == 0 {
85-
return nil, fmt.Errorf("est: CertsOnlyPKCS7: certificate %d is empty", i)
86-
}
87-
certBytes = append(certBytes, der...)
88-
}
89-
90-
sd := signedData{
91-
// Version 1: no eContent and no version-2 attribute certificates, so
92-
// the RFC 5652 section 5.1 rules land on 1.
93-
Version: 1,
94-
DigestAlgorithms: []asn1.RawValue{},
95-
EncapContentInfo: encapsulatedContentInfo{EContentType: oidData},
96-
Certificates: asn1.RawValue{
97-
Class: asn1.ClassContextSpecific,
98-
Tag: 0,
99-
IsCompound: true,
100-
Bytes: certBytes,
101-
},
102-
SignerInfos: []asn1.RawValue{},
103-
}
104-
sdDER, err := asn1.Marshal(sd)
41+
out, err := cms.Degenerate(certs, nil)
10542
if err != nil {
106-
return nil, fmt.Errorf("est: CertsOnlyPKCS7: marshal SignedData: %w", err)
107-
}
108-
109-
ci := contentInfo{
110-
ContentType: oidSignedData,
111-
Content: asn1.RawValue{
112-
Class: asn1.ClassContextSpecific,
113-
Tag: 0,
114-
IsCompound: true,
115-
Bytes: sdDER,
116-
},
117-
}
118-
out, err := asn1.Marshal(ci)
119-
if err != nil {
120-
return nil, fmt.Errorf("est: CertsOnlyPKCS7: marshal ContentInfo: %w", err)
43+
return nil, fmt.Errorf("est: CertsOnlyPKCS7: %w", err)
12144
}
12245
return out, nil
12346
}
@@ -127,36 +50,9 @@ func CertsOnlyPKCS7(certs [][]byte) ([]byte, error) {
12750
// so a caller can read a message it was handed; nothing on the serving path
12851
// consumes CMS.
12952
func ParseCertsOnlyPKCS7(der []byte) ([][]byte, error) {
130-
var ci contentInfo
131-
rest, err := asn1.Unmarshal(der, &ci)
53+
sd, err := cms.ParseSignedData(der)
13254
if err != nil {
133-
return nil, fmt.Errorf("est: ParseCertsOnlyPKCS7: ContentInfo: %w", err)
134-
}
135-
if len(rest) != 0 {
136-
return nil, errors.New("est: ParseCertsOnlyPKCS7: trailing bytes after ContentInfo")
137-
}
138-
if !ci.ContentType.Equal(oidSignedData) {
139-
return nil, fmt.Errorf("est: ParseCertsOnlyPKCS7: content type is %s, want signedData", ci.ContentType)
55+
return nil, fmt.Errorf("est: ParseCertsOnlyPKCS7: %w", err)
14056
}
141-
if ci.Content.Class != asn1.ClassContextSpecific || ci.Content.Tag != 0 || !ci.Content.IsCompound {
142-
return nil, errors.New("est: ParseCertsOnlyPKCS7: content is not the [0] EXPLICIT wrapper")
143-
}
144-
145-
var sd signedData
146-
if _, err := asn1.Unmarshal(ci.Content.Bytes, &sd); err != nil {
147-
return nil, fmt.Errorf("est: ParseCertsOnlyPKCS7: SignedData: %w", err)
148-
}
149-
150-
var out [][]byte
151-
remaining := sd.Certificates.Bytes
152-
for len(remaining) > 0 {
153-
var one asn1.RawValue
154-
next, err := asn1.Unmarshal(remaining, &one)
155-
if err != nil {
156-
return nil, fmt.Errorf("est: ParseCertsOnlyPKCS7: certificate: %w", err)
157-
}
158-
out = append(out, one.FullBytes)
159-
remaining = next
160-
}
161-
return out, nil
57+
return sd.Certificates, nil
16258
}

0 commit comments

Comments
 (0)