aboutsummaryrefslogtreecommitdiff
path: root/openssl/demos/eay
diff options
context:
space:
mode:
Diffstat (limited to 'openssl/demos/eay')
-rw-r--r--openssl/demos/eay/Makefile24
-rw-r--r--openssl/demos/eay/base64.c49
-rw-r--r--openssl/demos/eay/conn.c105
-rw-r--r--openssl/demos/eay/loadrsa.c53
4 files changed, 231 insertions, 0 deletions
diff --git a/openssl/demos/eay/Makefile b/openssl/demos/eay/Makefile
new file mode 100644
index 000000000..2d22eaca5
--- /dev/null
+++ b/openssl/demos/eay/Makefile
@@ -0,0 +1,24 @@
+CC=cc
+CFLAGS= -g -I../../include
+#LIBS= -L../.. -lcrypto -lssl
+LIBS= -L../.. ../../libssl.a ../../libcrypto.a
+
+# the file conn.c requires a file "proxy.h" which I couldn't find...
+#EXAMPLES=base64 conn loadrsa
+EXAMPLES=base64 loadrsa
+
+all: $(EXAMPLES)
+
+base64: base64.o
+ $(CC) -o base64 base64.o $(LIBS)
+#
+# sorry... can't find "proxy.h"
+#conn: conn.o
+# $(CC) -o conn conn.o $(LIBS)
+
+loadrsa: loadrsa.o
+ $(CC) -o loadrsa loadrsa.o $(LIBS)
+
+clean:
+ rm -f $(EXAMPLES) *.o
+
diff --git a/openssl/demos/eay/base64.c b/openssl/demos/eay/base64.c
new file mode 100644
index 000000000..4b8b0627d
--- /dev/null
+++ b/openssl/demos/eay/base64.c
@@ -0,0 +1,49 @@
+/* This is a simple example of using the base64 BIO to a memory BIO and then
+ * getting the data.
+ */
+#include <stdio.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+
+main()
+ {
+ int i;
+ BIO *mbio,*b64bio,*bio;
+ char buf[512];
+ char *p;
+
+ mbio=BIO_new(BIO_s_mem());
+ b64bio=BIO_new(BIO_f_base64());
+
+ bio=BIO_push(b64bio,mbio);
+ /* We now have bio pointing at b64->mem, the base64 bio encodes on
+ * write and decodes on read */
+
+ for (;;)
+ {
+ i=fread(buf,1,512,stdin);
+ if (i <= 0) break;
+ BIO_write(bio,buf,i);
+ }
+ /* We need to 'flush' things to push out the encoding of the
+ * last few bytes. There is special encoding if it is not a
+ * multiple of 3
+ */
+ BIO_flush(bio);
+
+ printf("We have %d bytes available\n",BIO_pending(mbio));
+
+ /* We will now get a pointer to the data and the number of elements. */
+ /* hmm... this one was not defined by a macro in bio.h, it will be for
+ * 0.9.1. The other option is too just read from the memory bio.
+ */
+ i=(int)BIO_ctrl(mbio,BIO_CTRL_INFO,0,(char *)&p);
+
+ printf("%d\n",i);
+ fwrite("---\n",1,4,stdout);
+ fwrite(p,1,i,stdout);
+ fwrite("---\n",1,4,stdout);
+
+ /* This call will walk the chain freeing all the BIOs */
+ BIO_free_all(bio);
+ }
diff --git a/openssl/demos/eay/conn.c b/openssl/demos/eay/conn.c
new file mode 100644
index 000000000..c4b8f5163
--- /dev/null
+++ b/openssl/demos/eay/conn.c
@@ -0,0 +1,105 @@
+/* NOCW */
+/* demos/eay/conn.c */
+
+/* A minimal program to connect to a port using the sock4a protocol.
+ *
+ * cc -I../../include conn.c -L../.. -lcrypto
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <openssl/err.h>
+#include <openssl/bio.h>
+/* #include "proxy.h" */
+
+extern int errno;
+
+int main(argc,argv)
+int argc;
+char *argv[];
+ {
+ PROXY *pxy;
+ char *host;
+ char buf[1024*10],*p;
+ BIO *bio;
+ int i,len,off,ret=1;
+
+ if (argc <= 1)
+ host="localhost:4433";
+ else
+ host=argv[1];
+
+ /* Lets get nice error messages */
+ ERR_load_crypto_strings();
+
+ /* First, configure proxy settings */
+ pxy=PROXY_new();
+ PROXY_add_server(pxy,PROXY_PROTOCOL_SOCKS,"gromit:1080");
+
+ bio=BIO_new(BIO_s_socks4a_connect());
+
+ BIO_set_conn_hostname(bio,host);
+ BIO_set_proxies(bio,pxy);
+ BIO_set_socks_userid(bio,"eay");
+ BIO_set_nbio(bio,1);
+
+ p="GET / HTTP/1.0\r\n\r\n";
+ len=strlen(p);
+
+ off=0;
+ for (;;)
+ {
+ i=BIO_write(bio,&(p[off]),len);
+ if (i <= 0)
+ {
+ if (BIO_should_retry(bio))
+ {
+ fprintf(stderr,"write DELAY\n");
+ sleep(1);
+ continue;
+ }
+ else
+ {
+ goto err;
+ }
+ }
+ off+=i;
+ len-=i;
+ if (len <= 0) break;
+ }
+
+ for (;;)
+ {
+ i=BIO_read(bio,buf,sizeof(buf));
+ if (i == 0) break;
+ if (i < 0)
+ {
+ if (BIO_should_retry(bio))
+ {
+ fprintf(stderr,"read DELAY\n");
+ sleep(1);
+ continue;
+ }
+ goto err;
+ }
+ fwrite(buf,1,i,stdout);
+ }
+
+ ret=1;
+
+ if (0)
+ {
+err:
+ if (ERR_peek_error() == 0) /* system call error */
+ {
+ fprintf(stderr,"errno=%d ",errno);
+ perror("error");
+ }
+ else
+ ERR_print_errors_fp(stderr);
+ }
+ BIO_free_all(bio);
+ if (pxy != NULL) PROXY_free(pxy);
+ exit(!ret);
+ return(ret);
+ }
+
diff --git a/openssl/demos/eay/loadrsa.c b/openssl/demos/eay/loadrsa.c
new file mode 100644
index 000000000..79f1885ca
--- /dev/null
+++ b/openssl/demos/eay/loadrsa.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <openssl/rsa.h>
+
+/* This is a simple program to generate an RSA private key. It then
+ * saves both the public and private key into a char array, then
+ * re-reads them. It saves them as DER encoded binary data.
+ */
+
+void callback(stage,count,arg)
+int stage,count;
+char *arg;
+ {
+ FILE *out;
+
+ out=(FILE *)arg;
+ fprintf(out,"%d",stage);
+ if (stage == 3)
+ fprintf(out,"\n");
+ fflush(out);
+ }
+
+main()
+ {
+ RSA *rsa,*pub_rsa,*priv_rsa;
+ int len;
+ unsigned char buf[1024],*p;
+
+ rsa=RSA_generate_key(512,RSA_F4,callback,(char *)stdout);
+
+ p=buf;
+
+ /* Save the public key into buffer, we know it will be big enough
+ * but we should really check how much space we need by calling the
+ * i2d functions with a NULL second parameter */
+ len=i2d_RSAPublicKey(rsa,&p);
+ len+=i2d_RSAPrivateKey(rsa,&p);
+
+ printf("The public and private key are now both in a char array\n");
+ printf("and are taking up %d bytes\n",len);
+
+ RSA_free(rsa);
+
+ p=buf;
+ pub_rsa=d2i_RSAPublicKey(NULL,&p,(long)len);
+ len-=(p-buf);
+ priv_rsa=d2i_RSAPrivateKey(NULL,&p,(long)len);
+
+ if ((pub_rsa == NULL) || (priv_rsa == NULL))
+ ERR_print_errors_fp(stderr);
+
+ RSA_free(pub_rsa);
+ RSA_free(priv_rsa);
+ }