3535import org .bouncycastle .asn1 .ASN1InputStream ;
3636import org .bouncycastle .asn1 .ASN1Sequence ;
3737import org .bouncycastle .asn1 .DERSequence ;
38+ import org .bouncycastle .asn1 .x500 .X500Name ;
3839import org .bouncycastle .asn1 .x509 .AuthorityKeyIdentifier ;
3940import org .bouncycastle .asn1 .x509 .BasicConstraints ;
4041import org .bouncycastle .asn1 .x509 .ExtendedKeyUsage ;
@@ -151,7 +152,6 @@ public X509Certificate generate(KeyPair keypair,
151152 X509V3CertificateGenerator generator ;
152153 BigInteger serial ;
153154 X509Certificate certificate ;
154- ASN1EncodableVector vector ;
155155
156156 serial = BigInteger .valueOf (generateSerial ());
157157 generator = new X509V3CertificateGenerator ();
@@ -174,10 +174,10 @@ public X509Certificate generate(KeyPair keypair,
174174 getPublic (),
175175 issuer ,
176176 serial ));
177- vector = new ASN1EncodableVector ();
178- vector .add (KeyPurposeId .id_kp_emailProtection );
179177
180- generator .addExtension (X509Extensions .ExtendedKeyUsage , false , new ExtendedKeyUsage (new DERSequence (vector )));
178+ var purposeIds = new KeyPurposeId [] { KeyPurposeId .id_kp_emailProtection };
179+
180+ generator .addExtension (X509Extensions .ExtendedKeyUsage , false , new ExtendedKeyUsage (purposeIds ));
181181
182182 switch (keyusage ) {
183183 case X509Constants .SIGNATURE_KEY_USAGE :
@@ -208,23 +208,14 @@ public X509Certificate generate(KeyPair keypair,
208208 * @param issuer the certificate issuer
209209 * @param serial the certificate serial number
210210 * @return the authority key identifier of the public key
211- * @throws IOException
212211 */
213212 private AuthorityKeyIdentifier getAuthorityKeyIdentifier (PublicKey publicKey ,
214213 String issuer ,
215- BigInteger serial )
216- throws IOException
217- {
218- InputStream input ;
219- SubjectPublicKeyInfo keyInfo ;
220- ASN1EncodableVector vector ;
221-
222- input = new ByteArrayInputStream (publicKey .getEncoded ());
223- keyInfo = new SubjectPublicKeyInfo ((ASN1Sequence )new ASN1InputStream (input ).readObject ());
224- vector = new ASN1EncodableVector ();
225- vector .add (new GeneralName (new X509Name (issuer )));
226-
227- return new AuthorityKeyIdentifier (keyInfo , new GeneralNames (new DERSequence (vector )), serial );
214+ BigInteger serial ) {
215+ SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo .getInstance (publicKey .getEncoded ());
216+ X500Name issuerName = new X500Name (issuer );
217+ GeneralNames generalNames = new GeneralNames (new GeneralName (issuerName ));
218+ return new AuthorityKeyIdentifier (keyInfo , generalNames , serial );
228219 }
229220
230221 /**
@@ -237,27 +228,19 @@ private AuthorityKeyIdentifier getAuthorityKeyIdentifier(PublicKey publicKey,
237228 private SubjectKeyIdentifier getSubjectKeyIdentifier (PublicKey publicKey )
238229 throws IOException
239230 {
240- InputStream input ;
241- SubjectPublicKeyInfo keyInfo ;
242-
243- input = new ByteArrayInputStream (publicKey .getEncoded ());
244- keyInfo = new SubjectPublicKeyInfo ((ASN1Sequence )new ASN1InputStream (input ).readObject ());
245-
246- return new SubjectKeyIdentifier (keyInfo );
231+ SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo .getInstance (publicKey .getEncoded ());
232+ return new SubjectKeyIdentifier (keyInfo .getEncoded ());
247233 }
248234
249235 /**
250- * Generates a random serial number
236+ * Generates a serial number from current timestamp
251237 *
252238 * @return the serial number
253239 */
254240 private long generateSerial () {
255- Date now ;
256-
257- now = new Date ();
258- String sNow = sdfSerial .format (now );
259-
260- return Long .valueOf (sNow ).longValue ();
241+ Date now = new Date ();
242+ String sNow = sdfSerial .format (now );
243+ return Long .parseLong (sNow );
261244 }
262245
263246 // --------------------------------------------------------------------
0 commit comments