No Description

openssl_wrapper.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // openssl_wrapper.m
  3. // ThirdDemoApp
  4. //
  5. // Created by Xu Hanjie on 11-1-20.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "openssl_wrapper.h"
  9. #import "rsa.h"
  10. #include "pem.h"
  11. #include "md5.h"
  12. #include "bio.h"
  13. #include "sha.h"
  14. #include <string.h>
  15. int rsa_sign_with_private_key_pem(char *message, int message_length
  16. , unsigned char *signature, unsigned int *signature_length
  17. , char *private_key_file_path)
  18. {
  19. unsigned char sha1[20];
  20. SHA1((unsigned char *)message, message_length, sha1);
  21. int success = 0;
  22. BIO *bio_private = NULL;
  23. RSA *rsa_private = NULL;
  24. bio_private = BIO_new(BIO_s_file());
  25. BIO_read_filename(bio_private, private_key_file_path);
  26. rsa_private = PEM_read_bio_RSAPrivateKey(bio_private, NULL, NULL, "");
  27. if (rsa_private != nil) {
  28. if (1 == RSA_check_key(rsa_private))
  29. {
  30. int rsa_sign_valid = RSA_sign(NID_sha1
  31. , sha1, 20
  32. , signature, signature_length
  33. , rsa_private);
  34. if (1 == rsa_sign_valid)
  35. {
  36. success = 1;
  37. }
  38. }
  39. BIO_free_all(bio_private);
  40. }
  41. else {
  42. NSLog(@"rsa_private read error : private key is NULL");
  43. }
  44. return success;
  45. }
  46. int rsa_verify_with_public_key_pem(char *message, int message_length
  47. , unsigned char *signature, unsigned int signature_length
  48. , char *public_key_file_path)
  49. {
  50. unsigned char sha1[20];
  51. SHA1((unsigned char *)message, message_length, sha1);
  52. BIO *bio_public = NULL;
  53. RSA *rsa_public = NULL;
  54. bio_public = BIO_new(BIO_s_file());
  55. BIO_read_filename(bio_public, public_key_file_path);
  56. rsa_public = PEM_read_bio_RSA_PUBKEY(bio_public, NULL, NULL, NULL);
  57. int rsa_verify_valid = RSA_verify(NID_sha1
  58. , sha1, 20
  59. , signature, signature_length
  60. , rsa_public);
  61. BIO_free_all(bio_public);
  62. if (1 == rsa_verify_valid)
  63. {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. NSString *base64StringFromData(NSData *signature)
  69. {
  70. int signatureLength = [signature length];
  71. unsigned char *outputBuffer = (unsigned char *)malloc(2 * 4 * (signatureLength / 3 + 1));
  72. int outputLength = EVP_EncodeBlock(outputBuffer, [signature bytes], signatureLength);
  73. outputBuffer[outputLength] = '\0';
  74. NSString *base64String = [NSString stringWithCString:(char *)outputBuffer encoding:NSASCIIStringEncoding];
  75. free(outputBuffer);
  76. return base64String;
  77. }
  78. NSData *dataFromBase64String(NSString *base64String)
  79. {
  80. int stringLength = [base64String length];
  81. const unsigned char *strBuffer = (const unsigned char *)[base64String UTF8String];
  82. unsigned char *outputBuffer = (unsigned char *)malloc(2 * 3 * (stringLength / 4 + 1));
  83. int outputLength = EVP_DecodeBlock(outputBuffer, strBuffer, stringLength);
  84. int zeroByteCounter = 0;
  85. for (int i = stringLength - 1; i >= 0; i--)
  86. {
  87. if (strBuffer[i] == '=')
  88. {
  89. zeroByteCounter++;
  90. }
  91. else
  92. {
  93. break;
  94. }
  95. }
  96. NSData *data = [[NSData alloc] initWithBytes:outputBuffer length:outputLength - zeroByteCounter];
  97. free(outputBuffer);
  98. return data;
  99. }
  100. NSString *rsaSignString(NSString *stringToSign, NSString *privateKeyFilePath, BOOL *signSuccess)
  101. {
  102. const char *message = [stringToSign cStringUsingEncoding:NSUTF8StringEncoding];
  103. int messageLength = strlen(message);
  104. unsigned char *sig = (unsigned char *)malloc(256);
  105. unsigned int sig_len;
  106. char *filePath = (char *)[privateKeyFilePath cStringUsingEncoding:NSUTF8StringEncoding];
  107. int sign_ok = rsa_sign_with_private_key_pem((char *)message, messageLength
  108. , sig, &sig_len
  109. , filePath);
  110. NSString *signedString = nil;
  111. if (1 == sign_ok)
  112. {
  113. *signSuccess = YES;
  114. signedString = base64StringFromData([NSData dataWithBytes:sig length:sig_len]);
  115. }
  116. else
  117. {
  118. *signSuccess = NO;
  119. }
  120. free(sig);
  121. return signedString;
  122. }
  123. void rsaVerifyString(NSString *stringToVerify, NSString *signature, NSString *publicKeyFilePath, BOOL *verifySuccess)
  124. {
  125. const char *message = [stringToVerify cStringUsingEncoding:NSUTF8StringEncoding];
  126. int messageLength = [stringToVerify lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  127. NSData *signatureData = dataFromBase64String(signature);
  128. unsigned char *sig = (unsigned char *)[signatureData bytes];
  129. unsigned int sig_len = [signatureData length];
  130. char *filePath = (char *)[publicKeyFilePath cStringUsingEncoding:NSUTF8StringEncoding];
  131. int verify_ok = rsa_verify_with_public_key_pem((char *)message, messageLength
  132. , sig, sig_len
  133. , filePath);
  134. if (1 == verify_ok)
  135. {
  136. *verifySuccess = YES;
  137. }
  138. else
  139. {
  140. *verifySuccess = NO;
  141. }
  142. }
  143. NSString *formattedPEMString(NSString *originalString)
  144. {
  145. NSString *trimmedString = [originalString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  146. const char *c = [trimmedString UTF8String];
  147. int len = [trimmedString length];
  148. NSMutableString *result = [NSMutableString string];
  149. [result appendString:@"-----BEGIN PRIVATE KEY-----\n"];
  150. int index = 0;
  151. while (index < len) {
  152. char cc = c[index];
  153. [result appendFormat:@"%c", cc];
  154. if ( (index+1) % 64 == 0)
  155. {
  156. [result appendString:@"\n"];
  157. }
  158. index++;
  159. }
  160. [result appendString:@"\n-----END PRIVATE KEY-----"];
  161. return result;
  162. }