diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java | 187 | ||||
-rw-r--r-- | src/main/java/org/uic/barcode/utils/SecurityUtils.java | 203 |
2 files changed, 328 insertions, 62 deletions
diff --git a/src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java b/src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java index a3154f3..28f90e6 100644 --- a/src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java +++ b/src/main/java/org/uic/barcode/utils/AlgorithmNameResolver.java @@ -3,98 +3,161 @@ package org.uic.barcode.utils; import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
+import java.util.HashMap;
+/**
+ * The Class AlgorithmNameResolver.
+ */
public class AlgorithmNameResolver {
+ /** The Constant TYPE_KEY_GENERATOR_ALG. */
public static final String TYPE_KEY_GENERATOR_ALG = "KeyPairGenerator";
+
+ /** The Constant TYPE_SIGNATURE_ALG. */
public static final String TYPE_SIGNATURE_ALG = "Signature";
+ /** The map. */
+ private static HashMap<String, HashMap<String, String>> map = new HashMap<String, HashMap<String, String>>();
+
+ /**
+ * Adds an entry for a mapping of algorithm type and oid to an algorithm name
+ *
+ * @param type the algorithm type
+ * @param oid the algorithm OID
+ * @param name the algorithm name
+ */
+ public static void addMap (String type, String oid, String name) {
+
+ if (map.get(type) == null) {
+ HashMap<String,String> list = new HashMap<String , String>();
+ list.put(oid, name);
+ map.put(type, list);
+ } else {
+ map.get(type).put(oid, name);
+ }
+ }
+
+ /**
+ * Gets the signature algorithm name.
+ *
+ * @param oid the oid
+ * @return the signature algorithm name
+ * @throws Exception the exception
+ */
public static String getSignatureAlgorithmName (String oid) throws Exception {
- Provider[] provs = Security.getProviders();
- for (Provider prov : provs) {
- Service service = prov.getService(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid);
- if (service != null) {
- return service.getAlgorithm();
- }
- }
- return null;
+ return getSignatureAlgorithmName (oid, null);
}
+ /**
+ * Gets the signature algorithm name.
+ *
+ * @param oid the oid
+ * @param provider the provider
+ * @return the signature algorithm name
+ * @throws Exception the exception
+ */
public static String getSignatureAlgorithmName (String oid, Provider provider) throws Exception {
+ return getAlgorithmName(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid,provider);
+
+ }
+
+ /**
+ * Gets the name.
+ *
+ * @param type the type
+ * @param oid the oid
+ * @return the name
+ * @throws Exception the exception
+ */
+ public static String getName (String type, String oid) throws Exception {
+
+ return getAlgorithmName(type,oid,null);
+
+ }
+
+
+ /**
+ * Gets the name.
+ *
+ * @param type the type
+ * @param oid the oid
+ * @param provider the provider
+ * @return the name
+ * @throws Exception the exception
+ */
+ public static String getName(String type, String oid, Provider provider) throws Exception {
+
+ return getAlgorithmName(type,oid,provider);
+
+ }
+
+
+
+ /**
+ * Gets the algorithm name.
+ *
+ * @param type the type
+ * @param oid the oid
+ * @param provider the provider
+ * @return the algorithm name
+ * @throws Exception the exception
+ */
+ public static String getAlgorithmName (String type, String oid, Provider provider) throws Exception {
+
+ String name = null;
+
if (provider != null) {
- Service service = provider.getService(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid);
- return service.getAlgorithm();
+ Service service = provider.getService(type,oid);
+ if (service != null) {
+ name = service.getAlgorithm();
+ }
+
+ if (name != null && name.length() > 0) {
+ return name;
+ }
}
Provider[] provs = Security.getProviders();
for (Provider prov : provs) {
- Service service = prov.getService(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid);
+ Service service = prov.getService(type,oid);
if (service != null) {
- return service.getAlgorithm();
+ name = service.getAlgorithm();
+ }
+ if (name != null && name.length() > 0) {
+ return name;
}
}
- return null;
-
- }
-
-
- public static String getName (String type, String oid) throws Exception {
-
- Provider[] provs = Security.getProviders();
- for (Provider prov : provs) {
- Service service = prov.getService(type,oid);
- if (service != null) {
- return service.getAlgorithm();
- }
- }
-
- if (oid.startsWith("1.2.840.10045")) {
+
+ if (map.get(type) != null) {
+ if (map.get(type).get(oid) != null) {
+ return map.get(type).get(oid);
+ }
+ }
+
+
+ //fallback if the provider did not implement OIDs
+ if (oid.startsWith("1.2.840.10045.4")) {
return "ECDSA";
- } else if (oid.startsWith("1.2.840.10040")) {
- return "DSA";
- }
-
- return null;
-
- }
-
-
- public static String getName(String type, String oid, Provider provider) throws Exception {
-
- Service service = null;
- if (provider == null) {
-
- Provider[] provs = Security.getProviders();
- for (Provider prov : provs) {
- service = prov.getService(type,oid);
- }
-
- } else {
- service = provider.getService(type,oid);
- }
-
-
-
- if (service != null) {
- return service.getAlgorithm();
- }
-
- if (oid.startsWith("1.2.840.10045")) {
+ } else if (oid.startsWith("1.2.840.10045.3")) {
+ return "EC";
+ } else if (oid.startsWith("1.2.840.10045")) {
return "ECDSA";
} else if (oid.startsWith("1.2.840.10040")) {
return "DSA";
}
-
- return null;
-
- }
+
+ return null;
+
+ }
+
+
diff --git a/src/main/java/org/uic/barcode/utils/SecurityUtils.java b/src/main/java/org/uic/barcode/utils/SecurityUtils.java new file mode 100644 index 0000000..1fcc18a --- /dev/null +++ b/src/main/java/org/uic/barcode/utils/SecurityUtils.java @@ -0,0 +1,203 @@ +package org.uic.barcode.utils;
+
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.Provider.Service;
+import java.security.PublicKey;
+import java.security.Security;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+/**
+ * The Class SecurityUtils.
+ */
+public class SecurityUtils {
+
+ /**
+ * Find provider by public key.
+ *
+ * @param algorithmOid the algorithm oid used to generate the key
+ * @param keyBytes the encoded bytes of the public key
+ * @return the provider
+ */
+ public static Provider findPublicKeyProvider(String keyAlgorithmOid, byte[] keyBytes) {
+
+
+ X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
+
+ String name;
+ try {
+ name = AlgorithmNameResolver.getAlgorithmName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, null);
+ } catch (Exception e2) {
+ return null;
+ }
+
+ KeyFactory keyFactory = null;
+
+ Provider[] provs = Security.getProviders();
+ for (Provider provider : provs) {
+ try {
+ keyFactory = KeyFactory.getInstance(name,provider);
+ } catch (NoSuchAlgorithmException e1) {
+ //try next
+ }
+ if (keyFactory != null) {
+ try {
+ keyFactory.generatePublic(keySpec);
+ return provider;
+ } catch (Exception e) {
+ provider = null;
+ //try next
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Find private key provider.
+ *
+ * @param key the private key
+ * @return the provider
+ */
+ public static Provider findPrivateKeyProvider(PrivateKey key) {
+
+ String name = key.getAlgorithm();
+ byte[] keyBytes = key.getEncoded();
+
+
+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
+
+
+ KeyFactory keyFactory = null;
+
+ Provider[] provs = Security.getProviders();
+ for (Provider provider : provs) {
+ try {
+ keyFactory = KeyFactory.getInstance(name,provider);
+ } catch (NoSuchAlgorithmException e1) {
+ //try next
+ }
+ if (keyFactory != null) {
+ try {
+ keyFactory.generatePrivate(keySpec);
+ return provider;
+ } catch (Exception e) {
+ provider = null;
+ //try next
+ }
+ }
+ }
+
+ return null;
+ }
+
+
+
+ /**
+ * Convert.
+ *
+ * @param key the key
+ * @param provider the provider
+ * @return the public key
+ */
+ public static PublicKey convert(PublicKey key, Provider provider) {
+
+ PublicKey publicKey;
+ KeyFactory keyFactory = null;
+
+ try {
+ if (key.getAlgorithm() != null && key.getAlgorithm().toUpperCase().contains("EC") ) {
+ keyFactory = KeyFactory.getInstance("EC",provider);
+ } else if (key.getAlgorithm() != null && key.getAlgorithm().length() > 0 ) {
+ keyFactory = KeyFactory.getInstance("DSA",provider);
+ } else {
+ return key;
+ }
+ publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(key.getEncoded()));
+ } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
+ return key;
+ }
+
+ return publicKey;
+
+
+ }
+
+
+ /**
+ * Convert.
+ *
+ * @param key the key
+ * @param provider the provider
+ * @return the private key
+ */
+ public static PrivateKey convert(PrivateKey key, Provider provider) {
+
+ PrivateKey privateKey;
+ KeyFactory keyFactory = null;
+
+ try {
+ if (key.getAlgorithm() != null && key.getAlgorithm().toUpperCase().contains("EC") ) {
+ keyFactory = KeyFactory.getInstance("EC",provider);
+ } else if (key.getAlgorithm() != null && key.getAlgorithm().length() > 0 ) {
+ keyFactory = KeyFactory.getInstance("DSA",provider);
+ } else {
+ return key;
+ }
+ privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded()));
+ } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
+ return key;
+ }
+
+ return privateKey;
+
+
+ }
+
+ /**
+ * Find signature provider.
+ *
+ * @param encoded the encoded
+ * @param oid the oid
+ * @return the provider
+ */
+ public static Provider findSignatureProvider(byte[] encoded, String oid) {
+
+ KeyFactory keyFactory = null;
+ String signatureAlgorithmName = null;
+
+ Provider[] provs = Security.getProviders();
+ for (Provider provider : provs) {
+ try {
+ Service service = provider.getService(AlgorithmNameResolver.TYPE_SIGNATURE_ALG, oid);
+ if (service != null) {
+ signatureAlgorithmName = service.getAlgorithm();
+ if (signatureAlgorithmName != null && signatureAlgorithmName.length() > 0) {
+ if (signatureAlgorithmName.toUpperCase().contains("EC") ) {
+ keyFactory = KeyFactory.getInstance("EC",provider);
+ } else {
+ keyFactory = KeyFactory.getInstance("DSA",provider);
+ }
+ if (keyFactory != null) {
+ X509EncodedKeySpec spec = new X509EncodedKeySpec(encoded);
+ //try to encode the key
+ keyFactory.generatePublic(spec);
+ }
+ }
+ }
+ } catch (Exception e1) {
+ keyFactory = null;
+ }
+ if (keyFactory != null) {
+ return keyFactory.getProvider();
+ }
+ }
+
+ return null;
+ }
+}
|