PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/autoinstaller/UpdateEngineExtensions/SignerTest.m

http://macfuse.googlecode.com/
Objective C | 199 lines | 138 code | 19 blank | 42 comment | 0 complexity | 7ef515f1b526868dcb85826db23757f3 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. //
  2. // Signer.h
  3. // autoinstaller
  4. //
  5. // Created by Greg Miller on 7/18/08.
  6. // Copyright 2008 Google Inc. All rights reserved.
  7. //
  8. #import <SenTestingKit/SenTestingKit.h>
  9. #import "Signer.h"
  10. @interface SignerTest : SenTestCase
  11. @end
  12. // Generated with:
  13. //
  14. // $ openssl genrsa 1024 > private_key.pem
  15. // $ openssl rsa -inform PEM -outform DER < private_key.pem > private_key.der
  16. // $ xxd -i private_key.der
  17. //
  18. unsigned char private_der[] = {
  19. 0x30, 0x82, 0x02, 0x5b, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xb5,
  20. 0x98, 0x0f, 0xc0, 0x21, 0x6f, 0xe3, 0xcd, 0xad, 0xea, 0xae, 0xc8, 0x76,
  21. 0xaa, 0x15, 0xfa, 0xfc, 0x4c, 0x16, 0x56, 0xf4, 0xc4, 0xe0, 0x73, 0xf2,
  22. 0xe4, 0xb4, 0x4d, 0x90, 0x27, 0x19, 0x56, 0x81, 0x22, 0xef, 0x65, 0xac,
  23. 0x01, 0xfb, 0xba, 0x14, 0x54, 0xaa, 0x3c, 0xfa, 0xbb, 0xf9, 0xc3, 0x1e,
  24. 0xe5, 0x55, 0xdd, 0x1e, 0x9c, 0xa7, 0x34, 0xf9, 0xe4, 0xdb, 0xd3, 0x47,
  25. 0x7a, 0x0e, 0x3a, 0xd8, 0xe1, 0x23, 0x82, 0xde, 0x51, 0x4e, 0xe6, 0x91,
  26. 0x1e, 0x5a, 0xee, 0x57, 0x58, 0x56, 0x99, 0x0e, 0x91, 0xfb, 0x7f, 0xc4,
  27. 0x98, 0xfb, 0x79, 0x22, 0x66, 0xb0, 0x59, 0x93, 0xe0, 0xc0, 0x7c, 0x8f,
  28. 0x4e, 0x1d, 0x3d, 0xbe, 0x45, 0x59, 0x8f, 0x90, 0x0a, 0x62, 0x95, 0x3c,
  29. 0xd3, 0x64, 0x30, 0x86, 0x43, 0xa7, 0x57, 0xb4, 0xb6, 0x03, 0x6e, 0xcc,
  30. 0x98, 0x09, 0x35, 0x68, 0xf8, 0x38, 0xbb, 0x02, 0x03, 0x01, 0x00, 0x01,
  31. 0x02, 0x81, 0x80, 0x43, 0x77, 0x07, 0x02, 0xfe, 0x69, 0x2f, 0x5d, 0x0e,
  32. 0xac, 0x0d, 0xdb, 0x74, 0x1a, 0xa4, 0xa7, 0xe6, 0x6b, 0xee, 0x30, 0xc8,
  33. 0x27, 0x0c, 0xac, 0x87, 0x24, 0xe8, 0xc7, 0x98, 0x32, 0xae, 0x7b, 0x30,
  34. 0xe6, 0xe5, 0xc2, 0x04, 0x31, 0x88, 0x70, 0x0d, 0x75, 0x96, 0xf9, 0xa8,
  35. 0xbd, 0x85, 0x81, 0x34, 0x4d, 0x47, 0xa5, 0x2a, 0xe6, 0x50, 0x48, 0xa2,
  36. 0x3e, 0xa0, 0x5c, 0xd2, 0x45, 0x4a, 0x5b, 0x7a, 0x56, 0x02, 0x78, 0x59,
  37. 0x26, 0x84, 0x34, 0x6b, 0x2c, 0x43, 0xca, 0xbe, 0x7c, 0x3d, 0xfb, 0x10,
  38. 0x36, 0x3a, 0x13, 0xcd, 0x8a, 0x8f, 0x41, 0xfb, 0x51, 0x35, 0x60, 0xba,
  39. 0xa6, 0x61, 0x7f, 0xbb, 0xaf, 0xe9, 0x3d, 0xa1, 0x4f, 0x4c, 0x1c, 0x27,
  40. 0xc8, 0xac, 0x1a, 0xb7, 0xa9, 0xf5, 0x47, 0x27, 0x16, 0xe6, 0xe0, 0x5e,
  41. 0xb3, 0xcc, 0x2e, 0x52, 0xbf, 0x54, 0x54, 0xaf, 0x12, 0x8c, 0x01, 0x02,
  42. 0x41, 0x00, 0xda, 0xd8, 0x3d, 0xf6, 0xc4, 0x10, 0xd4, 0xbb, 0x04, 0x89,
  43. 0x80, 0xca, 0xb9, 0xa4, 0x5c, 0x15, 0xc7, 0xc7, 0x1f, 0x1f, 0xa7, 0x53,
  44. 0x1a, 0xc1, 0x4f, 0x92, 0xfe, 0xee, 0x0f, 0x3a, 0x16, 0x1c, 0x6a, 0x70,
  45. 0xbe, 0xfd, 0xaf, 0x51, 0x53, 0x70, 0xe7, 0x4c, 0x08, 0xa6, 0xaa, 0x82,
  46. 0xdd, 0x1f, 0x3c, 0x49, 0x9b, 0x31, 0xdd, 0x35, 0x97, 0x3a, 0x64, 0xaf,
  47. 0xe7, 0x17, 0x9e, 0x65, 0xc4, 0x81, 0x02, 0x41, 0x00, 0xd4, 0x6c, 0xc5,
  48. 0xf0, 0x13, 0x5d, 0xd1, 0x93, 0x4f, 0x56, 0xe9, 0xcb, 0x70, 0xe1, 0x90,
  49. 0x33, 0x4d, 0xad, 0x1a, 0x43, 0x03, 0xd8, 0x85, 0x88, 0x7b, 0x86, 0x82,
  50. 0xed, 0x43, 0x63, 0x5f, 0x7d, 0xfd, 0xec, 0x7f, 0xb6, 0x15, 0xcc, 0x79,
  51. 0xbd, 0x61, 0xfe, 0xc8, 0x97, 0x02, 0xfa, 0x4d, 0x19, 0xfd, 0xa9, 0xfe,
  52. 0x1b, 0xc6, 0x0b, 0xda, 0x54, 0x4b, 0xd6, 0x65, 0x1c, 0xdf, 0x50, 0x6f,
  53. 0x3b, 0x02, 0x3f, 0x34, 0x25, 0x52, 0x9e, 0x7a, 0x19, 0x88, 0x97, 0x07,
  54. 0x7d, 0xbc, 0x32, 0x92, 0x09, 0x97, 0xe7, 0x98, 0xb2, 0xd9, 0xb7, 0xb1,
  55. 0x27, 0x1c, 0x38, 0xf3, 0xbd, 0x13, 0x01, 0xa6, 0x9f, 0x30, 0xa5, 0x94,
  56. 0xb7, 0x48, 0xcb, 0x29, 0xfb, 0x36, 0xe6, 0xb0, 0x36, 0x6d, 0xd9, 0x48,
  57. 0xc7, 0xe5, 0xe8, 0x8b, 0x74, 0xfe, 0x85, 0xe3, 0x2f, 0xe1, 0xd9, 0x08,
  58. 0x24, 0xe1, 0xeb, 0xbe, 0xd2, 0x81, 0x02, 0x40, 0x25, 0x03, 0x2a, 0xdd,
  59. 0xf0, 0xd1, 0xe9, 0x01, 0x4c, 0xd2, 0xac, 0xfb, 0xd8, 0x98, 0x44, 0xc2,
  60. 0xdb, 0xa6, 0xec, 0xab, 0x7e, 0x91, 0x0c, 0x7b, 0x42, 0x62, 0xa6, 0x8f,
  61. 0x66, 0xad, 0x7a, 0xe6, 0xfe, 0x06, 0x66, 0x1e, 0x38, 0xe0, 0x1e, 0xda,
  62. 0x23, 0xa7, 0xfc, 0x15, 0x6b, 0xdc, 0x66, 0xf0, 0xbf, 0x65, 0x0e, 0x34,
  63. 0x92, 0xa6, 0x60, 0x0d, 0x4e, 0x05, 0x76, 0x2a, 0xb8, 0xb2, 0x50, 0x1f,
  64. 0x02, 0x41, 0x00, 0xc5, 0xd4, 0x7f, 0xbf, 0x58, 0x08, 0x52, 0x1a, 0x38,
  65. 0xf9, 0x9d, 0x76, 0x8c, 0xa7, 0x06, 0x31, 0x6f, 0xdd, 0xb5, 0x2a, 0x5d,
  66. 0x6f, 0x02, 0x39, 0x60, 0x9b, 0xa9, 0x29, 0xdb, 0xf7, 0x34, 0x71, 0x3b,
  67. 0x0c, 0x4c, 0x1d, 0xcf, 0x20, 0x85, 0x10, 0x52, 0xfc, 0x8e, 0x38, 0x7b,
  68. 0x96, 0x41, 0x4a, 0x4e, 0x9f, 0xc1, 0xf8, 0x71, 0x1b, 0xec, 0xab, 0xa9,
  69. 0x85, 0x25, 0xb5, 0x0a, 0xfe, 0x7f, 0xca
  70. };
  71. unsigned int private_der_len = 607;
  72. // Generated with:
  73. //
  74. // $ openssl rsa -pubout < private_key.pem > public_key.pem
  75. // $ openssl rsa -inform PEM -outform DER -pubin <public_key.pem >public_key.der
  76. // $ xxd -i public_key.der
  77. //
  78. unsigned char public_der[] = {
  79. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  80. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
  81. 0x89, 0x02, 0x81, 0x81, 0x00, 0xb5, 0x98, 0x0f, 0xc0, 0x21, 0x6f, 0xe3,
  82. 0xcd, 0xad, 0xea, 0xae, 0xc8, 0x76, 0xaa, 0x15, 0xfa, 0xfc, 0x4c, 0x16,
  83. 0x56, 0xf4, 0xc4, 0xe0, 0x73, 0xf2, 0xe4, 0xb4, 0x4d, 0x90, 0x27, 0x19,
  84. 0x56, 0x81, 0x22, 0xef, 0x65, 0xac, 0x01, 0xfb, 0xba, 0x14, 0x54, 0xaa,
  85. 0x3c, 0xfa, 0xbb, 0xf9, 0xc3, 0x1e, 0xe5, 0x55, 0xdd, 0x1e, 0x9c, 0xa7,
  86. 0x34, 0xf9, 0xe4, 0xdb, 0xd3, 0x47, 0x7a, 0x0e, 0x3a, 0xd8, 0xe1, 0x23,
  87. 0x82, 0xde, 0x51, 0x4e, 0xe6, 0x91, 0x1e, 0x5a, 0xee, 0x57, 0x58, 0x56,
  88. 0x99, 0x0e, 0x91, 0xfb, 0x7f, 0xc4, 0x98, 0xfb, 0x79, 0x22, 0x66, 0xb0,
  89. 0x59, 0x93, 0xe0, 0xc0, 0x7c, 0x8f, 0x4e, 0x1d, 0x3d, 0xbe, 0x45, 0x59,
  90. 0x8f, 0x90, 0x0a, 0x62, 0x95, 0x3c, 0xd3, 0x64, 0x30, 0x86, 0x43, 0xa7,
  91. 0x57, 0xb4, 0xb6, 0x03, 0x6e, 0xcc, 0x98, 0x09, 0x35, 0x68, 0xf8, 0x38,
  92. 0xbb, 0x02, 0x03, 0x01, 0x00, 0x01
  93. };
  94. unsigned int public_der_len = 162;
  95. @implementation SignerTest
  96. - (void)testBasic {
  97. // NSData *publicKey = [NSData dataWithBytes:public_der length:public_der_len];
  98. // NSData *privateKey = [NSData dataWithBytes:private_der length:private_der_len];
  99. //
  100. // Signer *signer = [[Signer alloc] initWithPublicKey:publicKey
  101. // privateKey:privateKey];
  102. //
  103. // Run some basic tests with no keys
  104. //
  105. Signer *signer = nil;
  106. signer = [Signer signerWithPublicKey:nil privateKey:nil];
  107. STAssertNotNil(signer, @"signer shouldn't be nil");
  108. STAssertNil([signer signData:nil], @"shouldn't be able to nil data");
  109. char bytes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  110. NSData *someData = [NSData dataWithBytes:bytes length:sizeof(bytes)];
  111. STAssertNil([signer signData:someData], @"shouldn't be able to sign data");
  112. //
  113. // Run tests with just a private key
  114. //
  115. NSData *privateKey = [NSData dataWithBytes:private_der
  116. length:private_der_len];
  117. [signer setPrivateKey:privateKey];
  118. NSData *signature = [signer signData:someData];
  119. STAssertNotNil(signature, @"should be able to sign data now");
  120. STAssertNil([signer signData:nil], @"still shouldn't be able to sign nil");
  121. STAssertFalse([signer isSignature:signature validForData:someData],
  122. @"shouldn't be able to verify sig w/o a public key");
  123. STAssertFalse([signer isSignature:signature validForData:nil],
  124. @"shouldn't be able to verify sig for nil data");
  125. STAssertFalse([signer isSignature:nil validForData:nil],
  126. @"shouldn't be able to verify sig with nil sig and nil data");
  127. STAssertFalse([signer isSignature:nil validForData:someData],
  128. @"shouldn't be able to verify sig with nil sig");
  129. //
  130. // Run tests with both a public and private key
  131. //
  132. NSData *publicKey = [NSData dataWithBytes:public_der length:public_der_len];
  133. [signer setPublicKey:publicKey];
  134. STAssertTrue([signer isSignature:signature validForData:someData],
  135. @"should be able to verify data");
  136. STAssertFalse([signer isSignature:signature validForData:nil],
  137. @"shouldn't be able to verify sig for nil data");
  138. STAssertFalse([signer isSignature:nil validForData:nil],
  139. @"shouldn't be able to verify sig with nil sig and nil data");
  140. STAssertFalse([signer isSignature:nil validForData:someData],
  141. @"shouldn't be able to verify sig with nil sig");
  142. //
  143. // Run tests with just a public key
  144. // (These should pass just like the ones above since the private key shouldn't
  145. // be used.)
  146. //
  147. [signer setPrivateKey:nil];
  148. STAssertTrue([signer isSignature:signature validForData:someData],
  149. @"should be able to verify sig");
  150. STAssertFalse([signer isSignature:signature validForData:nil],
  151. @"shouldn't be able to verify sig for nil data");
  152. STAssertFalse([signer isSignature:nil validForData:nil],
  153. @"shouldn't be able to verify sig with nil sig and nil data");
  154. STAssertFalse([signer isSignature:nil validForData:someData],
  155. @"shouldn't be able to verify sig with nil sig");
  156. // Set the private key again
  157. [signer setPrivateKey:privateKey];
  158. //
  159. // Misc. tests
  160. //
  161. STAssertFalse([signer isSignature:[NSData data] validForData:someData],
  162. @"shouldn't be able to verify w/ 0 byte sig");
  163. STAssertFalse([signer isSignature:[NSData data] validForData:[NSData data]],
  164. @"shouldn't be able to verify w/ 0 byte sig and data");
  165. STAssertFalse([signer isSignature:signature validForData:[NSData data]],
  166. @"shouldn't be able to verify w/ 0 byte data");
  167. char other_bytes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  168. NSData *otherData = [NSData dataWithBytes:other_bytes
  169. length:sizeof(other_bytes)];
  170. STAssertTrue([signer isSignature:signature validForData:otherData],
  171. @"should be able to verify sig");
  172. char diff_bytes[] = { 1, 1, 1, 1, 1, 1, 1, 1, 2 };
  173. NSData *diffData = [NSData dataWithBytes:diff_bytes
  174. length:sizeof(diff_bytes)];
  175. STAssertFalse([someData isEqualTo:diffData],
  176. @"someData should not equal diffData");
  177. STAssertFalse([signer isSignature:signature validForData:diffData],
  178. @"shouldn't be able to verify sig against different data");
  179. }
  180. @end