OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / security / Signer.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package java.security;
19
20 /**
21  * {@link Signer} represents an identity (individual or corporation) that owns a
22  * private key and the corresponding public key.
23  *
24  * @deprecated Replaced by behavior in {@link java.security.cert
25  *             java.security.cert} package and {@link java.security.Principal
26  *             Principal}
27  */
28 @Deprecated
29 public abstract class Signer extends Identity {
30
31     private static final long serialVersionUID = -1763464102261361480L;
32
33     private PrivateKey privateKey;
34
35     /**
36      * Constructs a new instance of {@code Signer}.
37      */
38     protected Signer() {
39         super();
40     }
41
42     /**
43      * Constructs a new instance of {@code Signer} with the given name.
44      *
45      * @param name
46      *            the name of the signer.
47      */
48     public Signer(String name) {
49         super(name);
50     }
51
52     /**
53      * Constructs a new instance of {@code Signer} with the given name in the
54      * given scope.
55      *
56      * @param name
57      *            the name of the signer.
58      * @param scope
59      *            the scope of the signer.
60      * @throws KeyManagementException
61      *             if a signer with the specified name already exists in the
62      *             provided scope.
63      */
64     public Signer(String name, IdentityScope scope)
65             throws KeyManagementException {
66         super(name, scope);
67     }
68
69     /**
70      * Returns the private key of this {@code Signer}. If a {@code
71      * SecurityManager} is installed, code calling this method needs the {@code
72      * SecurityPermission} {@code "getSignerPrivateKey"} to be granted, otherwise
73      * a {@code SecurityException} will be thrown.
74      *
75      * @return the private key of this {@code Signer}.
76      * @throws SecurityException
77      *             if a {@code SecurityManager} is installed and the caller does
78      *             not have permission to invoke this method.
79      */
80     public PrivateKey getPrivateKey() {
81         SecurityManager sm = System.getSecurityManager();
82         if (sm != null) {
83             sm.checkSecurityAccess("getSignerPrivateKey");
84         }
85
86         return privateKey;
87     }
88
89     /**
90      * Associates the specified key pair with this {@code Signer}. If a {@code
91      * SecurityManager} is installed, code calling this method needs the {@code
92      * SecurityPermission} {@code getSignerPrivateKey} to be granted, otherwise
93      * a {@code SecurityException} will be thrown.
94      *
95      * @param pair
96      *            the key pair to associate with this {@code Signer}.
97      * @throws InvalidParameterException
98      *             if the key pair is invalid.
99      * @throws KeyException
100      *             if any other key related problem occurs.
101      * @throws SecurityException
102      *             if a {@code SecurityManager} is installed and the caller does
103      *             not have permission to invoke this method.
104      */
105     public final void setKeyPair(KeyPair pair)
106             throws InvalidParameterException, KeyException {
107
108         if (pair == null) {
109             throw new NullPointerException();
110         }
111
112         if ((pair.getPrivate() == null) || (pair.getPublic() == null)) {
113             throw new InvalidParameterException();
114         }
115         SecurityManager sm = System.getSecurityManager();
116         if (sm != null) {
117             sm.checkSecurityAccess("setSignerKeyPair");
118         }
119         final PublicKey pk = pair.getPublic();
120         try {
121             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
122                 public Void run() throws KeyManagementException {
123                     setPublicKey(pk);
124                     return null;
125                 }
126             });
127         } catch (PrivilegedActionException e) {
128             throw new KeyException(e.getException());
129         }
130         this.privateKey = pair.getPrivate();
131     }
132
133     /**
134      * Returns a string containing a concise, human-readable description of this
135      * {@code Signer} including its name and its scope if present.
136      *
137      * @return a printable representation for this {@code Signer}.
138      */
139     @Override
140     public String toString() {
141         String s = "[Signer]" + getName();
142         if (getScope() != null) {
143             s = s + '[' + getScope().toString() + ']';
144         }
145         return s;
146     }
147 }