1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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
50
51
52
53
54
55
56 public RSAKeySecret(byte[] privateKey, byte[] publicKey) {
57 this(createPrivateKey(privateKey), createPublicKey(publicKey));
58 }
59
60
61
62
63
64
65
66
67
68 public RSAKeySecret(String privateKey, String publicKey) {
69 this(base64Decode(privateKey), base64Decode(publicKey));
70 }
71
72
73
74
75
76
77 public RSAKeySecret(PublicKey publicKey) {
78 this(null, publicKey);
79 }
80
81
82
83
84
85
86
87 public RSAKeySecret(byte[] publicKey) {
88 this(null, createPublicKey(publicKey));
89 }
90
91
92
93
94
95
96
97 public RSAKeySecret(String publicKey) {
98 this(base64Decode(publicKey));
99 }
100
101
102
103
104
105
106 public RSAKeySecret(X509Certificate certificate) {
107 this(certificate.getPublicKey());
108 }
109
110
111
112
113
114
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
136
137
138
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
160
161
162
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
179
180
181
182 public PrivateKey getPrivateKey() {
183 return privateKey;
184 }
185
186
187
188
189
190
191 public PublicKey getPublicKey() {
192 return publicKey;
193 }
194 }