Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Pem.scala « crypto « savage « getbootstrap « com « scala « main « src - github.com/twbs/savage.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 47496f1a29eb43a3026eadf7c35accc072ba56ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.getbootstrap.savage.crypto

import scala.util.{Try,Success,Failure}
import java.io.StringReader
import java.security.spec.X509EncodedKeySpec
import org.bouncycastle.util.io.pem.PemReader
import org.bouncycastle.util.io.pem.PemObject


sealed class MalformedPemException(cause: Throwable) extends RuntimeException("The given data did not conform to the PEM format!", cause)

sealed class UnexpectedPemDataTypeException(expectedType: String, pemObj: PemObject)
  extends RuntimeException(s"PEM contained data of unexpected type! Expected: ${expectedType} Actual: ${pemObj.getType}")

// PEM is the name for the format that involves "-----BEGIN PUBLIC KEY-----" etc.
object Pem {
  private val PublicKeyPemType = "PUBLIC KEY"

  @throws[MalformedPemException]("if there is a problem decoding the PEM data")
  private def decode(pem: String): PemObject = {
    val pemReader = new PemReader(new StringReader(pem))
    val pemObjTry = Try { pemReader.readPemObject() }
    val closeTry = Try { pemReader.close() }
    (pemObjTry, closeTry) match {
      case (Failure(readExc), _) => throw new MalformedPemException(readExc)
      case (_, Failure(closeExc)) => throw new MalformedPemException(closeExc)
      case (Success(pemObj), Success(_)) => pemObj
    }
  }

  // Decodes PKCS8 data in PEM format into a X509EncodedKeySpec
  // which can be handled by sun.security.rsa.RSAKeyFactory
  @throws[UnexpectedPemDataTypeException]("if the PEM contains non-public-key data")
  def decodePublicKeyIntoSpec(publicKeyInPem: String): X509EncodedKeySpec = {
    val pemObj = decode(publicKeyInPem)
    pemObj.getType match {
      case PublicKeyPemType => new X509EncodedKeySpec(pemObj.getContent)
      case unexpectedType => throw new UnexpectedPemDataTypeException(PublicKeyPemType, pemObj)
    }
  }
}