alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (KeyStore.scala)

This example Play Framework source code file (KeyStore.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, bufferedinputstream, collection, file, fileinputstream, key, keystore, keystorebuilder, keystoreformats, lib, library, option, play framework, seq, string, ws

The KeyStore.scala Play Framework example source code

/*
 *
 *  * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 *
 */
package play.api.libs.ws.ssl

import java.security.KeyStore

import java.io._
import java.security.cert._
import org.apache.commons.codec.binary.Base64
import scala.collection.JavaConverters

trait KeyStoreBuilder {
  def build(): KeyStore
}

object KeystoreFormats {

  def loadCertificates(certs: TraversableOnce[Certificate]): KeyStore = {
    // Must be JKS internally, to enable trusted certificates in JDK 1.6
    val keystore = KeyStore.getInstance(KeyStore.getDefaultType)
    keystore.load(null)
    certs.foreach { cert =>
      val alias = cert.getSubjectX500Principal.getName
      keystore.setCertificateEntry(alias, cert)
    }
    keystore
  }

}

import KeystoreFormats._

/**
 * Builds a keystore from a string containing PEM encoded certificates, using CertificateFactory internally.
 *
 * @see java.security.cert.CertificateFactory
 */
class StringBasedKeyStoreBuilder(data: String, password: Option[Array[Char]] = None) extends KeyStoreBuilder {

  val logger = org.slf4j.LoggerFactory.getLogger(getClass)

  def build(): KeyStore = {
    val certs = readCertificates(data)
    val store = loadCertificates(certs)
    store

  }

  def readCertificates(certificateString: String): Seq[Certificate] = {
    val cf = CertificateFactory.getInstance("X.509")
    // CertificateFactory throws EOF on whitespace after end cert, which is very common in triple quoted strings.
    val trimmedString = certificateString.trim()
    val is = new ByteArrayInputStream(trimmedString.getBytes("UTF-8"))
    val bis = new BufferedInputStream(is)
    val buffer = new scala.collection.mutable.ListBuffer[Certificate]()
    while (bis.available() > 0) {
      val cert = cf.generateCertificate(bis)
      buffer.append(cert)
    }
    buffer.toList
  }

}

/**
 * Builds a keystore from a file containing PEM encoded certificates, using CertificateFactory internally.
 *
 * @see java.security.cert.CertificateFactory
 */
class FileBasedKeyStoreBuilder(keyStoreType: String,
    filePath: String,
    password: Option[Array[Char]]) extends KeyStoreBuilder {

  val logger = org.slf4j.LoggerFactory.getLogger(getClass)

  def build(): KeyStore = {
    val file = new File(filePath)

    require(file.exists, s"Key store file $filePath does not exist!")
    require(file.canRead, s"Cannot read from key store file $filePath!")

    keyStoreType match {
      case "PEM" =>
        val certs = readCertificates(file)
        loadCertificates(certs)
      case otherFormat =>
        buildFromKeystoreFile(otherFormat, file)
    }
  }

  def buildFromKeystoreFile(storeType: String, file: File): KeyStore = {
    val inputStream = new BufferedInputStream(new FileInputStream(file))
    try {
      val storeType = keyStoreType
      val store = KeyStore.getInstance(storeType)
      store.load(inputStream, password.orNull)
      store
    } finally {
      inputStream.close()
    }
  }

  def readCertificates(file: File): Iterable[Certificate] = {
    import scala.collection.JavaConverters._
    val cf = CertificateFactory.getInstance("X.509")
    val fis = new FileInputStream(file)
    val bis = new BufferedInputStream(fis)

    cf.generateCertificates(bis).asScala
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework KeyStore.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.