.github
Bump actions/setup-go from 6.5.0 to 7.0.0internal
Internalize `cryptobyte` package.editorconfig
editorconfig: merged the space-indented sections.gitignore
Add `.envrc` and `coverage.out` to `.gitignore`ber_test.go
Fix out-of-bounds panic in ber2der on malformed BER inputber.go
Fix out-of-bounds panic in ber2der on malformed BER inputdecrypt_test.go
Make all `RSA PRIVATE KEY`decrypt.go
Fix error prefixes, asymmetric encryption algorithm errorencrypt_test.go
Add encryption test with decryption by OpenSSLencrypt.go
`EncryptedContent` needs to be implicitly tagged and not explicitgo.mod
Internalize `cryptobyte` packagego.sum
Internalize `cryptobyte` packageLICENSE
Initial commitMakefile
Update go-tools usage to fix issues causing CI breakage. Address issues raised by new go-tools.parse_go1.23_test.go
Reset legacy X509 parser on test failureparse_test.go
Fix missing `+build` comment for older Go versionspkcs7_test.go
Make all `RSA PRIVATE KEY`pkcs7.go
Change Hasher to use io.ReaderREADME.md
README: amended an indentation issuesign_test.go
Add test for `SetDefaultDigestAlgorithm`sign.go
Add test for `SetDefaultDigestAlgorithm`verify_dsa_go1.15_test.go
Add comment regarding DSA tests on Go < 1.16verify_test.go
Remove crypto hash String() call also from testverify.go
Do not use crypto Hash's String() as it is not available in go 1.14pkcs7
pkcs7 implements parsing and creating signed and enveloped messages.
package main
import (
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"github.com/smallstep/pkcs7"
)
func SignAndDetach(content []byte, cert *x509.Certificate, privkey *rsa.PrivateKey) (signed []byte, err error) {
toBeSigned, err := NewSignedData(content)
if err != nil {
return fmt.Errorf("Cannot initialize signed data: %w", err)
}
if err = toBeSigned.AddSigner(cert, privkey, SignerInfoConfig{}); err != nil {
return fmt.Errorf("Cannot add signer: %w", err)
}
// Detach signature, omit if you want an embedded signature
toBeSigned.Detach()
signed, err = toBeSigned.Finish()
if err != nil {
return fmt.Errorf("Cannot finish signing data: %w", err)
}
// Verify the signature
pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: signed})
p7, err := pkcs7.Parse(signed)
if err != nil {
return fmt.Errorf("Cannot parse our signed data: %w", err)
}
// since the signature was detached, reattach the content here
p7.Content = content
if bytes.Compare(content, p7.Content) != 0 {
return fmt.Errorf("Our content was not in the parsed data:\n\tExpected: %s\n\tActual: %s", content, p7.Content)
}
if err = p7.Verify(); err != nil {
return fmt.Errorf("Cannot verify our signed data: %w", err)
}
return signed, nil
}Credits
This is a fork of mozilla-services/pkcs7 which, itself, was a fork of fullsailor/pkcs7.