|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Functional Streams for Scala |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package fs2.io |
| 23 | + |
| 24 | +import cats.effect.IO |
| 25 | +import scodec.bits.ByteVector |
| 26 | + |
| 27 | +import java.math.BigInteger |
| 28 | +import java.security.KeyPairGenerator |
| 29 | +import java.security.SecureRandom |
| 30 | +import java.util.Date |
| 31 | +import sun.security.x509.AlgorithmId |
| 32 | +import sun.security.x509.CertificateAlgorithmId |
| 33 | +import sun.security.x509.CertificateIssuerName |
| 34 | +import sun.security.x509.CertificateSerialNumber |
| 35 | +import sun.security.x509.CertificateSubjectName |
| 36 | +import sun.security.x509.CertificateValidity |
| 37 | +import sun.security.x509.CertificateVersion |
| 38 | +import sun.security.x509.CertificateX509Key |
| 39 | +import sun.security.x509.X500Name |
| 40 | +import sun.security.x509.X509CertImpl |
| 41 | +import sun.security.x509.X509CertInfo |
| 42 | + |
| 43 | +/** Platform-specific implementation for JVM and JS platforms that can generate certificates. |
| 44 | + */ |
| 45 | +private[io] object PlatformSpecificCertificateProvider { |
| 46 | + |
| 47 | + def create: IO[TestCertificateProvider] = IO.pure(new JvmJsCertificateProvider) |
| 48 | + |
| 49 | + private class JvmJsCertificateProvider extends TestCertificateProvider { |
| 50 | + |
| 51 | + def getCertificatePair: IO[TestCertificateProvider.CertificatePair] = |
| 52 | + IO(KeyPairGenerator.getInstance("RSA")).flatMap { keyPairGen => |
| 53 | + for { |
| 54 | + _ <- IO(keyPairGen.initialize(2048, new SecureRandom())) |
| 55 | + keyPair <- IO(keyPairGen.generateKeyPair()) |
| 56 | + |
| 57 | + // Create certificate info |
| 58 | + certInfo = new X509CertInfo() |
| 59 | + |
| 60 | + // Set validity period (1 year from now) |
| 61 | + now = new Date() |
| 62 | + expiration = new Date(now.getTime + 365L * 24 * 60 * 60 * 1000) |
| 63 | + validity = new CertificateValidity(now, expiration) |
| 64 | + _ <- IO(certInfo.set(CertificateValidity.NAME, validity)) |
| 65 | + |
| 66 | + // Set serial number |
| 67 | + serial = new BigInteger(64, new SecureRandom()) |
| 68 | + _ <- IO(certInfo.set(CertificateSerialNumber.NAME, new CertificateSerialNumber(serial))) |
| 69 | + |
| 70 | + // Set subject and issuer names |
| 71 | + subject = new X500Name("CN=Unknown,O=Unknown,OU=FS2 Tests,L=Unknown,ST=Unknown,C=Unknown") |
| 72 | + _ <- IO(certInfo.set(CertificateSubjectName.NAME, new CertificateSubjectName(subject))) |
| 73 | + _ <- IO(certInfo.set(CertificateIssuerName.NAME, new CertificateIssuerName(subject))) |
| 74 | + |
| 75 | + // Set public key |
| 76 | + publicKey = keyPair.getPublic() |
| 77 | + _ <- IO(certInfo.set(CertificateX509Key.NAME, new CertificateX509Key(publicKey))) |
| 78 | + |
| 79 | + // Set version |
| 80 | + _ <- IO( |
| 81 | + certInfo.set(CertificateVersion.NAME, new CertificateVersion(CertificateVersion.V3)) |
| 82 | + ) |
| 83 | + |
| 84 | + // Set algorithm |
| 85 | + algorithm = AlgorithmId.get("SHA256WithRSA") |
| 86 | + _ <- IO(certInfo.set(CertificateAlgorithmId.NAME, new CertificateAlgorithmId(algorithm))) |
| 87 | + |
| 88 | + // Sign the certificate |
| 89 | + cert = new X509CertImpl(certInfo) |
| 90 | + _ <- IO(cert.sign(keyPair.getPrivate(), "SHA256WithRSA")) |
| 91 | + |
| 92 | + // Convert to PEM format |
| 93 | + certBytes = cert.getEncoded |
| 94 | + certPem = pemEncode("CERTIFICATE", certBytes) |
| 95 | + |
| 96 | + // Convert private key to PEM format |
| 97 | + privateKeyBytes = keyPair.getPrivate().getEncoded() |
| 98 | + keyPem = pemEncode("PRIVATE KEY", privateKeyBytes) |
| 99 | + |
| 100 | + } yield TestCertificateProvider.CertificatePair( |
| 101 | + certificate = ByteVector.view(certBytes), |
| 102 | + privateKey = ByteVector.view(privateKeyBytes), |
| 103 | + certificateString = certPem, |
| 104 | + privateKeyString = keyPem |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + /** Encodes binary data in PEM format |
| 109 | + */ |
| 110 | + private def pemEncode(header: String, data: Array[Byte]): String = { |
| 111 | + val base64 = java.util.Base64.getEncoder.encodeToString(data) |
| 112 | + val lines = base64.grouped(64).toList |
| 113 | + s"""-----BEGIN $header----- |
| 114 | + |${lines.mkString("\n")} |
| 115 | + |-----END $header-----""".stripMargin |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments