summaryrefslogtreecommitdiff
path: root/cpp/src/IceSSL/GeneralConfig.cpp
blob: 84ef421254e7a021ddf47efd06625f26588086e4 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <IceSSL/GeneralConfig.h>

#include <string>
#include <algorithm>

using namespace std;

IceSSL::GeneralConfig::GeneralConfig()
{
    _sslVersion = SSL_V23;

    _verifyMode = SSL_VERIFY_NONE;

    // Unlimited verifcation depth.
    _verifyDepth = -1;

    _context = "";
    _cipherList = "";
    _randomBytesFiles = "";
}

IceSSL::SslProtocol
IceSSL::GeneralConfig::getProtocol() const
{
    return _sslVersion;
}

int
IceSSL::GeneralConfig::getVerifyMode() const
{
    return _verifyMode;
}

int
IceSSL::GeneralConfig::getVerifyDepth() const
{
    return _verifyDepth;
}

std::string
IceSSL::GeneralConfig::getContext() const
{
    return _context;
}

std::string
IceSSL::GeneralConfig::getCipherList() const
{
    return _cipherList;
}

std::string
IceSSL::GeneralConfig::getRandomBytesFiles() const
{
    return _randomBytesFiles;
}

void
IceSSL::GeneralConfig::set(const string& name, const string& value)
{
    if(name.compare("version") == 0)
    {
        parseVersion(value);
    }
    else if(name.compare("cipherlist") == 0)
    {
        _cipherList = value;
    }
    else if(name.compare("context") == 0)
    {
        _context = value;
    }
    else if(name.compare("verifymode") == 0)
    {
        parseVerifyMode(value);
    }
    else if(name.compare("verifydepth") == 0)
    {
        _verifyDepth = atoi(value.c_str());
    }
    else if(name.compare("randombytes") == 0)
    {
        _randomBytesFiles = value;
    }
    return;
}

//
// Protected Methods
//

void
IceSSL::GeneralConfig::parseVersion(const string& value)
{
    if(value.compare("SSLv23") == 0)
    {
        _sslVersion = SSL_V23;
    }
    else if(value.compare("SSLv3") == 0)
    {
        _sslVersion = SSL_V3;
    }
    else if(value.compare("TLSv1") == 0)
    {
        _sslVersion = TLS_V1;
    }

    return;
}

void
IceSSL::GeneralConfig::parseVerifyMode(const string& value)
{
    const string delim = " |\t\n\r";

    string s(value);
    transform(s.begin(), s.end(), s.begin(), ::tolower);

    string::size_type beg;
    string::size_type end = 0;

    while(true)
    {
	beg = s.find_first_not_of(delim, end);

        if(beg == string::npos)
	{
	    break;
	}
	
	end = s.find_first_of(delim, beg);

        if(end == string::npos)
	{
	    end = s.length();
	}

        string option = s.substr(beg, end - beg);

        if(option.compare("none") == 0)
        {
            _verifyMode |= SSL_VERIFY_NONE;
        }
        else if(option.compare("peer") == 0)
        {
            _verifyMode |= SSL_VERIFY_PEER;
        }
        else if(option.compare("fail_no_cert") == 0)
        {
            _verifyMode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
        }
        else if(option.compare("client_once") == 0)
        {
            _verifyMode |= SSL_VERIFY_CLIENT_ONCE;
        }
    }

    // Both SSL_VERIFY_FAIL_IF_NO_PEER_CERT and SSL_VERIFY_CLIENT_ONCE require
    // that SSL_VERIFY_PEER be set, otherwise it's an error.
    if((_verifyMode != SSL_VERIFY_NONE) && !(_verifyMode & SSL_VERIFY_PEER))
    {
        _verifyMode = SSL_VERIFY_NONE;
    }

    return;
}