PageRenderTime 59ms CodeModel.GetById 12ms app.highlight 44ms RepoModel.GetById 2ms app.codeStats 0ms

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