summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/util/HttpInput.java
blob: ea46c86ac37925508c2a71e4d79530392bae2cca (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.util;

import java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import netscape.ldap.LDAPDN;

public class HttpInput {
    public static int getPortNumberInInt(HttpServletRequest request, String name)
            throws IOException {
        String val = request.getParameter(name);
        int p = Integer.parseInt(val);
        return p;
    }

    public static String getBoolean(HttpServletRequest request, String name)
            throws IOException {
        String val = request.getParameter(name);
        if (val.equals("true") || val.equals("false")) {
            return val;
        }
        throw new IOException("Invalid boolean value '" + val + "'");
    }

    public static String getCheckbox(HttpServletRequest request, String name)
            throws IOException {
        String val = request.getParameter(name);
        if (val == null || val.equals("")) {
            return "off";
        } else if (val.equals("on") || val.equals("off")) {
            return val;
        }
        throw new IOException("Invalid checkbox value '" + val + "'");
    }

    public static String getInteger(HttpServletRequest request, String name)
            throws IOException {
        String val = request.getParameter(name);
        int p = 0;
        try {
            p = Integer.parseInt(val);
        } catch (NumberFormatException e) {
            throw new IOException("Input '" + val + "' is not an integer");
        }

        if (!val.equals(Integer.toString(p))) {
            throw new IOException("Input '" + val + "' is not an integer");
        }
        return val;
    }

    public static String getInteger(HttpServletRequest request, String name,
            int min, int max) throws IOException {
        String val = getInteger(request, name);
        int p = Integer.parseInt(val);
        if (p < min || p > max) {
            throw new IOException("Input '" + val + "' is out of range");
        }
        return val;
    }

    public static String getPortNumber(HttpServletRequest request, String name)
            throws IOException {
        String v = getInteger(request, name);
        return v;
    }

    public static String getString(HttpServletRequest request, String name)
            throws IOException {
        String val = request.getParameter(name);
        return val;
    }

    public static String getString(HttpServletRequest request, String name,
            int minlen, int maxlen) throws IOException {
        String val = request.getParameter(name);
        if (val.length() < minlen || val.length() > maxlen) {
            throw new IOException("String length of '" + val +
                    "' is out of range");
        }
        return val;
    }

    public static String getLdapDatabase(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getURL(HttpServletRequest request, String name)
            throws IOException {
        String v = getString(request, name);
        try {
            URL u = new URL(v);
        } catch (Exception e) {
            throw new IOException("Invalid URL " + v);
        }
        return v;
    }

    public static String getUID(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getPassword(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getKeyType(HttpServletRequest request, String name)
            throws IOException {
        String v = getString(request, name);
        if (v.equals("rsa")) {
            return v;
        }
        if (v.equals("ecc")) {
            return v;
        }
        throw new IOException("Invalid key type '" + v + "' not supported.");
    }

    public static String getKeySize(HttpServletRequest request, String name)
            throws IOException {
        String i = getInteger(request, name);
        if (i.equals("256") || i.equals("512") || i.equals("1024") ||
                i.equals("2048") || i.equals("4096")) {
            return i;
        }
        throw new IOException("Invalid key length '"
                + i + "'. Currently supported key lengths are 256, 512, 1024, 2048, 4096.");
    }

    public static String getKeySize(HttpServletRequest request, String name, String keyType)
            throws IOException {
        String i = getInteger(request, name);
        if (keyType.equals("rsa")) {
            if (i.equals("256") || i.equals("512") || i.equals("1024") ||
                    i.equals("2048") || i.equals("4096")) {
                return i;
            } else {
                throw new IOException("Invalid key length '"
                        + i + "'. Currently supported RSA key lengths are 256, 512, 1024, 2048, 4096.");
            }
        }
        if (keyType.equals("ecc")) {
            int p = 0;
            try {
                p = Integer.parseInt(i);
            } catch (NumberFormatException e) {
                throw new IOException("Input '" + i + "' is not an integer");
            }
            if ((p >= 112) && (p <= 571))
                return i;
            else {
                throw new IOException(
                        "Invalid key length '"
                                + i
                                + "'. Please consult your security officer for a proper length, or take the default value. Here are examples of some commonly used key lengths: 256, 384, 521.");
            }
            /*

                      if (i.equals("256") || i.equals("384") || i.equals("521")) { 
                        return i;
                      } else {
                        throw new IOException("Invalid key length '" + i + "'. Currently supported ECC key lengths are 256, 384, 521.");
                      }
            */
        }
        throw new IOException("Invalid key type '" + keyType + "'");
    }

    public static String getDN(HttpServletRequest request, String name)
            throws IOException {
        String v = getString(request, name);
        String dn[] = LDAPDN.explodeDN(v, true);
        if (dn == null || dn.length <= 0) {
            throw new IOException("Invalid DN " + v + " in " + name);
        }
        return v;
    }

    public static String getID(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getName(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getCertRequest(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getCertChain(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getCert(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getNickname(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getHostname(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getTokenName(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getReplicationAgreementName(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getEmail(HttpServletRequest request, String name)
            throws IOException {
        String v = getString(request, name);
        if (v.indexOf('@') == -1) {
            throw new IOException("Invalid email " + v);
        }
        return v;
    }

    public static String getDomainName(HttpServletRequest request, String name)
            throws IOException {
        return getString(request, name);
    }

    public static String getSecurityDomainName(HttpServletRequest request, String name)
            throws IOException {
        String v = getName(request, name);
        Pattern p = Pattern.compile("[A-Za-z0-9]+[A-Za-z0-9 -]*");
        Matcher m = p.matcher(v);
        if (!m.matches()) {
            throw new IOException("Invalid characters found in Security Domain Name "
                    + v + ". Valid characters are A-Z, a-z, 0-9, dash and space");
        }
        return v;
    }
}