Initial commit.
[BoarSSL] / SSLTLS / SSLQuirks.cs
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 using System;
26 using System.Collections.Generic;
27 using System.IO;
28
29 namespace SSLTLS {
30
31 /*
32 * An SSLQuirks instance is a set of named parameters that can be
33 * applied to an SSL engine (client or server) and alter its behaviour.
34 * Some of these variants are rarely used options permitted by the
35 * standard; others are downright invalid, and are meant to verify that
36 * a peer implementation is properly reacting to malformed messages
37 * and other anomalous conditions.
38 *
39 * Parameter names and values are strings. Parameter names are
40 * case-sensitive. As a matter of convention:
41 *
42 * - Boolean values are set as "true" or "false" string values.
43 *
44 * - Integers are encoded in decimal or hexadecimal; hexadecimal
45 * values have a leading "0x" header (or "-0x" for a negative
46 * hexadecimal value).
47 */
48
49 public class SSLQuirks {
50
51 /*
52 * When reading a parameter value, and the parameter is not
53 * defined, null is returned.
54 */
55 public string this[string name] {
56 get {
57 string v;
58 if (d.TryGetValue(name, out v)) {
59 return v;
60 } else {
61 return null;
62 }
63 }
64 set {
65 d[name] = value;
66 }
67 }
68
69 IDictionary<string, string> d;
70
71 public SSLQuirks()
72 {
73 d = new SortedDictionary<string, string>(
74 StringComparer.Ordinal);
75 }
76
77 /*
78 * Get a boolean quirk. If defined, then the boolean value is
79 * written in 'val' and true is returned; otherwise, 'val' is
80 * set to false, and false is returned.
81 */
82 public bool TryGetBoolean(string name, out bool val)
83 {
84 string s;
85 if (!d.TryGetValue(name, out s)) {
86 val = false;
87 return false;
88 }
89 switch (s) {
90 case "true": val = true; return true;
91 case "false": val = false; return true;
92 }
93 s = s.ToLowerInvariant();
94 switch (s) {
95 case "true": val = true; return true;
96 case "false": val = false; return true;
97 }
98 throw new Exception("Quirk value is not a boolean");
99 }
100
101 /*
102 * Get a boolean quirk. If undefined, the provided default value
103 * is returned.
104 */
105 public bool GetBoolean(string name, bool defaultValue)
106 {
107 bool val;
108 if (TryGetBoolean(name, out val)) {
109 return val;
110 } else {
111 return defaultValue;
112 }
113 }
114
115 /*
116 * Get an integer quirk. If defined, then the integer value is
117 * written in 'val' and true is returned; otherwise, 'val' is
118 * set to 0, and false is returned.
119 */
120 public bool TryGetInteger(string name, out int val)
121 {
122 string s;
123 if (!d.TryGetValue(name, out s)) {
124 val = 0;
125 return false;
126 }
127 bool neg = false;
128 if (s.StartsWith("-")) {
129 neg = true;
130 s = s.Substring(1);
131 }
132 int radix;
133 if (s.StartsWith("0x")) {
134 radix = 16;
135 s = s.Substring(2);
136 } else {
137 radix = 10;
138 }
139 int acc = 0;
140 if (s.Length == 0) {
141 throw new Exception("Quirk value is not an integer");
142 }
143 foreach (char c in s) {
144 int x;
145 if (c >= '0' && c <= '9') {
146 x = c - '0';
147 } else if (c >= 'A' && c <= 'F') {
148 x = c - ('A' - 10);
149 } else if (c >= 'a' && c <= 'f') {
150 x = c - ('a' - 10);
151 } else {
152 throw new Exception(
153 "Quirk value is not an integer");
154 }
155 if (x >= radix) {
156 throw new Exception(
157 "Quirk value is not an integer");
158 }
159 acc = (acc * radix) + x;
160 }
161 if (neg) {
162 acc = -acc;
163 }
164 val = acc;
165 return true;
166 }
167
168 /*
169 * Get an integer quirk. If undefined, the provided default value
170 * is returned.
171 */
172 public int GetInteger(string name, int defaultValue)
173 {
174 int val;
175 if (TryGetInteger(name, out val)) {
176 return val;
177 } else {
178 return defaultValue;
179 }
180 }
181
182 /*
183 * Get a string quirk. If undefined, the provided default value
184 * is returned.
185 */
186 public string GetString(string name, string defaultValue)
187 {
188 string s;
189 if (d.TryGetValue(name, out s)) {
190 return s;
191 } else {
192 return defaultValue;
193 }
194 }
195 }
196
197 }