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
49 "github.com/gmallard/stompngo/senv"
50 // sngecomm methods are used specifically for these example clients.
51 "github.com/gmallard/stompngo_examples/sngecomm"
57 srvCAFile
string // Name of file with broker's CA certificate, PEM format
59 ll
= log
.New(os
.Stdout
, "TLSU2 ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
65 flag
.StringVar(&srvCAFile
, "srvCAFile", "DUMMY", "Name of file with broker CA certificate")
68 // Connect to a STOMP broker using TLS and disconnect.
73 ll
.Printf("%stag:%s connsess:%s starts\n",
74 exampid
, tag
, sngecomm
.Lcs
)
76 flag
.Parse() // Parse flags
77 ll
.Printf("%stag:%s connsess:%s main_using_srvCAFile:%s\n",
78 exampid
, tag
, sngecomm
.Lcs
,
83 tc
.InsecureSkipVerify
= false // *Do* check the broker's certificate
84 // Be polite, allow SNI (Server Virtual Hosting)
85 tc
.ServerName
= senv
.Host()
86 // Finish TLS Config initialization, so client can authenticate broker.
87 b
, e
:= ioutil
.ReadFile(srvCAFile
) // Read broker's CA cert (PEM)
89 ll
.Fatalf("%stag:%s connsess:%s main_read_file error:%v",
90 exampid
, tag
, sngecomm
.Lcs
,
91 e
.Error()) // Handle this ......
93 k
, _
:= pem
.Decode(b
) // Decode PEM format (*pem.Block)
95 ll
.Fatalf("%stag:%s connsess:%s main_decode error:%v",
96 exampid
, tag
, sngecomm
.Lcs
,
97 e
.Error()) // Handle this ......
99 c
, e
:= x509
.ParseCertificate(k
.Bytes
) // Create *x509.Certificate
101 ll
.Fatalf("%stag:%s connsess:%s main_parse_cert error:%v",
102 exampid
, tag
, sngecomm
.Lcs
,
103 e
.Error()) // Handle this ......
106 tc
.RootCAs
= x509
.NewCertPool() // Create a cert "pool"
107 tc
.RootCAs
.AddCert(c
) // Add the CA cert to the pool
109 // Standard example TLS connect sequence
110 n
, conn
, e
:= sngecomm
.CommonTLSConnect(exampid
, tag
, ll
, tc
)
112 ll
.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
113 exampid
, tag
, sngecomm
.Lcs
,
114 e
.Error()) // Handle this ......
118 sngecomm
.DumpTLSConfig(exampid
, tc
, nc
)
120 // *NOTE* application specific functionaltiy starts here!
122 // *NOTE* application specific functionaltiy ends here!
124 // Standard example disconnect sequence
125 e
= sngecomm
.CommonDisconnect(n
, conn
, exampid
, tag
, ll
)
127 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
130 ll
.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
131 exampid
, tag
, conn
.Session(),