1
package com.eucalyptus.auth.util;
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.FileWriter;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
8
import java.io.OutputStreamWriter;
9
import java.security.cert.X509Certificate;
10
import org.apache.log4j.Logger;
11
import org.bouncycastle.openssl.PEMReader;
12
import org.bouncycastle.openssl.PEMWriter;
13
import com.eucalyptus.records.EventType;
14
import com.eucalyptus.records.EventRecord;
16
public class PEMFiles {
17
private static Logger LOG = Logger.getLogger( PEMFiles.class );
18
public static void write( final String fileName, final Object securityToken ) {
19
PEMWriter privOut = null;
21
privOut = new PEMWriter( new FileWriter( fileName ) );
22
EventRecord.caller( PEMFiles.class, EventType.CERTIFICATE_WRITE, fileName ).info( );
23
privOut.writeObject( securityToken );
25
} catch ( final IOException e ) {
30
public static byte[] getBytes( final Object o ) {
32
ByteArrayOutputStream pemByteOut = new ByteArrayOutputStream( );
34
pemOut = new PEMWriter( new OutputStreamWriter( pemByteOut ) );
35
pemOut.writeObject( o );
37
} catch ( IOException e ) {
38
LOG.error( e, e );//this can never happen
40
return pemByteOut.toByteArray( );
43
public static X509Certificate getCert( final byte[] o ) {
44
X509Certificate x509 = null;
46
ByteArrayInputStream pemByteIn = new ByteArrayInputStream( o );
47
in = new PEMReader( new InputStreamReader( pemByteIn ) );
49
x509 = ( X509Certificate ) in.readObject( );
50
} catch ( IOException e ) {
51
LOG.error( e, e );//this can never happen