//
// Copyright (c) 2015 by Appcelerator, Inc. All Rights Reserved.
//
// This code is proprietary and confidential to Appcelerator
// and not for redistribution.
//

#import "UIKit/UIKit.h"
#import "defines.h"
#import "TiLog.h"
#import "TiVerifyErrorController.h"
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>
#import "AppcVerifyKeychainItemWrapper.h"
#import "TiLog.h"
#import "defines.h"
#import "appcsecurity.h"
#import <unistd.h>
#import <sys/sysctl.h>
#import "tiverify-Swift.h"

// boost redefines nil which is a problem so we undefine and then redefine
#define NIL (id)0
#undef nil
#import "obfuscate/MetaString4.h"
#import "obfuscate/ObfuscatedCall.h"
#import "obfuscate/ObfuscatedCallWithPredicate.h"
#define nil NIL

@interface TiApp : NSObject
+(TiApp*)app;
-(void)showModalController:(UIViewController*)modalController animated:(BOOL)animated;
@end


@interface TiVerify : NSObject
@end

// redefine these, which must match the ApplicationRouting.m that is generated
#define initializeAppData   icucfasf7797nnzz
#define filterAppData       sdnmnciuuu66zzaq


EXTERN void initializeAppData (NSString * sha1);
EXTERN NSData* filterAppData (NSString *filename, NSData * thedata);



using namespace appc::verify;

extern NSString * const TI_APPLICATION_ID;
extern NSString * const TI_APPLICATION_GUID;

// mangle our internal symbols to make it harder to read outside this library

#define PhoneJailBrokeDetected                  jfn9asx
#define DebuggerAttachedDetected                debuasda09
#define keyVariableName                         asd8990asdfl
#define pepperVariableName                      asdfasdf9889d
#define HMACkeyVariableName                     fd988989d
#define keyMapVariableName                      d908asdd
#define RestoreFromKeyChain                     fxasd98
#define SaveToKeyChain                          asdfasd090098d
#define FetchCredentials                        dad88c88c
#define RetrieveEmbedCredentials                ggrjlkskkk
#define SecurityViolationDetected               zasdfasdf989x
#define SecurityIdentifier                      rasdfsd77vv
#define isDevelopmentDeployType                 zxv9880c08b
#define securityViolationUIShown                uucuucc999
#define ShowSecurityViolationUI                 asd098cv8v8v
#define Startup                                 asdfa90808808vv
#define DecryptBlob                             dc55555zzggg
#define DetectDebugger                          d7879787ndnzzz8
#define TI_APPLICATION_APPC_BUILD               xnnb88b9
#define TI_APPLICATION_APPC_URL                 zzz9090vv88
#define TI_APPLICATION_APPC_EMBED_BLOB          ffk332958921
#define TI_APPLICATION_APPC_EMBED_KEY           jg21934483322
#define TI_APPLICATION_APPC_ENC_POLICY          verkk3322k2

static NSString * TI_APPLICATION_APPC_URL;
static NSString * TI_APPLICATION_APPC_BUILD;
static NSString * TI_APPLICATION_APPC_EMBED_BLOB;
static NSString * TI_APPLICATION_APPC_EMBED_KEY;
static NSString * TI_APPLICATION_APPC_ENC_POLICY;

#if TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
#define TI_APPLICATION_APPC_DBG_CHECK           vv9800980890v
#define TI_APPLICATION_APPC_JBK_CHECK           c899089089
#define TI_APPLICATION_APPC_VERIFY_PEPPER       gggfk332944990
#define TI_APPLICATION_APPC_VERIFY_HMAC         ddkssg33jjg4jh
extern NSString * TI_APPLICATION_APPC_VERIFY_PEPPER;
extern NSString * TI_APPLICATION_APPC_VERIFY_HMAC;

extern bool TI_APPLICATION_APPC_DBG_CHECK;
extern bool TI_APPLICATION_APPC_JBK_CHECK;
#endif

static NSString *keyVariableName = 0;
static NSString *pepperVariableName = 0;
static NSString *HMACkeyVariableName = 0;
static NSDictionary *keyMapVariableName = 0;

static bool SecurityViolationDetected = false;
static bool securityViolationUIShown = false;

NSString *securityViolationUImsg = @"Application Security Violation";
#define SecurityViolationDetectedShowUI \
ShowSecurityViolationUI();\
return;\

// encrypted strings so that a hacker can't easily determine which internal strings we're using
#define BASE64      [NSString stringWithUTF8String:OBFUSCATED4("base64")]
#define PEPPER      [NSString stringWithUTF8String:OBFUSCATED4("pepper")]
#define HMACKEY     [NSString stringWithUTF8String:OBFUSCATED4("hmacKey")]
#define KEYS        [NSString stringWithUTF8String:OBFUSCATED4("keys")]
#define KEY         [NSString stringWithUTF8String:OBFUSCATED4("key")]
#define SHA         [NSString stringWithUTF8String:OBFUSCATED4("sha")]
#define BUILD       [NSString stringWithUTF8String:OBFUSCATED4("build")]
#define URL         [NSString stringWithUTF8String:OBFUSCATED4("url")]
#define KV          [NSString stringWithUTF8String:OBFUSCATED4("kv")]
#define SEED        [NSString stringWithUTF8String:OBFUSCATED4("seed")]
#define EMBEDBLOB   [NSString stringWithUTF8String:OBFUSCATED4("embedBlob")]
#define EMBEDKEY    [NSString stringWithUTF8String:OBFUSCATED4("embedKey")]
#define POLICY		[NSString stringWithUTF8String:OBFUSCATED4("policy")]

#define GETVAR(n,v) \
v = [dictionary objectForKey:n];\
if (!v) {\
SecurityViolationDetectedShowUI; \
return;\
}\
v = [decodeB64(v) retain]; \

//don't decodeB64 in embed case
#define GETEMBEDVAR(n,v) \
v = [dictionary objectForKey:n];\
if (!v) {\
SecurityViolationDetectedShowUI; \
return;\
}\
[v retain]; \

#define GETVARNUM(n,v) \
id val = [dictionary objectForKey:n];\
if (!val) {\
SecurityViolationDetectedShowUI; \
return;\
}\
v = [val shortValue];
