/core/autoinstaller/plist_signer.m

http://macfuse.googlecode.com/ · Objective C · 98 lines · 77 code · 14 blank · 7 comment · 13 complexity · 9635c9b7d9c859e99555379d9c1bafab MD5 · raw file

  1. //
  2. // rule_signer.m
  3. // autoinstaller
  4. //
  5. // Created by Greg Miller on 7/18/08.
  6. // Copyright 2008 Google Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <getopt.h>
  10. #import <stdio.h>
  11. #import <unistd.h>
  12. #import "Signer.h"
  13. #import "PlistSigner.h"
  14. static void Usage(void) {
  15. printf("Usage: plist_signer {-s|-v} -k <key> <plist>\n"
  16. " --sign,-s Signs the specified plist file using the *private*\n"
  17. " key specified with -k\n"
  18. " --verify,-v Verifies the signature of the specified plist using\n"
  19. " *public* key specified with -k\n"
  20. " --key,-k <f> Specifies the path to a DER key file. This path can\n"
  21. " be either a public or a private key, depending on\n"
  22. " whether signing (private) or verifying (public) was\n"
  23. " requested with either -s or -v\n"
  24. );
  25. }
  26. int main(int argc, char **argv) {
  27. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  28. int rc = 0;
  29. static struct option kLongOpts[] = {
  30. { "key", required_argument, NULL, 'k' },
  31. { "verify", no_argument, NULL, 'v' },
  32. { "sign", no_argument, NULL, 's' },
  33. { NULL, 0, NULL, 0 },
  34. };
  35. BOOL verify = NO, sign = NO;
  36. NSString *keyPath = nil;
  37. int ch = 0;
  38. while ((ch = getopt_long(argc, argv, "k:vs", kLongOpts, NULL)) != -1) {
  39. switch (ch) {
  40. case 'k':
  41. keyPath = [NSString stringWithUTF8String:optarg];
  42. break;
  43. case 'v':
  44. verify = YES;
  45. break;
  46. case 's':
  47. sign = YES;
  48. break;
  49. default:
  50. Usage();
  51. goto done;
  52. }
  53. }
  54. argc -= optind;
  55. argv += optind;
  56. if (argc != 1 || !(sign || verify)) {
  57. Usage();
  58. goto done;
  59. }
  60. NSString *plistPath = [NSString stringWithUTF8String:argv[0]];
  61. NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:plistPath];
  62. NSData *key = [NSData dataWithContentsOfFile:keyPath];
  63. Signer *signer = [Signer signerWithPublicKey:key privateKey:key];
  64. PlistSigner *plistSigner = [[[PlistSigner alloc]
  65. initWithSigner:signer
  66. plist:plist] autorelease];
  67. if (sign) {
  68. if ([plistSigner signPlist]) {
  69. [[plistSigner plist] writeToFile:plistPath atomically:YES];
  70. printf("%s: Signature OK\n", [plistPath UTF8String]);
  71. } else {
  72. printf("Failed to sign %s\n", [plistPath UTF8String]);
  73. rc = 1;
  74. }
  75. } else if (verify) {
  76. BOOL ok = [plistSigner isPlistSigned];
  77. printf("%s: %s\n", [plistPath UTF8String],
  78. (ok ? "Signature OK" : "Signature Invalid"));
  79. if (!ok) rc = 1;
  80. }
  81. done:
  82. [pool release];
  83. return rc;
  84. }