Changeset 104745 in vbox for trunk/src/VBox/Runtime/tools
- Timestamp:
- May 21, 2024 12:52:09 PM (7 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/tools/RTSignTool.cpp
r101647 r104745 6232 6232 6233 6233 6234 /********************************************************************************************************************************* 6235 * The 'create-self-signed-rsa-cert' command. * 6236 *********************************************************************************************************************************/ 6237 static RTEXITCODE HelpCreateSelfSignedRsaCert(PRTSTREAM pStrm, RTSIGNTOOLHELP enmLevel) 6238 { 6239 RT_NOREF_PV(enmLevel); 6240 RTStrmWrappedPrintf(pStrm, RTSTRMWRAPPED_F_HANGING_INDENT, 6241 "create-self-signed-rsa-cert [--verbose|--quiet] [--key-bits <count>] [--digest <hash>] [--out-cert=]<certificate-file.pem> [--out-pkey=]<private-key-file.pem>\n"); 6242 return RTEXITCODE_SUCCESS; 6243 } 6244 6245 6246 static RTDIGESTTYPE DigestTypeStringToValue(const char *pszType) 6247 { 6248 for (int iType = RTDIGESTTYPE_INVALID + 1; iType < RTDIGESTTYPE_END; iType++) 6249 if (iType != RTDIGESTTYPE_UNKNOWN) 6250 { 6251 const char * const pszName = RTCrDigestTypeToName((RTDIGESTTYPE)iType); 6252 size_t offType = 0; 6253 size_t offName = 0; 6254 for (;;) 6255 { 6256 char chType = RT_C_TO_UPPER(pszType[offType]); 6257 char chName = RT_C_TO_UPPER(pszName[offType]); 6258 if (chType != chName) 6259 { 6260 /* allow 'sha1' as well as 'sha-1' */ 6261 if (chName != '-') 6262 break; 6263 chName = pszName[++offName]; 6264 chName = RT_C_TO_UPPER(chName); 6265 if (chType != chName) 6266 break; 6267 } 6268 if (chType == '\0') 6269 return (RTDIGESTTYPE)iType; 6270 } 6271 } 6272 return RTDIGESTTYPE_INVALID; 6273 } 6274 6275 6276 static RTEXITCODE HandleCreateSelfSignedRsaCert(int cArgs, char **papszArgs) 6277 { 6278 /* 6279 * Parse arguments. 6280 */ 6281 static const RTGETOPTDEF s_aOptions[] = 6282 { 6283 { "--digest", 'd', RTGETOPT_REQ_STRING }, 6284 { "--bits", 'b', RTGETOPT_REQ_UINT32 }, 6285 { "--key-bits", 'b', RTGETOPT_REQ_UINT32 }, 6286 { "--days", 'D', RTGETOPT_REQ_UINT32 }, 6287 { "--days", 'D', RTGETOPT_REQ_UINT32 }, 6288 { "--out-cert", 'c', RTGETOPT_REQ_UINT32 }, 6289 { "--out-certificate", 'c', RTGETOPT_REQ_UINT32 }, 6290 { "--out-pkey", 'p', RTGETOPT_REQ_UINT32 }, 6291 { "--out-private-key", 'p', RTGETOPT_REQ_UINT32 }, 6292 { "--secs", 's', RTGETOPT_REQ_UINT32 }, 6293 { "--seconds", 's', RTGETOPT_REQ_UINT32 }, 6294 }; 6295 6296 RTDIGESTTYPE enmDigestType = RTDIGESTTYPE_SHA384; 6297 uint32_t cKeyBits = 4096; 6298 uint32_t cSecsValidFor = 365 * RT_SEC_1DAY; 6299 uint32_t fKeyUsage = 0; 6300 uint32_t fExtKeyUsage = 0; 6301 const char *pszOutCert = NULL; 6302 const char *pszOutPrivKey = NULL; 6303 6304 RTGETOPTSTATE GetState; 6305 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST); 6306 AssertRCReturn(rc, RTEXITCODE_FAILURE); 6307 RTGETOPTUNION ValueUnion; 6308 int ch; 6309 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0) 6310 { 6311 switch (ch) 6312 { 6313 case 'b': 6314 cKeyBits = ValueUnion.u32; 6315 break; 6316 6317 case 'd': 6318 enmDigestType = DigestTypeStringToValue(ValueUnion.psz); 6319 if (enmDigestType == RTDIGESTTYPE_INVALID) 6320 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unknown digest type: %s", ValueUnion.psz); 6321 break; 6322 6323 case 'D': 6324 cSecsValidFor = ValueUnion.u32 * RT_SEC_1DAY; 6325 if (cSecsValidFor / RT_SEC_1DAY != ValueUnion.u32) 6326 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The --days option value is out of range: %u", ValueUnion.u32); 6327 break; 6328 6329 case 'c': 6330 if (pszOutCert) 6331 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The --out-cert option can only be used once."); 6332 pszOutCert = ValueUnion.psz; 6333 break; 6334 6335 case 'p': 6336 if (pszOutPrivKey) 6337 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The --out-pkey option can only be used once."); 6338 pszOutPrivKey = ValueUnion.psz; 6339 break; 6340 6341 case 's': 6342 cSecsValidFor = ValueUnion.u32; 6343 break; 6344 6345 case VINF_GETOPT_NOT_OPTION: 6346 if (!pszOutCert) 6347 pszOutCert = ValueUnion.psz; 6348 else if (!pszOutPrivKey) 6349 pszOutPrivKey = ValueUnion.psz; 6350 else 6351 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Too many output files specified: %s", ValueUnion.psz); 6352 break; 6353 6354 case 'V': return HandleVersion(cArgs, papszArgs); 6355 case 'h': return HelpCreateSelfSignedRsaCert(g_pStdOut, RTSIGNTOOLHELP_FULL); 6356 default: return RTGetOptPrintError(ch, &ValueUnion); 6357 } 6358 } 6359 if (!pszOutCert) 6360 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No output certificate file name specified."); 6361 if (!pszOutPrivKey) 6362 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No output private key file name specified."); 6363 6364 /* 6365 * Do the work. 6366 */ 6367 RTERRINFOSTATIC StaticErrInfo; 6368 rc = RTCrX509Certificate_GenerateSelfSignedRsa(enmDigestType, cKeyBits, cSecsValidFor, 6369 fKeyUsage, fExtKeyUsage, NULL /*pvSubjectTodo*/, 6370 pszOutCert, pszOutPrivKey, RTErrInfoInitStatic(&StaticErrInfo)); 6371 if (RT_SUCCESS(rc)) 6372 { 6373 /* 6374 * Test load it. 6375 */ 6376 RTCRX509CERTIFICATE Certificate; 6377 rc = RTCrX509Certificate_ReadFromFile(&Certificate, pszOutCert, RTCRX509CERT_READ_F_PEM_ONLY, 6378 &g_RTAsn1DefaultAllocator, RTErrInfoInitStatic(&StaticErrInfo)); 6379 if (RT_FAILURE(rc)) 6380 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error reading the new certificate from %s: %Rrc%#RTeim", 6381 pszOutCert, rc, &StaticErrInfo.Core); 6382 RTCrX509Certificate_Delete(&Certificate); 6383 return RTEXITCODE_SUCCESS; 6384 } 6385 6386 return RTMsgErrorExitFailure("RTCrX509Certificate_GenerateSelfSignedRsa(%d,%u,%u,%s,%s,) failed: %Rrc%#RTeim", 6387 enmDigestType, cKeyBits, cSecsValidFor, pszOutCert, pszOutPrivKey, rc, &StaticErrInfo.Core); 6388 } 6389 6390 6391 6234 6392 /* 6235 6393 * The 'version' command. … … 6297 6455 { "hash-exe", HandleHashExe, HelpHashExe }, 6298 6456 { "make-tainfo", HandleMakeTaInfo, HelpMakeTaInfo }, 6457 #ifndef IPRT_IN_BUILD_TOOL 6458 { "create-self-signed-rsa-cert", HandleCreateSelfSignedRsaCert, HelpCreateSelfSignedRsaCert }, 6459 #endif 6299 6460 { "help", HandleHelp, HelpHelp }, 6300 6461 { "--help", HandleHelp, NULL },
Note:
See TracChangeset
for help on using the changeset viewer.