2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
26 using System.Collections.Generic;
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.
39 * Parameter names and values are strings. Parameter names are
40 * case-sensitive. As a matter of convention:
42 * - Boolean values are set as "true" or "false" string values.
44 * - Integers are encoded in decimal or hexadecimal; hexadecimal
45 * values have a leading "0x" header (or "-0x" for a negative
49 public class SSLQuirks {
52 * When reading a parameter value, and the parameter is not
53 * defined, null is returned.
55 public string this[string name] {
58 if (d.TryGetValue(name, out v)) {
69 IDictionary<string, string> d;
73 d = new SortedDictionary<string, string>(
74 StringComparer.Ordinal);
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.
82 public bool TryGetBoolean(string name, out bool val)
85 if (!d.TryGetValue(name, out s)) {
90 case "true": val = true; return true;
91 case "false": val = false; return true;
93 s = s.ToLowerInvariant();
95 case "true": val = true; return true;
96 case "false": val = false; return true;
98 throw new Exception("Quirk value is not a boolean");
102 * Get a boolean quirk. If undefined, the provided default value
105 public bool GetBoolean(string name, bool defaultValue)
108 if (TryGetBoolean(name, out val)) {
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.
120 public bool TryGetInteger(string name, out int val)
123 if (!d.TryGetValue(name, out s)) {
128 if (s.StartsWith("-")) {
133 if (s.StartsWith("0x")) {
141 throw new Exception("Quirk value is not an integer");
143 foreach (char c in s) {
145 if (c >= '0' && c <= '9') {
147 } else if (c >= 'A' && c <= 'F') {
149 } else if (c >= 'a' && c <= 'f') {
153 "Quirk value is not an integer");
157 "Quirk value is not an integer");
159 acc = (acc * radix) + x;
169 * Get an integer quirk. If undefined, the provided default value
172 public int GetInteger(string name, int defaultValue)
175 if (TryGetInteger(name, out val)) {
183 * Get a string quirk. If undefined, the provided default value
186 public string GetString(string name, string defaultValue)
189 if (d.TryGetValue(name, out s)) {