Karl Pauls | 3640732 | 2008-03-07 00:37:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.framework.security.util; |
| 20 | |
| 21 | import java.io.InputStream; |
| 22 | import java.security.KeyStore; |
| 23 | import java.security.cert.CertificateFactory; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
| 26 | import java.util.Enumeration; |
| 27 | import java.util.StringTokenizer; |
| 28 | |
| 29 | import org.apache.felix.framework.util.SecureAction; |
| 30 | |
| 31 | /* |
| 32 | * TODO: the certificate stores as well as the CRLs might change over time |
| 33 | * (added/removed certificates). We need a way to detect that and act on it. |
| 34 | * The problem is to find a good balance between re-checking and caching... |
| 35 | */ |
| 36 | public final class TrustManager |
| 37 | { |
| 38 | private final SecureAction m_action; |
| 39 | private final String m_crlList; |
| 40 | private final String m_typeList; |
| 41 | private final String m_passwdList; |
| 42 | private final String m_storeList; |
| 43 | private Collection m_caCerts = null; |
| 44 | private Collection m_crls = null; |
| 45 | |
| 46 | public TrustManager(String crlList, String typeList, String passwdList, |
| 47 | String storeList, SecureAction action) |
| 48 | { |
| 49 | m_crlList = crlList; |
| 50 | m_typeList = typeList; |
| 51 | m_passwdList = passwdList; |
| 52 | m_storeList = storeList; |
| 53 | m_action = action; |
| 54 | } |
| 55 | |
| 56 | private synchronized void init() |
| 57 | { |
| 58 | if (m_caCerts == null) |
| 59 | { |
| 60 | try |
| 61 | { |
| 62 | initCRLs(); |
| 63 | initCaCerts(); |
| 64 | } |
| 65 | catch (Exception ex) |
| 66 | { |
| 67 | m_caCerts = new ArrayList(); |
| 68 | m_crls = new ArrayList(); |
| 69 | // TODO: log this |
| 70 | ex.printStackTrace(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private void initCRLs() throws Exception |
| 76 | { |
| 77 | final Collection result = new ArrayList(); |
| 78 | |
| 79 | if (m_crlList.trim().length() != 0) |
| 80 | { |
| 81 | CertificateFactory fac = |
| 82 | CertificateFactory.getInstance("X509"); |
| 83 | |
| 84 | for (StringTokenizer tok = new StringTokenizer(m_crlList, "|"); tok |
| 85 | .hasMoreElements();) |
| 86 | { |
| 87 | InputStream input = null; |
| 88 | try |
| 89 | { |
| 90 | input = |
| 91 | m_action.getURLConnectionInputStream(m_action |
| 92 | .createURL(null, tok.nextToken(), null) |
| 93 | .openConnection()); |
| 94 | result.addAll(fac.generateCRLs(input)); |
| 95 | } |
| 96 | catch (Exception ex) |
| 97 | { |
| 98 | // TODO: log this or something |
| 99 | ex.printStackTrace(); |
| 100 | } |
| 101 | finally |
| 102 | { |
| 103 | if (input != null) |
| 104 | { |
| 105 | try |
| 106 | { |
| 107 | input.close(); |
| 108 | } |
| 109 | catch (Exception ex) |
| 110 | { |
| 111 | // TODO: log this or something |
| 112 | ex.printStackTrace(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | m_crls = result; |
| 120 | } |
| 121 | |
| 122 | private void initCaCerts() throws Exception |
| 123 | { |
| 124 | final Collection result = new ArrayList(); |
| 125 | |
| 126 | if (m_storeList.trim().length() != 0) |
| 127 | { |
| 128 | |
| 129 | StringTokenizer storeTok = new StringTokenizer(m_storeList, "|"); |
| 130 | StringTokenizer passwdTok = new StringTokenizer(m_passwdList, "|"); |
| 131 | StringTokenizer typeTok = new StringTokenizer(m_typeList, "|"); |
| 132 | |
| 133 | while (storeTok.hasMoreTokens()) |
| 134 | { |
| 135 | KeyStore ks = KeyStore.getInstance(typeTok.nextToken().trim()); |
| 136 | |
| 137 | InputStream input = null; |
| 138 | try |
| 139 | { |
| 140 | input = |
| 141 | m_action.getURLConnectionInputStream(m_action |
| 142 | .createURL(null, storeTok.nextToken().trim(), null) |
| 143 | .openConnection()); |
| 144 | |
| 145 | ks.load(input, passwdTok.nextToken().trim().toCharArray()); |
| 146 | |
| 147 | for (Enumeration e = ks.aliases(); e.hasMoreElements();) |
| 148 | { |
| 149 | String alias = (String) e.nextElement(); |
| 150 | if (ks.isCertificateEntry(alias)) |
| 151 | { |
| 152 | result.add(ks.getCertificate(alias)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | catch (Exception ex) |
| 157 | { |
| 158 | // TODO: log this or something |
| 159 | ex.printStackTrace(); |
| 160 | } |
| 161 | finally |
| 162 | { |
| 163 | if (input != null) |
| 164 | { |
| 165 | try |
| 166 | { |
| 167 | input.close(); |
| 168 | } |
| 169 | catch (Exception ex) |
| 170 | { |
| 171 | // TODO: log this or something |
| 172 | ex.printStackTrace(); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | m_caCerts = result; |
| 180 | } |
| 181 | |
| 182 | public Collection getCRLs() |
| 183 | { |
| 184 | init(); |
| 185 | |
| 186 | return m_crls; |
| 187 | } |
| 188 | |
| 189 | public Collection getCaCerts() |
| 190 | { |
| 191 | init(); |
| 192 | |
| 193 | return m_caCerts; |
| 194 | } |
| 195 | } |