From c698c34e93d325294935c8ae99269c509105e20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Ates?= Date: Mon, 31 Jan 2011 20:55:30 +0100 Subject: [PATCH] [authsaml2] Working java binding --- bindings/java/Myclass.java | 17 +++- bindings/java/lang.py | 175 +++++++++++++++++++++++++++++++----- bindings/java/wrapper_top.c | 83 +++++++++++------ 3 files changed, 226 insertions(+), 49 deletions(-) diff --git a/bindings/java/Myclass.java b/bindings/java/Myclass.java index 13ae843..252dacc 100644 --- a/bindings/java/Myclass.java +++ b/bindings/java/Myclass.java @@ -18,10 +18,16 @@ class Myclass{ System.out.println("Group generation..."); PrimeOrderGroup g = new PrimeOrderGroup(256); g.moreBases(5); + String[] bases = (String[]) g.getbases(); + int i; + for(i=0;i<5;i++){ + System.out.println("Base: " + bases[i]); + } System.out.println("Group generation successful."); System.out.println("Prover: DLREP to prove generation..."); - String dlrep = CrypticJNI.get_dlrep(3,quantities,g.getbases(),g.getp()); + String dlrep = CrypticJNI.get_dlrep(3,quantities,bases,g.getp()); + System.out.println("DLREP: " + dlrep); System.out.println("Prover: Proof round1"); ZkpkSchnorr s = new ZkpkSchnorr(g.getbases(),3,g.getp()); @@ -45,6 +51,15 @@ class Myclass{ private static void test_2(){ System.out.println("\n\t############## TEST 2 ##############\n"); + System.out.println("QRG generation..."); + QRG qrg = new QRG(1024); + System.out.println("QRG generated"); + System.out.println("qrg.getp() : " + qrg.getp()); + System.out.println("qrg.getn() : " + qrg.getn()); + System.out.println("qrg.getbase() : " + qrg.getbase()); + QRG qrg2 = QRG.newLoad(qrg.getp(), qrg.getn(), qrg.getbase()); + //QRG qrg2 = new QRG(qrg.getp(), qrg.getn(), qrg.getbase()); + System.out.println("Clsig server generation..."); Clsig c = new Clsig(1024,256,600,0,0,0,10); c.generateParameters(); diff --git a/bindings/java/lang.py b/bindings/java/lang.py index c7b1d00..b87a198 100644 --- a/bindings/java/lang.py +++ b/bindings/java/lang.py @@ -152,6 +152,9 @@ class Binding: self.generate_wrapper() self.generate_exception_classes() self.generate_cryptic_classes() +########################### + self.generate_functions() +########################### # CrypticConstants @@ -230,8 +233,10 @@ protected static native void destroy(long cptr); if is_boolean(vtype): return 'boolean' ############################ - elif is_tabbn(vtype) or is_tabint(vtype): - return 'Object[]' + elif is_tabbn(vtype): + return 'String[]' + elif is_tabint(vtype): + return 'int[]' elif is_bn(vtype): return 'String' ############################ @@ -261,9 +266,7 @@ protected static native void destroy(long cptr); # elif vtype in ('const GList*','GList*','GHashTable*'): ##################################" elif vtype in ('const GList*','GList*','GHashTable*', 'int*', 'BIGNUM**'): -##################################" return 'Object[]' -##################################" elif vtype == 'BIGNUM*': return 'String' ##################################" @@ -293,17 +296,12 @@ protected static native void destroy(long cptr); ##################################" def generate_JNI_functions(self, m, fd): - import sys - print >> sys.stderr, '%s %s' %(m.name,m.return_type) -##################################" - #if m.return_type.startswith('Cryptic'): if m.name.endswith('_new'): -##################################" jtype = 'long' else: jtype = self.JNI_return_type(m.return_type) name = self.JNI_function_name(m) - print >> fd, ' public static native %s %s(%s);' % (jtype, name, generate_arg_list3(self,m.args)) + print >> fd, ' public static native %s %s(%s);' % (jtype,name, generate_arg_list3(self,m.args)) def JNI_member_function_prefix(self,c,m): #############################" @@ -334,6 +332,18 @@ protected static native void destroy(long cptr); print >> fd, ' public static native %s[] %s(GObject obj);' % (jtype,name) name = '%s_set' % prefix print >> fd, ' public static native void %s(GObject obj, %s[] value);' % (name,jtype) +###################################### +# elif mtype == 'BIGNUM**': +# name = '%s_get' % prefix +# print >> fd, ' public static native %s[] %s(GObject obj);' % (jtype,name) +# name = '%s_set' % prefix +# print >> fd, ' public static native void %s(GObject obj, %s[] value);' % (name,jtype) +# elif mtype == 'int*': +# name = '%s_get' % prefix +# print >> fd, ' public static native %s[] %s(GObject obj);' % (jtype,name) +# name = '%s_set' % prefix +# print >> fd, ' public static native void %s(GObject obj, %s[] value);' % (name,jtype) +###################################### else: name = '%s_get' % prefix print >> fd, ' public static native %s %s(GObject obj);' % (jtype,name) @@ -413,11 +423,11 @@ protected static native void destroy(long cptr); elif is_cstring(type): return 'jstring' #######################################""" + elif is_glist(type) or is_hashtable(type) or is_tabbn(type) or is_tabint(type): + return 'jobjectArray' elif is_bn(type): return 'jstring' - elif is_glist(type) or is_hashtable(type) or is_tabbn(type) or is_tabint(type): #######################################""" - return 'jobjectArray' elif is_xml_node(type): return 'jstring' elif is_object(type): @@ -473,7 +483,8 @@ protected static native void destroy(long cptr); return 'jstring_to_string(env, %s, (char**)&%s);' % (right,left) #################### elif is_tabbn(type): - return 'set_list_of_bn(env, &%s,%s);' % (left,right) +# return 'set_list_of_bn(env, &%s,%s);' % (left,right) + return '%s = set_list_of_bn(env, %s);' % (left,right) elif is_bn(type): return 'jstring_to_bignum(env, %s, (BIGNUM**)&%s);' % (right,left) elif is_tabint(type): @@ -526,10 +537,7 @@ protected static native void destroy(long cptr); # print >> fd, '''static PyObject* #%s(PyObject *self, PyObject *args) #{''' % name -##################################" - #if m.return_type.startswith('Cryptic'): if m.name.endswith('_new'): -##################################" jtype = 'jlong' else: jtype = self.jni_return_type(m.return_type) @@ -607,10 +615,7 @@ protected static native void destroy(long cptr); # Return if m.return_type: -##################################" - #if m.return_type.startswith('Cryptic'): if m.name.endswith('_new'): -##################################" print >> fd, ' r_value = (jlong)(ptrdiff_t) return_value;' else: options = {} @@ -632,6 +637,7 @@ protected static native void destroy(long cptr); klass = c.name prefix = self.JNI_member_function_prefix(c,m) return_type = self.jni_return_type(m) + print 'return_type ' + return_type signature = wrapper_decl("%s_get" % prefix, return_type) field = 'gobj->%s' % name d = locals() @@ -730,7 +736,10 @@ protected static native void destroy(long cptr); def generate_wrapper_getter_setter(self, c, fd): klassname = c.name + print klassname for m in c.members: + print m + print m[0] # getter self.generate_wrapper_getter(c, m, fd) self.generate_wrapper_setter(c, m, fd) @@ -738,6 +747,7 @@ protected static native void destroy(long cptr); prefix = self.JNI_member_function_prefix(c,m) jtype = self.jni_return_type(mtype) # add/remove + print jtype if is_glist(mtype): self.generate_wrapper_adder(c, m, fd) self.generate_wrapper_remover(c, m, fd) @@ -860,9 +870,6 @@ protected static native void destroy(long cptr); print >> fd, ' static public %s %s(%s) {' % (class_name, name, generate_arg_list(self,m.args)) print >> fd, ' return (%s) CrypticJNI.%s(%s);' % (class_name, self.JNI_function_name(m),generate_arg_list2(m.args)) print >> fd, ' }' - #print >> fd, ' public %s(%s) {' % (class_name, generate_arg_list(self,m.args)) - #print >> fd, ' super(CrypticJNI.%s(%s));' % (self.JNI_function_name(m),generate_arg_list2(m.args)) - #print >> fd, ' }' print >> fd, ' /* Setters and getters */' for m in c.members: type, name, options = m @@ -904,6 +911,22 @@ protected static native void destroy(long cptr); print >> fd, ' public Map get%s() {' % jname print >> fd, ' return arrayToMap(CrypticJNI.%s_get(this));' % prefix print >> fd, ' }' +##############################" + elif type == 'BIGNUM**': + print >> fd, ' public void set%s(String[] value) {' % (jname) + print >> fd, ' CrypticJNI.%s_set(this, value);' % prefix + print >> fd, ' }' + print >> fd, ' public String[] get%s() {' % (jname) + print >> fd, ' return CrypticJNI.%s_get(this);' % prefix + print >> fd, ' }' + elif type == 'int*': + print >> fd, ' public void set%s(int[] value) {' % (jname) + print >> fd, ' CrypticJNI.%s_set(this, value);' % prefix + print >> fd, ' }' + print >> fd, ' public int[] get%s() {' % (jname) + print >> fd, ' return CrypticJNI.%s_get(this);' % prefix + print >> fd, ' }' +##############################" else: print >> fd, ' public void set%s(%s value) {' % (jname,jtype) print >> fd, ' CrypticJNI.%s_set(this, value);' % prefix @@ -992,3 +1015,111 @@ protected static native void destroy(long cptr); print >> fd, ' }' print >> fd, '}' fd.close() + + def generate_functions(self): + def method_name(m,class_name): + prefix = len(class_name) + if m.rename: + return m.rename + else: +#######################" +# name = format_as_camelcase(m.name[6:]) + name = format_as_camelcase(m.name[8:]) +#######################" + name = name[prefix:] + return name[0].lower() + name[1:] + + path = cryptic_java_path + 'Functions.java' + fd = open(path,'w') + print >> fd, 'package %s;' % cryptic_package_name + + print >> fd, 'public class Functions {' + print >> fd, ' /* Constructors */' + print >> fd, ' public Functions() {' + print >> fd, ' }' + + for m in self.binding_data.functions: + + if m.name.endswith('_new') or '_new_' in m.name: + continue + + return_type = self.JNI_return_type(m.return_type) + jni_name = self.JNI_function_name(m) + mname = format_as_camelcase(m.name[8:]) + args = m.args + doc = m.docstring + + + def normalize(str,first=' * '): + wrapper = textwrap.TextWrapper() + wrapper.initial_indent = first + wrapper.subsequent_indent = ' * ' + str = re.sub(r'\bNULL\b','null', str) + str = re.sub(r'#Cryptic(\w+)',r'{@@link \1}',str) + str = re.sub(r'[^.]*must *be *freed *by[^.]*\.?', '', str) + str = re.sub(r'[^.]*internally[^.]*\.?[^.]*freed[^.]*\.?', '', str) + + str = re.sub(r'[^.]*\bfreed?\b[^.]*\.?', '', str) + str = re.sub(r'(a +)?#?GList\*?','an array', str) + return wrapper.fill(re.sub(r'@\b(\w+)\b',r'\1',str)) + if doc: + first = normalize(doc.description, ' /** ') + if first: + print >> fd, first + else: + print >> fd, ' /**\n' + print >> fd, ' *' + for p in doc.parameters: + name = p[0] + desc = p[1] + print >> fd, normalize(desc, ' * @param %s ' % format_as_camelcase(name)) + if doc.return_value: + print >> fd, normalize(doc.return_value, ' * @return ') + if m.errors: + for err in m.errors: + err = error_to_exception(err)[0] + print >> fd, normalize(err,' * @throws ') + print >> fd, ' **/' + outarg = None + for a in args: + if is_out(a): + # only one output arg supported + assert not outarg + outarg = a + if outarg: + assert is_int(make_arg(m.return_type), self.binding_data) + new_return_type = self.JNI_return_type(var_type(outarg)) + print >> fd, ' public %s %s(%s) {' % (new_return_type, mname, generate_arg_list(self, args[1:])) + print >> fd, ' Object[] output = new Object[1];' + print >> fd, ' CrypticException.throwError(CrypticJNI.%s(this, %s));' % (jni_name, generate_arg_list2(args[1:])) + print >> fd, ' return (%s)output[0];' % new_return_type + print >> fd, ' }' + + elif m.return_type == 'GList*' or m.return_type == 'const GList*': + print >> fd, ' public List %s(%s) {' % (mname,generate_arg_list(self,args[1:])) + arglist = generate_arg_list2(args[1:]) + if arglist: + arglist = ', ' + arglist + print >> fd, ' Object[] arr = CrypticJNI.%s(this%s);' % (jni_name,arglist) + print >> fd, ' if (arr != null)' + print >> fd, ' return Arrays.asList(arr);' + print >> fd, ' else' + print >> fd, ' return null;' + print >> fd, ' }' + else: + print >> fd, ' public %s %s(%s) {' % (return_type,mname,generate_arg_list(self,args)) + print >> fd, ' ', + if m.return_type: + print >> fd, 'return', + arglist = generate_arg_list2(args) + if m.errors: + print >> fd, 'CrypticException.throwError(', + print >> fd,'CrypticJNI.%s(%s)' % (jni_name,arglist), + if m.errors: + print >> fd, ');' + else: + print >> fd, ';' + print >> fd, ' }' + + print >> fd, '}' + fd.close() diff --git a/bindings/java/wrapper_top.c b/bindings/java/wrapper_top.c index db16e86..651c42b 100644 --- a/bindings/java/wrapper_top.c +++ b/bindings/java/wrapper_top.c @@ -82,7 +82,8 @@ G_GNUC_UNUSED static void throw_by_name(JNIEnv *env, const char *name, const cha G_GNUC_UNUSED static int bignum_to_jstring(JNIEnv *env, BIGNUM *bn, jstring *jstr); G_GNUC_UNUSED static int jstring_to_bignum(JNIEnv *env, jstring jstr, BIGNUM **bn); -G_GNUC_UNUSED static int set_list_of_bn(JNIEnv *env, BIGNUM ***list, jobjectArray jarr); +//G_GNUC_UNUSED static int set_list_of_bn(JNIEnv *env, BIGNUM ***list, jobjectArray jarr); +G_GNUC_UNUSED static BIGNUM** set_list_of_bn(JNIEnv *env, jobjectArray jarr); G_GNUC_UNUSED static int get_list_of_bn(JNIEnv *env, BIGNUM **list, jobjectArray *jarr); #define get_list_of_bignum(env,list,jarr) get_list(env,"java/lang/String",list,(Converter)bignum_to_jstring,jarr) #define set_list_of_bignum(env,list,jarr) set_list(env,list,jarr,(GFunc)g_free,(OutConverter)jstring_to_bignum) @@ -99,8 +100,8 @@ static jint extractInt(JNIEnv *env, jobject arg); static int -bignum_to_jstring(JNIEnv *env, BIGNUM *bn, jstring *jstr) { - g_error_if_fail(env); +bignum_to_jstring(JNIEnv *env, BIGNUM *bn, jstring *jstr) +{ if (bn) { char *hex = BN_bn2hex(bn); *jstr = (*env)->NewStringUTF(env, hex); @@ -109,17 +110,19 @@ bignum_to_jstring(JNIEnv *env, BIGNUM *bn, jstring *jstr) { } else { *jstr = NULL; } - return 1; + return 1; } static int get_list_of_bn(JNIEnv *env, BIGNUM **list, jobjectArray *jarr) { - g_error_if_fail(env); jsize i; jclass cls; + g_error_if_fail (env); + int size = 0; while(list[size] != NULL) size++; + cls = get_jclass_by_name(env, "java/lang/String"); cryptic_return_val_if_fail(cls, 0); cryptic_return_val_if_fail(create_object_array(env, "java/lang/String", size, jarr), 0); @@ -133,70 +136,98 @@ get_list_of_bn(JNIEnv *env, BIGNUM **list, jobjectArray *jarr) { static int jstring_to_bignum(JNIEnv *env, jstring jstr, BIGNUM **bn) { - g_error_if_fail(env); const char *local_str = NULL; int r; + cryptic_return_val_if_fail(jstring_to_local_string(env, jstr, &local_str), 0); if (local_str) { - if (local_str[0] == '-') { - r = BN_hex2bn(bn, local_str); - BN_set_negative(*bn,1); - } else { - r = BN_hex2bn(bn, local_str); - } + + if(local_str[0] == '-'){ + r = BN_hex2bn(bn, local_str); + BN_set_negative(*bn,1); + }else{ + r = BN_hex2bn(bn, local_str); + } release_local_string(env, jstr, local_str); - if (r<0 || *bn == NULL) { - return 0; - } + if (r<0 || *bn == NULL) { + return 0; + } } else { *bn = NULL; } return 1; } -static int -set_list_of_bn(JNIEnv *env, BIGNUM ***list, jobjectArray jarr) { - g_error_if_fail(env); +/*static BIGNUM** +set_list_of_bn(JNIEnv *env, jobjectArray jarr) { jobject element = NULL; jsize size = 0; jsize i = 0; + BIGNUM **list; + printf("set_list_of_bn\n"); g_error_if_fail (list && env); if (jarr) { - if (! get_array_size(env, jarr, &size)) { + if (! get_array_size(env, jarr, &size)){ return 0; } list = NULL; list = (BIGNUM***) g_malloc0(sizeof(BIGNUM***)); list[0] = NULL; list[0] = (BIGNUM**) g_malloc0(size*sizeof(BIGNUM**)); - for (i=0; i