Search in sources :

Example 1 with ByteArrayOutputStream

use of com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream in project cyberduck by iterate-ch.

the class CryptoVault method create.

public synchronized Path create(final Session<?> session, final VaultCredentials credentials, final PasswordStore keychain, final int version) throws BackgroundException {
    final Host bookmark = session.getHost();
    if (credentials.isSaved()) {
        try {
            keychain.addPassword(String.format("Cryptomator Passphrase (%s)", bookmark.getCredentials().getUsername()), new DefaultUrlProvider(bookmark).toUrl(masterkey).find(DescriptiveUrl.Type.provider).getUrl(), credentials.getPassword());
        } catch (LocalAccessDeniedException e) {
            log.error(String.format("Failure %s saving credentials for %s in password store", e, bookmark));
        }
    }
    final String passphrase = credentials.getPassword();
    final ByteArrayOutputStream mkArray = new ByteArrayOutputStream();
    final Masterkey mk = Masterkey.generate(FastSecureRandomProvider.get().provide());
    final MasterkeyFileAccess access = new MasterkeyFileAccess(pepper, FastSecureRandomProvider.get().provide());
    final MasterkeyFile masterkeyFile;
    try {
        access.persist(mk, mkArray, passphrase, version);
        masterkeyFile = MasterkeyFile.read(new StringReader(new String(mkArray.toByteArray(), StandardCharsets.UTF_8)));
    } catch (IOException e) {
        throw new VaultException("Failure creating master key", e);
    }
    if (log.isDebugEnabled()) {
        log.debug(String.format("Write master key to %s", masterkey));
    }
    // Obtain non encrypted directory writer
    final Directory directory = session._getFeature(Directory.class);
    final TransferStatus status = new TransferStatus();
    final Encryption encryption = session.getFeature(Encryption.class);
    if (encryption != null) {
        status.setEncryption(encryption.getDefault(home));
    }
    final Path vault = directory.mkdir(home, status);
    new ContentWriter(session).write(masterkey, mkArray.toByteArray());
    if (VAULT_VERSION == version) {
        // Create vaultconfig.cryptomator
        final Algorithm algorithm = Algorithm.HMAC256(mk.getEncoded());
        final String conf = JWT.create().withJWTId(new UUIDRandomStringService().random()).withKeyId(String.format("masterkeyfile:%s", masterkey.getName())).withClaim("format", version).withClaim("cipherCombo", CryptorProvider.Scheme.SIV_CTRMAC.toString()).withClaim("shorteningThreshold", CryptoFilenameV7Provider.NAME_SHORTENING_THRESHOLD).sign(algorithm);
        new ContentWriter(session).write(config, conf.getBytes(StandardCharsets.US_ASCII));
    }
    this.open(masterkeyFile, passphrase);
    final Path secondLevel = directoryProvider.toEncrypted(session, home.attributes().getDirectoryId(), home);
    final Path firstLevel = secondLevel.getParent();
    final Path dataDir = firstLevel.getParent();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Create vault root directory at %s", secondLevel));
    }
    directory.mkdir(dataDir, status);
    directory.mkdir(firstLevel, status);
    directory.mkdir(secondLevel, status);
    return vault;
}
Also used : VaultException(ch.cyberduck.core.vault.VaultException) MasterkeyFileAccess(org.cryptomator.cryptolib.common.MasterkeyFileAccess) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Algorithm(com.auth0.jwt.algorithms.Algorithm) DefaultUrlProvider(ch.cyberduck.core.shared.DefaultUrlProvider) Masterkey(org.cryptomator.cryptolib.api.Masterkey) StringReader(java.io.StringReader) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) MasterkeyFile(org.cryptomator.cryptolib.common.MasterkeyFile) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException)

Example 2 with ByteArrayOutputStream

use of com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream in project eblocker by eblocker.

the class OpenVpnClientConfigurationService method readFileWithNewLine.

private byte[] readFileWithNewLine(Path path) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        for (String line : Files.readAllLines(path)) {
            baos.write(line.trim().getBytes());
            baos.write(newLine.getBytes());
        }
    } catch (IOException e) {
        log.debug("Error parsing file", e);
    } finally {
        baos.close();
    }
    return baos.toByteArray();
}
Also used : ByteArrayOutputStream(com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with ByteArrayOutputStream

use of com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream in project eblocker by eblocker.

the class OpenVpnClientConfigurationService method getOvpnProfile.

public byte[] getOvpnProfile(String deviceName, OperatingSystemType type) throws IOException {
    Path path;
    if (type == OperatingSystemType.WINDOWS) {
        path = FileSystems.getDefault().getPath(windowsClientTemplatePath);
        newLine = "\r\n";
    } else if (type == OperatingSystemType.MAC) {
        path = FileSystems.getDefault().getPath(macosClientTemplatePath);
        newLine = "\n";
    } else {
        path = FileSystems.getDefault().getPath(unixClientTemplatePath);
        newLine = "\n";
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // ** use user defined port with fallback to 1194 to avoid errors
    Integer mappedPortInRouter = openVpnServerService.getOpenVpnMappedPort();
    String remoteString = String.format("remote %s %d%s%s", openVpnServerService.getOpenVpnServerHost(), mappedPortInRouter, newLine, newLine);
    try {
        outputStream.write(remoteString.getBytes());
        outputStream.write(readFileWithNewLine(path));
        outputStream.write(createTag(extractLinesFromFile(String.format("%s/ca.crt", openVpnServerPath)), "ca"));
        outputStream.write(createTag(extractLinesFromFile(String.format("%s/easy-rsa/keys/%s.crt", openVpnServerPath, deviceName)), "cert"));
        outputStream.write(createTag(extractLinesFromFile(String.format("%s/easy-rsa/keys/%s.key", openVpnServerPath, deviceName)), "key"));
        outputStream.write(createTag(extractLinesFromFile(String.format("%s/ta.key", openVpnServerPath)), "tls-auth"));
    } catch (Exception e) {
        log.error("Error creating ovpn-profile.", e);
        throw (e);
    } finally {
        outputStream.close();
    }
    outputStream.close();
    return outputStream.toByteArray();
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with ByteArrayOutputStream

use of com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream in project eblocker by eblocker.

the class OpenVpnClientConfigurationService method createTag.

private byte[] createTag(List<String> lines, String tag) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeBytes(String.format("<%s>%s", tag, newLine));
        for (String element : lines) {
            out.writeBytes(element);
        }
        out.writeBytes(String.format("</%s>%s", tag, newLine));
    } catch (IOException e) {
        log.debug("Error parsing file", e);
    }
    return baos.toByteArray();
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 ByteArrayOutputStream (com.auth0.jwt.internal.org.apache.commons.io.output.ByteArrayOutputStream)3 LocalAccessDeniedException (ch.cyberduck.core.exception.LocalAccessDeniedException)1 DefaultUrlProvider (ch.cyberduck.core.shared.DefaultUrlProvider)1 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)1 VaultException (ch.cyberduck.core.vault.VaultException)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 Masterkey (org.cryptomator.cryptolib.api.Masterkey)1 MasterkeyFile (org.cryptomator.cryptolib.common.MasterkeyFile)1 MasterkeyFileAccess (org.cryptomator.cryptolib.common.MasterkeyFileAccess)1