2 // Copyright © 2013-2016 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Connect and Disconnect from a STOMP broker with a TLS connection, use case 2.
20 TLS Use Case 2 - client *does* authenticate broker.
22 Subcase 2.A - Message broker configuration does *not* require client authentication
24 - Expect connection success because the client did authenticate the
27 Subcase 2.B - Message broker configuration *does* require client authentication
29 - Expect connection failure (broker must be sent a valid client certificate)
34 ./tlsuc2 -srvCAFile=/ad3/gma/sslwork/2013/TestCA.crt # PEM format file
48 "github.com/gmallard/stompngo"
49 "github.com/gmallard/stompngo_examples/sngecomm"
54 testConfig
*tls
.Config
55 srvCAFile
string // Name of file with broker's CA certificate, PEM format
59 flag
.StringVar(&srvCAFile
, "srvCAFile", "DUMMY", "Name of file with broker CA certificate")
62 // Connect to a STOMP broker using TLS and disconnect.
64 log
.Println(exampid
, "starts ...")
66 flag
.Parse() // Parse flags
68 log
.Println(exampid
, "using srvCAFile", srvCAFile
)
71 testConfig
= new(tls
.Config
)
72 testConfig
.InsecureSkipVerify
= false // *Do* check the broker's certificate
75 h
, p
:= sngecomm
.HostAndPort()
76 log
.Println(exampid
, "host", h
, "port", p
)
78 // Be polite, allow SNI (Server Virtual Hosting)
79 testConfig
.ServerName
= h
81 // Finish TLS Config initialization, so client can authenticate broker.
82 b
, e
:= ioutil
.ReadFile(srvCAFile
) // Read broker's CA cert (PEM)
86 k
, _
:= pem
.Decode(b
) // Decode PEM format
91 c
, e
:= x509
.ParseCertificate(k
.Bytes
) // Create *x509.Certificate
95 testConfig
.RootCAs
= x509
.NewCertPool() // Create a cert "pool"
96 testConfig
.RootCAs
.AddCert(c
) // Add the CA cert to the pool
98 // Connect logic: use net.Dial and tls.Client
99 t
, e
:= net
.Dial("tcp", net
.JoinHostPort(h
, p
))
101 log
.Fatalln(e
) // Handle this ......
103 log
.Println(exampid
, "dial complete ...")
104 n
:= tls
.Client(t
, testConfig
)
107 log
.Fatalln(e
) // Handle this ......
109 log
.Println(exampid
, "handshake complete ...")
111 sngecomm
.DumpTLSConfig(exampid
, testConfig
, n
)
114 ch
:= sngecomm
.ConnectHeaders()
116 // Get a stomp connection. Parameters are:
117 // a) the opened net connection
118 // b) the connect Headers
119 conn
, e
:= stompngo
.Connect(n
, ch
)
121 log
.Fatalln(e
) // Handle this ......
123 log
.Println(exampid
, "stomp connect complete ...")
125 // *NOTE* your application functionaltiy goes here!
127 // Polite Stomp disconnects are not required, but highly recommended.
128 // Empty headers here.
129 e
= conn
.Disconnect(stompngo
.Headers
{})
131 log
.Fatalln(e
) // Handle this ......
133 log
.Println(exampid
, "stomp disconnect complete ...")
135 // Close the net connection.
138 log
.Fatalln(e
) // Handle this ......
140 log
.Println(exampid
, "network close complete ...")
142 log
.Println(exampid
, "ends ...")