Changeset 39748 in vbox for trunk/src/VBox/Additions/WINNT/tools/VBoxCertUtil.cpp
- Timestamp:
- Jan 11, 2012 2:42:05 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/WINNT/tools/VBoxCertUtil.cpp
r39746 r39748 1 /* $Id$ */ 2 /** @file 3 * VBoxCertUtil - VBox Certificate Utility - Windows Only. 4 */ 5 6 /* 7 * Copyright (C) 2012 Oracle Corporation 8 * 9 * This file is part of VirtualBox Open Source Edition (OSE), as 10 * available from http://www.virtualbox.org. This file is free software; 11 * you can redistribute it and/or modify it under the terms of the GNU 12 * General Public License (GPL) as published by the Free Software 13 * Foundation, in version 2 as it comes in the "COPYING" file of the 14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the 15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. 16 */ 1 17 2 18 … … 198 214 (PBYTE)pvFile, (DWORD)cbFile); 199 215 if (*ppOutCtx) 200 rc = true;216 fRc = true; 201 217 else 202 218 { … … 246 262 } 247 263 264 /** 265 * Removes a certificate, given by file, from a store 266 * 267 * @returns true on success, false on failure (error message written). 268 * @param dwDst The destination, like 269 * CERT_SYSTEM_STORE_LOCAL_MACHINE or 270 * ERT_SYSTEM_STORE_CURRENT_USER. 271 * @param pszStoreNm The store name. 272 * @param pszCertFile The file containing the certificate to add. 273 */ 274 static bool removeCertFromStoreByFile(DWORD dwDst, const char *pszStoreNm, const char *pszCertFile) 275 { 276 /* 277 * Read the certificate file first. 278 */ 279 PCCERT_CONTEXT pSrcCtx = NULL; 280 HCERTSTORE hSrcStore = NULL; 281 if (!readCertFile(pszCertFile, &pSrcCtx, &hSrcStore)) 282 return false; 283 284 WCHAR wszName[1024]; 285 if (!CertGetNameStringW(pSrcCtx, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0 /*dwFlags*/, NULL /*pvTypePara*/, 286 wszName, sizeof(wszName))) 287 { 288 RTMsgError("CertGetNameStringW(Subject) failed: %s\n", errorToString(GetLastError())); 289 wszName[0] = '\0'; 290 } 291 292 /* 293 * Open the destination store. 294 */ 295 bool fRc = false; 296 HCERTSTORE hDstStore = openCertStore(dwDst, pszStoreNm); 297 if (hDstStore) 298 { 299 if (pSrcCtx) 300 { 301 fRc = true; 302 unsigned cDeleted = 0; 303 PCCERT_CONTEXT pCurCtx = NULL; 304 while ((pCurCtx = CertEnumCertificatesInStore(hDstStore, pCurCtx)) != NULL) 305 { 306 if (CertCompareCertificate(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, pCurCtx->pCertInfo, pSrcCtx->pCertInfo)) 307 { 308 if (g_cVerbosityLevel > 1) 309 RTMsgInfo("Removing '%ls'...", wszName); 310 PCCERT_CONTEXT pDeleteCtx = CertDuplicateCertificateContext(pCurCtx); 311 if (pDeleteCtx) 312 { 313 if (CertDeleteCertificateFromStore(pDeleteCtx)) 314 cDeleted++; 315 else 316 RTMsgError("CertDeleteFromStore('%ls') failed: %s\n", wszName, errorToString(GetLastError())); 317 } 318 else 319 RTMsgError("CertDuplicateCertificateContext('%ls') failed: %s\n", wszName, errorToString(GetLastError())); 320 } 321 } 322 323 if (!cDeleted) 324 RTMsgInfo("Found no matching certificates to remove."); 325 } 326 else 327 { 328 RTMsgError("Path not implemented at line %d\n", __LINE__); 329 } 330 331 CertCloseStore(hDstStore, CERT_CLOSE_STORE_CHECK_FLAG); 332 } 333 if (pSrcCtx) 334 CertFreeCertificateContext(pSrcCtx); 335 if (hSrcStore) 336 CertCloseStore(hSrcStore, CERT_CLOSE_STORE_CHECK_FLAG); 337 return fRc; 338 } 248 339 249 340 /** … … 407 498 408 499 /** 409 * Handler for the ' add-trusted-publisher' command.410 */ 411 static RTEXITCODE cmd AddTrustedPublisher(int argc, char **argv)500 * Handler for the 'remove-trusted-publisher' command. 501 */ 502 static RTEXITCODE cmdRemoveTrustedPublisher(int argc, char **argv) 412 503 { 413 504 /* … … 431 522 { 432 523 case 'h': 433 RTPrintf("Usage: VBoxCertUtil add-trusted-publisher [--root <root-cert>] <trusted-cert>\n");524 RTPrintf("Usage: VBoxCertUtil remove-trusted-publisher [--root <root-cert>] <trusted-cert>\n"); 434 525 break; 435 526 … … 465 556 */ 466 557 if ( pszRootCert 558 && !removeCertFromStoreByFile(CERT_SYSTEM_STORE_LOCAL_MACHINE, "Root", pszRootCert)) 559 return RTEXITCODE_FAILURE; 560 if (!removeCertFromStoreByFile(CERT_SYSTEM_STORE_LOCAL_MACHINE, "TrustedPublisher", pszTrustedCert)) 561 return RTEXITCODE_FAILURE; 562 563 if (g_cVerbosityLevel > 0) 564 { 565 if (pszRootCert) 566 RTMsgInfo("Successfully removed '%s' as root and '%s' as trusted publisher", pszRootCert, pszTrustedCert); 567 else 568 RTMsgInfo("Successfully removed '%s' as trusted publisher", pszTrustedCert); 569 } 570 return RTEXITCODE_SUCCESS; 571 } 572 573 574 /** 575 * Handler for the 'add-trusted-publisher' command. 576 */ 577 static RTEXITCODE cmdAddTrustedPublisher(int argc, char **argv) 578 { 579 /* 580 * Parse arguments. 581 */ 582 static const RTGETOPTDEF s_aOptions[] = 583 { 584 { "--root", 'r', RTGETOPT_REQ_STRING }, 585 }; 586 587 const char *pszRootCert = NULL; 588 const char *pszTrustedCert = NULL; 589 590 int rc; 591 RTGETOPTUNION ValueUnion; 592 RTGETOPTSTATE GetState; 593 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); 594 while ((rc = RTGetOpt(&GetState, &ValueUnion))) 595 { 596 switch (rc) 597 { 598 case 'h': 599 RTPrintf("Usage: VBoxCertUtil add-trusted-publisher [--root <root-cert>] <trusted-cert>\n"); 600 break; 601 602 case 'V': 603 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision()); 604 return RTEXITCODE_SUCCESS; 605 606 case 'r': 607 if (pszRootCert) 608 return RTMsgErrorExit(RTEXITCODE_SUCCESS, 609 "You've already specified '%s' as root certificate.", 610 pszRootCert); 611 pszRootCert = ValueUnion.psz; 612 break; 613 614 case VINF_GETOPT_NOT_OPTION: 615 if (pszTrustedCert) 616 return RTMsgErrorExit(RTEXITCODE_SUCCESS, 617 "You've already specified '%s' as trusted certificate.", 618 pszRootCert); 619 pszTrustedCert = ValueUnion.psz; 620 break; 621 622 default: 623 return RTGetOptPrintError(rc, &ValueUnion); 624 } 625 } 626 if (!pszTrustedCert) 627 return RTMsgErrorExit(RTEXITCODE_SUCCESS, "No trusted certificate specified."); 628 629 /* 630 * Do the job. 631 */ 632 /** @todo The root-cert part needs to be made more flexible. */ 633 if ( pszRootCert 467 634 && !addCertToStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, "Root", pszRootCert, CERT_STORE_ADD_NEW)) 468 635 return RTEXITCODE_FAILURE; 636 469 637 if (!addCertToStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, "TrustedPublisher", pszTrustedCert, CERT_STORE_ADD_NEW)) 470 638 return RTEXITCODE_FAILURE; … … 481 649 482 650 651 /** 652 * Displays the usage info. 653 * @param argv0 Program name. 654 */ 655 static void showUsage(const char *argv0) 656 { 657 RTPrintf("Usage: %Rbn [-v[v]] <command>\n" 658 " or %Rbn <-V|--version>\n" 659 " or %Rbn <-h|--help>\n" 660 "\n" 661 "Available commands:\n" 662 " add-trusted-publisher, remove-trusted-publisher,\n" 663 " display-all\n" 664 , argv0, argv0, argv0); 665 } 666 667 483 668 int main(int argc, char **argv) 484 669 { … … 493 678 { 494 679 VCUACTION_ADD_TRUSTED_PUBLISHER = 1000, 680 VCUACTION_REMOVE_TRUSTED_PUBLISHER, 495 681 VCUACTION_DISPLAY_ALL, 496 682 VCUACTION_END … … 499 685 static const RTGETOPTDEF s_aOptions[] = 500 686 { 501 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 502 { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, 503 { "add-trusted-publisher", VCUACTION_ADD_TRUSTED_PUBLISHER, RTGETOPT_REQ_NOTHING }, 504 { "display-all", VCUACTION_DISPLAY_ALL, RTGETOPT_REQ_NOTHING }, 687 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 688 { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, 689 { "add-trusted-publisher", VCUACTION_ADD_TRUSTED_PUBLISHER, RTGETOPT_REQ_NOTHING }, 690 { "remove-trusted-publisher", VCUACTION_REMOVE_TRUSTED_PUBLISHER, RTGETOPT_REQ_NOTHING }, 691 { "display-all", VCUACTION_DISPLAY_ALL, RTGETOPT_REQ_NOTHING }, 505 692 }; 506 693 … … 522 709 523 710 case 'h': 524 RTPrintf("Usage: TODO\n");525 break;711 showUsage(argv[0]); 712 return RTEXITCODE_SUCCESS; 526 713 527 714 case 'V': … … 532 719 return cmdAddTrustedPublisher(argc - GetState.iNext + 1, argv + GetState.iNext - 1); 533 720 721 case VCUACTION_REMOVE_TRUSTED_PUBLISHER: 722 return cmdRemoveTrustedPublisher(argc - GetState.iNext + 1, argv + GetState.iNext - 1); 723 534 724 case VCUACTION_DISPLAY_ALL: 535 725 return cmdDisplayAll(argc - GetState.iNext + 1, argv + GetState.iNext - 1); … … 540 730 } 541 731 542 RTMsgError("Missing command...\n"); 732 RTMsgError("Missing command..."); 733 showUsage(argv[0]); 543 734 return RTEXITCODE_SYNTAX; 544 735 }
Note:
See TracChangeset
for help on using the changeset viewer.