Java: Prepare pem keys objects from strings

Task: Prepare Personal, Public and Certificate keys from strings

Implementation:

       String licenseFileName = null;
       String licensePem = "..."; // Read pem file into this string variable.
       String certificateStr = StringUtil.between(licensePem, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----");
       String privateKeyStr = StringUtil.between(licensePem, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----");
       if (privateKeyStr == null) {
           privateKeyStr = StringUtil.between(licensePem, "-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----");
       }
       String licenseJwtStr = StringUtil.between(licensePem, "-----BEGIN SERVICE LICENSE-----", "-----END SERVICE LICENSE-----");
       if (licenseJwtStr == null) {
           licenseJwtStr = StringUtil.betweenSharp(licensePem, "-----BEGIN ENTERPRISE LICENSE-----", "-----END ENTERPRISE LICENSE-----");
       }
       if (licenseJwtStr != null) {
           licenseJwtStr = licenseJwtStr.replaceAll("\n", StringUtils.EMPTY);
       }

       Security.addProvider(new BouncyCastleProvider());

       PublicKey publicKey = null;
       try ( PEMParser parser = new PEMParser(new StringReader(certificateStr))) {
           Object keyInfo = parser.readObject();
           if (keyInfo instanceof X509CertificateHolder) {
               publicKey = new JcaPEMKeyConverter().setProvider("BC")
                       .getPublicKey(((X509CertificateHolder) keyInfo).getSubjectPublicKeyInfo());
           } else {
               publicKey = new JcaPEMKeyConverter().setProvider("BC")
                       .getPublicKey((SubjectPublicKeyInfo) keyInfo);
           }
       } catch (IOException ex) {
           log.error("IOException: {}", ex);
       }

       PrivateKey privateKey = null;
       try ( PEMParser parser = new PEMParser(new StringReader(privateKeyStr))) {
           Object keyInfo = parser.readObject();
           if (keyInfo instanceof PEMKeyPair) {
               privateKey = new JcaPEMKeyConverter().setProvider("BC")
                       .getPrivateKey(((PEMKeyPair) keyInfo).getPrivateKeyInfo());
           }
       } catch (IOException ex) {
           log.error("IOException: {}", ex);
       }

       Map<String, Object> dataMap = null;
       try {
           JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                   .setRequireExpirationTime()
                   .setVerificationKey(publicKey)
                   .build();
           JwtClaims jwtDecoded = jwtConsumer.processToClaims(licenseJwtStr);
           dataMap = jwtDecoded.getClaimsMap();
       } catch (InvalidJwtException ex) {
           log.error("IOException: {}", ex);
       }

       // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
       // . . . . . . Use data stored in the dataMap object . . . . . . .
       // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


// . . . . . . . . . . . . . . . .
// . . . . . String Util . . . . .
// . . . . . . . . . . . . . . . .
   public static String between(String sourceString, String startSubstring, String endSubstring) {
       String result = null;
       int startPosition = sourceString.indexOf(startSubstring);
       if (startPosition >= 0) {
           int endPosition = sourceString.indexOf(endSubstring);
           if (endPosition >= 0) {
               result = sourceString.substring(startPosition, endPosition + endSubstring.length());
           }
       }
       return result;
   }

   public static String betweenSharp(String sourceString, String startSubstring, String endSubstring) {
       String result = null;
       int startPosition = sourceString.indexOf(startSubstring);
       if (startPosition >= 0) {
           int endPosition = sourceString.indexOf(endSubstring);
           if (endPosition >= 0) {
               result = sourceString.substring(startPosition+ startSubstring.length(), endPosition );
           }
       }
       return result;
   }

Done.

Leave a Reply

Your email address will not be published.

  +  80  =  82