View Javadoc
1   /*
2    * Copyright 2008 Web Cohesion
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.security.oauth.common.signature;
18  
19  import org.apache.commons.codec.binary.Base64;
20  
21  import java.security.PrivateKey;
22  import java.security.PublicKey;
23  import java.security.KeyFactory;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.cert.X509Certificate;
26  import java.security.spec.EncodedKeySpec;
27  import java.security.spec.X509EncodedKeySpec;
28  import java.security.spec.InvalidKeySpecException;
29  import java.security.spec.PKCS8EncodedKeySpec;
30  import java.io.UnsupportedEncodingException;
31  
32  /**
33   * Signature secret for RSA.
34   *
35   * @author Ryan Heaton
36   */
37  @SuppressWarnings("serial")
38  public class RSAKeySecret implements SignatureSecret {
39  
40    private final PrivateKey privateKey;
41    private final PublicKey publicKey;
42  
43    public RSAKeySecret(PrivateKey privateKey, PublicKey publicKey) {
44      this.privateKey = privateKey;
45      this.publicKey = publicKey;
46    }
47  
48    /**
49     * Create an RSA public key secret with the given private and public key value. The value of the private key is
50     * assumed to be the PKCS#8-encoded bytes of the private key. The value of the public key is assumed to be
51     * the X509-encoded bytes of the public key.
52     *
53     * @param privateKey The value of the private key.
54     * @param publicKey The value of the public key.
55     */
56    public RSAKeySecret(byte[] privateKey, byte[] publicKey) {
57      this(createPrivateKey(privateKey), createPublicKey(publicKey));
58    }
59  
60    /**
61     * Create an RSA public key secret with the given private and public key.  The values are assumed to be
62     * the Base64-encoded values of the bytes of the keys, X509-encoded for the public key and PKCS#8-encoded
63     * for the private key.
64     *
65     * @param privateKey The value of the private key.
66     * @param publicKey The value of the public key.
67     */
68    public RSAKeySecret(String privateKey, String publicKey) {
69      this(base64Decode(privateKey), base64Decode(publicKey));
70    }
71  
72    /**
73     * Construct an RSA public key secret with the given public key. The private key will be null.
74     *
75     * @param publicKey The public key.
76     */
77    public RSAKeySecret(PublicKey publicKey) {
78      this(null, publicKey);
79    }
80  
81    /**
82     * Create an RSA public key secret with the given public key value.  The value is assumed to be
83     * the X509-encoded bytes of the public key. The private key will be null.
84     *
85     * @param publicKey The value of the public key.
86     */
87    public RSAKeySecret(byte[] publicKey) {
88      this(null, createPublicKey(publicKey));
89    }
90  
91    /**
92     * Create an RSA public key secret with the given public key value.  The value is assumed to be
93     * the Base64-encoded value of the X509-encoded bytes of the public key. The private key will be null.
94     *
95     * @param publicKey The value of the public key.
96     */
97    public RSAKeySecret(String publicKey) {
98      this(base64Decode(publicKey));
99    }
100 
101   /**
102    * Create an RSA public key secret with the given X509 certificate. The private key will be null.
103    *
104    * @param certificate The certificate.
105    */
106   public RSAKeySecret(X509Certificate certificate) {
107     this(certificate.getPublicKey());
108   }
109 
110   /**
111    * Creates a public key from the X509-encoded value of the given bytes.
112    *
113    * @param publicKey The X509-encoded public key bytes.
114    * @return The public key.
115    */
116   public static PublicKey createPublicKey(byte[] publicKey) {
117     if (publicKey == null) {
118       return null;
119     }
120     
121     try {
122       KeyFactory fac = KeyFactory.getInstance("RSA");
123       EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
124       return fac.generatePublic(spec);
125     }
126     catch (NoSuchAlgorithmException e) {
127       throw new IllegalStateException(e);
128     }
129     catch (InvalidKeySpecException e) {
130       throw new IllegalStateException(e);
131     }
132   }
133 
134   /**
135    * Creates a private key from the PKCS#8-encoded value of the given bytes.
136    *
137    * @param privateKey The PKCS#8-encoded private key bytes.
138    * @return The private key.
139    */
140   public static PrivateKey createPrivateKey(byte[] privateKey) {
141     if (privateKey == null) {
142       return null;
143     }
144 
145     try {
146       KeyFactory fac = KeyFactory.getInstance("RSA");
147       EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
148       return fac.generatePrivate(spec);
149     }
150     catch (NoSuchAlgorithmException e) {
151       throw new IllegalStateException(e);
152     }
153     catch (InvalidKeySpecException e) {
154       throw new IllegalStateException(e);
155     }
156   }
157 
158   /**
159    * Utility method for decoding a base-64-encoded string.
160    *
161    * @param value The base-64-encoded string.
162    * @return The decoded value.
163    */
164   private static byte[] base64Decode(String value) {
165     if (value == null) {
166       return null;
167     }
168     
169     try {
170       return Base64.decodeBase64(value.getBytes("UTF-8"));
171     }
172     catch (UnsupportedEncodingException e) {
173       throw new RuntimeException(e);
174     }
175   }
176 
177   /**
178    * The private key.
179    *
180    * @return The private key.
181    */
182   public PrivateKey getPrivateKey() {
183     return privateKey;
184   }
185 
186   /**
187    * The public key.
188    *
189    * @return The public key.
190    */
191   public PublicKey getPublicKey() {
192     return publicKey;
193   }
194 }