Debian Codegraph @

c/src/core/autodetect.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "autodetect.h"
23
24 #define SASL_HEADER ("AMQP\x03\x01\x00\x00")
25 #define SSL_HEADER ("AMQP\x02\x01\x00\x00")
26 #define AMQP_HEADER ("AMQP\x00\x01\x00\x00")
27
28 #define SASL_HEADER_LEN 8
29
30 /*
31 * SSLv2 Client Hello format
32 * http://www.mozilla.org/projects/security/pki/nss/ssl/draft02.html
33 *
34 * Bytes 0-1: RECORD-LENGTH
35 * Byte 2: MSG-CLIENT-HELLO (1)
36 * Byte 3: CLIENT-VERSION-MSB
37 * Byte 4: CLIENT-VERSION-LSB
38 *
39 * Allowed versions:
40 * 2.0 - SSLv2
41 * 3.0 - SSLv3
42 * 3.1 - TLS 1.0
43 * 3.2 - TLS 1.1
44 * 3.3 - TLS 1.2
45 *
46 * The version sent in the Client-Hello is the latest version supported by
47 * the client. NSS may send version 3.x in an SSLv2 header for
48 * maximum compatibility.
49 */
50 /*
51 * SSLv3/TLS Client Hello format
52 * RFC 2246
53 *
54 * Byte 0: ContentType (handshake - 22)
55 * Bytes 1-2: ProtocolVersion {major, minor}
56 *
57 * Allowed versions:
58 * 3.0 - SSLv3
59 * 3.1 - TLS 1.0
60 * 3.2 - TLS 1.1
61 * 3.3 - TLS 1.2
62 */
63 /*
64 * AMQP 1.0 Header
65 *
66 * Bytes 0-3: "AMQP"
67 * Byte 4: 0==AMQP, 2==SSL, 3==SASL
68 * Byte 5: 1
69 * Bytes 6-7: 0
70 */
71 /*
72 * AMQP Pre 1.0 Header
73 *
74 * Bytes 0-3: 'AMQP'
75 * Byte 4: 1
76 * Byte 5: 1
77 * Byte 6: 0 (major version)
78 * Byte 7: Minor version
79 */
80 pni_protocol_type_t pni_sniff_header(const char *buf, size_t len)
81 {
82 if (len < 3) return PNI_PROTOCOL_INSUFFICIENT;
83 bool isSSL3Handshake = buf[0]==22 && // handshake
84 buf[1]==3 && buf[2]<=3; // SSL 3.0 & TLS 1.0-1.2 (v3.1-3.3)
85 if (isSSL3Handshake) return PNI_PROTOCOL_SSL;
86
87 bool isFirst3AMQP = buf[0]=='A' && buf[1]=='M' && buf[2]=='Q';
88 bool isFirst3SSL2CLientHello = buf[2]==1; // Client Hello
89 if (!isFirst3AMQP && !isFirst3SSL2CLientHello) return PNI_PROTOCOL_UNKNOWN;
90
91
92 if (len < 4) return PNI_PROTOCOL_INSUFFICIENT;
93 bool isAMQP = isFirst3AMQP && buf[3]=='P';
94 bool isFirst4SSL2ClientHello = isFirst3SSL2CLientHello && (buf[3]==2 || buf[3]==3);
95 if (!isAMQP && !isFirst4SSL2ClientHello) return PNI_PROTOCOL_UNKNOWN;
96
97 if (len < 5) return PNI_PROTOCOL_INSUFFICIENT;
98 bool isSSL2Handshake = buf[2] == 1 && // MSG-CLIENT-HELLO
99 ((buf[3] == 3 && buf[4] <= 3) || // SSL 3.0 & TLS 1.0-1.2 (v3.1-3.3)
100 (buf[3] == 2 && buf[4] == 0)); // SSL 2
101 if (isSSL2Handshake) return PNI_PROTOCOL_SSL;
102
103 bool isFirst5OldAMQP = isAMQP && buf[4]==1;
104 bool isFirst5AMQP = isAMQP && (buf[4]==0 || buf[4]==2 || buf[4]==3);
105 if (!isFirst5AMQP && !isFirst5OldAMQP) return PNI_PROTOCOL_UNKNOWN;
106
107 if (len < 6) return PNI_PROTOCOL_INSUFFICIENT;
108
109 // Both old and new versions of AMQP have 1 in byte 5
110 if (buf[5]!=1) return PNI_PROTOCOL_UNKNOWN;
111
112 // From here on it must be some sort of AMQP
113 if (len < 8) return PNI_PROTOCOL_INSUFFICIENT;
114 if (buf[6]==0 && buf[7]==0) {
115 // AM<QP 1.0
116 if (buf[4]==0) return PNI_PROTOCOL_AMQP1;
117 if (buf[4]==2) return PNI_PROTOCOL_AMQP_SSL;
118 if (buf[4]==3) return PNI_PROTOCOL_AMQP_SASL;
119 }
120 return PNI_PROTOCOL_AMQP_OTHER;
121 }
122
123 const char* pni_protocol_name(pni_protocol_type_t p)
124 {
125 static const char * const names[] = {
126 "Insufficient data to determine protocol",
127 "Unknown protocol",
128 "SSL/TLS connection",
129 "AMQP TLS layer",
130 "AMQP SASL layer",
131 "AMQP 1.0 layer",
132 "Pre standard AMQP connection"
133 };
134 return names[p];
135 }