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-2026 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.6.6
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 n;
143  size_t ivLen;
144  size_t paddingLen;
145  uint8_t iv[MAX_CIPHER_BLOCK_SIZE];
146  const CipherAlgo *cipherAlgo;
147  CipherContext cipherContext;
148 
149  //Retrieve cipher algorithm
150  cipherAlgo = pkcs7GetCipherAlgo(encryptedContentInfo->contentEncrAlgo.oid.value,
151  encryptedContentInfo->contentEncrAlgo.oid.length);
152  //Invalid cipher algorithm?
153  if(cipherAlgo == NULL)
155 
156  //Obtain the key length in octets
157  n = pkcs7GetKeyLength(encryptedContentInfo->contentEncrAlgo.oid.value,
158  encryptedContentInfo->contentEncrAlgo.oid.length);
159  //Invalid key length?
160  if(n == 0)
162 
163  //Check the length of the encryption key
164  if(keyLen != n)
166 
167  //Retrieve the length of the initialization vector
168  ivLen = encryptedContentInfo->contentEncrAlgo.iv.length;
169 
170  //Check the length of the initialization vector
171  if(ivLen != cipherAlgo->blockSize)
173 
174  //Copy the initialization vector
175  osMemcpy(iv, encryptedContentInfo->contentEncrAlgo.iv.value, ivLen);
176 
177  //Load encryption key
178  error = cipherAlgo->init(&cipherContext, key, keyLen);
179  //Any error to report?
180  if(error)
181  return error;
182 
183  //Get the length of the ciphertext
184  n = encryptedContentInfo->encryptedContent.length;
185 
186  //Perform CBC decryption
187  error = cbcDecrypt(cipherAlgo, &cipherContext, iv,
188  encryptedContentInfo->encryptedContent.value, plaintext, n);
189  //Any error to report?
190  if(error)
191  return error;
192 
193  //Verify padding string
194  error = pkcs7VerifyPadding(plaintext, n, cipherAlgo->blockSize, &paddingLen);
195  //Any error to report?
196  if(error)
197  return error;
198 
199  //Strip padding bytes from the plaintext
200  *plaintextLen = n - paddingLen;
201 
202  //Successful processing
203  return NO_ERROR;
204 }
205 
206 
207 /**
208  * @brief CBC padding verification (constant time)
209  * @param[in] data Pointer to the plaintext data
210  * @param[in] dataLen Length of the plaintext data
211  * @param[in] blockSize Block size of the underlying cipher algorithm
212  * @param[out] paddingLen Length of the padding string
213  * @return Error code
214  **/
215 
216 error_t pkcs7VerifyPadding(const uint8_t *data, size_t dataLen,
217  size_t blockSize, size_t *paddingLen)
218 {
219  size_t i;
220  size_t n;
221  uint8_t b;
222  uint8_t mask;
223  uint32_t c;
224  uint32_t bad;
225 
226  //Retrieve the length of the padding string
227  n = data[dataLen - 1];
228 
229  //Ensure that length of the padding string is valid
230  bad = CRYPTO_TEST_LT_32(n, 1);
231  bad |= CRYPTO_TEST_GT_32(n, blockSize);
232  bad |= CRYPTO_TEST_GT_32(n, dataLen);
233 
234  //Each byte in the padding data must be filled with the padding length value
235  for(i = 1; i < dataLen && i < blockSize; i++)
236  {
237  //Read current byte
238  b = data[dataLen - 1 - i];
239 
240  //Verify that the padding string is correct
241  c = CRYPTO_TEST_LT_32(i, n);
242  mask = CRYPTO_SELECT_8(b, n, c);
243  bad |= CRYPTO_TEST_NEQ_8(b, mask);
244  }
245 
246  //Save the length of the padding string
247  *paddingLen = CRYPTO_SELECT_32(n, 0, bad);
248 
249  //Return status code
250  return bad ? ERROR_DECRYPTION_FAILED : NO_ERROR;
251 }
252 
253 
254 /**
255  * @brief Search a list of per-recipient informations for a given recipient
256  * @param[in] recipientInfos Pointer to the collection of per-recipient
257  * information
258  * @param[in] recipientCertInfo Recipient's certificate
259  * @param[out] recipientInfo Pointer to the matching RecipientInfo structure,
260  * if any
261  * @return Error code
262  **/
263 
265  const X509CertInfo *recipientCertInfo, Pkcs7RecipientInfo *recipientInfo)
266 {
267  error_t error;
268  size_t n;
269  size_t length;
270  const uint8_t *data;
271 
272  //Point to the first recipientInfo entry
273  data = recipientInfos->raw.value;
274  length = recipientInfos->raw.length;
275 
276  //recipientInfos is a collection of per-recipient information. There must be
277  //at least one element in the collection (refer to RFC 2315, section 10.1)
278  while(length > 0)
279  {
280  //Per-recipient information is represented in the type RecipientInfo
281  error = pkcs7ParseRecipientInfo(data, length, &n, recipientInfo);
282  //Failed to decode ASN.1 tag?
283  if(error)
284  return error;
285 
286  //Matching issuer name?
288  recipientInfo->issuerAndSerialNumber.name.raw.length,
289  recipientCertInfo->tbsCert.issuer.raw.value,
290  recipientCertInfo->tbsCert.issuer.raw.length))
291  {
292  //Compare the length of the serial numbers
293  if(recipientInfo->issuerAndSerialNumber.serialNumber.length ==
294  recipientCertInfo->tbsCert.serialNumber.length)
295  {
296  //Matching serial number?
298  recipientCertInfo->tbsCert.serialNumber.value,
299  recipientCertInfo->tbsCert.serialNumber.length) == 0)
300  {
301  //A matching recipient has been found
302  return NO_ERROR;
303  }
304  }
305  }
306 
307  //Next field
308  data += n;
309  length -= n;
310  }
311 
312  //The specified recipient was not found
313  return ERROR_NOT_FOUND;
314 }
315 
316 #endif
uint8_t b
Definition: nbns_common.h:122
@ ERROR_NOT_FOUND
Definition: error.h:148
const uint8_t * value
Definition: x509_common.h:773
X509TbsCertificate tbsCert
Definition: x509_common.h:1196
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
#define CRYPTO_SELECT_32(a, b, c)
Definition: crypto.h:1131
OID (Object Identifier)
Collection of cipher algorithms.
uint8_t data[]
Definition: ethernet.h:224
Generic cipher algorithm context.
size_t blockSize
Definition: crypto.h:1289
#define osMemcmp(p1, p2, length)
Definition: os_port.h:159
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
#define MAX_CIPHER_BLOCK_SIZE
CipherAlgoInit init
Definition: crypto.h:1290
Pkcs7RecipientInfos recipientInfos
Definition: pkcs7_common.h:368
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
PKCS #7 message parsing.
X.509 certificate.
Definition: x509_common.h:1194
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
#define CRYPTO_TEST_GT_32(a, b)
Definition: crypto.h:1123
X509OctetString encryptedKey
Definition: pkcs7_common.h:296
X509OctetString oid
Definition: x509_common.h:835
#define CRYPTO_TEST_LT_32(a, b)
Definition: crypto.h:1115
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.
#define CRYPTO_SELECT_8(a, b, c)
Definition: crypto.h:1059
X509SerialNumber serialNumber
Definition: x509_common.h:1179
uint8_t mask
Definition: web_socket.h:319
uint8_t iv[]
Definition: ike.h:1695
error_t pkcs7VerifyPadding(const uint8_t *data, size_t dataLen, size_t blockSize, size_t *paddingLen)
CBC padding verification (constant time)
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.
uint32_t dataLen
Definition: sftp_common.h:229
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_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.
@ 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:1285
const uint8_t * value
Definition: x509_common.h:762
bool_t x509CompareName(const uint8_t *name1, size_t nameLen1, const uint8_t *name2, size_t nameLen2)
Compare distinguished names.
Definition: x509_common.c:188
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:784
#define CRYPTO_TEST_NEQ_8(a, b)
Definition: crypto.h:1039
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
uint8_t c
Definition: ndp.h:514
Debugging facilities.