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 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 ./tlsuc3 -cliCertFile=/ad3/gma/sslwork/2013/client.crt -cliKeyFile=/ad3/gma/sslwork/2013/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()
86 // Finish TLS Config initialization, so broker can authenticate client.
87 // cc -> tls.Certificate
88 cc
, e
:= tls
.LoadX509KeyPair(cliCertFile
, cliKeyFile
)
90 ll
.Fatalf("%stag:%s connsess:%s main_load_pair error:%v",
91 exampid
, tag
, sngecomm
.Lcs
,
92 e
.Error()) // Handle this ......
95 tc
.Certificates
= append(tc
.Certificates
, cc
)
96 // This is OK, but does not seem to be required
97 tc
.BuildNameToCertificate() // Build names map
99 // Standard example TLS connect sequence
100 n
, conn
, e
:= sngecomm
.CommonTLSConnect(exampid
, tag
, ll
, tc
)
102 ll
.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
103 exampid
, tag
, sngecomm
.Lcs
,
104 e
.Error()) // Handle this ......
108 sngecomm
.DumpTLSConfig(exampid
, tc
, nc
)
110 // *NOTE* application specific functionaltiy starts here!
112 // *NOTE* application specific functionaltiy ends here!
114 // Standard example disconnect sequence
115 e
= sngecomm
.CommonDisconnect(n
, conn
, exampid
, tag
, ll
)
117 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
120 ll
.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
121 exampid
, tag
, conn
.Session(),