| 1 | package pkcs7scip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.19 bytes/" |
| 5 | "crypto/aesscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/" |
| 6 | "crypto/cipherscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/" |
| 7 | "crypto/desscip-go gomod github.com/golang/go/src go1.19 `crypto/des`/" |
| 8 | "crypto/randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/" |
| 9 | "crypto/rsascip-go gomod github.com/golang/go/src go1.19 `crypto/rsa`/" |
| 10 | "crypto/x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/" |
| 11 | "crypto/x509/pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/" |
| 12 | "encoding/asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/" |
| 13 | "errorsscip-go gomod github.com/golang/go/src go1.19 errors/" |
| 14 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 15 | ) |
| 16 | |
| 17 | type envelopedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData# struct { |
| 18 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#Version. int |
| 19 | RecipientInfosscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#RecipientInfos. []recipientInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo# `asn1:"set"` |
| 20 | EncryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#EncryptedContentInfo. encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo# |
| 21 | } |
| 22 | |
| 23 | type encryptedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData# struct { |
| 24 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData#Version. int |
| 25 | EncryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData#EncryptedContentInfo. encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo# |
| 26 | } |
| 27 | |
| 28 | type recipientInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo# struct { |
| 29 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#Version. int |
| 30 | IssuerAndSerialNumberscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#IssuerAndSerialNumber. issuerAndSerialscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/issuerAndSerial# |
| 31 | KeyEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#KeyEncryptionAlgorithm. pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier# |
| 32 | EncryptedKeyscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#EncryptedKey. []byte |
| 33 | } |
| 34 | |
| 35 | type encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo# struct { |
| 36 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentType. asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier# |
| 37 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm. pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier# |
| 38 | EncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#EncryptedContent. asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue# `asn1:"tag:0,optional"` |
| 39 | } |
| 40 | |
| 41 | const ( |
| 42 | // EncryptionAlgorithmDESCBC is the DES CBC encryption algorithm |
| 43 | EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC. = iota |
| 44 | |
| 45 | // EncryptionAlgorithmAES128CBC is the AES 128 bits with CBC encryption algorithm |
| 46 | // Avoid this algorithm unless required for interoperability; use AES GCM instead. |
| 47 | EncryptionAlgorithmAES128CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128CBC. |
| 48 | |
| 49 | // EncryptionAlgorithmAES256CBC is the AES 256 bits with CBC encryption algorithm |
| 50 | // Avoid this algorithm unless required for interoperability; use AES GCM instead. |
| 51 | EncryptionAlgorithmAES256CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256CBC. |
| 52 | |
| 53 | // EncryptionAlgorithmAES128GCM is the AES 128 bits with GCM encryption algorithm |
| 54 | EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM. |
| 55 | |
| 56 | // EncryptionAlgorithmAES256GCM is the AES 256 bits with GCM encryption algorithm |
| 57 | EncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256GCM. |
| 58 | ) |
| 59 | |
| 60 | // ContentEncryptionAlgorithm determines the algorithm used to encrypt the |
| 61 | // plaintext message. Change the value of this variable to change which |
| 62 | // algorithm is used in the Encrypt() function. |
| 63 | var ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. = EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC. |
| 64 | |
| 65 | // ErrUnsupportedEncryptionAlgorithm is returned when attempting to encrypt |
| 66 | // content with an unsupported algorithm. |
| 67 | var ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrUnsupportedEncryptionAlgorithm. = errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("pkcs7: cannot encrypt content: only DES-CBC, AES-CBC, and AES-GCM supported") |
| 68 | |
| 69 | // ErrPSKNotProvided is returned when attempting to encrypt |
| 70 | // using a PSK without actually providing the PSK. |
| 71 | var ErrPSKNotProvidedscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrPSKNotProvided. = errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("pkcs7: cannot encrypt content: PSK not provided") |
| 72 | |
| 73 | const nonceSizescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/nonceSize. = 12 |
| 74 | |
| 75 | type aesGCMParametersscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters# struct { |
| 76 | Noncescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters#Nonce. []byte `asn1:"tag:4"` |
| 77 | ICVLenscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters#ICVLen. int |
| 78 | } |
| 79 | |
| 80 | func encryptAESGCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptAESGCM().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#, error) { |
| 81 | var keyLen int |
| 82 | var algID asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier# |
| 83 | switch ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. { |
| 84 | case EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM.: |
| 85 | keyLen = 16 |
| 86 | algID = OIDEncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmAES128GCM. |
| 87 | case EncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256GCM.: |
| 88 | keyLen = 32 |
| 89 | algID = OIDEncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmAES256GCM. |
| 90 | default: |
| 91 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("invalid ContentEncryptionAlgorithm in encryptAESGCM: %d", ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm.) |
| 92 | } |
| 93 | if key == nil { |
| 94 | // Create AES key |
| 95 | key = make([]byte, keyLen) |
| 96 | |
| 97 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(key) |
| 98 | if err != nil { |
| 99 | return nil, nil, err |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Create nonce |
| 104 | nonce := make([]byte, nonceSizescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/nonceSize.) |
| 105 | |
| 106 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(nonce) |
| 107 | if err != nil { |
| 108 | return nil, nil, err |
| 109 | } |
| 110 | |
| 111 | // Encrypt content |
| 112 | block, err := aesscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/.NewCipherscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/NewCipher().(key) |
| 113 | if err != nil { |
| 114 | return nil, nil, err |
| 115 | } |
| 116 | |
| 117 | gcm, err := cipherscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/.NewGCMscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/NewGCM().(block) |
| 118 | if err != nil { |
| 119 | return nil, nil, err |
| 120 | } |
| 121 | |
| 122 | ciphertext := gcm.Sealscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/AEAD#Seal().(nil, nonce, content, nil) |
| 123 | |
| 124 | // Prepare ASN.1 Encrypted Content Info |
| 125 | paramSeq := aesGCMParametersscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters#{ |
| 126 | Noncescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters#Nonce.: nonce, |
| 127 | ICVLenscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/aesGCMParameters#ICVLen.: gcm.Overheadscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/AEAD#Overhead().(), |
| 128 | } |
| 129 | |
| 130 | paramBytes, err := asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(paramSeq) |
| 131 | if err != nil { |
| 132 | return nil, nil, err |
| 133 | } |
| 134 | |
| 135 | eci := encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#{ |
| 136 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDData., |
| 137 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#{ |
| 138 | Algorithmscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algID, |
| 139 | Parametersscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{ |
| 140 | Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.TagSequencescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/TagSequence., |
| 141 | Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: paramBytes, |
| 142 | }, |
| 143 | }, |
| 144 | EncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/marshalEncryptedContent().(ciphertext), |
| 145 | } |
| 146 | |
| 147 | return key, &eci, nil |
| 148 | } |
| 149 | |
| 150 | func encryptDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptDESCBC().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#, error) { |
| 151 | if key == nil { |
| 152 | // Create DES key |
| 153 | key = make([]byte, 8) |
| 154 | |
| 155 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(key) |
| 156 | if err != nil { |
| 157 | return nil, nil, err |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Create CBC IV |
| 162 | iv := make([]byte, desscip-go gomod github.com/golang/go/src go1.19 `crypto/des`/.BlockSizescip-go gomod github.com/golang/go/src go1.19 `crypto/des`/BlockSize.) |
| 163 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(iv) |
| 164 | if err != nil { |
| 165 | return nil, nil, err |
| 166 | } |
| 167 | |
| 168 | // Encrypt padded content |
| 169 | block, err := desscip-go gomod github.com/golang/go/src go1.19 `crypto/des`/.NewCipherscip-go gomod github.com/golang/go/src go1.19 `crypto/des`/NewCipher().(key) |
| 170 | if err != nil { |
| 171 | return nil, nil, err |
| 172 | } |
| 173 | mode := cipherscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/.NewCBCEncrypterscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/NewCBCEncrypter().(block, iv) |
| 174 | plaintext, err := padscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/pad().(content, mode.BlockSizescip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/BlockMode#BlockSize().()) |
| 175 | if err != nil { |
| 176 | return nil, nil, err |
| 177 | } |
| 178 | cyphertext := make([]byte, len(plaintext)) |
| 179 | mode.CryptBlocksscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/BlockMode#CryptBlocks().(cyphertext, plaintext) |
| 180 | |
| 181 | // Prepare ASN.1 Encrypted Content Info |
| 182 | eci := encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#{ |
| 183 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDData., |
| 184 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#{ |
| 185 | Algorithmscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: OIDEncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmDESCBC., |
| 186 | Parametersscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: 4, Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: iv}, |
| 187 | }, |
| 188 | EncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/marshalEncryptedContent().(cyphertext), |
| 189 | } |
| 190 | |
| 191 | return key, &eci, nil |
| 192 | } |
| 193 | |
| 194 | func encryptAESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptAESCBC().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#, error) { |
| 195 | var keyLen int |
| 196 | var algID asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier# |
| 197 | switch ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. { |
| 198 | case EncryptionAlgorithmAES128CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128CBC.: |
| 199 | keyLen = 16 |
| 200 | algID = OIDEncryptionAlgorithmAES128CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmAES128CBC. |
| 201 | case EncryptionAlgorithmAES256CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256CBC.: |
| 202 | keyLen = 32 |
| 203 | algID = OIDEncryptionAlgorithmAES256CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmAES256CBC. |
| 204 | default: |
| 205 | return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("invalid ContentEncryptionAlgorithm in encryptAESCBC: %d", ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm.) |
| 206 | } |
| 207 | |
| 208 | if key == nil { |
| 209 | // Create AES key |
| 210 | key = make([]byte, keyLen) |
| 211 | |
| 212 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(key) |
| 213 | if err != nil { |
| 214 | return nil, nil, err |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Create CBC IV |
| 219 | iv := make([]byte, aesscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/.BlockSizescip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/BlockSize.) |
| 220 | _, err := randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Read().(iv) |
| 221 | if err != nil { |
| 222 | return nil, nil, err |
| 223 | } |
| 224 | |
| 225 | // Encrypt padded content |
| 226 | block, err := aesscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/.NewCipherscip-go gomod github.com/golang/go/src go1.19 `crypto/aes`/NewCipher().(key) |
| 227 | if err != nil { |
| 228 | return nil, nil, err |
| 229 | } |
| 230 | mode := cipherscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/.NewCBCEncrypterscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/NewCBCEncrypter().(block, iv) |
| 231 | plaintext, err := padscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/pad().(content, mode.BlockSizescip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/BlockMode#BlockSize().()) |
| 232 | if err != nil { |
| 233 | return nil, nil, err |
| 234 | } |
| 235 | cyphertext := make([]byte, len(plaintext)) |
| 236 | mode.CryptBlocksscip-go gomod github.com/golang/go/src go1.19 `crypto/cipher`/BlockMode#CryptBlocks().(cyphertext, plaintext) |
| 237 | |
| 238 | // Prepare ASN.1 Encrypted Content Info |
| 239 | eci := encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#{ |
| 240 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDData., |
| 241 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#{ |
| 242 | Algorithmscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algID, |
| 243 | Parametersscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: 4, Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: iv}, |
| 244 | }, |
| 245 | EncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/marshalEncryptedContent().(cyphertext), |
| 246 | } |
| 247 | |
| 248 | return key, &eci, nil |
| 249 | } |
| 250 | |
| 251 | // Encrypt creates and returns an envelope data PKCS7 structure with encrypted |
| 252 | // recipient keys for each recipient public key. |
| 253 | // |
| 254 | // The algorithm used to perform encryption is determined by the current value |
| 255 | // of the global ContentEncryptionAlgorithm package variable. By default, the |
| 256 | // value is EncryptionAlgorithmDESCBC. To use a different algorithm, change the |
| 257 | // value before calling Encrypt(). For example: |
| 258 | // |
| 259 | // ContentEncryptionAlgorithm = EncryptionAlgorithmAES128GCM |
| 260 | // |
| 261 | // TODO(fullsailor): Add support for encrypting content with other algorithms |
| 262 | func Encryptscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/Encrypt().(content []byte, recipients []*x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#) ([]byte, error) { |
| 263 | var eci *encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo# |
| 264 | var key []byte |
| 265 | var err error |
| 266 | |
| 267 | // Apply chosen symmetric encryption method |
| 268 | switch ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. { |
| 269 | case EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC.: |
| 270 | key, eci, err = encryptDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptDESCBC().(content, nil) |
| 271 | case EncryptionAlgorithmAES128CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128CBC.: |
| 272 | fallthrough |
| 273 | case EncryptionAlgorithmAES256CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256CBC.: |
| 274 | key, eci, err = encryptAESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptAESCBC().(content, nil) |
| 275 | case EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM.: |
| 276 | fallthrough |
| 277 | case EncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256GCM.: |
| 278 | key, eci, err = encryptAESGCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptAESGCM().(content, nil) |
| 279 | |
| 280 | default: |
| 281 | return nil, ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrUnsupportedEncryptionAlgorithm. |
| 282 | } |
| 283 | |
| 284 | if err != nil { |
| 285 | return nil, err |
| 286 | } |
| 287 | |
| 288 | // Prepare each recipient's encrypted cipher key |
| 289 | recipientInfos := make([]recipientInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#, len(recipients)) |
| 290 | for i, recipient := range recipients { |
| 291 | encrypted, err := encryptKeyscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptKey().(key, recipient) |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
| 295 | ias := cert2issuerAndSerialscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/cert2issuerAndSerial().(recipient) |
| 296 | info := recipientInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#{ |
| 297 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#Version.: 0, |
| 298 | IssuerAndSerialNumberscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#IssuerAndSerialNumber.: ias, |
| 299 | KeyEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#KeyEncryptionAlgorithm.: pkixscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#{ |
| 300 | Algorithmscip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: OIDEncryptionAlgorithmRSAscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptionAlgorithmRSA., |
| 301 | }, |
| 302 | EncryptedKeyscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/recipientInfo#EncryptedKey.: encrypted, |
| 303 | } |
| 304 | recipientInfos[i] = info |
| 305 | } |
| 306 | |
| 307 | // Prepare envelope content |
| 308 | envelope := envelopedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#{ |
| 309 | EncryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#EncryptedContentInfo.: *eci, |
| 310 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#Version.: 0, |
| 311 | RecipientInfosscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/envelopedData#RecipientInfos.: recipientInfos, |
| 312 | } |
| 313 | innerContent, err := asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(envelope) |
| 314 | if err != nil { |
| 315 | return nil, err |
| 316 | } |
| 317 | |
| 318 | // Prepare outer payload structure |
| 319 | wrapper := contentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#{ |
| 320 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#ContentType.: OIDEnvelopedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEnvelopedData., |
| 321 | Contentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#Content.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{Classscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Class.: 2, Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: 0, IsCompoundscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#IsCompound.: true, Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: innerContent}, |
| 322 | } |
| 323 | |
| 324 | return asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(wrapper) |
| 325 | } |
| 326 | |
| 327 | // EncryptUsingPSK creates and returns an encrypted data PKCS7 structure, |
| 328 | // encrypted using caller provided pre-shared secret. |
| 329 | func EncryptUsingPSKscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptUsingPSK().(content []byte, key []byte) ([]byte, error) { |
| 330 | var eci *encryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedContentInfo# |
| 331 | var err error |
| 332 | |
| 333 | if key == nil { |
| 334 | return nil, ErrPSKNotProvidedscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrPSKNotProvided. |
| 335 | } |
| 336 | |
| 337 | // Apply chosen symmetric encryption method |
| 338 | switch ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. { |
| 339 | case EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC.: |
| 340 | _, eci, err = encryptDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptDESCBC().(content, key) |
| 341 | |
| 342 | case EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM.: |
| 343 | fallthrough |
| 344 | case EncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256GCM.: |
| 345 | _, eci, err = encryptAESGCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptAESGCM().(content, key) |
| 346 | |
| 347 | default: |
| 348 | return nil, ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrUnsupportedEncryptionAlgorithm. |
| 349 | } |
| 350 | |
| 351 | if err != nil { |
| 352 | return nil, err |
| 353 | } |
| 354 | |
| 355 | // Prepare encrypted-data content |
| 356 | ed := encryptedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData#{ |
| 357 | Versionscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData#Version.: 0, |
| 358 | EncryptedContentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptedData#EncryptedContentInfo.: *eci, |
| 359 | } |
| 360 | innerContent, err := asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(ed) |
| 361 | if err != nil { |
| 362 | return nil, err |
| 363 | } |
| 364 | |
| 365 | // Prepare outer payload structure |
| 366 | wrapper := contentInfoscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#{ |
| 367 | ContentTypescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#ContentType.: OIDEncryptedDatascip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/OIDEncryptedData., |
| 368 | Contentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/contentInfo#Content.: asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{Classscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Class.: 2, Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: 0, IsCompoundscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#IsCompound.: true, Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: innerContent}, |
| 369 | } |
| 370 | |
| 371 | return asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(wrapper) |
| 372 | } |
| 373 | |
| 374 | func marshalEncryptedContentscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/marshalEncryptedContent().(content []byte) asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue# { |
| 375 | asn1Content, _ := asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/Marshal().(content) |
| 376 | return asn1scip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#{Tagscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Tag.: 0, Classscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Class.: 2, Bytesscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#Bytes.: asn1Content, IsCompoundscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/RawValue#IsCompound.: true} |
| 377 | } |
| 378 | |
| 379 | func encryptKeyscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/encryptKey().(key []byte, recipient *x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#) ([]byte, error) { |
| 380 | if pub := recipient.PublicKeyscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#PublicKey..(*rsascip-go gomod github.com/golang/go/src go1.19 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.19 `crypto/rsa`/PublicKey#); pub != nil { |
| 381 | return rsascip-go gomod github.com/golang/go/src go1.19 `crypto/rsa`/.EncryptPKCS1v15scip-go gomod github.com/golang/go/src go1.19 `crypto/rsa`/EncryptPKCS1v15().(randscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.19 `crypto/rand`/Reader., pub, key) |
| 382 | } |
| 383 | return nil, ErrUnsupportedAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ErrUnsupportedAlgorithm. |
| 384 | } |
| 385 | |
| 386 | func padscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/pad().(data []byte, blocklen int) ([]byte, error) { |
| 387 | if blocklen < 1 { |
| 388 | return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("invalid blocklen %d", blocklen) |
| 389 | } |
| 390 | padlen := blocklen - (len(data) % blocklen) |
| 391 | if padlen == 0 { |
| 392 | padlen = blocklen |
| 393 | } |
| 394 | pad := bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.Repeatscip-go gomod github.com/golang/go/src go1.19 bytes/Repeat().([]byte{byte(padlen)}, padlen) |
| 395 | return append(data, pad...), nil |
| 396 | } |