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.io.UnsupportedEncodingException;
22 import java.security.*;
23
24
25
26
27
28
29
30 public class RSA_SHA1SignatureMethod implements OAuthSignatureMethod {
31
32
33
34
35 public static final String SIGNATURE_NAME = "RSA-SHA1";
36
37 private final PrivateKey privateKey;
38 private final PublicKey publicKey;
39
40
41
42
43
44
45
46 public RSA_SHA1SignatureMethod(PrivateKey privateKey, PublicKey publicKey) {
47 this.privateKey = privateKey;
48 this.publicKey = publicKey;
49 }
50
51
52
53
54
55
56
57 public RSA_SHA1SignatureMethod(PrivateKey key) {
58 this(key, null);
59 }
60
61
62
63
64
65
66
67 public RSA_SHA1SignatureMethod(PublicKey key) {
68 this(null, key);
69 }
70
71
72
73
74
75
76 public String getName() {
77 return SIGNATURE_NAME;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public String sign(String signatureBaseString) {
93 if (privateKey == null) {
94 throw new UnsupportedOperationException("Cannot sign the base string: no private key supplied.");
95 }
96
97 try {
98 Signature signer = Signature.getInstance("SHA1withRSA");
99 signer.initSign(privateKey);
100 signer.update(signatureBaseString.getBytes("UTF-8"));
101 byte[] signatureBytes = signer.sign();
102 signatureBytes = Base64.encodeBase64(signatureBytes);
103 return new String(signatureBytes, "UTF-8");
104 }
105 catch (NoSuchAlgorithmException e) {
106 throw new IllegalStateException(e);
107 }
108 catch (InvalidKeyException e) {
109 throw new IllegalStateException(e);
110 }
111 catch (SignatureException e) {
112 throw new IllegalStateException(e);
113 }
114 catch (UnsupportedEncodingException e) {
115 throw new RuntimeException(e);
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 public void verify(String signatureBaseString, String signature) throws InvalidSignatureException {
131 if (publicKey == null) {
132 throw new UnsupportedOperationException("A public key must be provided to verify signatures.");
133 }
134
135 try {
136 byte[] signatureBytes = Base64.decodeBase64(signature.getBytes("UTF-8"));
137 Signature verifier = Signature.getInstance("SHA1withRSA");
138 verifier.initVerify(publicKey);
139 verifier.update(signatureBaseString.getBytes("UTF-8"));
140 if (!verifier.verify(signatureBytes)) {
141 throw new InvalidSignatureException("Invalid signature for signature method " + getName());
142 }
143 }
144 catch (UnsupportedEncodingException e) {
145 throw new RuntimeException(e);
146 }
147 catch (NoSuchAlgorithmException e) {
148 throw new IllegalStateException(e);
149 }
150 catch (InvalidKeyException e) {
151 throw new IllegalStateException(e);
152 }
153 catch (SignatureException e) {
154 throw new IllegalStateException(e);
155 }
156 }
157
158
159
160
161
162
163 public PrivateKey getPrivateKey() {
164 return privateKey;
165 }
166
167
168
169
170
171
172 public PublicKey getPublicKey() {
173 return publicKey;
174 }
175 }