Exemplo n.º 1
0
  public boolean WriteBytesToEncrStreamH(
      XStorage xStorage,
      String sStreamPath,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      String sPass,
      boolean bCommit) {
    // open substream element
    XStream xSubStream = null;
    try {
      XHierarchicalStorageAccess xHStorage =
          (XHierarchicalStorageAccess)
              UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
      if (xHStorage == null) {
        Error("The storage does not support hierarchical access!");
        return false;
      }

      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(
              sStreamPath, ElementModes.WRITE, sPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamPath + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamPath + "', exception : " + e + "!");
      return false;
    }

    if (!WriteBytesToStream(xSubStream, sStreamPath, sMediaType, bCompressed, pBytes)) return false;

    XTransactedObject xTransact =
        (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xSubStream);
    if (xTransact == null) {
      Error("Substream '" + sStreamPath + "', stream opened for writing must be transacted!");
      return false;
    }

    if (bCommit) {
      try {
        xTransact.commit();
      } catch (Exception e) {
        Error(
            "Can't commit storage after substream '"
                + sStreamPath
                + "' change, exception : "
                + e
                + "!");
        return false;
      }
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamPath)) return false;

    return true;
  }
Exemplo n.º 2
0
  public boolean cantOpenEncrStreamH(XStorage xStorage, String sPath, int nMode, String aPass) {
    // try to open the substream with specified mode must fail

    XHierarchicalStorageAccess xHStorage =
        (XHierarchicalStorageAccess)
            UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
    if (xHStorage == null) {
      Error("The storage does not support hierarchical access!");
      return false;
    }

    try {
      Object oDummyStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(sPath, nMode, aPass);
      Error("The trying to open substream '" + sPath + "' must fail!");
    } catch (WrongPasswordException wpe) {
      Error("The substream '" + sPath + "' must not exist!");
      return false;
    } catch (Exception e) {
      return true;
    }

    return false;
  }
Exemplo n.º 3
0
  public boolean checkEncrStreamH(
      XStorage xParentStorage, String sPath, String sMediaType, byte[] pBytes, String sPass) {
    // Important: a common password for any of parent storage should not be set or
    //            should be different from sPass
    XHierarchicalStorageAccess xHStorage =
        (XHierarchicalStorageAccess)
            UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xParentStorage);
    if (xHStorage == null) {
      Error("The storage does not support hierarchical access!");
      return false;
    }

    try {
      Object oSubStream = xHStorage.openStreamElementByHierarchicalName(sPath, ElementModes.READ);
      XStream xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      Error("Encrypted substream '" + sPath + "' was opened without password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sPath
              + "' without password: "******"!");
      return false;
    }

    String sWrongPass = "******";
    sWrongPass += sPass;
    try {
      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(
              sPath, ElementModes.READ, sWrongPass);
      XStream xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      Error("Encrypted substream '" + sPath + "' was opened with wrong password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sPath
              + "' with wrong password: "******"!");
      return false;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(sPath, ElementModes.READ, sPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sPath + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sPath + "', exception : " + e + "!");
      return false;
    }

    // encrypted streams will be compressed always, so after the storing this property is always
    // true,
    // although before the storing it can be set to false ( it is not always clear whether a stream
    // is encrypted
    // before the storing )
    boolean bResult = InternalCheckStream(xSubStream, sPath, sMediaType, true, pBytes, false);

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sPath)) return false;

    return bResult;
  }
Exemplo n.º 4
0
  public int ChangeStreamPassH(
      XStorage xStorage, String sPath, String sOldPass, String sNewPass, boolean bCommit) {
    // open substream element
    XHierarchicalStorageAccess xHStorage =
        (XHierarchicalStorageAccess)
            UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
    if (xHStorage == null) {
      Error("The storage does not support hierarchical access!");
      return 0;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(
              sPath, ElementModes.WRITE, sOldPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sPath + "'!");
        return 0;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sPath + "', exception : " + e + "!");
      return 0;
    }

    // change the password for the stream
    XEncryptionProtectedSource xStreamEncryption =
        (XEncryptionProtectedSource)
            UnoRuntime.queryInterface(XEncryptionProtectedSource.class, xSubStream);

    if (xStreamEncryption == null) {
      Message(
          "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!");
      return -1;
    }

    try {
      xStreamEncryption.setEncryptionPassword(sNewPass);
    } catch (Exception e) {
      Error("Can't change encryption key of the substream '" + sPath + "', exception:" + e);
      return 0;
    }

    XTransactedObject xTransact =
        (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xSubStream);
    if (xTransact == null) {
      Error("Substream '" + sPath + "', stream opened for writing must be transacted!");
      return 0;
    }

    if (bCommit) {
      try {
        xTransact.commit();
      } catch (Exception e) {
        Error(
            "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!");
        return 0;
      }
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sPath)) return 0;

    return 1;
  }