-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathRtt_DependencyUtilsApple.mm
136 lines (99 loc) · 3.45 KB
/
Rtt_DependencyUtilsApple.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//////////////////////////////////////////////////////////////////////////////
//
// This file is part of the Corona game engine.
// For overview and more information on licensing please refer to README.md
// Home page: https://github.com/coronalabs/corona
// Contact: support@coronalabs.com
//
//////////////////////////////////////////////////////////////////////////////
#include <CoreFoundation/CFData.h>
#include <Security/Secbase.h>
#include <dlfcn.h>
#include <CommonCrypto/CommonCrypto.h>
#import "NSData+Base64.h"
#include "Rtt_DependencyUtils.h"
// ----------------------------------------------------------------------------
namespace Corona
{
// ----------------------------------------------------------------------------
static SecKeyRef LoadKey( const char *pubKey )
{
SecKeyRef result = NULL;
//NSData *certData = [NSData dataWithContentsOfFile:(NSString *)path];
NSString *pKey = [NSString stringWithUTF8String:pubKey];
NSData *certData = [NSData Rtt_dataFromBase64String:pKey];
if ( certData )
{
SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef)certData);
SecTrustRef trust = NULL;
SecPolicyRef policy = NULL;
if (cert != NULL)
{
policy = SecPolicyCreateBasicX509();
if (policy)
{
if (SecTrustCreateWithCertificates((CFTypeRef)cert, policy, &trust) == noErr)
{
SecTrustResultType resultType;
if (SecTrustEvaluate(trust, &resultType) == noErr)
{
result = SecTrustCopyPublicKey(trust);
}
}
}
}
if (policy) CFRelease(policy);
if (trust) CFRelease(trust);
if (cert) CFRelease(cert);
}
return result;
}
static bool Verify(const char *data, int dataLen, CFDataRef dataSig, const char *pKey)
{
unsigned char hash[CC_SHA1_DIGEST_LENGTH + 1];
CC_SHA1(data, dataLen, hash);
//CFMutableStringRef key = CFStringCreateMutable(NULL, 0);
//NSBundle *bundle = [NSBundle mainBundle];
//key = (CFMutableStringRef)[bundle pathForResource:[NSString stringWithUTF8String:keyPath] ofType:@"der"];
SecKeyRef publicKeyRef = LoadKey( pKey );
bool result = false;
if ( publicKeyRef )
{
OSStatus status = SecKeyRawVerify(
publicKeyRef,
kSecPaddingPKCS1SHA1,
hash,
CC_SHA1_DIGEST_LENGTH,
CFDataGetBytePtr( dataSig ),
(size_t)CFDataGetLength( dataSig ));
//0 means no errors, 1 means errors
result = (0 == status);
}
return result;
}
int
DependencyUtils::DecodeBase64( lua_State *L )
{
const char *payloadData = lua_tostring( L, 1 );
NSString *payloadBase64 = [NSString stringWithUTF8String:payloadData];
NSData *data = [NSData Rtt_dataFromBase64String:(NSString *)payloadBase64];
lua_pushlstring(L, (const char *)[data bytes], [data length]);
return 1;
}
int
DependencyUtils::Check( lua_State *L )
{
const char *publicKey = lua_tostring( L, lua_upvalueindex( 1 ) );
//int sigLength = lua_objlen(L,1);
const char *signature = lua_tostring( L, 1 );
int dataLength = (int) lua_objlen(L,2);
const char *payloadData = lua_tostring( L, 2 );
NSString *signatureBase64 = [NSString stringWithUTF8String:signature];
CFDataRef dataSig = (CFDataRef)[NSData Rtt_dataFromBase64String:(NSString *)signatureBase64];
bool result = Verify(payloadData, dataLength, dataSig, publicKey);
lua_pushboolean(L, result);
return 1;
}
// ----------------------------------------------------------------------------
} // namespace Corona
// ----------------------------------------------------------------------------