blob: 1fadaddbca81c79a8d0025d4ec2ed5efcbae185c [file] [log] [blame]
Karl Pauls36407322008-03-07 00:37:30 +00001/*
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 */
19package org.apache.felix.framework;
20
21import java.security.Permission;
22import java.security.ProtectionDomain;
23
24import org.apache.felix.framework.ext.SecurityProvider;
25import org.apache.felix.framework.security.condpermadmin.ConditionalPermissionAdminImpl;
26import org.apache.felix.framework.security.permissionadmin.PermissionAdminImpl;
27import org.apache.felix.framework.security.util.TrustManager;
28import org.apache.felix.framework.security.verifier.BundleDNParser;
Karl Pauls36407322008-03-07 00:37:30 +000029import org.apache.felix.framework.util.SecureAction;
Karl Pauls23287bd2010-01-10 22:11:27 +000030import org.apache.felix.moduleloader.IModule;
Karl Pauls36407322008-03-07 00:37:30 +000031import org.osgi.framework.Bundle;
32
33/**
Karl Pauls23287bd2010-01-10 22:11:27 +000034 * This class is the entry point to the security. It is used to determine
35 * whether a given bundle is signed correctely and has permissions based on
Karl Pauls36407322008-03-07 00:37:30 +000036 * PermissionAdmin or ConditionalPermissionAdmin.
37 */
38public final class SecurityProviderImpl implements SecurityProvider
39{
40 private final BundleDNParser m_parser;
41 private final PermissionAdminImpl m_pai;
42 private final ConditionalPermissionAdminImpl m_cpai;
43 private final SecureAction m_action;
44
Karl Pauls23287bd2010-01-10 22:11:27 +000045 SecurityProviderImpl(String crlList, String typeList, String passwdList,
46 String storeList, PermissionAdminImpl pai,
Karl Pauls36407322008-03-07 00:37:30 +000047 ConditionalPermissionAdminImpl cpai, SecureAction action)
48 {
49 m_pai = pai;
50 m_cpai = cpai;
51 m_action = action;
Karl Pauls23287bd2010-01-10 22:11:27 +000052 m_parser = new BundleDNParser(new TrustManager(crlList, typeList,
53 passwdList, storeList, m_action));
Karl Pauls36407322008-03-07 00:37:30 +000054 }
55
56 /**
Karl Pauls23287bd2010-01-10 22:11:27 +000057 * If the given bundle is signed but can not be verified (e.g., missing
58 * files) then throw an exception.
Karl Pauls36407322008-03-07 00:37:30 +000059 */
60 public void checkBundle(Bundle bundle) throws Exception
61 {
Karl Pauls23287bd2010-01-10 22:11:27 +000062 IModule module = ((BundleImpl) bundle).getCurrentModule();
63 m_parser.checkDNChains(module, module.getContent(),
64 Bundle.SIGNERS_TRUSTED);
Karl Pauls36407322008-03-07 00:37:30 +000065 }
66
67 /**
68 * Get a signer matcher that can be used to match digital signed bundles.
69 */
Karl Pauls23287bd2010-01-10 22:11:27 +000070 public Object getSignerMatcher(final Bundle bundle, int signersType)
Karl Pauls36407322008-03-07 00:37:30 +000071 {
Karl Pauls23287bd2010-01-10 22:11:27 +000072 IModule module = ((BundleImpl) bundle).getCurrentModule();
73 return m_parser.getDNChains(module, module.getContent(), signersType);
Karl Pauls36407322008-03-07 00:37:30 +000074 }
75
76 /**
Karl Pauls23287bd2010-01-10 22:11:27 +000077 * If we have a permissionadmin then ask that one first and have it decide
78 * in case there is a location bound. If not then either use its default
79 * permission in case there is no conditional permission admin or else ask
80 * that one.
Karl Pauls36407322008-03-07 00:37:30 +000081 */
82 public boolean hasBundlePermission(ProtectionDomain bundleProtectionDomain,
83 Permission permission, boolean direct)
84 {
Karl Pauls23287bd2010-01-10 22:11:27 +000085 BundleProtectionDomain pd = (BundleProtectionDomain) bundleProtectionDomain;
Karl Paulsd093f2d2009-11-24 23:23:26 +000086 BundleImpl bundle = pd.getBundle();
Karl Pauls23287bd2010-01-10 22:11:27 +000087 IModule module = pd.getModule();
Karl Pauls36407322008-03-07 00:37:30 +000088
Karl Paulsd093f2d2009-11-24 23:23:26 +000089 if (bundle.getBundleId() == 0)
Karl Pauls36407322008-03-07 00:37:30 +000090 {
91 return true;
92 }
93
Karl Pauls35c1c342008-03-19 17:39:16 +000094 // System.out.println(info.getBundleId() + " - " + permission);
Karl Pauls36407322008-03-07 00:37:30 +000095 // TODO: using true, false, or null seems a bit awkward. Improve this.
96 Boolean result = null;
97 if (m_pai != null)
98 {
Karl Pauls23287bd2010-01-10 22:11:27 +000099 result = m_pai.hasPermission(bundle._getLocation(), pd.getBundle(),
100 permission, m_cpai, pd, bundle.getCurrentModule().getContent());
Karl Pauls36407322008-03-07 00:37:30 +0000101 }
102
103 if (result != null)
104 {
Karl Pauls23287bd2010-01-10 22:11:27 +0000105 if ((m_cpai != null) && !direct)
106 {
107 boolean allow = result.booleanValue();
108 if (!allow)
109 {
110 m_cpai.clearPD();
111 return false;
112 }
113 return m_cpai.handlePAHandle(pd);
114 }
Karl Pauls36407322008-03-07 00:37:30 +0000115 return result.booleanValue();
116 }
117
118 if (m_cpai != null)
119 {
120 try
121 {
Karl Pauls23287bd2010-01-10 22:11:27 +0000122 return m_cpai.hasPermission(module, module.getContent(), pd,
Karl Pauls35c1c342008-03-19 17:39:16 +0000123 permission, direct, m_pai);
Karl Pauls36407322008-03-07 00:37:30 +0000124 }
125 catch (Exception e)
126 {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130 }
131
132 return false;
Karl Pauls36407322008-03-07 00:37:30 +0000133 }
134}