github.com/IBM-Bluemix/golang-openssl-wrapper@v0.0.0-20160104220506-7f2d5273b515/ssl/ssl_test.go (about)

     1  package ssl_test
     2  
     3  import (
     4  	. "github.com/IBM-Bluemix/golang-openssl-wrapper/ssl"
     5  
     6  	"github.com/IBM-Bluemix/golang-openssl-wrapper/bio"
     7  	"github.com/IBM-Bluemix/golang-openssl-wrapper/crypto"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("ssl", func() {
    13  	Context("Using TLS for connections", func() {
    14  
    15  		/*
    16  		 * Do some basic initialization
    17  		 */
    18  		BeforeEach(func() {
    19  			SSL_load_error_strings()
    20  			Expect(SSL_library_init()).To(Equal(1))
    21  			crypto.OPENSSL_config("")
    22  		})
    23  
    24  		// AfterEach(func() {
    25  		// SSL_free(ssl)
    26  		// SSL_CTX_free(ctx)
    27  		// })
    28  
    29  		Context("Making a client connection", func() {
    30  			var ctx SSL_CTX
    31  			var sslInst SSL
    32  			var conn bio.BIO
    33  			var host, hostport string
    34  
    35  			BeforeEach(func() {
    36  				ctx = SSL_CTX_new(SSLv23_method())
    37  				Expect(ctx).NotTo(BeNil())
    38  				SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nil)
    39  				SSL_CTX_set_verify_depth(ctx, 4)
    40  				Expect(SSL_CTX_load_verify_locations(ctx, "", "/etc/ssl/certs")).To(Equal(1))
    41  				sslInst = SSL_new(ctx)
    42  				Expect(sslInst).NotTo(BeNil())
    43  			})
    44  
    45  			AfterEach(func() {
    46  				bio.BIO_free_all(conn)
    47  				SSL_free(sslInst)
    48  				SSL_CTX_free(ctx)
    49  			})
    50  
    51  			It("Connects to a known site", func() {
    52  				host = "www.random.org"
    53  				hostport = "www.random.org:443"
    54  
    55  				/* Setup the connect BIO, since we're a client */
    56  				conn = bio.BIO_new_ssl_connect(ctx)
    57  				Expect(conn).NotTo(BeNil())
    58  				Expect(bio.BIO_set_conn_hostname(conn, hostport)).To(BeEquivalentTo(1))
    59  				Expect(bio.BIO_get_conn_hostname(conn)).To(Equal(hostport))
    60  
    61  				/* Setup SSL */
    62  				Expect(bio.BIO_get_ssl(conn, sslInst)).To(BeEquivalentTo(1))
    63  				ciphers := "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4"
    64  				Expect(SSL_set_cipher_list(sslInst, ciphers)).To(Equal(1))
    65  				Expect(SSL_set_tlsext_host_name(sslInst, host)).To(BeEquivalentTo(1))
    66  				/* Make the connection */
    67  				Expect(bio.BIO_do_connect(conn)).To(BeEquivalentTo(1))
    68  			})
    69  
    70  			// Expect(crypto.BIO_do_handshake(conn.(crypto.BIO))).To(BeEquivalentTo(1))
    71  			/*flags := SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION
    72  			  SSL_CTX_set_options(ctx, flags)
    73  			  Expect(host).To(Equal(1))
    74  			  //port := BIO_set_conn_port(web, 443)
    75  			  BIO_get_ssl(web, &ssl)
    76  			  const PREFERRED_CIPHERS = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MDS:!RC4"
    77  			  cipher := SSL_set_cipher_list(ssl, PREFERRED_CIPHERS)
    78  			  Expect(cipher).To(Equal(1)) */
    79  		})
    80  	})
    81  	/* Cannot fail ??? */
    82  
    83  	/* Cannot fail ??? */
    84  
    85  	/* Cannot fail ??? */
    86  	//const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
    87  	//SSL_CTX_set_options(ctx, flags);
    88  })