containerd/vendor/github.com/smallstep/pkcs7
Kirtana Ashok 9d5cfce833 Update github.com/containerd/imgcrypt to v2.0.0
Signed-off-by: Kirtana Ashok <kiashok@microsoft.com>
2025-01-30 10:09:20 -08:00
..
internal/legacy/x509 Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
.gitignore Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
ber.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
decrypt.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
encrypt.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
LICENSE Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
Makefile Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
pkcs7.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
README.md Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
sign.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00
verify.go Update github.com/containerd/imgcrypt to v2.0.0 2025-01-30 10:09:20 -08:00

pkcs7

Go Reference Build Status

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.