Skip to content

Commit 9bc0dee

Browse files
authored
Merge pull request #6 from anovik/pades
Added AddSignerChainPAdES for PAdES compatibility.
2 parents dd59b9e + a089dc2 commit 9bc0dee

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

sign.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (sd *SignedData) SetEncryptionAlgorithm(d asn1.ObjectIdentifier) {
147147
// AddSigner is a wrapper around AddSignerChain() that adds a signer without any parent.
148148
func (sd *SignedData) AddSigner(ee *x509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error {
149149
var parents []*x509.Certificate
150-
return sd.addSignerChain(ee, pkey, parents, config, true)
150+
return sd.addSignerChain(ee, pkey, parents, config, true, true)
151151
}
152152

153153
// One of the practical use cases of AddSignerNoChain is:
@@ -168,7 +168,7 @@ func (sd *SignedData) AddSigner(ee *x509.Certificate, pkey crypto.PrivateKey, co
168168
// Use this method, if no certificate needs to be placed in SignedData certificates
169169
func (sd *SignedData) AddSignerNoChain(ee *x509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error {
170170
var parents []*x509.Certificate
171-
return sd.addSignerChain(ee, pkey, parents, config, false)
171+
return sd.addSignerChain(ee, pkey, parents, config, false, true)
172172
}
173173

174174
// AddSignerChain signs attributes about the content and adds certificates
@@ -185,10 +185,29 @@ func (sd *SignedData) AddSignerNoChain(ee *x509.Certificate, pkey crypto.Private
185185
// section of the SignedData.SignerInfo, alongside the serial number of
186186
// the end-entity.
187187
func (sd *SignedData) AddSignerChain(ee *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, config SignerInfoConfig) error {
188-
return sd.addSignerChain(ee, pkey, chain, config, true)
188+
return sd.addSignerChain(ee, pkey, chain, config, true, true)
189189
}
190190

191-
func (sd *SignedData) addSignerChain(ee *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, config SignerInfoConfig, includeCertificates bool) error {
191+
// AddSignerChainPAdES signs attributes about the content and adds certificates
192+
// and signers infos to the Signed Data. The certificate and private
193+
// of the end-entity signer are used to issue the signature, and any
194+
// parent of that end-entity that need to be added to the list of
195+
// certifications can be specified in the parents slice.
196+
//
197+
// It is compatible with PAdES specifications.
198+
//
199+
// The signature algorithm used to hash the data is the one of the end-entity
200+
// certificate.
201+
//
202+
// Following RFC 2315, 9.2 SignerInfo type, the distinguished name of
203+
// the issuer of the end-entity signer is stored in the issuerAndSerialNumber
204+
// section of the SignedData.SignerInfo, alongside the serial number of
205+
// the end-entity.
206+
func (sd *SignedData) AddSignerChainPAdES(ee *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, config SignerInfoConfig) error {
207+
return sd.addSignerChain(ee, pkey, chain, config, true, false)
208+
}
209+
210+
func (sd *SignedData) addSignerChain(ee *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, config SignerInfoConfig, includeCertificates bool, enableSigningTime bool) error {
192211
sd.sd.DigestAlgorithmIdentifiers = append(sd.sd.DigestAlgorithmIdentifiers, pkix.AlgorithmIdentifier{Algorithm: sd.digestOid})
193212
hash, err := getHashForOID(sd.digestOid)
194213
if err != nil {
@@ -204,7 +223,9 @@ func (sd *SignedData) addSignerChain(ee *x509.Certificate, pkey crypto.PrivateKe
204223
attrs := &attributes{}
205224
attrs.Add(OIDAttributeContentType, sd.sd.ContentInfo.ContentType)
206225
attrs.Add(OIDAttributeMessageDigest, sd.messageDigest)
207-
attrs.Add(OIDAttributeSigningTime, time.Now())
226+
if enableSigningTime {
227+
attrs.Add(OIDAttributeSigningTime, time.Now())
228+
}
208229

209230
// Add id-aa-signing-certificate-v2.
210231
if b, err := populateSigningCertificateV2Ext(ee); err == nil {

0 commit comments

Comments
 (0)