tags:

views:

280

answers:

2

With prior OpenSSL versions it was possible to do this in SWIG .i files:

STACK *ssl_get_ciphers(SSL *ssl) {
    return (STACK *)SSL_get_ciphers(ssl);
}

With OpenSSL 1.0.0beta3 this fails because STACK seems to be no longer defined. New OpenSSL tries to do a better job at type checking, so one is supposed to use the STACK_OF macro, which is defined like this:

#define STACK_OF(type) struct stack_st_##type

If I change the code above to use STACK_OF:

STACK_OF(SSL_CIPHER) *ssl_get_ciphers(SSL *ssl) {
    return SSL_get_ciphers(ssl);
}

Then SWIG does not like this:

Error: Syntax error in input(1).

I can get things to compile by changing that to:

struct stack_st_SSL_CIPHER *ssl_get_ciphers(SSL *ssl) {
    return SSL_get_ciphers(ssl);
}

but this is obviously not good, because OpenSSL could change the macro from release to release. There is _STACK struct, but again that is OpenSSL private detail and could change from release to release. Stacks can also come in as parameters, as in:

int sk_x509_num(STACK_OF(X509) *stack) {
    return sk_num(stack);
}

which SWIG does not like either.

Is there any way to make this work without resorting to using OpenSSL private details?

A: 

Can you create your own header that makes typedefs like this:

typedef t_stack_SSL_CIPHER STACK_OF(SSL_CIPHER)

Then in the .i file do

t_stack_SSL_CIPHER *ssl_get_ciphers(SSL *ssl) {
    return SSL_get_ciphers(ssl);
}

...and get your header #included in the right place at compile time?

caf
A: 

After some more reading I found why I had the problem, and how to get around it. The problem happened because SWIG's preprocessor does not know about the STACK_OF macro, so that is why SWIG fails. I can fix that by adding this in my .i file:

%include <openssl/safestack.h>

Now I also run into the same problem with LHASH_OF macro. Unfortunately it is included in lhash.h which also includes stdio.h etc. which I don't want (and my build actually can't find them the way it is set up). My build happens to need -includeall command line option to swig. So unfortunately with LHASH_OF I ended up with copying the definition from OpenSSL :(

#define LHASH_OF(type) struct lhash_st_##type

right under the %include line I mentioned above.

Heikki Toivonen