Hi everybody,
I'm playing around with encoding/decoding of ASN1 DER data records. For that i made under mac a simple test program:
#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
#include <openssl/err.h>
const unsigned char tokenInit[] = { 0xa0,0x3e,0x30,0x3c,0xa0,0x0e,0x30,0x0c,0x06,0x0a,0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x02,0x02,0x0a,0xa2,0x2a,0x04,0x28,0x4e,0x54,0x4c,0x4d,0x53,0x53,0x50,0x00,0x01,0x00,0x00,0x00,0x35,0x82,0x08,0xe2,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x35,0x0e,0x00,0x00,0x00,0x00,0x00,0x0f };
const size_t tokenInitSize = sizeof( tokenInit );
const unsigned char tokenResponse[] = { 0xa1,0x81,0xa2,0x30,0x81,0x9f,0xa0,0x03,0x0a,0x01,0x01,0xa1,0x0c,0x06,0x0a,0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x02,0x02,0x0a,0xa2,0x81,0x89,0x04,0x81,0x86,0x4e,0x54,0x4c,0x4d,0x53,0x53,0x50,0x00,0x02,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x38,0x00,0x00,0x00,0x35,0x82,0x89,0x62,0x57,0xcd,0x8f,0xad,0x37,0xf8,0x89,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x00,0x48,0x00,0x3e,0x00,0x00,0x00,0x06,0x01,0xb0,0x1d,0x0f,0x00,0x00,0x00,0x54,0x00,0x4f,0x00,0x4d,0x00,0x01,0x00,0x06,0x00,0x54,0x00,0x4f,0x00,0x4d,0x00,0x02,0x00,0x06,0x00,0x54,0x00,0x4f,0x00,0x4d,0x00,0x03,0x00,0x12,0x00,0x74,0x00,0x6f,0x00,0x6d,0x00,0x2e,0x00,0x6c,0x00,0x6f,0x00,0x63,0x00,0x61,0x00,0x6c,0x00,0x04,0x00,0x0a,0x00,0x6c,0x00,0x6f,0x00,0x63,0x00,0x61,0x00,0x6c,0x00,0x07,0x00,0x08,0x00,0x00,0xc1,0xea,0x2e,0x73,0xbf,0xd7,0x01,0x00,0x00,0x00,0x00 };
const size_t tokenResponseSize = sizeof( tokenResponse );
const unsigned char *token = tokenResponse;
const size_t tokenSize = sizeof( tokenResponse );
typedef struct MechType_st {
ASN1_OBJECT *mechType;
} SPNEGO_MECHTYPE;
DECLARE_ASN1_FUNCTIONS( SPNEGO_MECHTYPE )
DEFINE_STACK_OF( SPNEGO_MECHTYPE )
ASN1_SEQUENCE( SPNEGO_MECHTYPE ) = {
ASN1_SIMPLE( SPNEGO_MECHTYPE,mechType,ASN1_OBJECT )
} ASN1_SEQUENCE_END( SPNEGO_MECHTYPE )
IMPLEMENT_ASN1_FUNCTIONS( SPNEGO_MECHTYPE )
typedef struct NegTokenInit_st {
STACK_OF( SPNEGO_MECHTYPE ) *mechTypeList;
ASN1_BIT_STRING *reqFlags;
ASN1_OCTET_STRING *mechToken;
ASN1_OCTET_STRING *mechListMIC;
} SPNEGO_NEGTOKENINIT;
DECLARE_ASN1_FUNCTIONS( SPNEGO_NEGTOKENINIT )
ASN1_SEQUENCE( SPNEGO_NEGTOKENINIT ) = {
ASN1_IMP_SEQUENCE_OF( SPNEGO_NEGTOKENINIT, mechTypeList, SPNEGO_MECHTYPE, 0 ),
ASN1_EXP_OPT( SPNEGO_NEGTOKENINIT, reqFlags, ASN1_BIT_STRING, 1 ),
ASN1_EXP_OPT( SPNEGO_NEGTOKENINIT, mechToken, ASN1_OCTET_STRING, 2 ),
ASN1_EXP_OPT( SPNEGO_NEGTOKENINIT, mechListMIC, ASN1_OCTET_STRING, 3 )
} ASN1_SEQUENCE_END( SPNEGO_NEGTOKENINIT )
IMPLEMENT_ASN1_FUNCTIONS( SPNEGO_NEGTOKENINIT )
typedef struct NegTokenResp_st {
ASN1_ENUMERATED *negState;
SPNEGO_MECHTYPE *supportedMech;
ASN1_OCTET_STRING *responseToken;
ASN1_OCTET_STRING *mechListMIC;
} SPNEGO_NEGTOKENRESP;
DECLARE_ASN1_FUNCTIONS( SPNEGO_NEGTOKENRESP )
ASN1_SEQUENCE( SPNEGO_NEGTOKENRESP ) = {
ASN1_EXP_OPT( SPNEGO_NEGTOKENRESP, negState, ASN1_ENUMERATED, 0 ),
ASN1_IMP_OPT( SPNEGO_NEGTOKENRESP, supportedMech, SPNEGO_MECHTYPE, 1 ),
ASN1_EXP_OPT( SPNEGO_NEGTOKENRESP, responseToken, ASN1_OCTET_STRING, 2 ),
ASN1_EXP_OPT( SPNEGO_NEGTOKENRESP, mechListMIC, ASN1_OCTET_STRING, 3 )
} ASN1_SEQUENCE_END( SPNEGO_NEGTOKENRESP )
IMPLEMENT_ASN1_FUNCTIONS( SPNEGO_NEGTOKENRESP )
typedef struct NegotiationToken_st {
int type;
union {
SPNEGO_NEGTOKENINIT *NegTokenInit;
SPNEGO_NEGTOKENRESP *NegTokenResp;
} token;
} SPNEGO_NEGOTIATIONTOKEN;
DECLARE_ASN1_FUNCTIONS( SPNEGO_NEGOTIATIONTOKEN )
DECLARE_ASN1_PRINT_FUNCTION( SPNEGO_NEGOTIATIONTOKEN )
ASN1_CHOICE( SPNEGO_NEGOTIATIONTOKEN ) = {
ASN1_EXP_OPT( SPNEGO_NEGOTIATIONTOKEN, token.NegTokenInit, SPNEGO_NEGTOKENINIT,0 ),
ASN1_EXP_OPT( SPNEGO_NEGOTIATIONTOKEN, token.NegTokenResp, SPNEGO_NEGTOKENRESP,1 )
} ASN1_CHOICE_END( SPNEGO_NEGOTIATIONTOKEN )
IMPLEMENT_ASN1_FUNCTIONS( SPNEGO_NEGOTIATIONTOKEN )
IMPLEMENT_ASN1_PRINT_FUNCTION( SPNEGO_NEGOTIATIONTOKEN )
void SPNEGO_NEGOTIATIONTOKEN_print( SPNEGO_NEGOTIATIONTOKEN *negToken ) {
if( negToken != NULL ) {
printf( "Token provided.\n" );
switch( negToken->type ) {
case 0:
printf( "\tType: NegTokenInit\n" );
SPNEGO_NEGTOKENINIT *NegTokenInit = negToken->token.NegTokenInit;
if( NegTokenInit->mechTypeList == NULL )
printf( "\tmechTypeList: Not present\n" );
if( NegTokenInit->reqFlags == NULL )
printf( "\treqFlags: Not present\n" );
if( NegTokenInit->mechToken == NULL )
printf( "\tmechToken: Not present\n" );
if( NegTokenInit->mechListMIC == NULL )
printf( "\tmechListMIC: Not present\n" );
break;
case 1:
printf( "\tType: NegTokenResp\n" );
SPNEGO_NEGTOKENRESP *NegTokenResp = negToken->token.NegTokenResp;
if( NegTokenResp->negState == NULL )
printf( "\tnegState: Not present\n" );
if( NegTokenResp->supportedMech == NULL )
printf( "\tsupportedMech: Not present\n" );
else {
char buff[1024];
OBJ_obj2txt(buff, 1024, NegTokenResp->supportedMech->mechType, 0);
printf( "\tsupportedMech: %s\n",buff );
}
if( NegTokenResp->responseToken == NULL )
printf( "\tresponseToken: Not present\n" );
if( NegTokenResp->mechListMIC == NULL )
printf( "\tmechListMIC: Not present\n" );
break;
default:
printf( "\tType: Unknown, value %d\n",negToken->type );
}
}
else {
printf( "No token provided\n" );
}
}
int main( const int argc,const char *argv[] ) {
const unsigned char *buffer = token;
SPNEGO_NEGOTIATIONTOKEN *negToken = d2i_SPNEGO_NEGOTIATIONTOKEN( NULL,&buffer,tokenSize );
if( negToken != NULL ) {
printf( "Token decoded.\n" );
SPNEGO_NEGOTIATIONTOKEN_print( negToken );
SPNEGO_NEGOTIATIONTOKEN_free( negToken );
}
else {
printf( "Decoding of token failed:\n" );
ERR_load_crypto_strings();
ERR_print_errors_fp(stderr);
}
negToken = SPNEGO_NEGOTIATIONTOKEN_new();
if( negToken != NULL ) {
negToken->type = 0;
negToken->token.NegTokenInit = SPNEGO_NEGTOKENINIT_new();
negToken->token.NegTokenInit->mechTypeList = sk_SPNEGO_MECHTYPE_new_null();
negToken->token.NegTokenInit->mechToken = ASN1_OCTET_STRING_new();
SPNEGO_MECHTYPE *mechType = SPNEGO_MECHTYPE_new();
mechType->mechType = OBJ_txt2obj( "1.3.6.1.4.1.311.2.2.10",0 );
sk_SPNEGO_MECHTYPE_push( negToken->token.NegTokenInit->mechTypeList,mechType );
const unsigned char mechToken[] = "\x4e\x54\x4c\x4d\x53\x53\x50\x00\x01\x00\x00\x00\x35\x82\x08\xe2\x00\x00\x00\x00\x28\x00\x00\x00\x00\x00\x00\x00\x28\x00\x00\x00\x35\x0e\x00\x00\x00\x00\x00\x0f";
const size_t mechTokenSize = sizeof( mechToken );
ASN1_OCTET_STRING_set( negToken->token.NegTokenInit->mechToken,mechToken,mechTokenSize );
buffer = NULL;
size_t bufferSize = i2d_SPNEGO_NEGOTIATIONTOKEN( negToken,NULL );
printf( "Required buffer size for DER encoding of ASN1 structure: %zu\n",bufferSize );
SPNEGO_NEGOTIATIONTOKEN_print( negToken );
ERR_load_crypto_strings();
ERR_print_errors_fp(stderr);
SPNEGO_NEGOTIATIONTOKEN_free( negToken );
}
else {
printf( "Creating of token failed.\n" );
}
}
I can compile it under mac os with this command.
Quote:
gcc -L/usr/local/opt/openssl@3/lib -I/usr/local/opt/openssl@3/include -o example example.c -lssl -lcrypto
It compiles and executing it output some stuff, as expected.
Now i want to compile it for AmigaOS4. For that i'm using walkero docker image for cross compiling. My best guess how to instruct the compiler to compile this is as following:
Quote:
gcc -static -I./includes/ -I/opt/sdk/AmiSSL/include/ -o example example.c -lssl -lcrypto
That fails with a a lot of compile errors, where i have no clue what the cause is.
amidev@345c9e948f2a:/opt/code/ans.1$ gcc -static -I./includes/ -I/opt/sdk/AmiSSL/include/ -o example example.c -lssl -lcrypto
In file included from /opt/sdk/AmiSSL/include/openssl/crypto.h:26,
from /opt/sdk/AmiSSL/include/openssl/bio.h:23,
from /opt/sdk/AmiSSL/include/openssl/asn1.h:19,
from /opt/sdk/AmiSSL/include/amissl/amissl.h:21,
from /opt/sdk/AmiSSL/include/proto/amissl.h:31,
from /opt/sdk/AmiSSL/include/openssl/asn1.h:2,
from example.c:1:
/opt/sdk/AmiSSL/include/openssl/safestack.h:23:32: warning: ‘struct stack_st_SPNEGO_MECHTYPE’ declared inside parameter list will not be visible outside of this definition or declaration
23 | # define STACK_OF(type) struct stack_st_##type
| ^~~~~~~~~
/opt/sdk/AmiSSL/include/amissl/inline.h:29:48: note: in expansion of macro ‘STACK_OF’
29 | static ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
| ^~~~~~~~
/opt/sdk/AmiSSL/include/openssl/safestack.h:138:29: note: in expansion of macro ‘SKM_DEFINE_STACK_OF’
138 | # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
| ^~~~~~~~~~~~~~~~~~~
example.c:20:1: note: in expansion of macro ‘DEFINE_STACK_OF’
20 | DEFINE_STACK_OF( SPNEGO_MECHTYPE )
| ^~~~~~~~~~~~~~~
/opt/sdk/AmiSSL/include/openssl/safestack.h:23:32: warning: ‘struct stack_st_SPNEGO_MECHTYPE’ declared inside parameter list will not be visible outside of this definition or declaration
23 | # define STACK_OF(type) struct stack_st_##type
| ^~~~~~~~~
/opt/sdk/AmiSSL/include/amissl/inline.h:33:50: note: in expansion of macro ‘STACK_OF’
33 | static ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
| ^~~~~~~~
/opt/sdk/AmiSSL/include/openssl/safestack.h:138:29: note: in expansion of macro ‘SKM_DEFINE_STACK_OF’
138 | # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
| ^~~~~~~~~~~~~~~~~~~
example.c:20:1: note: in expansion of macro ‘DEFINE_STACK_OF’
20 | DEFINE_STACK_OF( SPNEGO_MECHTYPE )
| ^~~~~~~~~~~~~~~
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/10.1.0/../../../../ppc-amigaos/bin/ld: cannot find -lssl
/opt/ppc-amigaos/lib/gcc/ppc-amigaos/10.1.0/../../../../ppc-amigaos/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status
The last two errors i can explain to myself. But nevertheless i'm clueless. Is it that AmiSSL doesn't support the used functions, or do i need to instruct the compiler in a special way?