private static void writePemEncrypted( BufferedWriter out, String pemHeader, byte[] encoding, CipherSpec cipher, char[] passwd) throws IOException { Cipher c = cipher.getCipher(); byte[] iv = new byte[c.getBlockSize()]; random.nextBytes(iv); byte[] salt = new byte[8]; System.arraycopy(iv, 0, salt, 0, 8); OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator(); pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(passwd), salt); KeyParameter param = (KeyParameter) pGen.generateDerivedParameters(cipher.getKeyLenInBits()); SecretKey secretKey = new SecretKeySpec( param.getKey(), org.jruby.ext.openssl.Cipher.Algorithm.getAlgorithmBase(c)); byte[] encData = null; try { c.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); encData = c.doFinal(encoding); } catch (GeneralSecurityException gse) { throw new IOException("exception using cipher: " + gse.toString()); } out.write(BEF_G + pemHeader + AFT); out.newLine(); out.write("Proc-Type: 4,ENCRYPTED"); out.newLine(); out.write("DEK-Info: " + cipher.getOsslName() + ","); writeHexEncoded(out, iv); out.newLine(); out.newLine(); writeEncoded(out, encData); out.write(BEF_E + pemHeader + AFT); out.flush(); }
public static void writeDHParameters(Writer _out, DHParameterSpec params) throws IOException { BufferedWriter out = makeBuffered(_out); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ASN1OutputStream aOut = new ASN1OutputStream(bOut); ASN1EncodableVector v = new ASN1EncodableVector(); BigInteger value; if ((value = params.getP()) != null) { v.add(new DERInteger(value)); } if ((value = params.getG()) != null) { v.add(new DERInteger(value)); } aOut.writeObject(new DERSequence(v)); byte[] encoding = bOut.toByteArray(); out.write(BEF_G + PEM_STRING_DHPARAMS + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_DHPARAMS + AFT); out.newLine(); out.flush(); }
public static void writePKCS7(Writer _out, byte[] encoded) throws IOException { BufferedWriter out = makeBuffered(_out); out.write(BEF_G + PEM_STRING_PKCS7 + AFT); out.newLine(); writeEncoded(out, encoded); out.write(BEF_E + PEM_STRING_PKCS7 + AFT); out.newLine(); out.flush(); }
private static void writePemPlain(BufferedWriter out, String pemHeader, byte[] encoding) throws IOException { out.write(BEF_G + pemHeader + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + pemHeader + AFT); out.newLine(); out.flush(); }
public static void writeX509CRL(Writer _out, X509CRL obj) throws IOException { BufferedWriter out = makeBuffered(_out); byte[] encoding = getEncoded(obj); out.write(BEF_G + PEM_STRING_X509_CRL + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_X509_CRL + AFT); out.newLine(); out.flush(); }
public static void writePKCS7(Writer _out, CMSSignedData obj) throws IOException { BufferedWriter out = makeBuffered(_out); byte[] encoding = getEncoded(obj); out.write(BEF_G + PEM_STRING_PKCS7 + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_PKCS7 + AFT); out.newLine(); out.flush(); }
/** writes an RSA public key encoded in an PKCS#1 RSA structure. */ public static void writeRSAPublicKey(Writer _out, RSAPublicKey obj) throws IOException { BufferedWriter out = makeBuffered(_out); byte[] encoding = getEncoded(obj); out.write(BEF_G + PEM_STRING_RSA_PUBLIC + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_RSA_PUBLIC + AFT); out.newLine(); out.flush(); }
public static void writeX509Request(Writer _out, PKCS10CertificationRequestExt obj) throws IOException { BufferedWriter out = makeBuffered(_out); byte[] encoding = getEncoded(obj); out.write(BEF_G + PEM_STRING_X509_REQ + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_X509_REQ + AFT); out.newLine(); out.flush(); }
public static void writeX509Aux(Writer _out, X509AuxCertificate obj) throws IOException { BufferedWriter out = makeBuffered(_out); byte[] encoding = null; try { if (obj.getAux() == null) { encoding = obj.getEncoded(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] ymp = obj.getEncoded(); baos.write(ymp, 0, ymp.length); X509Aux aux = obj.getAux(); ASN1EncodableVector a1 = new ASN1EncodableVector(); if (aux.trust.size() > 0) { ASN1EncodableVector a2 = new ASN1EncodableVector(); for (String trust : aux.trust) { a2.add(new DERObjectIdentifier(trust)); } a1.add(new DERSequence(a2)); } if (aux.reject.size() > 0) { ASN1EncodableVector a2 = new ASN1EncodableVector(); for (String reject : aux.reject) { a2.add(new DERObjectIdentifier(reject)); } a1.add(new DERTaggedObject(0, new DERSequence(a2))); } if (aux.alias != null) { a1.add(new DERUTF8String(aux.alias)); } if (aux.keyid != null) { a1.add(new DEROctetString(aux.keyid)); } if (aux.other.size() > 0) { ASN1EncodableVector a2 = new ASN1EncodableVector(); for (DERObject other : aux.other) { a2.add(other); } a1.add(new DERTaggedObject(1, new DERSequence(a2))); } ymp = new DERSequence(a1).getEncoded(); baos.write(ymp, 0, ymp.length); encoding = baos.toByteArray(); } } catch (CertificateEncodingException e) { throw new IOException("problem with encoding object in write_X509_AUX"); } out.write(BEF_G + PEM_STRING_X509_TRUSTED + AFT); out.newLine(); writeEncoded(out, encoding); out.write(BEF_E + PEM_STRING_X509_TRUSTED + AFT); out.newLine(); out.flush(); }