authcms

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 7 Imported by: 0

README

authcms

Go library for decrypting CMS AuthEnvelopedData (RFC 5083).

Supports:

  • RSA-OAEP key transport + AES-256-GCM content encryption
  • Symmetric key (KEK) with AES-256-KeyWrap + AES-256-GCM content encryption

Generating test data with OpenSSL

RSA-OAEP
# 1. generate RSA keypair
openssl req -x509 -newkey rsa:2048 -keyout rsa_key.pem -out rsa_cert.pem -days 365 -nodes -subj "/CN=Test"

# 2. encrypt with OAEP
echo -n "secret_string" > ./testdata/plain.txt
openssl cms -encrypt -in ./testdata/plain.txt -out ./testdata/aes256gcm_rsa_oaep.pem \
    -outform PEM -recip ./testdata/rsa_cert.pem -aes-256-gcm \
    -keyopt rsa_padding_mode:oaep \
    -keyopt rsa_oaep_md:sha256

# or with pkcs1.5
openssl cms -encrypt \
    -in ./testdata/plain.txt \
    -out ./testdata/aes256gcm_rsa.pem \
    -outform PEM \
    -recip ./testdata/rsa_cert.pem \
    -aes-256-gcm

# 3. view
openssl cms -cmsout -in ./testdata/aes256gcm_rsa_oaep.pem -inform PEM -noout -print

# 4. decrypt with openssl
openssl cms -decrypt -inform PEM -in ./testdata/aes256gcm_rsa_oaep.pem \
    -out decrypted.txt -recip ./testdata/rsa_cert.pem -inkey ./testdata/rsa_key.pem
ECDH

TODO:

openssl cms -encrypt -aes-256-gcm \
    -originator mycert.pem -inkey mykey.pem \
    -recip reccert.pem \
    -keyopt ecdh_kdf_md:sha256 \
    -in plain.txt -out auth_ecdh.pem -outform PEM
Symmetric key (KEK)
# 1. generate a 256-bit symmetric key
openssl rand -hex 32
# output: be788673a432901d7f1d9f42380b93c47b06e2b8f132617ae0821a7a45d47fd8

# 2. encrypt
openssl cms -encrypt \
    -in ./testdata/plain.txt -out ./testdata/aes256gcm_kek.pem \
    -secretkey "be788673a432901d7f1d9f42380b93c47b06e2b8f132617ae0821a7a45d47fd8" \
    -secretkeyid "01" -aes-256-gcm -outform PEM
    
# 3. decrypt
openssl cms -decrypt \
    -in auth_pw.pem \
    -inform PEM \
    -secretkey "be788673a432901d7f1d9f42380b93c47b06e2b8f132617ae0821a7a45d47fd8" \
    -secretkeyid "01" \
    -out decrypted.txt

Go API

import "authcms"

aed, err := ParseAuthEnvelopedData(cms)

// decrypt DER-encoded AuthEnvelopedData with RSA
plaintext, err := aed.DecryptWithRSA(key)

// decrypt DER-encoded AuthEnvelopedData with symmetric key
plaintext, err := authcms.DecryptWithKEK(kek, kekID)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlgorithmIdentifier

type AlgorithmIdentifier struct {
	Algorithm  asn1.ObjectIdentifier
	Parameters asn1.RawValue `asn1:"optional"`
}

type AuthEnvelopedData

type AuthEnvelopedData struct {
	RecipientInfos []RecipientInfo
	EncContentInfo EncryptedContentInfo
	MAC            []byte
	AuthAttrs      []byte
}

func ParseAuthEnvelopedData

func ParseAuthEnvelopedData(der []byte) (*AuthEnvelopedData, error)

func (*AuthEnvelopedData) DecryptWithKEK

func (aed *AuthEnvelopedData) DecryptWithKEK(kek []byte, kekID []byte) ([]byte, error)

func (*AuthEnvelopedData) DecryptWithRSA

func (aed *AuthEnvelopedData) DecryptWithRSA(key *rsa.PrivateKey) ([]byte, error)

type EncryptedContentInfo

type EncryptedContentInfo struct {
	ContentType      asn1.ObjectIdentifier
	ContentEncAlgo   AlgorithmIdentifier
	EncryptedContent []byte `asn1:"optional,tag:0,implicit"`
}

type KEKRecipientInfo

type KEKRecipientInfo struct {
	Version           int
	KEKID             asn1.RawValue
	KeyEncryptionAlgo AlgorithmIdentifier
	EncryptedKey      []byte
}

type KeyTransRecipientInfo

type KeyTransRecipientInfo struct {
	Version           int
	RID               asn1.RawValue
	KeyEncryptionAlgo AlgorithmIdentifier
	EncryptedKey      []byte
}

type RecipientInfo

type RecipientInfo struct {
	Raw   asn1.RawValue
	KTRI  *KeyTransRecipientInfo
	KEKRI *KEKRecipientInfo
}

func (*RecipientInfo) IsKEK

func (ri *RecipientInfo) IsKEK() bool

func (*RecipientInfo) IsKeyTrans

func (ri *RecipientInfo) IsKeyTrans() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL