34 import java.security.KeyStoreException; 35 import java.security.NoSuchAlgorithmException; 36 import java.security.ProviderException; 37 import java.security.MessageDigest; 38 import java.security.SecureRandom; 39 import java.security.Signature; 40 import java.security.SignatureSpi; 41 import java.security.SignatureException; 42 import java.math.BigInteger; 43 44 import sun.security.rsa.RSAKeyFactory; 45 46 /** 47 * RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding. 48 * 49 * Objects should be instantiated by calling Signature.getInstance() using the 50 * following algorithm names: 51 * 52 * . "NONEwithRSA" 53 * . "SHA1withRSA" 54 * . "SHA256withRSA" 55 * . "SHA384withRSA" 56 * . "SHA512withRSA" 57 * . "MD5withRSA" 58 * . "MD2withRSA" 59 * 60 * NOTE: RSA keys must be at least 512 bits long. 61 * 62 * NOTE: NONEwithRSA must be supplied with a pre-computed message digest. 63 * Only the following digest algorithms are supported: MD5, SHA-1, 64 * SHA-256, SHA-384, SHA-512 and a special-purpose digest algorithm 65 * which is a concatenation of SHA-1 and MD5 digests. 66 * 67 * @since 1.6 68 * @author Stanley Man-Kit Ho 69 */ 70 abstract class RSASignature extends java.security.SignatureSpi 71 { 72 // message digest implementation we use 73 private final MessageDigest messageDigest; 74 75 // message digest name 76 private String messageDigestAlgorithm; 77 78 // flag indicating whether the digest has been reset 79 private boolean needsReset; 80 81 // the signing key 82 private Key privateKey = null; 83 84 // the verification key 85 private Key publicKey = null; 167 // Returns the precomputed message digest value. 168 @Override 169 protected byte[] getDigestValue() throws SignatureException { 170 if (offset > RAW_RSA_MAX) { 171 throw new SignatureException("Message digest is too long"); 172 } 173 174 // Determine the digest algorithm from the digest length 175 if (offset == 20) { 176 setDigestName("SHA1"); 177 } else if (offset == 36) { 178 setDigestName("SHA1+MD5"); 179 } else if (offset == 32) { 180 setDigestName("SHA-256"); 181 } else if (offset == 48) { 182 setDigestName("SHA-384"); 183 } else if (offset == 64) { 184 setDigestName("SHA-512"); 185 } else if (offset == 16) { 186 setDigestName("MD5"); 187 } else { 188 throw new SignatureException( 189 "Message digest length is not supported"); 190 } 191 192 byte[] result = new byte[offset]; 193 System.arraycopy(precomputedDigest, 0, result, 0, offset); 194 offset = 0; 195 196 return result; 197 } 198 } 199 200 public static final class SHA1 extends RSASignature { 201 public SHA1() { 202 super("SHA1"); 203 } 204 } 205 206 public static final class SHA256 extends RSASignature { 207 public SHA256() { 208 super("SHA-256"); 209 } 210 } 211 212 public static final class SHA384 extends RSASignature { 213 public SHA384() { 214 super("SHA-384"); 215 } 216 } 217 218 public static final class SHA512 extends RSASignature { 219 public SHA512() { 220 super("SHA-512"); 221 } 222 } | 34 import java.security.KeyStoreException; 35 import java.security.NoSuchAlgorithmException; 36 import java.security.ProviderException; 37 import java.security.MessageDigest; 38 import java.security.SecureRandom; 39 import java.security.Signature; 40 import java.security.SignatureSpi; 41 import java.security.SignatureException; 42 import java.math.BigInteger; 43 44 import sun.security.rsa.RSAKeyFactory; 45 46 /** 47 * RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding. 48 * 49 * Objects should be instantiated by calling Signature.getInstance() using the 50 * following algorithm names: 51 * 52 * . "NONEwithRSA" 53 * . "SHA1withRSA" 54 * . "SHA224withRSA" 55 * . "SHA256withRSA" 56 * . "SHA384withRSA" 57 * . "SHA512withRSA" 58 * . "MD5withRSA" 59 * . "MD2withRSA" 60 * 61 * NOTE: RSA keys must be at least 512 bits long. 62 * 63 * NOTE: NONEwithRSA must be supplied with a pre-computed message digest. 64 * Only the following digest algorithms are supported: MD5, SHA-1, 65 * SHA-224, SHA-256, SHA-384, SHA-512 and a special-purpose digest 66 * algorithm which is a concatenation of SHA-1 and MD5 digests. 67 * 68 * @since 1.6 69 * @author Stanley Man-Kit Ho 70 */ 71 abstract class RSASignature extends java.security.SignatureSpi 72 { 73 // message digest implementation we use 74 private final MessageDigest messageDigest; 75 76 // message digest name 77 private String messageDigestAlgorithm; 78 79 // flag indicating whether the digest has been reset 80 private boolean needsReset; 81 82 // the signing key 83 private Key privateKey = null; 84 85 // the verification key 86 private Key publicKey = null; 168 // Returns the precomputed message digest value. 169 @Override 170 protected byte[] getDigestValue() throws SignatureException { 171 if (offset > RAW_RSA_MAX) { 172 throw new SignatureException("Message digest is too long"); 173 } 174 175 // Determine the digest algorithm from the digest length 176 if (offset == 20) { 177 setDigestName("SHA1"); 178 } else if (offset == 36) { 179 setDigestName("SHA1+MD5"); 180 } else if (offset == 32) { 181 setDigestName("SHA-256"); 182 } else if (offset == 48) { 183 setDigestName("SHA-384"); 184 } else if (offset == 64) { 185 setDigestName("SHA-512"); 186 } else if (offset == 16) { 187 setDigestName("MD5"); 188 } else if (offset == 28) { 189 setDigestName("SHA-224"); 190 } else { 191 throw new SignatureException( 192 "Message digest length is not supported"); 193 } 194 195 byte[] result = new byte[offset]; 196 System.arraycopy(precomputedDigest, 0, result, 0, offset); 197 offset = 0; 198 199 return result; 200 } 201 } 202 203 public static final class SHA1 extends RSASignature { 204 public SHA1() { 205 super("SHA1"); 206 } 207 } 208 209 public static final class SHA224 extends RSASignature { 210 public SHA224() { 211 super("SHA-224"); 212 } 213 } 214 215 public static final class SHA256 extends RSASignature { 216 public SHA256() { 217 super("SHA-256"); 218 } 219 } 220 221 public static final class SHA384 extends RSASignature { 222 public SHA384() { 223 super("SHA-384"); 224 } 225 } 226 227 public static final class SHA512 extends RSASignature { 228 public SHA512() { 229 super("SHA-512"); 230 } 231 } |