2828import java .net .URLEncoder ;
2929import java .nio .charset .Charset ;
3030import java .security .InvalidKeyException ;
31+ import java .security .KeyFactory ;
3132import java .security .KeyPair ;
3233import java .security .NoSuchAlgorithmException ;
3334import java .security .NoSuchProviderException ;
3435import java .security .PrivateKey ;
36+ import java .security .PublicKey ;
3537import java .security .SecureRandom ;
3638import java .security .Signature ;
3739import java .security .SignatureException ;
3840import java .security .cert .CertificateException ;
3941import java .security .cert .X509Certificate ;
42+ import java .security .spec .InvalidKeySpecException ;
43+ import java .security .spec .PKCS8EncodedKeySpec ;
44+ import java .security .spec .X509EncodedKeySpec ;
4045import java .util .List ;
4146import java .util .zip .Deflater ;
4247import java .util .zip .DeflaterOutputStream ;
@@ -264,12 +269,6 @@ public static String generateSAMLRequestSignature(final String urlEncodedString,
264269 return url ;
265270 }
266271
267- public static X509Certificate generateRandomX509Certificate (KeyPair keyPair ) throws NoSuchAlgorithmException , NoSuchProviderException , CertificateException , SignatureException , InvalidKeyException , OperatorCreationException {
268- return CertUtils .generateV1Certificate (keyPair ,
269- "CN=ApacheCloudStack" , "CN=ApacheCloudStack" ,
270- 3 , "SHA256WithRSA" );
271- }
272-
273272 public static void setupSamlUserCookies (final LoginCmdResponse loginResponse , final HttpServletResponse resp ) throws IOException {
274273 resp .addCookie (new Cookie ("userid" , URLEncoder .encode (loginResponse .getUserId (), HttpUtils .UTF_8 )));
275274 resp .addCookie (new Cookie ("domainid" , URLEncoder .encode (loginResponse .getDomainId (), HttpUtils .UTF_8 )));
@@ -284,4 +283,82 @@ public static void setupSamlUserCookies(final LoginCmdResponse loginResponse, fi
284283 resp .addHeader ("SET-COOKIE" , String .format ("%s=%s;HttpOnly" , ApiConstants .SESSIONKEY , loginResponse .getSessionKey ()));
285284 }
286285
286+ /**
287+ * Returns base64 encoded PublicKey
288+ * @param key PublicKey
289+ * @return public key encoded string
290+ */
291+ public static String encodePublicKey (PublicKey key ) {
292+ try {
293+ KeyFactory keyFactory = CertUtils .getKeyFactory ();
294+ if (keyFactory == null ) return null ;
295+ X509EncodedKeySpec spec = keyFactory .getKeySpec (key , X509EncodedKeySpec .class );
296+ return new String (org .bouncycastle .util .encoders .Base64 .encode (spec .getEncoded ()), Charset .forName ("UTF-8" ));
297+ } catch (InvalidKeySpecException e ) {
298+ s_logger .error ("Unable to create KeyFactory:" + e .getMessage ());
299+ }
300+ return null ;
301+ }
302+
303+ /**
304+ * Returns base64 encoded PrivateKey
305+ * @param key PrivateKey
306+ * @return privatekey encoded string
307+ */
308+ public static String encodePrivateKey (PrivateKey key ) {
309+ try {
310+ KeyFactory keyFactory = CertUtils .getKeyFactory ();
311+ if (keyFactory == null ) return null ;
312+ PKCS8EncodedKeySpec spec = keyFactory .getKeySpec (key ,
313+ PKCS8EncodedKeySpec .class );
314+ return new String (org .bouncycastle .util .encoders .Base64 .encode (spec .getEncoded ()), Charset .forName ("UTF-8" ));
315+ } catch (InvalidKeySpecException e ) {
316+ s_logger .error ("Unable to create KeyFactory:" + e .getMessage ());
317+ }
318+ return null ;
319+ }
320+
321+ /**
322+ * Decodes base64 encoded public key to PublicKey
323+ * @param publicKey encoded public key string
324+ * @return returns PublicKey
325+ */
326+ public static PublicKey decodePublicKey (String publicKey ) {
327+ byte [] sigBytes = org .bouncycastle .util .encoders .Base64 .decode (publicKey );
328+ X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (sigBytes );
329+ KeyFactory keyFactory = CertUtils .getKeyFactory ();
330+ if (keyFactory == null )
331+ return null ;
332+ try {
333+ return keyFactory .generatePublic (x509KeySpec );
334+ } catch (InvalidKeySpecException e ) {
335+ s_logger .error ("Unable to create PrivateKey from privateKey string:" + e .getMessage ());
336+ }
337+ return null ;
338+ }
339+
340+ /**
341+ * Decodes base64 encoded private key to PrivateKey
342+ * @param privateKey encoded private key string
343+ * @return returns PrivateKey
344+ */
345+ public static PrivateKey decodePrivateKey (String privateKey ) {
346+ byte [] sigBytes = org .bouncycastle .util .encoders .Base64 .decode (privateKey );
347+ PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec (sigBytes );
348+ KeyFactory keyFactory = CertUtils .getKeyFactory ();
349+ if (keyFactory == null )
350+ return null ;
351+ try {
352+ return keyFactory .generatePrivate (pkscs8KeySpec );
353+ } catch (InvalidKeySpecException e ) {
354+ s_logger .error ("Unable to create PrivateKey from privateKey string:" + e .getMessage ());
355+ }
356+ return null ;
357+ }
358+
359+ public static X509Certificate generateRandomX509Certificate (KeyPair keyPair ) throws NoSuchAlgorithmException , NoSuchProviderException , CertificateException , SignatureException , InvalidKeyException , OperatorCreationException {
360+ return CertUtils .generateV1Certificate (keyPair ,
361+ "CN=ApacheCloudStack" , "CN=ApacheCloudStack" ,
362+ 3 , "SHA256WithRSA" );
363+ }
287364}
0 commit comments