Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetDefaultDigestAlgorithm #29

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,51 @@ import (
"errors"
"fmt"
"math/big"
"sync"
"time"
)

// SetDefaultDigestAlgorithm sets the default digest algorithm
// to be used for signing operations on [SignedData].
//
// This must be called before creating a new instance of [SignedData]
// using [NewSignedData].
func SetDefaultDigestAlgorithm(d asn1.ObjectIdentifier) error {
defaultMessageDigestAlgorithm.Lock()
defer defaultMessageDigestAlgorithm.Unlock()
switch {
case d.Equal(OIDDigestAlgorithmSHA1), d.Equal(OIDDigestAlgorithmSHA256), d.Equal(OIDDigestAlgorithmSHA384),
d.Equal(OIDDigestAlgorithmSHA512), d.Equal(OIDDigestAlgorithmSHA224),
d.Equal(OIDDigestAlgorithmDSA), d.Equal(OIDDigestAlgorithmDSASHA1),
d.Equal(OIDDigestAlgorithmECDSASHA1), d.Equal(OIDDigestAlgorithmECDSASHA256),
d.Equal(OIDDigestAlgorithmECDSASHA384), d.Equal(OIDDigestAlgorithmECDSASHA512):
break
default:
return fmt.Errorf("unsupported message digest algorithm %v", d)
}

defaultMessageDigestAlgorithm.oid = d

return nil
}

var defaultMessageDigestAlgorithm struct {
sync.RWMutex
oid asn1.ObjectIdentifier
}

func defaultMessageDigestAlgorithmOID() asn1.ObjectIdentifier {
defaultMessageDigestAlgorithm.RLock()
defer defaultMessageDigestAlgorithm.RUnlock()

oid := defaultMessageDigestAlgorithm.oid
if oid.Equal(asn1.ObjectIdentifier{}) {
return OIDDigestAlgorithmSHA1
}

return oid
}

// SignedData is an opaque data structure for creating signed data payloads
type SignedData struct {
sd signedData
Expand All @@ -39,7 +81,7 @@ func NewSignedData(data []byte) (*SignedData, error) {
ContentInfo: ci,
Version: 1,
}
return &SignedData{sd: sd, data: data, digestOid: OIDDigestAlgorithmSHA1}, nil
return &SignedData{sd: sd, data: data, digestOid: defaultMessageDigestAlgorithmOID()}, nil
}

// SignerInfoConfig are optional values to include when adding a signer
Expand Down
Loading