Esempio n. 1
0
 /**
  * Return a new instance of task result represented by a task id and its exception.
  *
  * @param id the identification of the task that send this result.
  * @param exception the exception that occurred in the task.
  * @param output the output of the task.
  * @param execDuration the execution duration of the task itself
  * @param propagatedProperties the map of all properties that has been propagated through
  *     Exporter.propagateProperty
  */
 public TaskResultImpl(
     TaskId id,
     Throwable exception,
     TaskLogs output,
     long execDuration,
     Map<String, BigString> propagatedProperties) {
   this(id, output);
   this.taskDuration = execDuration;
   this.exception = exception;
   this.propagatedProperties = propagatedProperties;
   try {
     // try to serialize the user exception
     this.serializedException = ObjectToByteConverter.ObjectStream.convert(exception);
   } catch (IOException ioe2) {
     // cannot serialize the exception
     logger.error("", ioe2);
     try {
       // serialize a NotSerializableException with the cause message
       this.serializedException =
           ObjectToByteConverter.ObjectStream.convert(
               new NotSerializableException(ioe2.getMessage()));
     } catch (IOException ioe3) {
       // this should not append as the NotSerializableException is serializable and
       // the given argument is a string (also serializable)
       logger.error("", ioe3);
     }
   }
 }
  /**
   * Creates new encrypted credentials
   *
   * <p>Encrypts the message '<code>login</code>:<code>password</code>' using the public key <code>
   * pubKey</code> and <code>cipher</code> and store it in a new Credentials object.
   *
   * @see KeyPairUtil#encrypt(String, String, String, byte[])
   * @param login the login to encrypt
   * @param password the corresponding password to encrypt
   * @param pubKey public key used for encryption
   * @param cipher cipher parameters: combination of transformations
   * @return the Credentials object containing the encrypted data
   * @throws KeyException key generation or encryption failed
   */
  @Deprecated
  public static Credentials createCredentials(
      String login, String password, byte[] datakey, PublicKey pubKey, String cipher)
      throws KeyException {

    CredData cc = new CredData();
    cc.setLogin(CredData.parseLogin(login));
    cc.setDomain(CredData.parseDomain(login));
    cc.setPassword(password);
    cc.setKey(datakey);

    // serialize clear credentials to byte array
    byte[] clearCred = null;
    try {
      clearCred = ObjectToByteConverter.ObjectStream.convert(cc);
    } catch (IOException e1) {
      throw new KeyException(e1.getMessage());
    }

    int size = -1;
    if (pubKey instanceof java.security.interfaces.RSAPublicKey) {
      size = ((RSAPublicKey) pubKey).getModulus().bitLength();
    } else if (pubKey instanceof java.security.interfaces.DSAPublicKey) {
      size = ((DSAPublicKey) pubKey).getParams().getP().bitLength();
    } else if (pubKey instanceof javax.crypto.interfaces.DHPublicKey) {
      size = ((DHPublicKey) pubKey).getParams().getP().bitLength();
    }

    // generate symmetric key
    SecretKey aesKey = KeyUtil.generateKey(AES_ALGO, AES_KEYSIZE);

    byte[] encData = null;
    byte[] encAes = null;

    // encrypt AES key with public RSA key
    try {
      encAes = KeyPairUtil.encrypt(pubKey, size, cipher, aesKey.getEncoded());
    } catch (KeyException e) {
      throw new KeyException("Symmetric key encryption failed", e);
    }

    // encrypt clear credentials with AES key
    try {
      encData = KeyUtil.encrypt(aesKey, AES_CIPHER, clearCred);
    } catch (KeyException e) {
      throw new KeyException("Message encryption failed", e);
    }

    Credentials cred = new Credentials(pubKey.getAlgorithm(), size, cipher, encAes, encData);
    return cred;
  }