blob: 2d24fb3480355a2ad6ab5c69b14eaaaba34f04df [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 Paulsfbb32572010-05-30 22:16:56 +000030//import org.apache.felix.moduleloader.IModule;
31import org.apache.felix.framework.resolver.Module;
Karl Pauls36407322008-03-07 00:37:30 +000032import org.osgi.framework.Bundle;
33
34/**
Karl Pauls23287bd2010-01-10 22:11:27 +000035 * This class is the entry point to the security. It is used to determine
36 * whether a given bundle is signed correctely and has permissions based on
Karl Pauls36407322008-03-07 00:37:30 +000037 * PermissionAdmin or ConditionalPermissionAdmin.
38 */
39public final class SecurityProviderImpl implements SecurityProvider
40{
41 private final BundleDNParser m_parser;
42 private final PermissionAdminImpl m_pai;
43 private final ConditionalPermissionAdminImpl m_cpai;
44 private final SecureAction m_action;
45
Karl Pauls23287bd2010-01-10 22:11:27 +000046 SecurityProviderImpl(String crlList, String typeList, String passwdList,
47 String storeList, PermissionAdminImpl pai,
Karl Pauls36407322008-03-07 00:37:30 +000048 ConditionalPermissionAdminImpl cpai, SecureAction action)
49 {
50 m_pai = pai;
51 m_cpai = cpai;
52 m_action = action;
Karl Pauls23287bd2010-01-10 22:11:27 +000053 m_parser = new BundleDNParser(new TrustManager(crlList, typeList,
54 passwdList, storeList, m_action));
Karl Pauls36407322008-03-07 00:37:30 +000055 }
56
57 /**
Karl Pauls23287bd2010-01-10 22:11:27 +000058 * If the given bundle is signed but can not be verified (e.g., missing
59 * files) then throw an exception.
Karl Pauls36407322008-03-07 00:37:30 +000060 */
61 public void checkBundle(Bundle bundle) throws Exception
62 {
Karl Paulsfbb32572010-05-30 22:16:56 +000063 Module module = ((BundleImpl) bundle).getCurrentModule();
Karl Pauls23287bd2010-01-10 22:11:27 +000064 m_parser.checkDNChains(module, module.getContent(),
65 Bundle.SIGNERS_TRUSTED);
Karl Pauls36407322008-03-07 00:37:30 +000066 }
67
68 /**
69 * Get a signer matcher that can be used to match digital signed bundles.
70 */
Karl Pauls23287bd2010-01-10 22:11:27 +000071 public Object getSignerMatcher(final Bundle bundle, int signersType)
Karl Pauls36407322008-03-07 00:37:30 +000072 {
Karl Paulsfbb32572010-05-30 22:16:56 +000073 Module module = ((BundleImpl) bundle).getCurrentModule();
Karl Pauls23287bd2010-01-10 22:11:27 +000074 return m_parser.getDNChains(module, module.getContent(), signersType);
Karl Pauls36407322008-03-07 00:37:30 +000075 }
76
77 /**
Karl Pauls23287bd2010-01-10 22:11:27 +000078 * If we have a permissionadmin then ask that one first and have it decide
79 * in case there is a location bound. If not then either use its default
80 * permission in case there is no conditional permission admin or else ask
81 * that one.
Karl Pauls36407322008-03-07 00:37:30 +000082 */
83 public boolean hasBundlePermission(ProtectionDomain bundleProtectionDomain,
84 Permission permission, boolean direct)
85 {
Karl Pauls23287bd2010-01-10 22:11:27 +000086 BundleProtectionDomain pd = (BundleProtectionDomain) bundleProtectionDomain;
Karl Paulsd093f2d2009-11-24 23:23:26 +000087 BundleImpl bundle = pd.getBundle();
Karl Paulsfbb32572010-05-30 22:16:56 +000088 Module module = pd.getModule();
Karl Pauls36407322008-03-07 00:37:30 +000089
Karl Paulsd093f2d2009-11-24 23:23:26 +000090 if (bundle.getBundleId() == 0)
Karl Pauls36407322008-03-07 00:37:30 +000091 {
92 return true;
93 }
94
Karl Pauls35c1c342008-03-19 17:39:16 +000095 // System.out.println(info.getBundleId() + " - " + permission);
Karl Pauls36407322008-03-07 00:37:30 +000096 // TODO: using true, false, or null seems a bit awkward. Improve this.
97 Boolean result = null;
98 if (m_pai != null)
99 {
Karl Pauls23287bd2010-01-10 22:11:27 +0000100 result = m_pai.hasPermission(bundle._getLocation(), pd.getBundle(),
101 permission, m_cpai, pd, bundle.getCurrentModule().getContent());
Karl Pauls36407322008-03-07 00:37:30 +0000102 }
103
104 if (result != null)
105 {
Karl Pauls23287bd2010-01-10 22:11:27 +0000106 if ((m_cpai != null) && !direct)
107 {
108 boolean allow = result.booleanValue();
109 if (!allow)
110 {
111 m_cpai.clearPD();
112 return false;
113 }
114 return m_cpai.handlePAHandle(pd);
115 }
Karl Pauls36407322008-03-07 00:37:30 +0000116 return result.booleanValue();
117 }
118
119 if (m_cpai != null)
120 {
121 try
122 {
Karl Pauls23287bd2010-01-10 22:11:27 +0000123 return m_cpai.hasPermission(module, module.getContent(), pd,
Karl Pauls35c1c342008-03-19 17:39:16 +0000124 permission, direct, m_pai);
Karl Pauls36407322008-03-07 00:37:30 +0000125 }
126 catch (Exception e)
127 {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131 }
132
133 return false;
Karl Pauls36407322008-03-07 00:37:30 +0000134 }
Karl Paulsfbb32572010-05-30 22:16:56 +0000135}