ClassCastException from RSAPublicKey to CaviumRSAPublicKey

2

I have an exception when performing a Cast, my code is as follows:

public static RSAPublicKey getKey(String filename)
            throws Exception {

            byte[] keyBytes = Files.readAllBytes(Paths.get(filename));

            X509EncodedKeySpec spec =
              new X509EncodedKeySpec(keyBytes);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            return (RSAPublicKey)kf.generatePublic(spec);
    }

Here I charge the public key and load it successfully. Then I have the cast as follows:

RSAPublicKey pubKey = (RSAPublicKey) GeneraKeysRsa.getKey(file);
CaviumRSAPublicKey key = (CaviumRSAPublicKey) pubKey;

I try to perform the cast but I get the following:

ClassCastException: sun.security.rsa.RSAPublicKeyImpl cannot be cast to com.cavium.key.CaviumRSAPublicKey

Looking for the documentation of both parties I found the following: Here we see that it inherits from PublicKey RSAPublicKey which in turn inherits of Key, then CaviumRSAPublicKey inherits from CaviumRSAKey and is in turn from CaviumKey, it is also inherited from Key then there should be no problem when performing the cast since a Key is being made with another Key.

UPDATE 1 The issue goes like this, I have a public RSA key, this is the charge to perform "x" encryption, but I must enter this in a HSM , which is Amazon-specific, for security reasons, so you do not have to load the key from X side, but consult it directly from the HSM, but this must be Cavium to store it in the HSM, that's why I try to perform the cast but I have not succeeded so far.

They could tell me why I get that exception. Thanks and regards.

    
asked by 5frags 19.10.2018 в 01:14
source

2 answers

2

What you are saying is not really possible, you should actually get a RSAPublicKey that is an instance of CaviumRSAPublicKey , if this is so then you can get / define properties:

    if(pubKey instanceof CaviumRSAPublicKey) {
            CaviumRSAPublicKey cavRSAPublicKey = (CaviumRSAPublicKey) pubKey;
      ...
      ...
      ...
   }

This is an example of how to get RSAPublicKey with provider "Cavium" :

   KeyPairGenerator keyPairGen;

    try {        
      //Create an instance of the provider Cavium.
      keyPairGen = KeyPairGenerator.getInstance("RSA", "Cavium");          
      //Generate the Key pair. 
      keyPairGen.initialize(new CaviumRSAKeyGenParameterSpec(keySize, exponent));
      KeyPair kp = keyPairGen.generateKeyPair();
      if (kp == null) {
        System.out.println("Error creating keypair!.");
      }

      //Generate RSA Key Pair.
      RSAPrivateKey privKey = (RSAPrivateKey) kp.getPrivate();
      RSAPublicKey pubKey = (RSAPublicKey) kp.getPublic();
      System.out.println("Generated RSA Key Pair with Cavium provider!.");


    if(pubKey instanceof CaviumRSAPublicKey) {
            CaviumRSAPublicKey cavRSAPublicKey = (CaviumRSAPublicKey) pubKey;

            // Save the public key handle. You'll need this to perform future encryption and decryption operations.
            System.out.println("Public Key Handle = " + cavRSAPublicKey.getHandle());

            // Get the public key label generated by the SDK.       
            System.out.println("Public Key Label = " + cavRSAPublicKey.getLabel());

            // Get the Extractable property of the public key.
            System.out.println("Is Public Key Extractable = " +cavRSAPublicKey.isExtractable());

            // Get the Persistent property of the public key.
            System.out.println("Is Public Key Persistent = " + cavRSAPublicKey.isPersistent());                      

            // Verify the key type and size.
            System.out.println("Public Key Algorithm : " + cavRSAPublicKey.getAlgorithm());
            System.out.println("Public Key Size : " + cavRSAPublicKey.getSize());
          }

      } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
         System.out.println("NoSuchAlgorithmException | NoSuchProviderException" + e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
         System.out.println("InvalidAlgorithmParameterException" + e.getMessage());

    }
    
answered by 19.10.2018 в 01:56
1

The "is-a" relationship goes from subclasses to superclasses, but not the other way around (and certainly not naturally).

Let's give an example service: Number as superclass, and Float e Integer as subclasses.

An instance of Integer will always be an instance of Number , since Integer is a subclass of Number . But that instance is not a class of Float . You do not have to have the methods defined in Float , and you will always use the methods of Integer (either defined in Integer , or inherited from Number or Integer ).

Setting the example in nature, Gato and Perro are subclasses of Mamífero . You can assign a cat to a variable type Mamífero , because it is. But you can not assign a cat to a variable of type Perro , because it is not. Any cat is a mammal, but not all mammals are cats.

    
answered by 19.10.2018 в 01:36