100 throw new SignatureException(
"The signature algorithm is null");
103 throw new SignatureException(
"The RSA public key is null");
105 if (signingInput == null) {
106 throw new SignatureException(
"The signing input is null");
109 String algorithm = null;
112 algorithm =
"SHA-256";
115 algorithm =
"SHA-384";
118 algorithm =
"SHA-512";
121 throw new SignatureException(
"Unsupported signature algorithm");
124 ASN1InputStream aIn = null;
126 byte[] sigBytes = Base64Util.base64urldecode(signature);
127 byte[] sigInBytes = signingInput.getBytes(Util.UTF8_STRING_ENCODING);
129 RSAPublicKeySpec rsaPublicKeySpec =
new RSAPublicKeySpec(
133 KeyFactory keyFactory = KeyFactory.getInstance(
"RSA",
"BC");
134 PublicKey publicKey = keyFactory.generatePublic(rsaPublicKeySpec);
136 Cipher cipher = Cipher.getInstance(
"RSA/None/PKCS1Padding",
"BC");
137 cipher.init(Cipher.DECRYPT_MODE, publicKey);
139 byte[] decSig = cipher.doFinal(sigBytes);
140 aIn =
new ASN1InputStream(decSig);
142 ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
144 MessageDigest hash = MessageDigest.getInstance(algorithm,
"BC");
145 hash.update(sigInBytes);
147 ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
148 return MessageDigest.isEqual(hash.digest(), sigHash.getOctets());
149 }
catch (IOException e) {
150 throw new SignatureException(e);
151 }
catch (NoSuchAlgorithmException e) {
152 throw new SignatureException(e);
153 }
catch (InvalidKeyException e) {
154 throw new SignatureException(e);
155 }
catch (InvalidKeySpecException e) {
156 throw new SignatureException(e);
157 }
catch (NoSuchPaddingException e) {
158 throw new SignatureException(e);
159 }
catch (BadPaddingException e) {
160 throw new SignatureException(e);
161 }
catch (NoSuchProviderException e) {
162 throw new SignatureException(e);
163 }
catch (IllegalBlockSizeException e) {
164 throw new SignatureException(e);
165 }
catch (Exception e) {
166 throw new SignatureException(e);
168 IOUtils.closeQuietly(aIn);
BigInteger getModulus()
Definition: RSAPublicKey.java:43
SignatureAlgorithm getSignatureAlgorithm()
Definition: AbstractJwsSigner.java:37
RSAPublicKey rsaPublicKey
Definition: RSASigner.java:38
BigInteger getPublicExponent()
Definition: RSAPublicKey.java:51