Hello,
I've recently downloaded and installed the OpenSSL toolkit.
It is really cool!
What I'm doing now is preparing a binding for the OCaml language,
allowing use of the cryptographic functionality of OpenSSL from OCaml.
I'm having a bit of trouble with the Diffie-Hellman stuff, specifically,
the d2i_DHparam, and i2d_DHparam functions. Perhaps you can explain
what I'm suppose to be doing.
I've written a simple program that creates a DH pair, converts it into a
string, and back. It doesn't work properly, and I couldn't figure out
the percise workings of the ASN1 package.
Any help appreciated,
Ohad.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WINDOWS
#include "../bio/bss_file.c"
#endif
#include <openssl/crypto.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#ifdef NO_DH
int main(int argc, char *argv[])
{
printf("No DH support\n");
return(0);
}
#else
#include <openssl/dh.h>
#ifdef WIN16
#define MS_CALLBACK _far _loadds
#else
#define MS_CALLBACK
#endif
static void MS_CALLBACK cb(int p, int n, char *arg);
#ifdef NO_STDIO
#define APPS_WIN16
#include "bss_file.c"
#endif
BIO *out=NULL;
int main(int argc, char *argv[])
{
DH *a,*b;
char buf[12];
unsigned char *abuf=NULL,*bbuf=NULL;
int i,alen,blen,aout,bout,ret=1;
long len = 0;
unsigned char *p,**pp;
int i;
DH *c = NULL;
#ifdef WIN32
CRYPTO_malloc_init();
#endif
out=BIO_new(BIO_s_file());
if (out == NULL) exit(1);
BIO_set_fp(out,stdout,BIO_NOCLOSE);
a=DH_generate_parameters(64,DH_GENERATOR_5,cb,(char *)out);
if (a == NULL) goto err;
if (!DH_generate_key(a)) goto err;
p = malloc(16);
pp = &p;
memset((char*)p,0,16);
len = i2d_DHparams(a,pp);
c = d2i_DHparams(NULL,pp,16);
if (c == NULL) {
printf ("Null return value in DH routines.\n");
} else {
BIO_puts(out,"\np =");
BN_print(out,c->p);
BIO_puts(out,"\ng =");
BN_print(out,c->g);
BIO_puts(out,"\n");
}
err:
exit(0)
}
static void MS_CALLBACK cb(int p, int n, char *arg)
{
char c='*';
if (p == 0) c='.';
if (p == 1) c='+';
if (p == 2) c='*';
if (p == 3) c='\n';
BIO_write((BIO *)arg,&c,1);
BIO_flush((BIO *)arg);
#ifdef LINT
p=n;
#endif
}
#endif
#
# SSLeay/crypto/dh/Makefile
#
#*************************************************************#
INC = ../../include/
LIB = ../../lib
CC = gcc
#*************************************************************#
.SUFFIXES: .ml .mli .cmo .cmi .cmx
.mli.cmi:
ocamlc -c $< -o $@
.ml.cmo:
ocamlc -c $< -o $@
.ml.cmx:
ocamlopt -c $< -o $@
#*************************************************************#
.SUFFIXES: .o .c
.c.o.:
gcc -g -o $@ -c -I$(INC) $<
all : check_this #dh
check_this.o : check_this.c
gcc -g -o check_this.o -c -I$(INC) check_this.c
check_this : check_this.o
gcc -g -o check_this check_this.o -L$(LIB) -lcrypto
dhtest.o : dhtest.c
gcc -g -o dhtest.o -c -I$(INC) dhtest.c
dhtest : dhtest.o
gcc -g -o dhtest dhtest.o -L$(LIB) -lcrypto
t : t.o $(LIBOBJ)
gcc -o t t.o -L$(LIB) -lcrypto
times.o : times.c
gcc -O2 -o times.o -c -I$(INC) times.c
times : times.o
gcc -O2 -o times times.o -L$(LIB) -lcrypto
dh_c.o : dh_c.c
gcc -O2 -o dh_c.o -c -I$(INC) -I$(CAMLLIB) dh_c.c
#dh : dh.cmi dh.cmx dh_c.o mltest.cmx
# ocamlopt -o dh dh_c.o -cclib -lunix unix.cmxa -cclib ../../lib/libcrypto.a
dh.cmx mltest.cmx
dh : dh.cmi dh.cmo dh_c.o mltest.cmo
ocamlc -custom -o dh dh_c.o -cclib -lunix unix.cma -cclib
../../lib/libcrypto.a dh.cmo mltest.cmo
clean :
rm -f *.o *.cm* dh times dhtest