1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"encoding/xml"
"fmt"
"io"
"net"
"net/url"
"os"
"strings"
)
type xmppStreamsFeatures struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
}
type xmppTlsProceed struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"`
}
func xmppStartTLS(connRaw net.Conn, host string) error {
decoder := xml.NewDecoder(connRaw)
// send <stream> start
_, err := fmt.Fprintf(connRaw, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='%s' version='1.0'>", host)
if err != nil {
return err
}
// read <stream> start
for {
t, err := decoder.Token()
if err != nil || t == nil {
return err
}
if se, ok := t.(xml.StartElement); ok {
if se.Name.Local != "stream" {
return xml.UnmarshalError(fmt.Sprintf("expected element of type <%s> but have <%s>", "stream", se.Name.Local))
}
break
}
}
// read <features>
var features xmppStreamsFeatures
err = decoder.DecodeElement(&features, nil)
if err != nil {
return err
}
// send <starttls>
_, err = io.WriteString(connRaw, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
if err != nil {
return err
}
// read <proceed>
var proceed xmppTlsProceed
err = decoder.DecodeElement(&proceed, nil)
if err != nil {
return err
}
return nil
}
func getcert(socket string) (*x509.Certificate, error) {
u, err := url.Parse(socket)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return nil, err
}
connRaw, err := net.Dial(u.Scheme, u.Host)
if err != nil {
return nil, err
}
switch u.Path {
case "", "/":
// do nothing
case "/xmpp":
err = xmppStartTLS(connRaw, host)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Unknown negotiation path: %q", u.Path)
}
connTLS := tls.Client(connRaw, &tls.Config{InsecureSkipVerify: true})
defer connTLS.Close()
err = connTLS.Handshake()
if err != nil {
return nil, err
}
cstate := connTLS.ConnectionState()
opts := x509.VerifyOptions{
DNSName: host,
Intermediates: x509.NewCertPool(),
}
for _, cert := range cstate.PeerCertificates[1:] {
opts.Intermediates.AddCert(cert)
}
cert := cstate.PeerCertificates[0]
_, err = cert.Verify(opts)
return cert, err
}
func split(socket string) (net, addr string) {
ary := strings.SplitN(socket, ":", 2)
if len(ary) == 1 {
return "tcp", ary[0]
}
return ary[0], ary[1]
}
func main() {
for _, socket := range os.Args[1:] {
block := pem.Block{
Type: "CERTIFICATE",
Headers: map[string]string{"X-Socket": socket},
Bytes: nil,
}
cert, err := getcert(socket)
if cert != nil {
block.Bytes = cert.Raw
}
if err != nil {
block.Headers["X-Error"] = err.Error()
}
pem.Encode(os.Stdout, &block)
}
}
|