From 7331ee349be9c514c69b7216b73b60c35920edc0 Mon Sep 17 00:00:00 2001 From: James Yonan Date: Wed, 1 Aug 2012 12:28:13 +0000 Subject: [PATCH] Full iOS beta for OpenVPN Connect. --- client/ovpncli.cpp | 6 +- openvpn/applecrypto/cf/cf.hpp | 39 ++++--- openvpn/applecrypto/cf/cfhelper.hpp | 166 ++++++++++++---------------- openvpn/applecrypto/ssl/sslctx.hpp | 4 +- openvpn/openssl/ssl/sslctx.hpp | 6 +- openvpn/polarssl/ssl/sslctx.hpp | 6 +- test/ssl/simple.cpp | 4 +- 7 files changed, 104 insertions(+), 127 deletions(-) diff --git a/client/ovpncli.cpp b/client/ovpncli.cpp index 7effec0a4..a60fa8516 100644 --- a/client/ovpncli.cpp +++ b/client/ovpncli.cpp @@ -4,14 +4,14 @@ #include // Set up export of our public interface unless -// CLIENT_API_VISIBILITY_HIDDEN is defined +// OPENVPN_CORE_API_VISIBILITY_HIDDEN is defined #if defined(__GNUC__) #define OPENVPN_CLIENT_EXPORT -#ifndef CLIENT_API_VISIBILITY_HIDDEN +#ifndef OPENVPN_CORE_API_VISIBILITY_HIDDEN #pragma GCC visibility push(default) #endif #include "ovpncli.hpp" // public interface -#ifndef CLIENT_API_VISIBILITY_HIDDEN +#ifndef OPENVPN_CORE_API_VISIBILITY_HIDDEN #pragma GCC visibility pop #endif #else diff --git a/openvpn/applecrypto/cf/cf.hpp b/openvpn/applecrypto/cf/cf.hpp index 30e75a575..0184456aa 100644 --- a/openvpn/applecrypto/cf/cf.hpp +++ b/openvpn/applecrypto/cf/cf.hpp @@ -5,12 +5,7 @@ #include #include -#include -#include -#include -#include -#include -#include +#include #include #include @@ -83,6 +78,8 @@ namespace openvpn { T operator()() const { return obj_; } + CFTypeRef generic() const { return (CFTypeRef)obj_; } + T release() { T ret = obj_; @@ -90,6 +87,13 @@ namespace openvpn { return ret; } + CFTypeRef generic_release() + { + T ret = obj_; + obj_ = NULL; + return (CFTypeRef)ret; + } + // Intended for use with Core Foundation methods that require // a T* for saving a (non-borrowed) return value T* mod_ref() @@ -153,11 +157,21 @@ namespace openvpn { return String(CFStringCreateWithCString(kCFAllocatorDefault, str, kCFStringEncodingUTF8)); } + inline String string(CFStringRef str) + { + return String(str, BORROW); + } + inline String string(const std::string& str) { return String(CFStringCreateWithCString(kCFAllocatorDefault, str.c_str(), kCFStringEncodingUTF8)); } + inline String string(const std::string* str) + { + return String(CFStringCreateWithCString(kCFAllocatorDefault, str->c_str(), kCFStringEncodingUTF8)); + } + inline Number number_from_int(const int n) { return Number(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n)); @@ -248,8 +262,8 @@ namespace openvpn { return NULL; } - template - inline CFTypeRef dict_index(const DICT& dict, const char *key) + template + inline CFTypeRef dict_index(const DICT& dict, const KEY key) { if (dict.defined()) { @@ -260,15 +274,6 @@ namespace openvpn { return NULL; } - template - inline CFTypeRef dict_index(const DICT& dict, CFStringRef key) - { - if (dict.defined() && key) - return CFDictionaryGetValue(dict(), key); - else - return NULL; - } - // string methods OPENVPN_SIMPLE_EXCEPTION(cppstring_error); diff --git a/openvpn/applecrypto/cf/cfhelper.hpp b/openvpn/applecrypto/cf/cfhelper.hpp index 09ea1d2ad..eb952c7ef 100644 --- a/openvpn/applecrypto/cf/cfhelper.hpp +++ b/openvpn/applecrypto/cf/cfhelper.hpp @@ -18,18 +18,20 @@ namespace openvpn { // Lookup or create (if absent) an item in a mutable dictionary. // Return the item, which will be owned by base. + template inline CFTypeRef dict_get_create(CFMutableDictionaryRef base, - CFStringRef key, + const KEY& key, CFTypeRef (*create_method)()) { if (base) { - CFTypeRef ret = CFDictionaryGetValue(base, key); // try lookup first + String keystr = string(key); + CFTypeRef ret = CFDictionaryGetValue(base, keystr()); // try lookup first if (!ret) { // doesn't exist, must create ret = (*create_method)(); - CFDictionaryAddValue(base, key, ret); + CFDictionaryAddValue(base, keystr(), ret); CFRelease(ret); // because ret is now owned by base } return ret; @@ -37,59 +39,39 @@ namespace openvpn { return NULL; } - // variation on above function that accepts a char * key - inline CFTypeRef dict_get_create(CFMutableDictionaryRef base, - const char *key, - CFTypeRef (*create_method)()) - { - String keystr = string(key); - return dict_get_create(base, keystr(), create_method); - } - // lookup a dict in another dict (base) and return or create if absent - - inline MutableDict dict_get_create_dict(MutableDict& base, CFStringRef key) - { - return CF::mutable_dict_cast(CF::dict_get_create(base(), key, CF::mutable_dict_new)); - } - - inline MutableDict dict_get_create_dict(MutableDict& base, const char *key) + template + inline MutableDict dict_get_create_dict(MutableDict& base, const KEY& key) { String keystr = string(key); - return CF::mutable_dict_cast(CF::dict_get_create(base(), keystr(), CF::mutable_dict_new)); + return mutable_dict_cast(dict_get_create(base(), keystr(), mutable_dict_new)); } // lookup an array in a dict (base) and return or create if absent - - inline MutableArray dict_get_create_array(MutableDict& base, CFStringRef key) - { - return CF::mutable_array_cast(CF::dict_get_create(base(), key, CF::mutable_array_new)); - } - - inline MutableArray dict_get_create_array(MutableDict& base, const char *key) + template + inline MutableArray dict_get_create_array(MutableDict& base, const KEY& key) { String keystr = string(key); - return CF::mutable_array_cast(CF::dict_get_create(base(), keystr(), CF::mutable_array_new)); + return mutable_array_cast(dict_get_create(base(), keystr(), mutable_array_new)); } - - // lookup a string in a dictionary (DICT should be a Dict or a MutableDict) - template - inline std::string dict_get_str(const DICT& dict, CFStringRef key) + // lookup an object in a dictionary (DICT should be a Dict or a MutableDict) + template + inline CFTypeRef dict_get_obj(const DICT& dict, const KEY& key) { - return cppstring(string_cast(dict_index(dict, key))); + return dict_index(dict, key); } // lookup a string in a dictionary (DICT should be a Dict or a MutableDict) - template - inline std::string dict_get_str(const DICT& dict, const char *key) + template + inline std::string dict_get_str(const DICT& dict, const KEY& key) { return cppstring(string_cast(dict_index(dict, key))); } // lookup an integer in a dictionary (DICT should be a Dict or a MutableDict) - template - inline int dict_get_int(const DICT& dict, const char *key, const int default_value) + template + inline int dict_get_int(const DICT& dict, const KEY& key, const int default_value) { int ret; Number num = number_cast(dict_index(dict, key)); @@ -99,95 +81,94 @@ namespace openvpn { return default_value; } - // set a string in a mutable dictionary - - inline void dict_set_str(MutableDict& dict, CFStringRef key, CFStringRef value) + // lookup a boolean in a dictionary (DICT should be a Dict or a MutableDict) + template + inline bool dict_get_bool(const DICT& dict, const KEY& key, const bool default_value) { - CFDictionarySetValue(dict(), key, value); + Bool b = bool_cast(dict_index(dict, key)); + if (b.defined()) + { + if (b() == kCFBooleanTrue) + return true; + else if (b() == kCFBooleanFalse) + return false; + } + return default_value; } - inline void dict_set_str(MutableDict& dict, const char *key, CFStringRef value) + // set a CFTypeRef in a mutable dictionary + + template + inline void dict_set_obj(MutableDict& dict, const KEY& key, CFTypeRef value) { String keystr = string(key); CFDictionarySetValue(dict(), keystr(), value); } - inline void dict_set_str(MutableDict& dict, CFStringRef key, const std::string& value) - { - String valstr = string(value); - CFDictionarySetValue(dict(), key, valstr()); - } + // set a string in a mutable dictionary - inline void dict_set_str(MutableDict& dict, const char* key, const std::string& value) + template + inline void dict_set_str(MutableDict& dict, const KEY& key, const VALUE& value) { String keystr = string(key); String valstr = string(value); CFDictionarySetValue(dict(), keystr(), valstr()); } - // append string to a mutable array - - inline void array_append_str(MutableArray& array, const std::string& value) - { - String valstr = string(value); - CFArrayAppendValue(array(), valstr()); - } - // set a number in a mutable dictionary - inline void dict_set_int(MutableDict& dict, CFStringRef key, int value) - { - Number num = number_from_int(value); - CFDictionarySetValue(dict(), key, num()); - } - - inline void dict_set_int(MutableDict& dict, const char *key, int value) + template + inline void dict_set_int(MutableDict& dict, const KEY& key, int value) { String keystr = string(key); Number num = number_from_int(value); CFDictionarySetValue(dict(), keystr(), num()); } - inline void dict_set_int32(MutableDict& dict, CFStringRef key, SInt32 value) - { - Number num = number_from_int32(value); - CFDictionarySetValue(dict(), key, num()); - } - - inline void dict_set_int32(MutableDict& dict, const char *key, SInt32 value) + template + inline void dict_set_int32(MutableDict& dict, const KEY& key, SInt32 value) { String keystr = string(key); Number num = number_from_int32(value); CFDictionarySetValue(dict(), keystr(), num()); } - inline void dict_set_long_long(MutableDict& dict, CFStringRef key, long long value) - { - Number num = number_from_long_long(value); - CFDictionarySetValue(dict(), key, num()); - } - - inline void dict_set_long_long(MutableDict& dict, const char *key, long long value) + template + inline void dict_set_long_long(MutableDict& dict, const KEY& key, long long value) { String keystr = string(key); Number num = number_from_long_long(value); CFDictionarySetValue(dict(), keystr(), num()); } - inline void dict_set_index(MutableDict& dict, CFStringRef key, CFIndex value) + template + inline void dict_set_index(MutableDict& dict, const KEY& key, CFIndex value) { + String keystr = string(key); Number num = number_from_index(value); - CFDictionarySetValue(dict(), key, num()); + CFDictionarySetValue((CFMutableDictionaryRef)dict(), keystr(), num()); } - inline void dict_set_index(MutableDict& dict, const char *key, CFIndex value) + // set a boolean in a mutable dictionary + + template + inline void dict_set_bool(MutableDict& dict, const KEY& key, bool value) { String keystr = string(key); - Number num = number_from_index(value); - CFDictionarySetValue((CFMutableDictionaryRef)dict(), keystr(), num()); + CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse; + CFDictionarySetValue(dict(), keystr(), boolref); } - // append number to a mutable array + // append string to a mutable array + + template + inline void array_append_str(MutableArray& array, const VALUE& value) + { + String valstr = string(value); + CFArrayAppendValue(array(), valstr()); + } + + // append a number to a mutable array inline void array_append_int(MutableArray& array, int value) { @@ -201,25 +182,16 @@ namespace openvpn { CFArrayAppendValue(array(), num()); } - inline void array_append_index(MutableArray& array, CFIndex value) + inline void array_append_long_long(MutableArray& array, long long value) { - Number num = number_from_index(value); + Number num = number_from_long_long(value); CFArrayAppendValue(array(), num()); } - // set a boolean in a mutable dictionary - - inline void dict_set_bool(MutableDict& dict, CFStringRef key, bool value) - { - CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse; - CFDictionarySetValue(dict(), key, boolref); - } - - inline void dict_set_bool(MutableDict& dict, const char *key, bool value) + inline void array_append_index(MutableArray& array, CFIndex value) { - String keystr = string(key); - CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse; - CFDictionarySetValue(dict(), keystr(), boolref); + Number num = number_from_index(value); + CFArrayAppendValue(array(), num()); } } } diff --git a/openvpn/applecrypto/ssl/sslctx.hpp b/openvpn/applecrypto/ssl/sslctx.hpp index bd89bcfa4..f54eb5888 100644 --- a/openvpn/applecrypto/ssl/sslctx.hpp +++ b/openvpn/applecrypto/ssl/sslctx.hpp @@ -44,7 +44,7 @@ namespace openvpn { struct Config { enum { - DEBUG = 1<<0, + SSL_DEBUG_FLAG = 1<<0, }; typedef unsigned int Flags; @@ -57,7 +57,7 @@ namespace openvpn { void enable_debug() { - flags |= DEBUG; + flags |= SSL_DEBUG_FLAG; } void load_identity(const std::string& subject_match) diff --git a/openvpn/openssl/ssl/sslctx.hpp b/openvpn/openssl/ssl/sslctx.hpp index 8c05192a5..fb384aa78 100644 --- a/openvpn/openssl/ssl/sslctx.hpp +++ b/openvpn/openssl/ssl/sslctx.hpp @@ -55,7 +55,7 @@ namespace openvpn { struct Config { enum { - DEBUG = 1<<0, + SSL_DEBUG_FLAG = 1<<0, }; typedef unsigned int Flags; @@ -80,7 +80,7 @@ namespace openvpn { void enable_debug() { - flags |= DEBUG; + flags |= SSL_DEBUG_FLAG; } // if this callback is defined, no private key needs to be loaded @@ -612,7 +612,7 @@ namespace openvpn { ctx_->app_verify_arg = this; // Show handshake debugging info - if (config.flags & Config::DEBUG) + if (config.flags & Config::SSL_DEBUG_FLAG) SSL_CTX_set_info_callback (ctx_, info_callback); // Keep a reference to vars so we can hand them off to SSL objects derived from us diff --git a/openvpn/polarssl/ssl/sslctx.hpp b/openvpn/polarssl/ssl/sslctx.hpp index 7d94970c8..a7b670f3a 100644 --- a/openvpn/polarssl/ssl/sslctx.hpp +++ b/openvpn/polarssl/ssl/sslctx.hpp @@ -60,7 +60,7 @@ namespace openvpn { struct Config { enum { - DEBUG = 1<<0, + SSL_DEBUG_FLAG = 1<<0, }; typedef unsigned int Flags; @@ -86,7 +86,7 @@ namespace openvpn { void enable_debug() { - flags |= DEBUG; + flags |= SSL_DEBUG_FLAG; } // if this callback is defined, no private key needs to be loaded @@ -368,7 +368,7 @@ namespace openvpn { throw PolarSSLException("RNG not defined"); // set debug callback - if (c.flags & Config::DEBUG) + if (c.flags & Config::SSL_DEBUG_FLAG) ssl_set_dbg(ssl, dbg_callback, this); } catch (...) diff --git a/test/ssl/simple.cpp b/test/ssl/simple.cpp index bad1b71e6..a85c44ebd 100644 --- a/test/ssl/simple.cpp +++ b/test/ssl/simple.cpp @@ -71,7 +71,7 @@ int main(int /*argc*/, char* /*argv*/[]) SSLConfig cc; cc.mode = SSLConfig::CLIENT; #if ITER <= 10 - cc.flags = SSLConfig::DEBUG; + cc.flags = SSLConfig::SSL_DEBUG_FLAG; #endif cc.ca = ca1_crt + ca2_crt; cc.cert = client_crt; @@ -83,7 +83,7 @@ int main(int /*argc*/, char* /*argv*/[]) SSLConfig sc; sc.mode = SSLConfig::SERVER; #if ITER <= 10 - sc.flags = SSLConfig::DEBUG; + sc.flags = SSLConfig::SSL_DEBUG_FLAG; #endif sc.ca = ca1_crt + ca2_crt; sc.cert = server_crt;