2 // Copyright © 2013-2018 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 3.
20 TLS Use Case 3 - broker *does* authenticate client, client does *not* authenticate broker
22 Subcase 3.A - Message broker configuration does *not* require client authentication
24 - Expect connection success
26 Subcase 3.B - Message broker configuration *does* require client authentication
28 - Expect connection success if the broker can authenticate the client certificate
33 STOMP_PORT=61611 ./tlsuc3 -cliCertFile=/ad3/gma/sslwork/2016-02/client.crt -cliKeyFile=/ad3/gma/sslwork/2016-02/client.key
44 // senv methods could be used in general by stompngo clients.
45 "github.com/gmallard/stompngo/senv"
46 // sngecomm methods are used specifically for these example clients.
47 "github.com/gmallard/stompngo_examples/sngecomm"
55 ll
= log
.New(os
.Stdout
, "TLSU3 ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
61 flag
.StringVar(&cliCertFile
, "cliCertFile", "DUMMY_CERT", "Name of client cert file")
62 flag
.StringVar(&cliKeyFile
, "cliKeyFile", "DUMMY_KEY", "Name of client key file")
65 // Connect to a STOMP broker using TLS and disconnect.
70 ll
.Printf("%stag:%s connsess:%s starts\n",
71 exampid
, tag
, sngecomm
.Lcs
)
73 flag
.Parse() // Parse flags
74 ll
.Printf("%stag:%s connsess:%s main_using_cliCertFile:%s\n",
75 exampid
, tag
, sngecomm
.Lcs
,
77 ll
.Printf("%stag:%s connsess:%s main_using_cliKeyFile:%s\n",
78 exampid
, tag
, sngecomm
.Lcs
,
83 tc
.InsecureSkipVerify
= true // Do *not* check the broker's certificate
84 // Be polite, allow SNI (Server Virtual Hosting)
85 tc
.ServerName
= senv
.Host()
87 // Usually one will use the default cipher suites that go provides.
88 // However, if a custom cipher squite list is needed/required this
89 // is how it is accomplished.
90 if sngecomm
.UseCustomCiphers() { // Set custom cipher suite list
91 tc
.CipherSuites
= append(tc
.CipherSuites
, sngecomm
.CustomCiphers()...)
94 // Finish TLS Config initialization, so broker can authenticate client.
95 // cc -> tls.Certificate
96 cc
, e
:= tls
.LoadX509KeyPair(cliCertFile
, cliKeyFile
)
98 ll
.Fatalf("%stag:%s connsess:%s main_load_pair error:%v",
99 exampid
, tag
, sngecomm
.Lcs
,
100 e
.Error()) // Handle this ......
102 // Add cert to config
103 tc
.Certificates
= append(tc
.Certificates
, cc
)
104 // This is OK, but does not seem to be required
105 tc
.BuildNameToCertificate() // Build names map
107 // Standard example TLS connect sequence
108 n
, conn
, e
:= sngecomm
.CommonTLSConnect(exampid
, tag
, ll
, tc
)
110 ll
.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
111 exampid
, tag
, sngecomm
.Lcs
,
112 e
.Error()) // Handle this ......
116 sngecomm
.DumpTLSConfig(exampid
, tc
, nc
)
118 // *NOTE* application specific functionaltiy starts here!
120 // *NOTE* application specific functionaltiy ends here!
122 // Standard example disconnect sequence
123 e
= sngecomm
.CommonDisconnect(n
, conn
, exampid
, tag
, ll
)
125 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
128 ll
.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
129 exampid
, tag
, conn
.Session(),