Package br.net.woodstock.rockframework.security.crypt.impl

Source Code of br.net.woodstock.rockframework.security.crypt.impl.AsynchronousOpenSSLCrypterReader

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.security.crypt.impl;

import java.io.InputStream;
import java.io.StringReader;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Scanner;

import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;

import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.security.crypt.CrypterException;
import br.net.woodstock.rockframework.security.crypt.CrypterReader;
import br.net.woodstock.rockframework.security.crypt.KeyPairType;
import br.net.woodstock.rockframework.security.crypt.util.Keys;

public class AsynchronousOpenSSLCrypterReader implements CrypterReader<AsynchronousCrypter> {

  public AsynchronousOpenSSLCrypterReader() {
    super();
  }

  @Override
  public AsynchronousCrypter read(final InputStream inputStream) {
    Assert.notNull(inputStream, "inputStream");
    try {
      String algorithm = null;

      PrivateKey privateKey = null;
      PublicKey publicKey = null;

      Scanner scanner = new Scanner(inputStream);
      StringBuilder privateKeyText = new StringBuilder();
      StringBuilder publicKeyText = new StringBuilder();

      StringBuilder currentBulder = null;

      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if ((!line.startsWith(CrypterIOHelper.SEPARATOR)) && (currentBulder != null)) {
          currentBulder.append(line);
          currentBulder.append(CrypterIOHelper.NEW_LINE);
        } else if ((line.indexOf(CrypterIOHelper.PRIVATE_KEY_TEXT) != -1) && (line.indexOf(CrypterIOHelper.BEGIN) != -1)) {
          algorithm = CrypterIOHelper.getAlgorithm(line);
          currentBulder = privateKeyText;
          currentBulder.append(line);
          currentBulder.append(CrypterIOHelper.NEW_LINE);
        } else if ((line.indexOf(CrypterIOHelper.PUBLIC_KEY_TEXT) != -1) && (line.indexOf(CrypterIOHelper.BEGIN) != -1)) {
          algorithm = CrypterIOHelper.getAlgorithm(line);
          currentBulder = publicKeyText;
          currentBulder.append(line);
          currentBulder.append(CrypterIOHelper.NEW_LINE);
        } else if (line.indexOf(CrypterIOHelper.END) != -1) {
          currentBulder.append(line);
          currentBulder.append(CrypterIOHelper.NEW_LINE);
          currentBulder = null;
        }
      }
     
      scanner.close();

      if (algorithm == null) {
        throw new IllegalStateException("Could not read key pair algorithm");
      }

      if (privateKeyText.length() > 0) {
        PemReader reader = new PemReader(new StringReader(privateKeyText.toString()));
        PemObject obj = reader.readPemObject();
        reader.close();

        PrivateKeyInfo privateKeyInfo = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, null), RSAPrivateKey.getInstance(obj.getContent()));
        byte[] encoded = privateKeyInfo.getEncoded();

        privateKey = Keys.getPrivateKeyFromPKCS8File(encoded, KeyPairType.RSA);
      }

      if (publicKeyText.length() > 0) {
        PemReader reader = new PemReader(new StringReader(publicKeyText.toString()));
        PemObject obj = reader.readPemObject();
        reader.close();

        byte[] publicKeyBytes = obj.getContent();
        publicKey = Keys.getPublicKeyFromX509File(publicKeyBytes, KeyPairType.RSA);
      }

      KeyPair keyPair = new KeyPair(publicKey, privateKey);
      AsynchronousCrypter crypter = new AsynchronousCrypter(keyPair);
      return crypter;
    } catch (Exception e) {
      throw new CrypterException(e);
    }
  }
}
TOP

Related Classes of br.net.woodstock.rockframework.security.crypt.impl.AsynchronousOpenSSLCrypterReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.