Changeset 71876 in vbox for trunk/src/VBox/Runtime/tools/RTSignTool.cpp
- Timestamp:
- Apr 17, 2018 2:22:53 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/tools/RTSignTool.cpp
r69434 r71876 342 342 * @param pSrc The signature to add as nested. 343 343 * @param cVerbosity The verbosity. 344 */ 345 static RTEXITCODE SignToolPkcs7_AddNestedSignature(PSIGNTOOLPKCS7 pThis, PSIGNTOOLPKCS7 pSrc, unsigned cVerbosity) 344 * @param fPrepend Whether to prepend (true) or append (false) the 345 * source signature to the nested attribute. 346 */ 347 static RTEXITCODE SignToolPkcs7_AddNestedSignature(PSIGNTOOLPKCS7 pThis, PSIGNTOOLPKCS7 pSrc, 348 unsigned cVerbosity, bool fPrepend) 346 349 { 347 350 PRTCRPKCS7SIGNERINFO pSignerInfo = pThis->pSignedData->SignerInfos.papItems[0]; … … 370 373 } 371 374 372 int32_t iPos = RTCrPkcs7Attributes_Append(&pSignerInfo->UnauthenticatedAttributes); 373 if (iPos >= 0) 374 { 375 if (cVerbosity >= 3) 376 RTMsgInfo("Adding UnauthenticatedAttribute #%u...", iPos); 377 Assert((uint32_t)iPos < pSignerInfo->UnauthenticatedAttributes.cItems); 378 379 PRTCRPKCS7ATTRIBUTE pAttr = pSignerInfo->UnauthenticatedAttributes.papItems[iPos]; 380 rc = RTAsn1ObjId_InitFromString(&pAttr->Type, RTCR_PKCS9_ID_MS_NESTED_SIGNATURE, pAttr->Allocation.pAllocator); 381 if (RT_SUCCESS(rc)) 382 { 383 /** @todo Generalize the Type + enmType DYN stuff and generate setters. */ 384 Assert(pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT); 385 Assert(pAttr->uValues.pContentInfos == NULL); 386 pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE; 387 rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pContentInfos, 388 sizeof(*pAttr->uValues.pContentInfos)); 375 /* 376 * Find or add an unauthenticated attribute for nested signatures. 377 */ 378 rc = VERR_NOT_FOUND; 379 PRTCRPKCS7ATTRIBUTE pAttr = NULL; 380 int32_t iPos = pSignerInfo->UnauthenticatedAttributes.cItems; 381 while (iPos-- > 0) 382 if (pSignerInfo->UnauthenticatedAttributes.papItems[iPos]->enmType == RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE) 383 { 384 pAttr = pSignerInfo->UnauthenticatedAttributes.papItems[iPos]; 385 rc = VINF_SUCCESS; 386 break; 387 } 388 if (iPos < 0) 389 { 390 iPos = RTCrPkcs7Attributes_Append(&pSignerInfo->UnauthenticatedAttributes); 391 if (iPos >= 0) 392 { 393 if (cVerbosity >= 3) 394 RTMsgInfo("Adding UnauthenticatedAttribute #%u...", iPos); 395 Assert((uint32_t)iPos < pSignerInfo->UnauthenticatedAttributes.cItems); 396 397 PRTCRPKCS7ATTRIBUTE pAttr = pSignerInfo->UnauthenticatedAttributes.papItems[iPos]; 398 rc = RTAsn1ObjId_InitFromString(&pAttr->Type, RTCR_PKCS9_ID_MS_NESTED_SIGNATURE, pAttr->Allocation.pAllocator); 389 399 if (RT_SUCCESS(rc)) 390 400 { 391 rc = RTCrPkcs7SetOfContentInfos_Init(pAttr->uValues.pContentInfos, pAttr->Allocation.pAllocator); 401 /** @todo Generalize the Type + enmType DYN stuff and generate setters. */ 402 Assert(pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT); 403 Assert(pAttr->uValues.pContentInfos == NULL); 404 pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE; 405 rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pContentInfos, 406 sizeof(*pAttr->uValues.pContentInfos)); 392 407 if (RT_SUCCESS(rc)) 393 408 { 394 iPos = RTCrPkcs7SetOfContentInfos_Append(pAttr->uValues.pContentInfos); 395 Assert(iPos == 0); 396 if (iPos >= 0) 397 { 398 PRTCRPKCS7CONTENTINFO pCntInfo = pAttr->uValues.pContentInfos->papItems[iPos]; 399 rc = RTCrPkcs7ContentInfo_Clone(pCntInfo, &pSrc->ContentInfo, pAttr->Allocation.pAllocator); 400 if (RT_SUCCESS(rc)) 401 { 402 if (cVerbosity > 0) 403 RTMsgInfo("Added nested signature"); 404 if (cVerbosity >= 3) 405 { 406 RTMsgInfo("SingerInfo dump after change:"); 407 RTAsn1Dump(RTCrPkcs7SignerInfo_GetAsn1Core(pSignerInfo), 0, 2, RTStrmDumpPrintfV, g_pStdOut); 408 } 409 410 return RTEXITCODE_SUCCESS; 411 } 412 413 RTMsgError("RTCrPkcs7ContentInfo_Clone failed: %Rrc", iPos); 414 } 415 else 416 RTMsgError("RTCrPkcs7ContentInfos_Append failed: %Rrc", iPos); 409 rc = RTCrPkcs7SetOfContentInfos_Init(pAttr->uValues.pContentInfos, pAttr->Allocation.pAllocator); 410 if (!RT_SUCCESS(rc)) 411 RTMsgError("RTCrPkcs7ContentInfos_Init failed: %Rrc", rc); 417 412 } 418 413 else 419 RTMsgError("RT CrPkcs7ContentInfos_Initfailed: %Rrc", rc);414 RTMsgError("RTAsn1MemAllocZ failed: %Rrc", rc); 420 415 } 421 416 else 422 RTMsgError("RTAsn1 MemAllocZfailed: %Rrc", rc);417 RTMsgError("RTAsn1ObjId_InitFromString failed: %Rrc", rc); 423 418 } 424 419 else 425 RTMsgError("RTAsn1ObjId_InitFromString failed: %Rrc", rc); 426 } 427 else 428 RTMsgError("RTCrPkcs7Attributes_Append failed: %Rrc", iPos); 429 NOREF(cVerbosity); 420 RTMsgError("RTCrPkcs7Attributes_Append failed: %Rrc", iPos); 421 } 422 else if (cVerbosity >= 2) 423 RTMsgInfo("Found UnauthenticatedAttribute #%u...", iPos); 424 if (RT_SUCCESS(rc)) 425 { 426 /* 427 * Append/prepend the signature. 428 */ 429 uint32_t iActualPos = UINT32_MAX; 430 iPos = fPrepend ? 0 : pAttr->uValues.pContentInfos->cItems; 431 rc = RTCrPkcs7SetOfContentInfos_InsertEx(pAttr->uValues.pContentInfos, iPos, &pSrc->ContentInfo, 432 pAttr->Allocation.pAllocator, &iActualPos); 433 if (RT_SUCCESS(rc)) 434 { 435 //PRTCRPKCS7CONTENTINFO pCntInfo = pAttr->uValues.pContentInfos->papItems[iPos]; 436 //rc = RTCrPkcs7ContentInfo_Clone(pCntInfo, &pSrc->ContentInfo, pAttr->Allocation.pAllocator); 437 if (RT_SUCCESS(rc)) 438 { 439 if (cVerbosity > 0) 440 RTMsgInfo("Added nested signature"); 441 if (cVerbosity >= 3) 442 { 443 RTMsgInfo("SingerInfo dump after change:"); 444 RTAsn1Dump(RTCrPkcs7SignerInfo_GetAsn1Core(pSignerInfo), 0, 2, RTStrmDumpPrintfV, g_pStdOut); 445 } 446 447 return RTEXITCODE_SUCCESS; 448 } 449 450 RTMsgError("RTCrPkcs7ContentInfo_Clone failed: %Rrc", iPos); 451 } 452 else 453 RTMsgError("RTCrPkcs7ContentInfos_Append failed: %Rrc", iPos); 454 } 430 455 return RTEXITCODE_FAILURE; 431 456 } … … 1003 1028 { 1004 1029 RT_NOREF_PV(enmLevel); 1005 RTStrmPrintf(pStrm, "add-nested-exe-signature [-v|--verbose] [-d|--debug] <destination-exe> <source-exe>\n");1030 RTStrmPrintf(pStrm, "add-nested-exe-signature [-v|--verbose] [-d|--debug] [-p|--prepend] <destination-exe> <source-exe>\n"); 1006 1031 if (enmLevel == RTSIGNTOOLHELP_FULL) 1007 1032 RTStrmPrintf(pStrm, 1008 1033 "\n" 1009 1034 "The --debug option allows the source-exe to be omitted in order to test the\n" 1010 "encoding and PE file modification.\n"); 1035 "encoding and PE file modification.\n" 1036 "\n" 1037 "The --prepend option puts the nested signature first rather than appending it\n" 1038 "to the end of of the nested signature set. Windows reads nested signatures in\n" 1039 "reverse order, so --prepend will logically putting it last.\n" 1040 ); 1011 1041 return RTEXITCODE_SUCCESS; 1012 1042 } … … 1020 1050 static const RTGETOPTDEF s_aOptions[] = 1021 1051 { 1052 { "--prepend", 'p', RTGETOPT_REQ_NOTHING }, 1022 1053 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 1023 1054 { "--debug", 'd', RTGETOPT_REQ_NOTHING }, … … 1028 1059 unsigned cVerbosity = 0; 1029 1060 bool fDebug = false; 1061 bool fPrepend = false; 1030 1062 1031 1063 RTGETOPTSTATE GetState; … … 1040 1072 case 'v': cVerbosity++; break; 1041 1073 case 'd': fDebug = pszSrc == NULL; break; 1074 case 'p': fPrepend = true; break; 1042 1075 case 'V': return HandleVersion(cArgs, papszArgs); 1043 1076 case 'h': return HelpAddNestedExeSignature(g_pStdOut, RTSIGNTOOLHELP_FULL); … … 1079 1112 /* Do the signature manipulation. */ 1080 1113 if (pszSrc) 1081 rcExit = SignToolPkcs7_AddNestedSignature(&Dst, &Src, cVerbosity );1114 rcExit = SignToolPkcs7_AddNestedSignature(&Dst, &Src, cVerbosity, fPrepend); 1082 1115 if (rcExit == RTEXITCODE_SUCCESS) 1083 1116 rcExit = SignToolPkcs7_Encode(&Dst, cVerbosity); … … 1103 1136 { 1104 1137 RT_NOREF_PV(enmLevel); 1105 RTStrmPrintf(pStrm, "add-nested-cat-signature [-v|--verbose] <destination-cat> <source-cat>\n");1138 RTStrmPrintf(pStrm, "add-nested-cat-signature [-v|--verbose] [-d|--debug] [-p|--prepend] <destination-cat> <source-cat>\n"); 1106 1139 if (enmLevel == RTSIGNTOOLHELP_FULL) 1107 1140 RTStrmPrintf(pStrm, 1108 1141 "\n" 1109 1142 "The --debug option allows the source-cat to be omitted in order to test the\n" 1110 "ASN.1 re-encoding of the destination catalog file.\n"); 1143 "ASN.1 re-encoding of the destination catalog file.\n" 1144 "\n" 1145 "The --prepend option puts the nested signature first rather than appending it\n" 1146 "to the end of of the nested signature set. Windows reads nested signatures in\n" 1147 "reverse order, so --prepend will logically putting it last.\n" 1148 ); 1111 1149 return RTEXITCODE_SUCCESS; 1112 1150 } … … 1120 1158 static const RTGETOPTDEF s_aOptions[] = 1121 1159 { 1160 { "--prepend", 'p', RTGETOPT_REQ_NOTHING }, 1122 1161 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 1123 1162 { "--debug", 'd', RTGETOPT_REQ_NOTHING }, … … 1128 1167 unsigned cVerbosity = 0; 1129 1168 bool fDebug = false; 1169 bool fPrepend = false; 1130 1170 1131 1171 RTGETOPTSTATE GetState; … … 1140 1180 case 'v': cVerbosity++; break; 1141 1181 case 'd': fDebug = pszSrc == NULL; break; 1182 case 'p': fPrepend = true; break; 1142 1183 case 'V': return HandleVersion(cArgs, papszArgs); 1143 1184 case 'h': return HelpAddNestedCatSignature(g_pStdOut, RTSIGNTOOLHELP_FULL); … … 1179 1220 /* Do the signature manipulation. */ 1180 1221 if (pszSrc) 1181 rcExit = SignToolPkcs7_AddNestedSignature(&Dst, &Src, cVerbosity );1222 rcExit = SignToolPkcs7_AddNestedSignature(&Dst, &Src, cVerbosity, fPrepend); 1182 1223 if (rcExit == RTEXITCODE_SUCCESS) 1183 1224 rcExit = SignToolPkcs7_Encode(&Dst, cVerbosity);
Note:
See TracChangeset
for help on using the changeset viewer.