public void run() throws Exception { String localhost = InetAddress.getByName("localhost").getCanonicalHostName(); HeldCertificate localhostCertificate = new HeldCertificate.Builder() .addSubjectAlternativeName(localhost) .build(); HandshakeCertificates serverCertificates = new HandshakeCertificates.Builder() .heldCertificate(localhostCertificate) .build(); MockWebServer server = new MockWebServer(); server.useHttps(serverCertificates.sslSocketFactory(), false); server.enqueue(new MockResponse()); HandshakeCertificates clientCertificates = new HandshakeCertificates.Builder() .addTrustedCertificate(localhostCertificate.certificate()) .build(); OkHttpClient client = new OkHttpClient.Builder() .sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager()) .build(); Call call = client.newCall(new Request.Builder() .url(server.url("/")) .build()); Response response = call.execute(); System.out.println(response.handshake().tlsVersion()); }
: generateKeyPair(); X500Principal subject = buildSubject();
/** Returns an SSL client for this host's localhost address. */ public static synchronized HandshakeCertificates localhost() { if (localhost != null) return localhost; try { // Generate a self-signed cert for the server to serve and the client to trust. HeldCertificate heldCertificate = new HeldCertificate.Builder() .commonName("localhost") .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) .build(); localhost = new HandshakeCertificates.Builder() .heldCertificate(heldCertificate) .addTrustedCertificate(heldCertificate.certificate()) .build(); return localhost; } catch (UnknownHostException e) { throw new RuntimeException(e); } }
: generateKeyPair(); X500Principal subject = buildSubject();
/** Returns an SSL client for this host's localhost address. */ public static synchronized HandshakeCertificates localhost() { if (localhost != null) return localhost; try { // Generate a self-signed cert for the server to serve and the client to trust. HeldCertificate heldCertificate = new HeldCertificate.Builder() .commonName("localhost") .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) .build(); localhost = new HandshakeCertificates.Builder() .heldCertificate(heldCertificate) .addTrustedCertificate(heldCertificate.certificate()) .build(); return localhost; } catch (UnknownHostException e) { throw new RuntimeException(e); } }
/** Sets this certificate's serial number. If unset the serial number will be 1. */ public Builder serialNumber(long serialNumber) { return serialNumber(BigInteger.valueOf(serialNumber)); }
public Builder() { ecdsa256(); }
/** * Sets the public/private key pair used for this certificate. If unset a key pair will be * generated. */ public Builder keyPair(PublicKey publicKey, PrivateKey privateKey) { return keyPair(new KeyPair(publicKey, privateKey)); }
/** * Sets the certificate to be valid immediately and until the specified duration has elapsed. * The precision of this field is seconds; further precision will be truncated. */ public Builder duration(long duration, TimeUnit unit) { long now = System.currentTimeMillis(); return validityInterval(now, now + unit.toMillis(duration)); }
public Builder() { ecdsa256(); }
/** * Sets the public/private key pair used for this certificate. If unset a key pair will be * generated. */ public Builder keyPair(PublicKey publicKey, PrivateKey privateKey) { return keyPair(new KeyPair(publicKey, privateKey)); }
/** Sets this certificate's serial number. If unset the serial number will be 1. */ public Builder serialNumber(long serialNumber) { return serialNumber(BigInteger.valueOf(serialNumber)); }
/** * Sets the certificate to be valid immediately and until the specified duration has elapsed. * The precision of this field is seconds; further precision will be truncated. */ public Builder duration(long duration, TimeUnit unit) { long now = System.currentTimeMillis(); return validityInterval(now, now + unit.toMillis(duration)); }