1 /* Security.java --- Java base security class implementation
2 Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
39 package java
.security
;
41 import gnu
.classpath
.SystemProperties
;
43 import gnu
.classpath
.Configuration
;
45 import java
.io
.IOException
;
46 import java
.io
.InputStream
;
48 import java
.util
.Collections
;
49 import java
.util
.Enumeration
;
50 import java
.util
.HashMap
;
51 import java
.util
.HashSet
;
52 import java
.util
.Iterator
;
53 import java
.util
.LinkedHashSet
;
55 import java
.util
.Properties
;
57 import java
.util
.Vector
;
60 * This class centralizes all security properties and common security methods.
61 * One of its primary uses is to manage providers.
63 * @author Mark Benvenuto (ivymccough@worldnet.att.net)
65 public final class Security
67 private static final String ALG_ALIAS
= "Alg.Alias.";
69 private static Vector providers
= new Vector();
70 private static Properties secprops
= new Properties();
74 String base
= SystemProperties
.getProperty("gnu.classpath.home.url");
75 String vendor
= SystemProperties
.getProperty("gnu.classpath.vm.shortname");
77 // Try VM specific security file
78 boolean loaded
= loadProviders (base
, vendor
);
80 // Append classpath standard provider if possible
81 if (!loadProviders (base
, "classpath")
83 && providers
.size() == 0)
85 if (Configuration
.DEBUG
)
87 /* No providers found and both security files failed to
88 * load properly. Give a warning in case of DEBUG is
89 * enabled. Could be done with java.util.logging later.
92 ("WARNING: could not properly read security provider files:");
94 (" " + base
+ "/security/" + vendor
97 (" " + base
+ "/security/" + "classpath"
100 (" Falling back to standard GNU security provider");
102 providers
.addElement (new gnu
.java
.security
.provider
.Gnu());
105 // This class can't be instantiated.
111 * Tries to load the vender specific security providers from the given
112 * base URL. Returns true if the resource could be read and completely
113 * parsed successfully, false otherwise.
115 private static boolean loadProviders(String baseUrl
, String vendor
)
117 if (baseUrl
== null || vendor
== null)
120 boolean result
= true;
121 String secfilestr
= baseUrl
+ "/security/" + vendor
+ ".security";
124 InputStream fin
= new URL(secfilestr
).openStream();
129 while ((name
= secprops
.getProperty("security.provider." + i
)) != null)
131 Exception exception
= null;
134 providers
.addElement(Class
.forName(name
).newInstance());
136 catch (ClassNotFoundException x
)
140 catch (InstantiationException x
)
144 catch (IllegalAccessException x
)
149 if (exception
!= null)
151 System
.err
.println ("WARNING: Error loading security provider "
152 + name
+ ": " + exception
);
158 catch (IOException ignored
)
167 * Gets a specified property for an algorithm. The algorithm name should be a
168 * standard name. See Appendix A in the Java Cryptography Architecture API
169 * Specification & Reference for information about standard algorithm
170 * names. One possible use is by specialized algorithm parsers, which may map
171 * classes to algorithms which they understand (much like {@link Key} parsers
174 * @param algName the algorithm name.
175 * @param propName the name of the property to get.
176 * @return the value of the specified property.
177 * @deprecated This method used to return the value of a proprietary property
178 * in the master file of the "SUN" Cryptographic Service Provider in order to
179 * determine how to parse algorithm-specific parameters. Use the new
180 * provider-based and algorithm-independent {@link AlgorithmParameters} and
181 * {@link KeyFactory} engine classes (introduced in the Java 2 platform)
184 public static String
getAlgorithmProperty(String algName
, String propName
)
186 if (algName
== null || propName
== null)
189 String property
= String
.valueOf(propName
) + "." + String
.valueOf(algName
);
191 for (Iterator i
= providers
.iterator(); i
.hasNext(); )
193 p
= (Provider
) i
.next();
194 for (Iterator j
= p
.keySet().iterator(); j
.hasNext(); )
196 String key
= (String
) j
.next();
197 if (key
.equalsIgnoreCase(property
))
198 return p
.getProperty(key
);
205 * <p>Adds a new provider, at a specified position. The position is the
206 * preference order in which providers are searched for requested algorithms.
207 * Note that it is not guaranteed that this preference will be respected. The
208 * position is 1-based, that is, <code>1</code> is most preferred, followed by
209 * <code>2</code>, and so on.</p>
211 * <p>If the given provider is installed at the requested position, the
212 * provider that used to be at that position, and all providers with a
213 * position greater than position, are shifted up one position (towards the
214 * end of the list of installed providers).</p>
216 * <p>A provider cannot be added if it is already installed.</p>
218 * <p>First, if there is a security manager, its <code>checkSecurityAccess()
219 * </code> method is called with the string <code>"insertProvider."+provider.
220 * getName()</code> to see if it's ok to add a new provider. If the default
221 * implementation of <code>checkSecurityAccess()</code> is used (i.e., that
222 * method is not overriden), then this will result in a call to the security
223 * manager's <code>checkPermission()</code> method with a
224 * <code>SecurityPermission("insertProvider."+provider.getName())</code>
227 * @param provider the provider to be added.
228 * @param position the preference position that the caller would like for
230 * @return the actual preference position in which the provider was added, or
231 * <code>-1</code> if the provider was not added because it is already
233 * @throws SecurityException if a security manager exists and its
234 * {@link SecurityManager#checkSecurityAccess(String)} method denies access
235 * to add a new provider.
236 * @see #getProvider(String)
237 * @see #removeProvider(String)
238 * @see SecurityPermission
240 public static int insertProviderAt(Provider provider
, int position
)
242 SecurityManager sm
= System
.getSecurityManager();
244 sm
.checkSecurityAccess("insertProvider." + provider
.getName());
247 int max
= providers
.size ();
248 for (int i
= 0; i
< max
; i
++)
250 if (((Provider
) providers
.elementAt(i
)).getName().equals(provider
.getName()))
259 providers
.insertElementAt(provider
, position
);
265 * <p>Adds a provider to the next position available.</p>
267 * <p>First, if there is a security manager, its <code>checkSecurityAccess()
268 * </code> method is called with the string <code>"insertProvider."+provider.
269 * getName()</code> to see if it's ok to add a new provider. If the default
270 * implementation of <code>checkSecurityAccess()</code> is used (i.e., that
271 * method is not overriden), then this will result in a call to the security
272 * manager's <code>checkPermission()</code> method with a
273 * <code>SecurityPermission("insertProvider."+provider.getName())</code>
276 * @param provider the provider to be added.
277 * @return the preference position in which the provider was added, or
278 * <code>-1</code> if the provider was not added because it is already
280 * @throws SecurityException if a security manager exists and its
281 * {@link SecurityManager#checkSecurityAccess(String)} method denies access
282 * to add a new provider.
283 * @see #getProvider(String)
284 * @see #removeProvider(String)
285 * @see SecurityPermission
287 public static int addProvider(Provider provider
)
289 return insertProviderAt (provider
, providers
.size () + 1);
293 * <p>Removes the provider with the specified name.</p>
295 * <p>When the specified provider is removed, all providers located at a
296 * position greater than where the specified provider was are shifted down
297 * one position (towards the head of the list of installed providers).</p>
299 * <p>This method returns silently if the provider is not installed.</p>
301 * <p>First, if there is a security manager, its <code>checkSecurityAccess()
302 * </code> method is called with the string <code>"removeProvider."+name</code>
303 * to see if it's ok to remove the provider. If the default implementation of
304 * <code>checkSecurityAccess()</code> is used (i.e., that method is not
305 * overriden), then this will result in a call to the security manager's
306 * <code>checkPermission()</code> method with a <code>SecurityPermission(
307 * "removeProvider."+name)</code> permission.</p>
309 * @param name the name of the provider to remove.
310 * @throws SecurityException if a security manager exists and its
311 * {@link SecurityManager#checkSecurityAccess(String)} method denies access
312 * to remove the provider.
313 * @see #getProvider(String)
314 * @see #addProvider(Provider)
316 public static void removeProvider(String name
)
318 SecurityManager sm
= System
.getSecurityManager();
320 sm
.checkSecurityAccess("removeProvider." + name
);
322 int max
= providers
.size ();
323 for (int i
= 0; i
< max
; i
++)
325 if (((Provider
) providers
.elementAt(i
)).getName().equals(name
))
334 * Returns an array containing all the installed providers. The order of the
335 * providers in the array is their preference order.
337 * @return an array of all the installed providers.
339 public static Provider
[] getProviders()
341 Provider
[] array
= new Provider
[providers
.size ()];
342 providers
.copyInto (array
);
347 * Returns the provider installed with the specified name, if any. Returns
348 * <code>null</code> if no provider with the specified name is installed.
350 * @param name the name of the provider to get.
351 * @return the provider of the specified name.
352 * @see #removeProvider(String)
353 * @see #addProvider(Provider)
355 public static Provider
getProvider(String name
)
358 int max
= providers
.size ();
359 for (int i
= 0; i
< max
; i
++)
361 p
= (Provider
) providers
.elementAt(i
);
362 if (p
.getName().equals(name
))
369 * <p>Gets a security property value.</p>
371 * <p>First, if there is a security manager, its <code>checkPermission()</code>
372 * method is called with a <code>SecurityPermission("getProperty."+key)</code>
373 * permission to see if it's ok to retrieve the specified security property
376 * @param key the key of the property being retrieved.
377 * @return the value of the security property corresponding to key.
378 * @throws SecurityException if a security manager exists and its
379 * {@link SecurityManager#checkPermission(Permission)} method denies access
380 * to retrieve the specified security property value.
381 * @see #setProperty(String, String)
382 * @see SecurityPermission
384 public static String
getProperty(String key
)
386 SecurityManager sm
= System
.getSecurityManager();
388 sm
.checkSecurityAccess("getProperty." + key
);
390 return secprops
.getProperty(key
);
394 * <p>Sets a security property value.</p>
396 * <p>First, if there is a security manager, its <code>checkPermission()</code>
397 * method is called with a <code>SecurityPermission("setProperty."+key)</code>
398 * permission to see if it's ok to set the specified security property value.
401 * @param key the name of the property to be set.
402 * @param datnum the value of the property to be set.
403 * @throws SecurityException if a security manager exists and its
404 * {@link SecurityManager#checkPermission(Permission)} method denies access
405 * to set the specified security property value.
406 * @see #getProperty(String)
407 * @see SecurityPermission
409 public static void setProperty(String key
, String datnum
)
411 SecurityManager sm
= System
.getSecurityManager();
413 sm
.checkSecurityAccess("setProperty." + key
);
415 secprops
.put(key
, datnum
);
419 * Returns a Set of Strings containing the names of all available algorithms
420 * or types for the specified Java cryptographic service (e.g., Signature,
421 * MessageDigest, Cipher, Mac, KeyStore). Returns an empty Set if there is no
422 * provider that supports the specified service. For a complete list of Java
423 * cryptographic services, please see the Java Cryptography Architecture API
424 * Specification & Reference. Note: the returned set is immutable.
426 * @param serviceName the name of the Java cryptographic service (e.g.,
427 * Signature, MessageDigest, Cipher, Mac, KeyStore). Note: this parameter is
429 * @return a Set of Strings containing the names of all available algorithms
430 * or types for the specified Java cryptographic service or an empty set if
431 * no provider supports the specified service.
434 public static Set
getAlgorithms(String serviceName
)
436 HashSet result
= new HashSet();
437 if (serviceName
== null || serviceName
.length() == 0)
440 serviceName
= serviceName
.trim();
441 if (serviceName
.length() == 0)
444 serviceName
= serviceName
.toUpperCase()+".";
445 Provider
[] providers
= getProviders();
447 for (int i
= 0; i
< providers
.length
; i
++)
448 for (Enumeration e
= providers
[i
].propertyNames(); e
.hasMoreElements(); )
450 String service
= ((String
) e
.nextElement()).trim();
451 if (service
.toUpperCase().startsWith(serviceName
))
453 service
= service
.substring(serviceName
.length()).trim();
454 ndx
= service
.indexOf(' '); // get rid of attributes
456 service
= service
.substring(0, ndx
);
460 return Collections
.unmodifiableSet(result
);
464 * <p>Returns an array containing all installed providers that satisfy the
465 * specified selection criterion, or <code>null</code> if no such providers
466 * have been installed. The returned providers are ordered according to their
467 * preference order.</p>
469 * <p>A cryptographic service is always associated with a particular
470 * algorithm or type. For example, a digital signature service is always
471 * associated with a particular algorithm (e.g., <i>DSA</i>), and a
472 * CertificateFactory service is always associated with a particular
473 * certificate type (e.g., <i>X.509</i>).</p>
475 * <p>The selection criterion must be specified in one of the following two
479 * <li><p><crypto_service>.<algorithm_or_type></p>
480 * <p>The cryptographic service name must not contain any dots.</p>
481 * <p>A provider satisfies the specified selection criterion iff the
482 * provider implements the specified algorithm or type for the specified
483 * cryptographic service.</p>
484 * <p>For example, "CertificateFactory.X.509" would be satisfied by any
485 * provider that supplied a CertificateFactory implementation for X.509
486 * certificates.</p></li>
488 * <li><p><crypto_service>.<algorithm_or_type> <attribute_name>:<attribute_value></p>
489 * <p>The cryptographic service name must not contain any dots. There must
490 * be one or more space charaters between the the <algorithm_or_type>
491 * and the <attribute_name>.</p>
492 * <p>A provider satisfies this selection criterion iff the provider
493 * implements the specified algorithm or type for the specified
494 * cryptographic service and its implementation meets the constraint
495 * expressed by the specified attribute name/value pair.</p>
496 * <p>For example, "Signature.SHA1withDSA KeySize:1024" would be satisfied
497 * by any provider that implemented the SHA1withDSA signature algorithm
498 * with a keysize of 1024 (or larger).</p></li>
501 * <p>See Appendix A in the Java Cryptogaphy Architecture API Specification
502 * & Reference for information about standard cryptographic service names,
503 * standard algorithm names and standard attribute names.</p>
505 * @param filter the criterion for selecting providers. The filter is case-
507 * @return all the installed providers that satisfy the selection criterion,
508 * or null if no such providers have been installed.
509 * @throws InvalidParameterException if the filter is not in the required
511 * @see #getProviders(Map)
513 public static Provider
[] getProviders(String filter
)
515 if (providers
== null || providers
.isEmpty())
518 if (filter
== null || filter
.length() == 0)
519 return getProviders();
521 HashMap map
= new HashMap(1);
522 int i
= filter
.indexOf(':');
523 if (i
== -1) // <service>.<algorithm>
525 else // <service>.<algorithm> <attribute>:<value>
526 map
.put(filter
.substring(0, i
), filter
.substring(i
+1));
528 return getProviders(map
);
532 * <p>Returns an array containing all installed providers that satisfy the
533 * specified selection criteria, or <code>null</code> if no such providers
534 * have been installed. The returned providers are ordered according to their
535 * preference order.</p>
537 * <p>The selection criteria are represented by a map. Each map entry
538 * represents a selection criterion. A provider is selected iff it satisfies
539 * all selection criteria. The key for any entry in such a map must be in one
540 * of the following two formats:</p>
543 * <li><p><crypto_service>.<algorithm_or_type></p>
544 * <p>The cryptographic service name must not contain any dots.</p>
545 * <p>The value associated with the key must be an empty string.</p>
546 * <p>A provider satisfies this selection criterion iff the provider
547 * implements the specified algorithm or type for the specified
548 * cryptographic service.</p></li>
550 * <li><p><crypto_service>.<algorithm_or_type> <attribute_name></p>
551 * <p>The cryptographic service name must not contain any dots. There must
552 * be one or more space charaters between the <algorithm_or_type> and
553 * the <attribute_name>.</p>
554 * <p>The value associated with the key must be a non-empty string. A
555 * provider satisfies this selection criterion iff the provider implements
556 * the specified algorithm or type for the specified cryptographic service
557 * and its implementation meets the constraint expressed by the specified
558 * attribute name/value pair.</p></li>
561 * <p>See Appendix A in the Java Cryptogaphy Architecture API Specification
562 * & Reference for information about standard cryptographic service names,
563 * standard algorithm names and standard attribute names.</p>
565 * @param filter the criteria for selecting providers. The filter is case-
567 * @return all the installed providers that satisfy the selection criteria,
568 * or <code>null</code> if no such providers have been installed.
569 * @throws InvalidParameterException if the filter is not in the required
571 * @see #getProviders(String)
573 public static Provider
[] getProviders(Map filter
)
575 if (providers
== null || providers
.isEmpty())
579 return getProviders();
581 Set querries
= filter
.keySet();
582 if (querries
== null || querries
.isEmpty())
583 return getProviders();
585 LinkedHashSet result
= new LinkedHashSet(providers
); // assume all
587 String querry
, service
, algorithm
, attribute
, value
;
588 LinkedHashSet serviceProviders
= new LinkedHashSet(); // preserve insertion order
589 for (Iterator i
= querries
.iterator(); i
.hasNext(); )
591 querry
= (String
) i
.next();
592 if (querry
== null) // all providers
595 querry
= querry
.trim();
596 if (querry
.length() == 0) // all providers
599 dot
= querry
.indexOf('.');
600 if (dot
== -1) // syntax error
601 throw new InvalidParameterException(
602 "missing dot in '" + String
.valueOf(querry
)+"'");
604 value
= (String
) filter
.get(querry
);
605 // deconstruct querry into [service, algorithm, attribute]
606 if (value
== null || value
.trim().length() == 0) // <service>.<algorithm>
610 service
= querry
.substring(0, dot
).trim();
611 algorithm
= querry
.substring(dot
+1).trim();
613 else // <service>.<algorithm> <attribute>
615 ws
= querry
.indexOf(' ');
617 throw new InvalidParameterException(
618 "value (" + String
.valueOf(value
) +
619 ") is not empty, but querry (" + String
.valueOf(querry
) +
620 ") is missing at least one space character");
621 value
= value
.trim();
622 attribute
= querry
.substring(ws
+1).trim();
623 // was the dot in the attribute?
624 if (attribute
.indexOf('.') != -1)
625 throw new InvalidParameterException(
626 "attribute_name (" + String
.valueOf(attribute
) +
627 ") in querry (" + String
.valueOf(querry
) + ") contains a dot");
629 querry
= querry
.substring(0, ws
).trim();
630 service
= querry
.substring(0, dot
).trim();
631 algorithm
= querry
.substring(dot
+1).trim();
634 // service and algorithm must not be empty
635 if (service
.length() == 0)
636 throw new InvalidParameterException(
637 "<crypto_service> in querry (" + String
.valueOf(querry
) +
640 if (algorithm
.length() == 0)
641 throw new InvalidParameterException(
642 "<algorithm_or_type> in querry (" + String
.valueOf(querry
) +
645 selectProviders(service
, algorithm
, attribute
, value
, result
, serviceProviders
);
646 result
.retainAll(serviceProviders
); // eval next retaining found providers
647 if (result
.isEmpty()) // no point continuing
651 if (result
.isEmpty())
654 return (Provider
[]) result
.toArray(new Provider
[result
.size()]);
657 private static void selectProviders(String svc
, String algo
, String attr
,
658 String val
, LinkedHashSet providerSet
,
659 LinkedHashSet result
)
661 result
.clear(); // ensure we start with an empty result set
662 for (Iterator i
= providerSet
.iterator(); i
.hasNext(); )
664 Provider p
= (Provider
) i
.next();
665 if (provides(p
, svc
, algo
, attr
, val
))
670 private static boolean provides(Provider p
, String svc
, String algo
,
671 String attr
, String val
)
674 String serviceDotAlgorithm
= null;
677 boolean found
= false;
678 // if <svc>.<algo> <attr> is in the set then so is <svc>.<algo>
679 // but it may be stored under an alias <algo>. resolve
680 outer
: for (int r
= 0; r
< 3; r
++) // guard against circularity
682 serviceDotAlgorithm
= (svc
+"."+String
.valueOf(algo
)).trim();
683 for (it
= p
.keySet().iterator(); it
.hasNext(); )
685 key
= (String
) it
.next();
686 if (key
.equalsIgnoreCase(serviceDotAlgorithm
)) // eureka
691 // it may be there but as an alias
692 if (key
.equalsIgnoreCase(ALG_ALIAS
+ serviceDotAlgorithm
))
694 algo
= p
.getProperty(key
);
697 // else continue inner
704 // found a candidate for the querry. do we have an attr to match?
705 if (val
== null) // <service>.<algorithm> querry
708 // <service>.<algorithm> <attribute>; find the key entry that match
710 int limit
= serviceDotAlgorithm
.length() + 1;
711 for (it
= p
.keySet().iterator(); it
.hasNext(); )
713 key
= (String
) it
.next();
714 if (key
.length() <= limit
)
717 if (key
.substring(0, limit
).equalsIgnoreCase(serviceDotAlgorithm
+" "))
719 realAttr
= key
.substring(limit
).trim();
720 if (! realAttr
.equalsIgnoreCase(attr
))
723 // eveything matches so far. do the value
724 realVal
= p
.getProperty(key
);
728 realVal
= realVal
.trim();
729 // is it a string value?
730 if (val
.equalsIgnoreCase(realVal
))
733 // assume value is a number. cehck for greater-than-or-equal
734 return (new Integer(val
).intValue() >= new Integer(realVal
).intValue());