All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.crypto.PEM Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common.crypto;

import com.unbound.common.Base64;
import com.unbound.common.STR;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Scanner;

public class PEM
{
  private Scanner reader;
  private StringBuilder writer;

  public static final int X509 = 1;
  public static final int PUBLIC_KEY = 2;
  public static final int RSA_PRIVATE_KEY = 3;
  public static final int RSA_PUBLIC_KEY = 4;
  public static final int PKCS8 = 5;
  public static final int PKCS8INF = 6;
  public static final int EC_PRIVATE_KEY = 7;
  public static final int PKCS10 = 8;

  private static final String PEM_STRING_X509_OLD = "X509 CERTIFICATE";
  private static final String PEM_STRING_X509 =  "CERTIFICATE";
  private static final String PEM_STRING_X509_TRUSTED = "TRUSTED CERTIFICATE";
  private static final String PEM_STRING_PUBLIC = "PUBLIC KEY";
  private static final String PEM_STRING_RSA_PRIVATE_KEY = "RSA PRIVATE KEY";
  private static final String PEM_STRING_RSA_PUBLIC_KEY = "RSA PUBLIC KEY";
  private static final String PEM_STRING_PKCS8 = "ENCRYPTED PRIVATE KEY";
  private static final String PEM_STRING_PKCS8INF = "PRIVATE KEY";
  private static final String PEM_STRING_EC_PRIVATE_KEY = "EC PRIVATE KEY";
  private static final String PEM_STRING_X509_REQ = "CERTIFICATE REQUEST";

  private static String headerString(int code)
  {
    switch (code)
    {
      case X509:            return PEM_STRING_X509;
      case PUBLIC_KEY:      return PEM_STRING_PUBLIC;
      case RSA_PRIVATE_KEY: return PEM_STRING_RSA_PRIVATE_KEY;
      case RSA_PUBLIC_KEY:  return PEM_STRING_RSA_PUBLIC_KEY;
      case PKCS8:           return PEM_STRING_PKCS8;
      case PKCS8INF:        return PEM_STRING_PKCS8INF;
      case EC_PRIVATE_KEY:  return PEM_STRING_EC_PRIVATE_KEY;
      case PKCS10:          return PEM_STRING_X509_REQ;
    }
    return null;
  }

  private int headerCode(String header)
  {
    if (header.equals(PEM_STRING_X509_OLD) || header.equals(PEM_STRING_X509) || header.equals(PEM_STRING_X509_TRUSTED)) return X509;
    if (header.equals(PEM_STRING_PUBLIC)) return PUBLIC_KEY;
    if (header.equals(PEM_STRING_RSA_PRIVATE_KEY)) return RSA_PRIVATE_KEY;
    if (header.equals(PEM_STRING_RSA_PUBLIC_KEY)) return RSA_PUBLIC_KEY;
    if (header.equals(PEM_STRING_PKCS8)) return PKCS8;
    if (header.equals(PEM_STRING_PKCS8INF)) return PKCS8INF;
    if (header.equals(PEM_STRING_EC_PRIVATE_KEY)) return EC_PRIVATE_KEY;
    if (header.equals(PEM_STRING_X509_REQ)) return PKCS10;
    throw new IllegalArgumentException("PEM header parser error");
  }

  public PEM(byte[] in)
  {
    String s = STR.utf8(in);
    reader = new Scanner(new ByteArrayInputStream(in));
  }

  public PEM()
  {
    writer = new StringBuilder();
  }


  public PEM append(int code, byte[] in) throws IOException
  {
    String header = headerString(code);
    writer.append("-----BEGIN "); writer.append(header); writer.append("-----\n");
    writer.append(Base64.encode(in, true));
    writer.append("-----END "); writer.append(header); writer.append("-----\n");
    return this;
  }

  public static byte[] create(int code, byte[] in) throws IOException
  {
    return new PEM().append(code, in).end();
  }

  byte[] end()
  {
    return STR.utf8(writer.toString());
  }

  public byte[] getNext()
  {
    StringBuilder sb = new StringBuilder();
    for (;;)
    {
      String s = reader.nextLine().trim();
      if (s.startsWith("-----END ") && s.endsWith("-----")) break;
      sb.append(s);
    }
    return Base64.decode(sb.toString());
  }

  public int getNextHeader()
  {
    String header = "";
    while (header.isEmpty()) header = reader.nextLine().trim();
    if (header.startsWith("-----BEGIN ") && header.endsWith("-----"))
    {
      header = header.substring(11, header.length()-5);
      return headerCode(header);
    }
    throw new IllegalArgumentException("PEM header parser error");
  }

  public static byte[] get(int code, byte[] in)
  {
    PEM pem = new PEM(in);
    if (code!=pem.getNextHeader()) throw new IllegalArgumentException("PEM header parser error");
    return pem.getNext();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy