Changeset 95672 in vbox
- Timestamp:
- Jul 16, 2022 5:20:11 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/crypto/pkcs7-sign.cpp
r95656 r95672 145 145 } 146 146 147 148 static int rtCrPkcs7SimpleSignSignedDataDoV1TweakedFinal(PKCS7 *pOsslPkcs7, const char *pszContentId, 149 const void *pvData, size_t cbData, PRTERRINFO pErrInfo) 150 { 151 AssertReturn(pszContentId, RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_MISSING_CONTENT_TYPE_ATTRIB, 152 "RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP requires content type in additional attribs")); 153 154 /* 155 * Prepare a BIO of what should be hashed with all the hashing filters attached. 156 */ 157 BIO *pOsslBio = PKCS7_dataInit(pOsslPkcs7, NULL); 158 if (!pOsslBio) 159 return RTErrInfoSet(pErrInfo, VERR_CR_CIPHER_OSSL_ENCRYPT_FINAL_FAILED, "PKCS7_dataInit failed"); 160 161 /* 162 * Now write the data. 163 * 164 * We must skip the outer wrapper here (see RTCrPkcs7VerifySignedData). This 165 * is probably a bit presumptive about what we're working on, so add an extra 166 * flag for this later. 167 */ 168 uint8_t const *pbToWrite = (uint8_t const *)pvData; 169 size_t cbToWrite = cbData; 170 171 /** @todo add extra flag for this? */ 172 RTASN1CURSORPRIMARY SkipCursor; 173 RTAsn1CursorInitPrimary(&SkipCursor, pvData, (uint32_t)cbData, 174 pErrInfo,&g_RTAsn1DefaultAllocator, RTASN1CURSOR_FLAGS_DER, "skip"); 175 RTASN1CORE SkipAsn1Core = { 0 }; 176 int rc = RTAsn1CursorReadHdr(&SkipCursor.Cursor, &SkipAsn1Core, "skip-core"); 177 if (RT_SUCCESS(rc)) 178 { 179 pbToWrite += SkipAsn1Core.cbHdr; 180 cbToWrite -= SkipAsn1Core.cbHdr; 181 182 rc = BIO_write(pOsslBio, pbToWrite, (int)cbToWrite); 183 if (rc == (ssize_t)cbToWrite) 184 { 185 BIO_flush(pOsslBio); /** @todo error check this */ 186 if (true) 187 { 188 /* 189 * Finalize the job - produce the signer info signatures and stuff. 190 */ 191 rc = PKCS7_dataFinal(pOsslPkcs7, pOsslBio); 192 if (rc > 0) 193 { 194 /* 195 * Now tweak the content so we get the desired content type and 196 * no extra wrappers and stuff. 197 */ 198 rc = rtCrPkcs7SimpleSignSignedDataDoV1TweakContent(pOsslPkcs7, pszContentId, pvData, cbData, pErrInfo); 199 } 200 else 201 rc = RTErrInfoSetF(pErrInfo, VERR_CR_CIPHER_OSSL_ENCRYPT_FINAL_FAILED, "PKCS7_dataFinal failed: %d", rc); 202 } 203 } 204 else 205 rc = RTErrInfoSetF(pErrInfo, VERR_CR_CIPHER_OSSL_ENCRYPT_FINAL_FAILED, 206 "%zu byte data write failed: %d", cbToWrite, rc); 207 } 208 BIO_free_all(pOsslBio); 209 return rc; 210 } 211 212 147 213 static int rtCrPkcs7SimpleSignSignedDataDoV1AttribConversion(PKCS7_SIGNER_INFO *pSignerInfo, 148 214 PCRTCRPKCS7ATTRIBUTES pAdditionalAuthenticatedAttribs, … … 242 308 * Finalized and actually sign the data. 243 309 */ 244 rc = PKCS7_final(pCms, pOsslData, fOsslSign); 245 if (rc > 0) 310 bool const fTweaked = (fFlags & (RTCRPKCS7SIGN_SD_F_DEATCHED | RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP)) 311 == RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP; 312 if (fTweaked) 313 rc = rtCrPkcs7SimpleSignSignedDataDoV1TweakedFinal(pCms, pszContentId, pvData, cbData, pErrInfo); 314 else 315 { 316 rc = PKCS7_final(pCms, pOsslData, fOsslSign); 317 if (rc > 0) 318 rc = VINF_SUCCESS; 319 else 320 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "PKCS7_final"); 321 /** @todo maybe we want to use rtCrPkcs7SimpleSignSignedDataDoV1TweakContent 322 * for when the content type isn't 'data'... */ 323 } 324 if (RT_SUCCESS(rc)) 246 325 { 247 326 /* 248 * Do content type/enclosure tweaking if requested.327 * Get the output and copy it into the result buffer. 249 328 */ 250 rc = VINF_SUCCESS; 251 if ( (fFlags & (RTCRPKCS7SIGN_SD_F_DEATCHED | RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP)) 252 == RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP) /** @todo maybe we want to also do this when the content type isn't 'data'. */ 253 rc = rtCrPkcs7SimpleSignSignedDataDoV1TweakContent(pCms, pszContentId, pvData, cbData, pErrInfo); 254 else 329 BIO *pOsslResult = BIO_new(BIO_s_mem()); 330 if (pOsslResult) 255 331 { 256 /** @todo Set content type if needed? */ 257 AssertMsg(!pszContentId || strcmp(pszContentId, RTCR_PKCS7_DATA_OID) == 0, 258 ("pszContentId=%s\n", pszContentId)); 259 rc = VINF_SUCCESS; 260 } 261 if (RT_SUCCESS(rc)) 262 { 263 /* 264 * Get the output and copy it into the result buffer. 265 */ 266 BIO *pOsslResult = BIO_new(BIO_s_mem()); 267 if (pOsslResult) 332 rc = i2d_PKCS7_bio(pOsslResult, pCms); 333 if (rc > 0) 268 334 { 269 rc = i2d_PKCS7_bio(pOsslResult, pCms); 270 if (rc > 0) 271 { 272 *ppOsslResult = pOsslResult; 273 rc = VINF_SUCCESS; 274 } 275 else 276 { 277 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "i2d_CMS_bio"); 278 BIO_free(pOsslResult); 279 } 335 *ppOsslResult = pOsslResult; 336 rc = VINF_SUCCESS; 280 337 } 281 338 else 282 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "BIO_new/BIO_s_mem"); 339 { 340 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "i2d_PKCS7_bio"); 341 BIO_free(pOsslResult); 342 } 283 343 } 284 }285 else286 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "CMS_final");344 else 345 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "BIO_new/BIO_s_mem"); 346 } 287 347 } 288 348 else 289 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, " CMS_add1_signer");349 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "PKCS7_sign_add_signer"); 290 350 } 291 351 PKCS7_free(pCms); 292 352 } 293 353 else 294 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, " CMS_sign");354 rc = RTErrInfoSet(pErrInfo, VERR_GENERAL_FAILURE, "PKCS7_sign"); 295 355 return rc; 296 356 }
Note:
See TracChangeset
for help on using the changeset viewer.