Changeset 95625 in vbox
- Timestamp:
- Jul 13, 2022 8:32:39 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/tools/RTSignTool.cpp
r95616 r95625 963 963 #ifndef IPRT_IN_BUILD_TOOL 964 964 965 static PRTCRPKCS7ATTRIBUTE SignToolPkcs7_AuthAttribAppend(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId) 966 { 967 int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs); 968 if (iPos >= 0) 969 { 970 /* Create the attrib and its sub-set of timestamps. */ 971 PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos]; 972 int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, pszAttrId, pAttr->Allocation.pAllocator); 973 if (RT_SUCCESS(rc)) 974 return pAttr; 975 RTMsgError("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc); 976 } 977 else 978 RTMsgError("RTCrPkcs7Attributes_Append failed: %Rrc", iPos); 979 980 return NULL; 981 } 982 983 965 984 static RTEXITCODE SignToolPkcs7_AuthAttribsAddSigningTime(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime) 966 985 { … … 972 991 * ^^- end 2016-10-05T07:50:30.000000000Z (161005075030Z) 973 992 */ 974 int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs); 975 if (iPos < 0) 976 return RTMsgErrorExitFailure("RTCrPkcs7Attributes_Append failed: %Rrc", iPos); 977 978 /* Create the attrib and its sub-set of timestamps. */ 979 PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos]; 980 int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, RTCR_PKCS9_ID_SIGNING_TIME_OID, pAttr->Allocation.pAllocator); 981 if (RT_FAILURE(rc)) 982 return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/RTCR_PKCS9_ID_SIGNING_TIME_OID failed: %Rrc", rc); 993 PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, RTCR_PKCS9_ID_SIGNING_TIME_OID); 994 if (!pAttr) 995 return RTEXITCODE_FAILURE; 983 996 984 997 /** @todo Generalize the Type + enmType DYN stuff and generate setters. */ … … 986 999 Assert(pAttr->uValues.pContentInfos == NULL); 987 1000 pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME; 988 rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pSigningTime, sizeof(*pAttr->uValues.pSigningTime));1001 int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pSigningTime, sizeof(*pAttr->uValues.pSigningTime)); 989 1002 if (RT_FAILURE(rc)) 990 1003 return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc); … … 995 1008 996 1009 /* Create the timestamp. */ 997 i Pos = RTAsn1SetOfTimes_Append(pAttr->uValues.pSigningTime);1010 int32_t iPos = RTAsn1SetOfTimes_Append(pAttr->uValues.pSigningTime); 998 1011 if (iPos < 0) 999 1012 return RTMsgErrorExitFailure("RTAsn1SetOfTimes_Append failed: %Rrc", iPos); … … 1007 1020 } 1008 1021 1022 1009 1023 static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdSeqsEmpty(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId) 1010 1024 { 1011 int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs); 1012 if (iPos < 0) 1013 return RTMsgErrorExitFailure("RTCrPkcs7Attributes_Append failed: %Rrc", iPos); 1014 1015 /* Create the attrib and its sub-set of timestamps. */ 1016 PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos]; 1017 int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, pszAttrId, pAttr->Allocation.pAllocator); 1018 if (RT_FAILURE(rc)) 1019 return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc); 1025 PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, pszAttrId); 1026 if (!pAttr) 1027 return RTEXITCODE_FAILURE; 1020 1028 1021 1029 /** @todo Generalize the Type + enmType DYN stuff and generate setters. */ … … 1023 1031 Assert(pAttr->uValues.pContentInfos == NULL); 1024 1032 pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_MS_STATEMENT_TYPE; 1025 rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIdSeqs, sizeof(*pAttr->uValues.pObjIdSeqs));1033 int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIdSeqs, sizeof(*pAttr->uValues.pObjIdSeqs)); 1026 1034 if (RT_FAILURE(rc)) 1027 1035 return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc); … … 1033 1041 return RTEXITCODE_SUCCESS; 1034 1042 } 1043 1035 1044 1036 1045 static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdSeqsValue(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId, … … 1057 1066 RTAsn1ObjId_Delete(&ObjIdValue); 1058 1067 if (RT_FAILURE(rc)) 1059 return RTMsgErrorExitFailure("RTAsn1SeqOfObjIds_InsertEx failed: %Rrc", iPos);1068 return RTMsgErrorExitFailure("RTAsn1SeqOfObjIds_InsertEx failed: %Rrc", rc); 1060 1069 1061 1070 return RTEXITCODE_SUCCESS; 1062 1071 } 1072 1073 1074 static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdValue(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId, 1075 const char *pszValueId) 1076 { 1077 /* Create the attrib and its sub-set of object IDs. */ 1078 PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, pszAttrId); 1079 if (!pAttr) 1080 return RTEXITCODE_FAILURE; 1081 1082 /** @todo Generalize the Type + enmType DYN stuff and generate setters. */ 1083 Assert(pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT); 1084 Assert(pAttr->uValues.pContentInfos == NULL); 1085 pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS; 1086 int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIds, sizeof(*pAttr->uValues.pObjIds)); 1087 if (RT_FAILURE(rc)) 1088 return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc); 1089 1090 rc = RTAsn1SetOfObjIds_Init(pAttr->uValues.pObjIds, pAttr->Allocation.pAllocator); 1091 if (RT_FAILURE(rc)) 1092 return RTMsgErrorExitFailure("RTAsn1SetOfObjIdSeqs_Init failed: %Rrc", rc); 1093 1094 /* Add a object id to the value. */ 1095 RTASN1OBJID ObjIdValue; 1096 rc = RTAsn1ObjId_InitFromString(&ObjIdValue, pszValueId, &g_RTAsn1DefaultAllocator); 1097 if (RT_FAILURE(rc)) 1098 return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc); 1099 1100 rc = RTAsn1SetOfObjIds_InsertEx(pAttr->uValues.pObjIds, 0 /*iPos*/, &ObjIdValue, &g_RTAsn1DefaultAllocator, NULL); 1101 RTAsn1ObjId_Delete(&ObjIdValue); 1102 if (RT_FAILURE(rc)) 1103 return RTMsgErrorExitFailure("RTAsn1SetOfObjIds_InsertEx failed: %Rrc", rc); 1104 1105 return RTEXITCODE_SUCCESS; 1106 } 1107 1063 1108 1064 1109 static RTEXITCODE SignToolPkcs7_AddAuthAttribsForTimestamp(PRTCRPKCS7ATTRIBUTES pAuthAttribs, bool fTimestampTypeOld, … … 1080 1125 return RTEXITCODE_SUCCESS; 1081 1126 } 1127 1082 1128 1083 1129 static RTEXITCODE SignToolPkcs7_AddAuthAttribsForImageSignature(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime) … … 1090 1136 * Set Of -^^ ^^- Empty Sequence. 1091 1137 */ 1138 /** @todo ends up with zero byte instead of two. */ 1092 1139 RTEXITCODE rcExit = SignToolPkcs7_AuthAttribsAddObjIdSeqsEmpty(pAuthAttribs, RTCR_PKCS9_ID_MS_SP_OPUS_INFO); 1093 1140 if (rcExit != RTEXITCODE_SUCCESS) … … 1102 1149 * ^- Set Of 1103 1150 */ 1151 rcExit = SignToolPkcs7_AuthAttribsAddObjIdValue(pAuthAttribs, RTCR_PKCS9_ID_CONTENT_TYPE_OID, 1152 RTCRSPCINDIRECTDATACONTENT_OID); 1153 if (rcExit != RTEXITCODE_SUCCESS) 1154 return rcExit; 1104 1155 1105 1156 /* … … 1472 1523 } 1473 1524 1474 static RTEXITCODE SignToolPkcs7_SpcCompleteWithoutPageHashes( SIGNTOOLPKCS7EXE *pThis,RTCRSPCINDIRECTDATACONTENT *pSpcIndData)1475 { 1476 RT_NOREF(pThis);1477 PRTCRSPCPEIMAGEDATA pPeImage= pSpcIndData->Data.uValue.pPeImage;1525 static RTEXITCODE SignToolPkcs7_SpcCompleteWithoutPageHashes(RTCRSPCINDIRECTDATACONTENT *pSpcIndData) 1526 { 1527 PCRTASN1ALLOCATORVTABLE const pAllocator = &g_RTAsn1DefaultAllocator; 1528 PRTCRSPCPEIMAGEDATA const pPeImage = pSpcIndData->Data.uValue.pPeImage; 1478 1529 Assert(pPeImage); 1479 //pPeImage->Flags 1480 RT_NOREF(pPeImage); 1481 1482 return RTEXITCODE_SUCCESS; 1530 1531 /* 1532 * Set it to File with an empty name. 1533 * RTCRSPCPEIMAGEDATA::Flags -vv 1534 * RTCRSPCPEIMAGEDATA::SeqCore -vv T0 -vv vv- pT2/CtxTag2 1535 * 0040: 04 01 82 37 02 01 0f 30-09 03 01 00 a0 04 a2 02 ...7...0........ 1536 * 0050: 80 00 30 21 30 09 06 05-2b 0e 03 02 1a 05 00 04 ..0!0...+....... 1537 * ^^- pUcs2 / empty string 1538 */ 1539 1540 /* Create an empty BMP string. */ 1541 RTASN1STRING EmptyStr; 1542 int rc = RTAsn1BmpString_Init(&EmptyStr, pAllocator); 1543 if (RT_FAILURE(rc)) 1544 return RTMsgErrorExitFailure("RTAsn1BmpString_Init/Ucs2 failed: %Rrc", rc); 1545 1546 /* Create an SPC string and use the above empty string with the Ucs2 setter. */ 1547 RTEXITCODE rcExit = RTEXITCODE_FAILURE; 1548 RTCRSPCSTRING SpcString; 1549 rc = RTCrSpcString_Init(&SpcString, pAllocator); 1550 if (RT_SUCCESS(rc)) 1551 { 1552 rc = RTCrSpcString_SetUcs2(&SpcString, &EmptyStr, pAllocator); 1553 if (RT_SUCCESS(rc)) 1554 { 1555 /* Create a temporary SpcLink with the empty SpcString. */ 1556 RTCRSPCLINK SpcLink; 1557 rc = RTCrSpcLink_Init(&SpcLink, pAllocator); 1558 if (RT_SUCCESS(rc)) 1559 { 1560 /* Use the setter on the SpcLink object to copy the SpcString to it. */ 1561 rc = RTCrSpcLink_SetFile(&SpcLink, &SpcString, pAllocator); 1562 if (RT_SUCCESS(rc)) 1563 { 1564 /* Use the setter to copy SpcLink to the PeImage structure. */ 1565 rc = RTCrSpcPeImageData_SetFile(pPeImage, &SpcLink, pAllocator); 1566 if (RT_SUCCESS(rc)) 1567 rcExit = RTEXITCODE_SUCCESS; 1568 else 1569 RTMsgError("RTCrSpcPeImageData_SetFile failed: %Rrc", rc); 1570 } 1571 else 1572 RTMsgError("RTCrSpcLink_SetFile failed: %Rrc", rc); 1573 RTCrSpcLink_Delete(&SpcLink); 1574 } 1575 else 1576 RTMsgError("RTCrSpcLink_Init failed: %Rrc", rc); 1577 } 1578 else 1579 RTMsgError("RTCrSpcString_SetUcs2 failed: %Rrc", rc); 1580 RTCrSpcString_Delete(&SpcString); 1581 } 1582 else 1583 RTMsgError("RTCrSpcString_Init failed: %Rrc", rc); 1584 RTAsn1BmpString_Delete(&EmptyStr); 1585 return rcExit; 1483 1586 } 1484 1587 … … 1532 1635 * and signed. 1533 1636 */ 1534 RTCRSPCINDIRECTDATACONTENT SpcIndData; 1535 int rc = RTCrSpcIndirectDataContent_Init(&SpcIndData, &g_RTAsn1DefaultAllocator); 1637 PCRTASN1ALLOCATORVTABLE const pAllocator = &g_RTAsn1DefaultAllocator; 1638 RTCRSPCINDIRECTDATACONTENT SpcIndData; 1639 int rc = RTCrSpcIndirectDataContent_Init(&SpcIndData, pAllocator); 1536 1640 if (RT_FAILURE(rc)) 1537 1641 return RTMsgErrorExitFailure("RTCrSpcIndirectDataContent_Init failed: %Rrc", rc); … … 1541 1645 Assert(SpcIndData.Data.enmType == RTCRSPCAAOVTYPE_NOT_PRESENT); 1542 1646 Assert(SpcIndData.Data.uValue.pPeImage == NULL); 1543 SpcIndData.Data.enmType = RTCRSPCAAOVTYPE_PE_IMAGE_DATA;1544 rc = RTAsn1MemAllocZ(&SpcIndData.Data.Allocation, (void **)&SpcIndData.Data.uValue.pPeImage,1545 sizeof(*SpcIndData.Data.uValue.pPeImage));1546 1647 RTEXITCODE rcExit; 1648 rc = RTAsn1ObjId_SetFromString(&SpcIndData.Data.Type, RTCRSPCPEIMAGEDATA_OID, pAllocator); 1547 1649 if (RT_SUCCESS(rc)) 1548 1650 { 1549 rc = RTCrSpcPeImageData_Init(SpcIndData.Data.uValue.pPeImage, SpcIndData.Data.Allocation.pAllocator); 1651 SpcIndData.Data.enmType = RTCRSPCAAOVTYPE_PE_IMAGE_DATA; 1652 rc = RTAsn1MemAllocZ(&SpcIndData.Data.Allocation, (void **)&SpcIndData.Data.uValue.pPeImage, 1653 sizeof(*SpcIndData.Data.uValue.pPeImage)); 1550 1654 if (RT_SUCCESS(rc)) 1551 1655 { 1552 /* Add the hashes. */ 1553 rcExit = SignToolPkcs7_SpcAddImageHash(pThis, &SpcIndData, enmSigType); 1554 if (rcExit == RTEXITCODE_SUCCESS) 1656 rc = RTCrSpcPeImageData_Init(SpcIndData.Data.uValue.pPeImage, pAllocator); 1657 if (RT_SUCCESS(rc)) 1555 1658 { 1556 if (fHashPages) 1557 rcExit = SignToolPkcs7_SpcAddImagePageHashes(pThis, &SpcIndData, enmSigType); 1558 else 1559 rcExit = SignToolPkcs7_SpcCompleteWithoutPageHashes(pThis, &SpcIndData); 1659 /* Old (SHA1) signatures has a Flags member, it's zero bits, though. */ 1660 if (enmSigType == RTDIGESTTYPE_SHA1) 1661 { 1662 uint8_t bFlags = 0; 1663 RTASN1BITSTRING Flags; 1664 rc = RTAsn1BitString_InitWithData(&Flags, &bFlags, 0, pAllocator); 1665 if (RT_SUCCESS(rc)) 1666 { 1667 rc = RTCrSpcPeImageData_SetFlags(SpcIndData.Data.uValue.pPeImage, &Flags, pAllocator); 1668 RTAsn1BitString_Delete(&Flags); 1669 if (RT_FAILURE(rc)) 1670 rcExit = RTMsgErrorExitFailure("RTCrSpcPeImageData_SetFlags failed: %Rrc", rc); 1671 } 1672 else 1673 rcExit = RTMsgErrorExitFailure("RTAsn1BitString_InitWithData failed: %Rrc", rc); 1674 } 1560 1675 1561 1676 /* 1562 * Encode and sign the SPC data, timestamp it, and line it up for adding to the executable.1677 * Add the hashes. 1563 1678 */ 1679 rcExit = SignToolPkcs7_SpcAddImageHash(pThis, &SpcIndData, enmSigType); 1564 1680 if (rcExit == RTEXITCODE_SUCCESS) 1565 rcExit = SignToolPkcs7_SignSpcIndData(pThis, &SpcIndData, cVerbosity, fReplaceExisting, pSigningCertKey, 1566 hAddCerts, fTimestampTypeOld, SigningTime, pTimestampCertKey); 1681 { 1682 if (fHashPages) 1683 rcExit = SignToolPkcs7_SpcAddImagePageHashes(pThis, &SpcIndData, enmSigType); 1684 else 1685 rcExit = SignToolPkcs7_SpcCompleteWithoutPageHashes(&SpcIndData); 1686 1687 /* 1688 * Encode and sign the SPC data, timestamp it, and line it up for adding to the executable. 1689 */ 1690 if (rcExit == RTEXITCODE_SUCCESS) 1691 rcExit = SignToolPkcs7_SignSpcIndData(pThis, &SpcIndData, cVerbosity, fReplaceExisting, pSigningCertKey, 1692 hAddCerts, fTimestampTypeOld, SigningTime, pTimestampCertKey); 1693 } 1567 1694 } 1695 else 1696 rcExit = RTMsgErrorExitFailure("RTCrPkcs7SignerInfos_Init failed: %Rrc", rc); 1568 1697 } 1569 1698 else 1570 rcExit = RTMsgErrorExitFailure("RT CrPkcs7SignerInfos_Init failed: %Rrc", rc);1699 rcExit = RTMsgErrorExitFailure("RTAsn1MemAllocZ failed for RTCRSPCPEIMAGEDATA: %Rrc", rc); 1571 1700 } 1572 1701 else 1573 rcExit = RTMsgErrorExitFailure("RTAsn1 MemAllocZ failed for RTCRSPCPEIMAGEDATA: %Rrc", rc);1702 rcExit = RTMsgErrorExitFailure("RTAsn1ObjId_SetWithString/SpcPeImageData failed: %Rrc", rc); 1574 1703 1575 1704 RTCrSpcIndirectDataContent_Delete(&SpcIndData); … … 3307 3436 else 3308 3437 RTPrintf("%s File: rc=%Rrc\n", pThis->szPrefix, rc); 3438 if (pThis->cVerbosity > 4 && pPeImage->T0.File.u.pT2 == NULL) 3439 RTPrintf("%s pT2=NULL\n", pThis->szPrefix); 3440 else if (pThis->cVerbosity > 4) 3441 { 3442 PCRTASN1STRING pStr = pPeImage->T0.File.u.pT2->File.u.pAscii; 3443 RTPrintf("%s pT2=%p/%p LB %#x fFlags=%#x pOps=%p (%s)\n" 3444 "%s enmChoice=%d pStr=%p/%p LB %#x fFlags=%#x\n", 3445 pThis->szPrefix, 3446 pPeImage->T0.File.u.pT2, 3447 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.uData.pu8, 3448 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.cb, 3449 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.fFlags, 3450 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps, 3451 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps 3452 ? pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps->pszName : "", 3453 pThis->szPrefix, 3454 pPeImage->T0.File.u.pT2->File.enmChoice, 3455 pStr, 3456 pStr ? pStr->Asn1Core.uData.pu8 : NULL, 3457 pStr ? pStr->Asn1Core.cb : 0, 3458 pStr ? pStr->Asn1Core.fFlags : 0); 3459 } 3309 3460 break; 3310 3461 }
Note:
See TracChangeset
for help on using the changeset viewer.