mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-08 23:13:38 +08:00
first commit, SM2-DSA only
This commit is contained in:
16
demos/bio/Makefile
Normal file
16
demos/bio/Makefile
Normal file
@@ -0,0 +1,16 @@
|
||||
CC=cc
|
||||
CFLAGS= -g -I../../include
|
||||
LIBS= -L../.. ../../libssl.a ../../libcrypto.a
|
||||
EXAMPLES=saccept sconnect
|
||||
|
||||
all: $(EXAMPLES)
|
||||
|
||||
saccept: saccept.o
|
||||
$(CC) -o saccept saccept.o $(LIBS)
|
||||
|
||||
sconnect: sconnect.o
|
||||
$(CC) -o sconnect sconnect.o $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(EXAMPLES) *.o
|
||||
|
||||
3
demos/bio/README
Normal file
3
demos/bio/README
Normal file
@@ -0,0 +1,3 @@
|
||||
This directory contains some simple examples of the use of BIO's
|
||||
to simplify socket programming.
|
||||
|
||||
112
demos/bio/saccept.c
Normal file
112
demos/bio/saccept.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/saccept.c */
|
||||
|
||||
/* A minimal program to server an SSL connection.
|
||||
* It uses blocking.
|
||||
* saccept host:port
|
||||
* host is the interface IP to use. If any interface, use *:port
|
||||
* The default it *:4433
|
||||
*
|
||||
* cc -I../../include saccept.c -L../.. -lssl -lcrypto
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#define CERT_FILE "server.pem"
|
||||
|
||||
BIO *in=NULL;
|
||||
|
||||
void close_up()
|
||||
{
|
||||
if (in != NULL)
|
||||
BIO_free(in);
|
||||
}
|
||||
|
||||
int main(argc,argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
char *port=NULL;
|
||||
BIO *ssl_bio,*tmp;
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
char buf[512];
|
||||
int ret=1,i;
|
||||
|
||||
if (argc <= 1)
|
||||
port="*:4433";
|
||||
else
|
||||
port=argv[1];
|
||||
|
||||
signal(SIGINT,close_up);
|
||||
|
||||
SSL_load_error_strings();
|
||||
|
||||
#ifdef WATT32
|
||||
dbug_init();
|
||||
sock_init();
|
||||
#endif
|
||||
|
||||
/* Add ciphers and message digests */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
ctx=SSL_CTX_new(SSLv23_server_method());
|
||||
if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
|
||||
goto err;
|
||||
if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
|
||||
goto err;
|
||||
if (!SSL_CTX_check_private_key(ctx))
|
||||
goto err;
|
||||
|
||||
/* Setup server side SSL bio */
|
||||
ssl=SSL_new(ctx);
|
||||
ssl_bio=BIO_new_ssl(ctx,0);
|
||||
|
||||
if ((in=BIO_new_accept(port)) == NULL) goto err;
|
||||
|
||||
/* This means that when a new connection is acceptede on 'in',
|
||||
* The ssl_bio will be 'dupilcated' and have the new socket
|
||||
* BIO push into it. Basically it means the SSL BIO will be
|
||||
* automatically setup */
|
||||
BIO_set_accept_bios(in,ssl_bio);
|
||||
|
||||
again:
|
||||
/* The first call will setup the accept socket, and the second
|
||||
* will get a socket. In this loop, the first actual accept
|
||||
* will occur in the BIO_read() function. */
|
||||
|
||||
if (BIO_do_accept(in) <= 0) goto err;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
i=BIO_read(in,buf,512);
|
||||
if (i == 0)
|
||||
{
|
||||
/* If we have finished, remove the underlying
|
||||
* BIO stack so the next time we call any function
|
||||
* for this BIO, it will attempt to do an
|
||||
* accept */
|
||||
printf("Done\n");
|
||||
tmp=BIO_pop(in);
|
||||
BIO_free_all(tmp);
|
||||
goto again;
|
||||
}
|
||||
if (i < 0) goto err;
|
||||
fwrite(buf,1,i,stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ret=0;
|
||||
err:
|
||||
if (ret)
|
||||
{
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
if (in != NULL) BIO_free(in);
|
||||
exit(ret);
|
||||
return(!ret);
|
||||
}
|
||||
|
||||
121
demos/bio/sconnect.c
Normal file
121
demos/bio/sconnect.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/sconnect.c */
|
||||
|
||||
/* A minimal program to do SSL to a passed host and port.
|
||||
* It is actually using non-blocking IO but in a very simple manner
|
||||
* sconnect host:port - it does a 'GET / HTTP/1.0'
|
||||
*
|
||||
* cc -I../../include sconnect.c -L../.. -lssl -lcrypto
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
extern int errno;
|
||||
|
||||
int main(argc,argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
char *host;
|
||||
BIO *out;
|
||||
char buf[1024*10],*p;
|
||||
SSL_CTX *ssl_ctx=NULL;
|
||||
SSL *ssl;
|
||||
BIO *ssl_bio;
|
||||
int i,len,off,ret=1;
|
||||
|
||||
if (argc <= 1)
|
||||
host="localhost:4433";
|
||||
else
|
||||
host=argv[1];
|
||||
|
||||
#ifdef WATT32
|
||||
dbug_init();
|
||||
sock_init();
|
||||
#endif
|
||||
|
||||
/* Lets get nice error messages */
|
||||
SSL_load_error_strings();
|
||||
|
||||
/* Setup all the global SSL stuff */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
ssl_ctx=SSL_CTX_new(SSLv23_client_method());
|
||||
|
||||
/* Lets make a SSL structure */
|
||||
ssl=SSL_new(ssl_ctx);
|
||||
SSL_set_connect_state(ssl);
|
||||
|
||||
/* Use it inside an SSL BIO */
|
||||
ssl_bio=BIO_new(BIO_f_ssl());
|
||||
BIO_set_ssl(ssl_bio,ssl,BIO_CLOSE);
|
||||
|
||||
/* Lets use a connect BIO under the SSL BIO */
|
||||
out=BIO_new(BIO_s_connect());
|
||||
BIO_set_conn_hostname(out,host);
|
||||
BIO_set_nbio(out,1);
|
||||
out=BIO_push(ssl_bio,out);
|
||||
|
||||
p="GET / HTTP/1.0\r\n\r\n";
|
||||
len=strlen(p);
|
||||
|
||||
off=0;
|
||||
for (;;)
|
||||
{
|
||||
i=BIO_write(out,&(p[off]),len);
|
||||
if (i <= 0)
|
||||
{
|
||||
if (BIO_should_retry(out))
|
||||
{
|
||||
fprintf(stderr,"write DELAY\n");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
off+=i;
|
||||
len-=i;
|
||||
if (len <= 0) break;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
i=BIO_read(out,buf,sizeof(buf));
|
||||
if (i == 0) break;
|
||||
if (i < 0)
|
||||
{
|
||||
if (BIO_should_retry(out))
|
||||
{
|
||||
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(out);
|
||||
if (ssl_ctx != NULL) SSL_CTX_free(ssl_ctx);
|
||||
exit(!ret);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
30
demos/bio/server.pem
Normal file
30
demos/bio/server.pem
Normal file
@@ -0,0 +1,30 @@
|
||||
subject=/C=AU/SP=QLD/O=Mincom Pty. Ltd./OU=CS/CN=SSLeay demo server
|
||||
issuer= /C=AU/SP=QLD/O=Mincom Pty. Ltd./OU=CS/CN=CA
|
||||
-----BEGIN X509 CER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n63AiEA
|
||||
g9VNIQ3xwpw7og3IbONifeku+J9qGMGQJMKwSTwrFtI=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
|
||||
-----BEGIN DH PARAMETERS-----
|
||||
MEYCQQDaWDwW2YUiidDkr3VvTMqS3UvlM7gE+w/tlO+cikQD7VdGUNNpmdsp13Yn
|
||||
a6LT1BLiGPTdHghM9tgAPnxHdOgzAgEC
|
||||
-----END DH PARAMETERS-----
|
||||
|
||||
Reference in New Issue
Block a user