29
29
30
30
import java .io .IOException ;
31
31
import java .security .GeneralSecurityException ;
32
+ import java .security .NoSuchAlgorithmException ;
32
33
import java .security .PublicKey ;
33
34
34
35
import org .bouncycastle .asn1 .ASN1EncodableVector ;
39
40
import org .bouncycastle .asn1 .ASN1Sequence ;
40
41
import org .bouncycastle .asn1 .DLSequence ;
41
42
import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
42
- //import org.bouncycastle.jce.netscape.NetscapeCertRequest;
43
43
44
44
import org .jruby .Ruby ;
45
45
import org .jruby .RubyClass ;
52
52
import org .jruby .runtime .ObjectAllocator ;
53
53
import org .jruby .runtime .Visibility ;
54
54
import org .jruby .runtime .builtin .IRubyObject ;
55
+ import org .jruby .runtime .ThreadContext ;
55
56
import org .jruby .runtime .Visibility ;
57
+ import org .jruby .util .ByteList ;
56
58
57
59
// org.bouncycastle.jce.netscape.NetscapeCertRequest emulator:
58
60
import org .jruby .ext .openssl .impl .NetscapeCertRequest ;
59
61
60
62
import static org .jruby .ext .openssl .PKeyDSA ._DSA ;
61
63
import static org .jruby .ext .openssl .PKeyRSA ._RSA ;
62
- import org .jruby .runtime .ThreadContext ;
64
+ import static org .jruby .ext .openssl .OpenSSLReal .debugStackTrace ;
65
+ import static org .jruby .ext .openssl .OpenSSLReal .warn ;
63
66
64
67
/**
65
68
* @author <a href="mailto:[email protected] ">Ola Bini</a>
@@ -99,17 +102,17 @@ public NetscapeSPKI(Ruby runtime, RubyClass type) {
99
102
public IRubyObject _initialize (final ThreadContext context , final IRubyObject [] args ) {
100
103
final Ruby runtime = context .runtime ;
101
104
if ( args .length > 0 ) {
102
- byte [] b = args [0 ].convertToString ().getBytes ();
103
- b = tryBase64Decode (b );
105
+ byte [] request = args [0 ].convertToString ().getBytes ();
106
+ request = tryBase64Decode (request );
104
107
105
108
final NetscapeCertRequest cert ;
106
109
try {
107
- this .cert = cert = new NetscapeCertRequest (b );
110
+ this .cert = cert = new NetscapeCertRequest (request );
108
111
challenge = runtime .newString ( cert .getChallenge () );
109
112
}
110
- catch (IOException ioe ) {
111
- throw newSPKIError (runtime , ioe . getMessage ());
112
- }
113
+ catch (GeneralSecurityException e ) { throw newSPKIError ( e ); }
114
+ catch ( IllegalArgumentException e ) { throw newSPKIError (e ); }
115
+
113
116
final PublicKey publicKey = cert .getPublicKey ();
114
117
final String algorithm = publicKey .getAlgorithm ();
115
118
final RubyString pub_key = RubyString .newString (runtime , publicKey .getEncoded ());
@@ -128,34 +131,36 @@ else if ( "DSA".equalsIgnoreCase(algorithm) ) {
128
131
}
129
132
130
133
// just try to decode for the time when the given bytes are base64 encoded.
131
- private byte [] tryBase64Decode (byte [] b ) {
134
+ private static byte [] tryBase64Decode (byte [] b ) {
132
135
try {
133
136
b = Base64 .decode (b , 0 , b .length , Base64 .NO_OPTIONS );
134
- } catch (Exception ignored ) { }
137
+ }
138
+ catch (IOException ignored ) { }
139
+ catch (IllegalArgumentException ignored ) { }
135
140
return b ;
136
141
}
137
142
138
143
@ JRubyMethod
139
144
public IRubyObject to_der () {
140
145
try {
141
- return RubyString .newString (getRuntime (), internalToDer ());
142
- } catch (IOException ioe ) {
143
- throw newSPKIError (getRuntime (), ioe .getMessage ());
146
+ final byte [] derBytes = toDER ();
147
+ return getRuntime ().newString (new ByteList (derBytes , false ));
144
148
}
149
+ catch (IOException ioe ) { throw newSPKIError (ioe ); }
145
150
}
146
151
147
- @ JRubyMethod (name ={ "to_pem" ,"to_s" })
152
+ @ JRubyMethod (name = { "to_pem" , "to_s" })
148
153
public IRubyObject to_pem () {
149
154
try {
150
- byte [] source = internalToDer ();
151
- // no Base64.DO_BREAK_LINES option needed for NSPKI.
152
- return getRuntime ().newString (Base64 .encodeBytes (source , 0 , source .length , Base64 .NO_OPTIONS ));
153
- } catch (IOException ioe ) {
154
- throw newSPKIError (getRuntime (), ioe .getMessage ());
155
+ byte [] source = toDER ();
156
+ // no Base64.DO_BREAK_LINES option needed for NSPKI :
157
+ source = Base64 .encodeBytesToBytes (source , 0 , source .length , Base64 .NO_OPTIONS );
158
+ return getRuntime ().newString (new ByteList (source , false ));
155
159
}
160
+ catch (IOException ioe ) { throw newSPKIError (ioe ); }
156
161
}
157
162
158
- private byte [] internalToDer () throws IOException {
163
+ private byte [] toDER () throws IOException {
159
164
ASN1Sequence b = (ASN1Sequence ) ((NetscapeCertRequest ) cert ).toASN1Primitive ();
160
165
ASN1ObjectIdentifier encType = (ASN1ObjectIdentifier )((ASN1Sequence )((ASN1Sequence )((ASN1Sequence )b .getObjectAt (0 )).getObjectAt (0 )).getObjectAt (0 )).getObjectAt (0 );
161
166
ASN1ObjectIdentifier sigAlg = ((AlgorithmIdentifier )b .getObjectAt (1 )).getAlgorithm ();
@@ -170,22 +175,22 @@ private byte[] internalToDer() throws IOException {
170
175
ASN1EncodableVector v3 = new ASN1EncodableVector ();
171
176
ASN1EncodableVector v4 = new ASN1EncodableVector ();
172
177
v4 .add (encType );
173
- v4 .add (new DERNull () );
178
+ v4 .add (DERNull . INSTANCE );
174
179
v3 .add (new DLSequence (v4 ));
175
180
v3 .add (publicKey );
176
181
v2 .add (new DLSequence (v3 ));
177
182
v2 .add (encodedChallenge );
178
183
v1 .add (new DLSequence (v2 ));
179
184
v1_2 .add (sigAlg );
180
- v1_2 .add (new DERNull () );
185
+ v1_2 .add (DERNull . INSTANCE );
181
186
v1 .add (new DLSequence (v1_2 ));
182
187
v1 .add (sig );
183
188
return new DLSequence (v1 ).getEncoded ();
184
189
}
185
190
186
191
@ JRubyMethod
187
192
public IRubyObject to_text () {
188
- System . err . println ( "WARNING: calling unimplemented method: to_text" );
193
+ warn ( getRuntime (). getCurrentContext (), "WARNING: unimplemented method called: Netscape::SPKI# to_text" );
189
194
return getRuntime ().getNil ();
190
195
}
191
196
@@ -206,28 +211,36 @@ public IRubyObject sign(final IRubyObject key, final IRubyObject digest) {
206
211
final String symKey = keyAlg .toLowerCase () + '-' + digAlg .toLowerCase ();
207
212
try {
208
213
final ASN1ObjectIdentifier alg = ASN1 .getOIDLookup (getRuntime ()).get ( symKey );
209
- final PublicKey publicKey = ((PKey ) public_key ).getPublicKey ();
214
+ final PublicKey publicKey = ( (PKey ) this . public_key ).getPublicKey ();
210
215
final String challengeStr = challenge .toString ();
211
216
final NetscapeCertRequest cert ;
212
217
this .cert = cert = new NetscapeCertRequest (challengeStr , new AlgorithmIdentifier (alg ), publicKey );
213
218
cert .sign ( ((PKey ) key ).getPrivateKey () );
214
219
}
215
- catch (GeneralSecurityException gse ) {
216
- throw newSPKIError (getRuntime (), gse .getMessage ());
220
+ catch (NoSuchAlgorithmException e ) {
221
+ debugStackTrace (getRuntime (), e );
222
+ throw newSPKIError (e );
223
+ }
224
+ catch (GeneralSecurityException e ) {
225
+ throw newSPKIError (e );
217
226
}
218
227
return this ;
219
228
}
220
229
221
230
@ JRubyMethod
222
231
public IRubyObject verify (final IRubyObject pkey ) {
223
232
final NetscapeCertRequest cert = (NetscapeCertRequest ) this .cert ;
224
- cert .setPublicKey (((PKey ) pkey ).getPublicKey ());
233
+ cert .setPublicKey ( ((PKey ) pkey ).getPublicKey () );
225
234
try {
226
235
boolean result = cert .verify (challenge .toString ());
227
236
return getRuntime ().newBoolean (result );
228
237
}
229
- catch (GeneralSecurityException gse ) {
230
- throw newSPKIError (getRuntime (), gse .getMessage ());
238
+ catch (NoSuchAlgorithmException e ) {
239
+ debugStackTrace (getRuntime (), e );
240
+ throw newSPKIError (e );
241
+ }
242
+ catch (GeneralSecurityException e ) {
243
+ throw newSPKIError (e );
231
244
}
232
245
}
233
246
@@ -241,6 +254,10 @@ public IRubyObject set_challenge(final IRubyObject challenge) {
241
254
return this .challenge = challenge ;
242
255
}
243
256
257
+ private RaiseException newSPKIError (final Exception e ) {
258
+ return newSPKIError (getRuntime (), e .getMessage ());
259
+ }
260
+
244
261
private static RaiseException newSPKIError (Ruby runtime , String message ) {
245
262
return Utils .newError (runtime , _Netscape (runtime ).getClass ("SPKIError" ), message );
246
263
}
0 commit comments