pkcs7_decrypt.c
Go to the documentation of this file.
1 /**
2  * @file pkcs7_decrypt.c
3  * @brief PKCS #7 message decryption
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "pkcs7/pkcs7_parse.h"
37 #include "pkcs7/pkcs7_decrypt.h"
39 #include "cipher_modes/cbc.h"
40 #include "encoding/oid.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (PKCS7_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Decrypt enveloped-data content
49  * @param[in] envelopedData Pointer to the enveloped-data content
50  * @param[in] recipientCertInfo Recipient's certificate
51  * @param[in] recipientPrivateKey Recipient's private key
52  * @param[out] plaintext Plaintext resulting from the decryption operation
53  * @param[out] plaintextLen Length of the resulting plaintext
54  * @return Error code
55  **/
56 
58  const X509CertInfo *recipientCertInfo, const void *recipientPrivateKey,
59  uint8_t *plaintext, size_t *plaintextLen)
60 {
61  error_t error;
62  size_t keyLen;
63  uint8_t key[PKCS7_MAX_ENCR_KEY_SIZE];
64  Pkcs7RecipientInfo recipientInfo;
65 
66  //recipientInfos is a collection of per-recipient information. There must be
67  //at least one element in the collection (refer to RFC 2315, section 10.1)
68  error = pkcs7FindRecipient(&envelopedData->recipientInfos, recipientCertInfo,
69  &recipientInfo);
70 
71  //Any matching recipientInfo entry?
72  if(!error)
73  {
74  //Perform key decryption
75  error = pkcs7DecryptKey(&recipientInfo, recipientPrivateKey, key, &keyLen);
76 
77  //Check status code
78  if(!error)
79  {
80  //Perform data decryption
81  error = pkcs7DecryptData(&envelopedData->encryptedContentInfo, key,
82  keyLen, plaintext, plaintextLen);
83  }
84  }
85 
86  //Return status code
87  return error;
88 }
89 
90 
91 /**
92  * @brief Perform key decryption
93  * @param[in] recipientInfo Pointer to the RecipientInfo structure
94  * @param[in] recipientPrivateKey Recipient's private key
95  * @param[out] plaintext Key resulting from the decryption operation
96  * @param[out] plaintextLen Length of the resulting key
97  * @return Error code
98  **/
99 
101  const void *recipientPrivateKey, uint8_t *plaintext, size_t *plaintextLen)
102 {
103  error_t error;
104 
105 #if (PKCS7_RSA_SUPPORT == ENABLED && RSA_SUPPORT == ENABLED)
106  //RSA encryption algorithm?
107  if(OID_COMP(recipientInfo->keyEncryptionAlgo.oid.value,
108  recipientInfo->keyEncryptionAlgo.oid.length, RSA_ENCRYPTION_OID) == 0)
109  {
110  //Perform RSA decryption
111  error = rsaesPkcs1v15Decrypt(recipientPrivateKey,
112  recipientInfo->encryptedKey.value, recipientInfo->encryptedKey.length,
113  plaintext, PKCS7_MAX_ENCR_KEY_SIZE, plaintextLen);
114  }
115  else
116 #endif
117  //Unknown algorithm?
118  {
119  //Report an error
120  error = ERROR_DECRYPTION_FAILED;
121  }
122 
123  //Return status code
124  return error;
125 }
126 
127 
128 /**
129  * @brief Perform data decryption
130  * @param[in] encryptedContentInfo Pointer to the encryptedContentInfo structure
131  * @param[in] key Pointer to the encryption key
132  * @param[in] keyLen Length of the encryption key, in bytes
133  * @param[out] plaintext Plaintext resulting from the decryption operation
134  * @param[out] plaintextLen Length of the resulting plaintext
135  * @return Error code
136  **/
137 
139  const uint8_t *key, size_t keyLen, uint8_t *plaintext, size_t *plaintextLen)
140 {
141  error_t error;
142  size_t i;
143  size_t n;
144  size_t ivLen;
145  size_t paddingLen;
146  uint8_t iv[MAX_CIPHER_BLOCK_SIZE];
147  const CipherAlgo *cipherAlgo;
148  CipherContext cipherContext;
149 
150  //Retrieve cipher algorithm
151  cipherAlgo = pkcs7GetCipherAlgo(encryptedContentInfo->contentEncrAlgo.oid.value,
152  encryptedContentInfo->contentEncrAlgo.oid.length);
153  //Invalid cipher algorithm?
154  if(cipherAlgo == NULL)
156 
157  //Obtain the key length in octets
158  n = pkcs7GetKeyLength(encryptedContentInfo->contentEncrAlgo.oid.value,
159  encryptedContentInfo->contentEncrAlgo.oid.length);
160  //Invalid key length?
161  if(n == 0)
163 
164  //Check the length of the encryption key
165  if(keyLen != n)
167 
168  //Retrieve the length of the initialization vector
169  ivLen = encryptedContentInfo->contentEncrAlgo.iv.length;
170 
171  //Check the length of the initialization vector
172  if(ivLen != cipherAlgo->blockSize)
174 
175  //Copy the initialization vector
176  osMemcpy(iv, encryptedContentInfo->contentEncrAlgo.iv.value, ivLen);
177 
178  //Load encryption key
179  error = cipherAlgo->init(&cipherContext, key, keyLen);
180  //Any error to report?
181  if(error)
182  return error;
183 
184  //Get the length of the ciphertext
185  n = encryptedContentInfo->encryptedContent.length;
186 
187  //Perform CBC decryption
188  error = cbcDecrypt(cipherAlgo, &cipherContext, iv,
189  encryptedContentInfo->encryptedContent.value, plaintext, n);
190  //Any error to report?
191  if(error)
192  return error;
193 
194  //Retrieve the length of the padding string
195  paddingLen = plaintext[n - 1];
196 
197  //Ensure that length of the padding string is valid
198  if(paddingLen < 1 || paddingLen > cipherAlgo->blockSize)
200 
201  //Malformed padding?
202  if(paddingLen > n)
204 
205  //Verify padding string
206  for(i = 0; i < paddingLen; i++)
207  {
208  if(plaintext[n - i - 1] != paddingLen)
210  }
211 
212  //Strip padding bytes from the plaintext
213  *plaintextLen = n - paddingLen;
214 
215  //Successful processing
216  return NO_ERROR;
217 }
218 
219 
220 /**
221  * @brief Search a list of per-recipient informations for a given recipient
222  * @param[in] recipientInfos Pointer to the collection of per-recipient
223  * information
224  * @param[in] recipientCertInfo Recipient's certificate
225  * @param[out] recipientInfo Pointer to the matching RecipientInfo structure,
226  * if any
227  * @return Error code
228  **/
229 
231  const X509CertInfo *recipientCertInfo, Pkcs7RecipientInfo *recipientInfo)
232 {
233  error_t error;
234  size_t n;
235  size_t length;
236  const uint8_t *data;
237 
238  //Point to the first recipientInfo entry
239  data = recipientInfos->raw.value;
240  length = recipientInfos->raw.length;
241 
242  //recipientInfos is a collection of per-recipient information. There must be
243  //at least one element in the collection (refer to RFC 2315, section 10.1)
244  while(length > 0)
245  {
246  //Per-recipient information is represented in the type RecipientInfo
247  error = pkcs7ParseRecipientInfo(data, length, &n, recipientInfo);
248  //Failed to decode ASN.1 tag?
249  if(error)
250  return error;
251 
252  //Matching issuer name?
254  recipientInfo->issuerAndSerialNumber.name.raw.length,
255  recipientCertInfo->tbsCert.issuer.raw.value,
256  recipientCertInfo->tbsCert.issuer.raw.length))
257  {
258  //Compare the length of the serial numbers
259  if(recipientInfo->issuerAndSerialNumber.serialNumber.length ==
260  recipientCertInfo->tbsCert.serialNumber.length)
261  {
262  //Matching serial number?
264  recipientCertInfo->tbsCert.serialNumber.value,
265  recipientCertInfo->tbsCert.serialNumber.length) == 0)
266  {
267  //A matching recipient has been found
268  return NO_ERROR;
269  }
270  }
271  }
272 
273  //Next field
274  data += n;
275  length -= n;
276  }
277 
278  //The specified recipient was not found
279  return ERROR_NOT_FOUND;
280 }
281 
282 #endif
@ ERROR_NOT_FOUND
Definition: error.h:148
const uint8_t * value
Definition: x509_common.h:713
X509TbsCertificate tbsCert
Definition: x509_common.h:1121
X509SerialNumber serialNumber
Definition: pkcs7_common.h:216
error_t pkcs7DecryptData(const Pkcs7EncryptedContentInfo *encryptedContentInfo, const uint8_t *key, size_t keyLen, uint8_t *plaintext, size_t *plaintextLen)
Perform data decryption.
X509OctetString encryptedContent
Definition: pkcs7_common.h:342
@ ERROR_DECRYPTION_FAILED
Definition: error.h:243
OID (Object Identifier)
Collection of AEAD algorithms.
uint8_t data[]
Definition: ethernet.h:224
Generic cipher algorithm context.
size_t blockSize
Definition: crypto.h:1115
#define osMemcmp(p1, p2, length)
Definition: os_port.h:156
X509OctetString iv
Definition: pkcs7_common.h:330
const CipherAlgo * pkcs7GetCipherAlgo(const uint8_t *oid, size_t length)
Get the cipher algorithm that matches the specified OID.
Definition: pkcs7_common.c:215
error_t rsaesPkcs1v15Decrypt(const RsaPrivateKey *key, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen)
RSAES-PKCS1-v1_5 decryption operation.
Definition: rsa.c:507
#define MAX_CIPHER_BLOCK_SIZE
CipherAlgoInit init
Definition: crypto.h:1116
Pkcs7RecipientInfos recipientInfos
Definition: pkcs7_common.h:368
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
PKCS #7 message parsing.
X.509 certificate.
Definition: x509_common.h:1119
error_t pkcs7FindRecipient(const Pkcs7RecipientInfos *recipientInfos, const X509CertInfo *recipientCertInfo, Pkcs7RecipientInfo *recipientInfo)
Search a list of per-recipient informations for a given recipient.
error_t
Error codes.
Definition: error.h:43
Encrypted content information.
Definition: pkcs7_common.h:339
X509AlgoId keyEncryptionAlgo
Definition: pkcs7_common.h:295
X509OctetString encryptedKey
Definition: pkcs7_common.h:296
X509OctetString oid
Definition: x509_common.h:775
uint_t pkcs7GetKeyLength(const uint8_t *oid, size_t length)
Get the encryption key length to be used for PBES2 operation.
Definition: pkcs7_common.c:270
General definitions for cryptographic algorithms.
X509SerialNumber serialNumber
Definition: x509_common.h:1104
uint8_t iv[]
Definition: ike.h:1626
uint8_t length
Definition: tcp.h:375
Recipient information.
Definition: pkcs7_common.h:292
Pkcs7EncryptedContentInfo encryptedContentInfo
Definition: pkcs7_common.h:369
Cipher Block Chaining (CBC) mode.
PKCS #7 message decryption.
X509OctetString raw
Definition: pkcs7_common.h:306
const uint8_t RSA_ENCRYPTION_OID[9]
Definition: rsa.c:54
Pkcs7ContentEncrAlgo contentEncrAlgo
Definition: pkcs7_common.h:341
error_t pkcs7ParseRecipientInfo(const uint8_t *data, size_t length, size_t *totalLength, Pkcs7RecipientInfo *recipientInfo)
Parse RecipientInfo structure.
Definition: pkcs7_parse.c:1203
X509OctetString oid
Definition: pkcs7_common.h:329
@ ERROR_UNSUPPORTED_CIPHER_ALGO
Definition: error.h:129
#define OID_COMP(oid1, oidLen1, oid2)
Definition: oid.h:42
uint8_t n
error_t pkcs7DecryptEnvelopedData(const Pkcs7EnvelopedData *envelopedData, const X509CertInfo *recipientCertInfo, const void *recipientPrivateKey, uint8_t *plaintext, size_t *plaintextLen)
Decrypt enveloped-data content.
Definition: pkcs7_decrypt.c:57
__weak_func error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
Definition: cbc.c:108
Common interface for encryption algorithms.
Definition: crypto.h:1111
const uint8_t * value
Definition: x509_common.h:702
bool_t x509CompareName(const uint8_t *name1, size_t nameLen1, const uint8_t *name2, size_t nameLen2)
Compare distinguished names.
Definition: x509_common.c:184
Collection of recipient informations.
Definition: pkcs7_common.h:305
Pkcs7IssuerAndSerialNumber issuerAndSerialNumber
Definition: pkcs7_common.h:294
Enveloped data content.
Definition: pkcs7_common.h:366
#define PKCS7_MAX_ENCR_KEY_SIZE
Definition: pkcs7_common.h:151
X509OctetString raw
Definition: x509_common.h:724
error_t pkcs7DecryptKey(const Pkcs7RecipientInfo *recipientInfo, const void *recipientPrivateKey, uint8_t *plaintext, size_t *plaintextLen)
Perform key decryption.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.