OSDN Git Service

* config/i386/i386.md (*sinxf2): Rename to *sinxf2_i387.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / net / ssl / provider / Extensions.java
1 /* Extensions.java -- various static extension utilities.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3
4 This file is a part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version.  */
37
38
39 package gnu.javax.net.ssl.provider;
40
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.UnsupportedEncodingException;
44 import java.util.Collections;
45 import java.util.LinkedList;
46 import java.util.List;
47
48 import javax.security.auth.x500.X500Principal;
49
50 import gnu.java.security.x509.X500DistinguishedName;
51
52 final class Extensions
53 {
54
55   // Constants.
56   // -------------------------------------------------------------------------
57
58   private static final Integer _512  = new Integer(512),
59     _1024 = new Integer(1024), _2048 = new Integer(2048),
60     _4096 = new Integer(4096);
61
62   // Class methods only.
63   private Extensions() { }
64
65   // Class methods.
66   // -------------------------------------------------------------------------
67
68   static List getServerName(Extension ex)
69   {
70     LinkedList l = new LinkedList();
71     byte[] buf = ex.getValue();
72     int pos = 0;
73     try
74       {
75         while (pos < buf.length)
76           {
77             if (buf[pos++] != 0)
78               break;
79             int len = (buf[pos++] & 0xFF) << 8;
80             len |= buf[pos++] & 0xFF;
81             l.add(new String(buf, pos, len, "UTF-8"));
82             pos += len;
83           }
84       }
85     catch (Exception x)
86       {
87       }
88     return Collections.unmodifiableList(l);
89   }
90
91   static List getClientCertTypes(Extension ex) throws IOException
92   {
93     List l = new LinkedList();
94     ByteArrayInputStream in = new ByteArrayInputStream(ex.getValue());
95     final int len = in.read() & 0xFF;
96     for (int i = 0; i < len; i++)
97       {
98         l.add(CertificateType.read(in));
99       }
100     return Collections.unmodifiableList(l);
101   }
102
103   static CertificateType getServerCertType(Extension ex) throws IOException
104   {
105     return CertificateType.read(new ByteArrayInputStream(ex.getValue()));
106   }
107
108   static Integer getMaxFragmentLength(Extension ex)
109   {
110     switch (ex.getValue()[0] & 0xFF)
111       {
112       case 1: return _512;
113       case 2: return _1024;
114       case 3: return _2048;
115       case 4: return _4096;
116       }
117     throw new IllegalArgumentException();
118   }
119
120   static Object[] getTrustedCA(Extension ex)
121   {
122     byte[] buf = ex.getValue();
123     int type = buf[0] & 0xFF;
124     try
125       {
126         switch (type)
127           {
128           case 0:
129             return new Object[] { new Integer(type), null };
130           case 1:
131           case 3:
132             return new Object[] { new Integer(type),
133                                   Util.trim(buf, 1, 20) };
134           case 2:
135             return new Object[] { new Integer(type),
136                                   new X500Principal(Util.trim(buf, 1, 20)) };
137           }
138       }
139     catch (Exception x)
140       {
141       }
142     throw new IllegalArgumentException();
143   }
144
145   static String getSRPUsername(Extension ex)
146   {
147     int len = ex.getValue()[0] & 0xFF;
148     if (len > ex.getValue().length - 1)
149       throw new IllegalArgumentException();
150     try
151       {
152         return new String(ex.getValue(), 1, len, "UTF-8");
153       }
154     catch (UnsupportedEncodingException uee)
155       {
156         throw new Error(uee.toString());
157       }
158   }
159 }