34
34
import java .io .BufferedReader ;
35
35
import java .io .Reader ;
36
36
import java .io .ByteArrayInputStream ;
37
- import java .io .ByteArrayOutputStream ;
38
-
39
37
import java .math .BigInteger ;
40
38
41
39
import java .security .GeneralSecurityException ;
130
128
import org .jruby .ext .openssl .impl .ASN1Registry ;
131
129
import org .jruby .ext .openssl .impl .CipherSpec ;
132
130
import org .jruby .ext .openssl .impl .PKCS10Request ;
133
-
134
131
import org .jruby .ext .openssl .SecurityHelper ;
132
+ import org .jruby .ext .openssl .util .ByteArrayOutputStream ;
135
133
136
134
/**
137
135
* Helper class to read and write PEM files correctly.
@@ -846,10 +844,10 @@ private static byte[] getEncoded(X509CRL crl) throws IOException {
846
844
847
845
public static void writeDSAPublicKey (Writer _out , DSAPublicKey obj ) throws IOException {
848
846
BufferedWriter out = makeBuffered (_out );
849
- byte [] encoding = getEncoded (obj );
847
+ final byte [] enc = getEncoded (obj );
850
848
out .write (BEF_G + PEM_STRING_PUBLIC + AFT );
851
849
out .newLine ();
852
- writeEncoded (out , encoding );
850
+ writeEncoded (out , enc , enc . length );
853
851
out .write (BEF_E + PEM_STRING_PUBLIC + AFT );
854
852
out .newLine ();
855
853
out .flush ();
@@ -858,71 +856,71 @@ public static void writeDSAPublicKey(Writer _out, DSAPublicKey obj) throws IOExc
858
856
/** writes an RSA public key encoded in an PKCS#1 RSA structure. */
859
857
public static void writeRSAPublicKey (Writer _out , RSAPublicKey obj ) throws IOException {
860
858
BufferedWriter out = makeBuffered (_out );
861
- byte [] encoding = getEncoded (obj );
859
+ final byte [] enc = getEncoded (obj );
862
860
out .write (BEF_G + PEM_STRING_PUBLIC + AFT );
863
861
out .newLine ();
864
- writeEncoded (out , encoding );
862
+ writeEncoded (out , enc , enc . length );
865
863
out .write (BEF_E + PEM_STRING_PUBLIC + AFT );
866
864
out .newLine ();
867
865
out .flush ();
868
866
}
869
867
870
868
public static void writeECPublicKey (Writer _out , ECPublicKey obj ) throws IOException {
871
869
BufferedWriter out = makeBuffered (_out );
872
- byte [] encoding = getEncoded (obj );
870
+ final byte [] enc = getEncoded (obj );
873
871
out .write (BEF_G ); out .write (PEM_STRING_PUBLIC ); out .write (AFT );
874
872
out .newLine ();
875
- writeEncoded (out , encoding );
873
+ writeEncoded (out , enc , enc . length );
876
874
out .write (BEF_E ); out .write (PEM_STRING_PUBLIC ); out .write (AFT );
877
875
out .newLine ();
878
876
out .flush ();
879
877
}
880
878
881
879
public static void writePKCS7 (Writer _out , ContentInfo obj ) throws IOException {
882
880
BufferedWriter out = makeBuffered (_out );
883
- byte [] encoding = getEncoded (obj );
881
+ final byte [] enc = getEncoded (obj );
884
882
out .write (BEF_G + PEM_STRING_PKCS7 + AFT );
885
883
out .newLine ();
886
- writeEncoded (out ,encoding );
884
+ writeEncoded (out , enc , enc . length );
887
885
out .write (BEF_E + PEM_STRING_PKCS7 + AFT );
888
886
out .newLine ();
889
887
out .flush ();
890
888
}
891
889
public static void writePKCS7 (Writer _out , CMSSignedData obj ) throws IOException {
892
890
BufferedWriter out = makeBuffered (_out );
893
- byte [] encoding = getEncoded (obj );
891
+ final byte [] enc = getEncoded (obj );
894
892
out .write (BEF_G + PEM_STRING_PKCS7 + AFT );
895
893
out .newLine ();
896
- writeEncoded (out ,encoding );
894
+ writeEncoded (out , enc , enc . length );
897
895
out .write (BEF_E + PEM_STRING_PKCS7 + AFT );
898
896
out .newLine ();
899
897
out .flush ();
900
898
}
901
- public static void writePKCS7 (final Writer _out , final byte [] encoded ) throws IOException {
899
+ public static void writePKCS7 (final Writer _out , final byte [] enc ) throws IOException {
902
900
BufferedWriter out = makeBuffered (_out );
903
901
out .write (BEF_G + PEM_STRING_PKCS7 + AFT );
904
902
out .newLine ();
905
- writeEncoded (out ,encoded );
903
+ writeEncoded (out , enc , enc . length );
906
904
out .write (BEF_E + PEM_STRING_PKCS7 + AFT );
907
905
out .newLine ();
908
906
out .flush ();
909
907
}
910
908
public static void writeX509Certificate (final Writer _out , final X509Certificate cert ) throws IOException {
911
909
BufferedWriter out = makeBuffered (_out );
912
- byte [] encoding = getEncoded (cert );
910
+ final byte [] enc = getEncoded (cert );
913
911
out .write (BEF_G + PEM_STRING_X509 + AFT );
914
912
out .newLine ();
915
- writeEncoded (out , encoding );
913
+ writeEncoded (out , enc , enc . length );
916
914
out .write (BEF_E + PEM_STRING_X509 + AFT );
917
915
out .newLine ();
918
916
out .flush ();
919
917
}
920
918
public static void writeX509Aux (final Writer _out , final X509AuxCertificate cert ) throws IOException {
921
919
BufferedWriter out = makeBuffered (_out );
922
- byte [] encoding ;
920
+ final byte [] encoding ; final int encLen ;
923
921
try {
924
922
if ( cert .aux == null ) {
925
- encoding = cert .getEncoded ();
923
+ encoding = cert .getEncoded (); encLen = encoding . length ;
926
924
}
927
925
else {
928
926
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -958,15 +956,15 @@ public static void writeX509Aux(final Writer _out, final X509AuxCertificate cert
958
956
}
959
957
enc = new DLSequence (a1 ).getEncoded ();
960
958
baos .write (enc , 0 , enc .length );
961
- encoding = baos .toByteArray ();
959
+ encoding = baos .buffer (); encLen = baos . size ();
962
960
}
963
961
}
964
962
catch (CertificateEncodingException e ) {
965
963
throw new IOException ("problem with encoding object in write_X509_AUX" , e );
966
964
}
967
965
out .write (BEF_G + PEM_STRING_X509_TRUSTED + AFT );
968
966
out .newLine ();
969
- writeEncoded (out ,encoding );
967
+ writeEncoded (out , encoding , encLen );
970
968
out .write (BEF_E + PEM_STRING_X509_TRUSTED + AFT );
971
969
out .newLine ();
972
970
out .flush ();
@@ -976,7 +974,7 @@ public static void writeX509CRL(Writer _out, X509CRL obj) throws IOException {
976
974
byte [] encoding = getEncoded (obj );
977
975
out .write (BEF_G + PEM_STRING_X509_CRL + AFT );
978
976
out .newLine ();
979
- writeEncoded (out , encoding );
977
+ writeEncoded (out , encoding , encoding . length );
980
978
out .write (BEF_E + PEM_STRING_X509_CRL + AFT );
981
979
out .newLine ();
982
980
out .flush ();
@@ -986,7 +984,7 @@ public static void writeX509Request(Writer _out, PKCS10Request obj) throws IOExc
986
984
byte [] encoding = getEncoded (obj .toASN1Structure ());
987
985
out .write (BEF_G + PEM_STRING_X509_REQ + AFT );
988
986
out .newLine ();
989
- writeEncoded (out ,encoding );
987
+ writeEncoded (out , encoding , encoding . length );
990
988
out .write (BEF_E + PEM_STRING_X509_REQ + AFT );
991
989
out .newLine ();
992
990
out .flush ();
@@ -1012,12 +1010,11 @@ public static void writeDSAPrivateKey(Writer _out, DSAPrivateKey obj, CipherSpec
1012
1010
v .add (new ASN1Integer (x ));
1013
1011
1014
1012
aOut .writeObject (new DLSequence (v ));
1015
- byte [] encoding = bOut .toByteArray ();
1016
1013
1017
1014
if (cipher != null && passwd != null ) {
1018
- writePemEncrypted (out , PEM_STRING_DSA , encoding , cipher , passwd );
1015
+ writePemEncrypted (out , PEM_STRING_DSA , bOut . buffer (), bOut . size () , cipher , passwd );
1019
1016
} else {
1020
- writePemPlain (out , PEM_STRING_DSA , encoding );
1017
+ writePemPlain (out , PEM_STRING_DSA , bOut . buffer (), bOut . size () );
1021
1018
}
1022
1019
}
1023
1020
@@ -1060,9 +1057,14 @@ public static void writeECParameters(Writer _out, ASN1ObjectIdentifier obj, Ciph
1060
1057
1061
1058
private static void writePemPlain (final BufferedWriter out ,
1062
1059
final String PEM_ID , final byte [] encoding ) throws IOException {
1060
+ writePemPlain (out , PEM_ID , encoding , encoding .length );
1061
+ }
1062
+
1063
+ private static void writePemPlain (final BufferedWriter out ,
1064
+ final String PEM_ID , final byte [] encoding , final int encLen ) throws IOException {
1063
1065
out .write (BEF_G ); out .write (PEM_ID ); out .write (AFT );
1064
1066
out .newLine ();
1065
- writeEncoded (out , encoding );
1067
+ writeEncoded (out , encoding , encLen );
1066
1068
out .write (BEF_E ); out .write (PEM_ID ); out .write (AFT );
1067
1069
out .newLine ();
1068
1070
out .flush ();
@@ -1071,6 +1073,12 @@ private static void writePemPlain(final BufferedWriter out,
1071
1073
private static void writePemEncrypted (final BufferedWriter out ,
1072
1074
final String PEM_ID , final byte [] encoding ,
1073
1075
final CipherSpec cipherSpec , final char [] passwd ) throws IOException {
1076
+ writePemEncrypted (out , PEM_ID , encoding , encoding .length , cipherSpec , passwd );
1077
+ }
1078
+
1079
+ private static void writePemEncrypted (final BufferedWriter out ,
1080
+ final String PEM_ID , final byte [] encoding , final int encCount ,
1081
+ final CipherSpec cipherSpec , final char [] passwd ) throws IOException {
1074
1082
1075
1083
final Cipher cipher = cipherSpec .getCipher ();
1076
1084
final byte [] iv = new byte [cipher .getBlockSize ()];
@@ -1085,7 +1093,7 @@ private static void writePemEncrypted(final BufferedWriter out,
1085
1093
final byte [] encData ;
1086
1094
try {
1087
1095
cipher .init (Cipher .ENCRYPT_MODE , secretKey , new IvParameterSpec (iv ));
1088
- encData = cipher .doFinal (encoding );
1096
+ encData = cipher .doFinal (encoding , 0 , encCount );
1089
1097
}
1090
1098
catch (InvalidKeyException e ) {
1091
1099
final String msg = e .getMessage ();
@@ -1105,7 +1113,7 @@ private static void writePemEncrypted(final BufferedWriter out,
1105
1113
writeHexEncoded (out , iv );
1106
1114
out .newLine ();
1107
1115
out .newLine ();
1108
- writeEncoded (out , encData );
1116
+ writeEncoded (out , encData , encData . length );
1109
1117
out .write (BEF_E ); out .write (PEM_ID ); out .write (AFT );
1110
1118
out .flush ();
1111
1119
}
@@ -1141,11 +1149,10 @@ public static void writeDHParameters(Writer _out, DHParameterSpec params) throws
1141
1149
ASN1OutputStream aOut = new ASN1OutputStream (bOut );
1142
1150
1143
1151
aOut .writeObject (new DLSequence (v ));
1144
- byte [] encoding = bOut .toByteArray ();
1145
1152
1146
1153
out .write (BEF_G ); out .write (PEM_STRING_DHPARAMS ); out .write (AFT );
1147
1154
out .newLine ();
1148
- writeEncoded (out , encoding );
1155
+ writeEncoded (out , bOut . buffer (), bOut . size () );
1149
1156
out .write (BEF_E ); out .write (PEM_STRING_DHPARAMS ); out .write (AFT );
1150
1157
out .newLine ();
1151
1158
out .flush ();
@@ -1483,17 +1490,18 @@ private static void writeHexEncoded(BufferedWriter out, byte[] bytes) throws IOE
1483
1490
}
1484
1491
}
1485
1492
1486
- private static void writeEncoded (BufferedWriter out , byte [] bytes ) throws IOException {
1487
- char [] buf = new char [64 ];
1488
- bytes = Base64 .encode (bytes );
1493
+ private static void writeEncoded (BufferedWriter out ,
1494
+ byte [] bytes , final int bytesLen ) throws IOException {
1495
+ final char [] buf = new char [64 ];
1496
+ bytes = Base64 .encode (bytes , 0 ,bytesLen );
1489
1497
for (int i = 0 ; i < bytes .length ; i += buf .length ) {
1490
1498
int index = 0 ;
1491
1499
1492
1500
while (index != buf .length ) {
1493
1501
if ((i + index ) >= bytes .length ) {
1494
1502
break ;
1495
1503
}
1496
- buf [index ] = (char )bytes [i + index ];
1504
+ buf [index ] = (char ) bytes [i + index ];
1497
1505
index ++;
1498
1506
}
1499
1507
out .write (buf , 0 , index );
0 commit comments