|
81 | 81 | LogLevel]
|
82 | 82 | [java.security.cert X509Certificate]
|
83 | 83 | [java.security PrivateKey]
|
84 |
| - [javax.net.ssl SSLHandshakeException])) |
| 84 | + [javax.net.ssl |
| 85 | + SSLHandshakeException |
| 86 | + TrustManagerFactory])) |
85 | 87 |
|
86 | 88 | ;;;
|
87 | 89 |
|
|
730 | 732 |
|
731 | 733 | ;;;
|
732 | 734 |
|
733 |
| -(defn coerce-ssl-provider [provider] |
| 735 | +(defn coerce-ssl-provider ^SslProvider [provider] |
734 | 736 | (case provider
|
735 | 737 | :jdk SslProvider/JDK
|
736 | 738 | :openssl SslProvider/OPENSSL
|
737 | 739 | :openssl-refcnt SslProvider/OPENSSL_REFCNT))
|
738 | 740 |
|
739 |
| -(set! *warn-on-reflection* false) |
740 |
| - |
741 | 741 | (let [cert-array-class (class (into-array X509Certificate []))]
|
742 |
| - (defn- check-ssl-args! [private-key certificate-chain] |
743 |
| - (when-not (or |
744 |
| - (and (instance? File private-key) |
745 |
| - (instance? File certificate-chain)) |
746 |
| - (and (instance? InputStream private-key) |
747 |
| - (instance? InputStream certificate-chain)) |
748 |
| - (and (instance? PrivateKey private-key) |
749 |
| - (instance? cert-array-class certificate-chain))) |
750 |
| - (throw |
751 |
| - (IllegalArgumentException. |
752 |
| - "ssl context arguments invalid")))) |
| 742 | + (defn- add-ssl-trust-manager! ^SslContextBuilder [^SslContextBuilder builder trust-store] |
| 743 | + (cond (instance? File trust-store) |
| 744 | + (.trustManager builder ^File trust-store) |
| 745 | + (instance? InputStream trust-store) |
| 746 | + (.trustManager builder ^InputStream trust-store) |
| 747 | + (instance? TrustManagerFactory trust-store) |
| 748 | + (.trustManager builder ^TrustManagerFactory trust-store) |
| 749 | + (instance? cert-array-class trust-store) |
| 750 | + (.trustManager builder ^"[Ljava.security.cert.X509Certificate;" trust-store) |
| 751 | + (sequential? trust-store) |
| 752 | + (let [^"[Ljava.security.cert.X509Certificate;" trust-store' (into-array X509Certificate trust-store)] |
| 753 | + (.trustManager builder trust-store')) |
| 754 | + :else |
| 755 | + (throw |
| 756 | + (IllegalArgumentException. |
| 757 | + "ssl context arguments invalid")))) |
753 | 758 |
|
754 | 759 | (defn ssl-client-context
|
755 | 760 | "Creates a new client SSL context.
|
|
767 | 772 | Note that if specified, the types of `private-key` and `certificate-chain` must be \"compatible\": either both input streams, both files, or a private key and an array of certificates."
|
768 | 773 | ([] (ssl-client-context {}))
|
769 | 774 | ([{:keys [private-key
|
770 |
| - private-key-password |
| 775 | + ^String private-key-password |
771 | 776 | certificate-chain
|
772 | 777 | trust-store
|
773 | 778 | ssl-provider
|
774 |
| - ciphers |
| 779 | + ^Iterable ciphers |
775 | 780 | protocols
|
776 |
| - session-cache-size |
777 |
| - session-timeout]}] |
778 |
| - (let [^SslContextBuilder builder (SslContextBuilder/forClient) |
779 |
| - certificate-chain' (if-not (sequential? certificate-chain) |
780 |
| - certificate-chain |
781 |
| - (into-array X509Certificate certificate-chain))] |
782 |
| - (when (and private-key certificate-chain') |
783 |
| - (check-ssl-args! private-key certificate-chain') |
784 |
| - (if (instance? cert-array-class certificate-chain') |
785 |
| - (.keyManager builder |
786 |
| - private-key |
787 |
| - private-key-password |
788 |
| - certificate-chain') |
789 |
| - (.keyManager builder |
790 |
| - certificate-chain' |
791 |
| - private-key |
792 |
| - private-key-password))) |
793 |
| - |
794 |
| - (cond-> builder |
795 |
| - (some? trust-store) |
796 |
| - (.trustManager (if-not (sequential? trust-store) |
797 |
| - trust-store |
798 |
| - (into-array X509Certificate trust-store))) |
799 |
| - |
800 |
| - (some? ssl-provider) |
801 |
| - (.provider (coerce-ssl-provider ssl-provider)) |
802 |
| - |
803 |
| - (some? ciphers) |
804 |
| - (.ciphers ciphers) |
805 |
| - |
806 |
| - (some? protocols) |
807 |
| - (.protocols (into-array String protocols)) |
808 |
| - |
809 |
| - (some? session-cache-size) |
810 |
| - (.sessionCacheSize session-cache-size) |
811 |
| - |
812 |
| - (some? session-timeout) |
813 |
| - (.sessionTimeout session-timeout)) |
| 781 | + ^long session-cache-size |
| 782 | + ^long session-timeout]}] |
| 783 | + (let [^SslContextBuilder |
| 784 | + builder (SslContextBuilder/forClient) |
| 785 | + |
| 786 | + ^SslContextBuilder |
| 787 | + builder (if (or private-key certificate-chain) |
| 788 | + (cond (and (instance? File private-key) |
| 789 | + (instance? File certificate-chain)) |
| 790 | + (.keyManager builder |
| 791 | + ^File certificate-chain |
| 792 | + ^File private-key |
| 793 | + private-key-password) |
| 794 | + (and (instance? InputStream private-key) |
| 795 | + (instance? InputStream certificate-chain)) |
| 796 | + (.keyManager builder |
| 797 | + ^InputStream certificate-chain |
| 798 | + ^InputStream private-key |
| 799 | + private-key-password) |
| 800 | + (and (instance? PrivateKey private-key) |
| 801 | + (instance? cert-array-class certificate-chain)) |
| 802 | + (.keyManager builder |
| 803 | + ^PrivateKey private-key |
| 804 | + private-key-password |
| 805 | + ^"[Ljava.security.cert.X509Certificate;" certificate-chain) |
| 806 | + (and (instance? PrivateKey private-key) |
| 807 | + (sequential? certificate-chain)) |
| 808 | + (let [^"[Ljava.security.cert.X509Certificate;" certificate-chain' (into-array X509Certificate certificate-chain)] |
| 809 | + (.keyManager builder |
| 810 | + ^PrivateKey private-key |
| 811 | + private-key-password |
| 812 | + certificate-chain')) |
| 813 | + :else |
| 814 | + (throw |
| 815 | + (IllegalArgumentException. |
| 816 | + "ssl context arguments invalid"))) |
| 817 | + builder) |
| 818 | + |
| 819 | + ^SslContextBuilder |
| 820 | + builder (cond-> builder |
| 821 | + (some? trust-store) |
| 822 | + (add-ssl-trust-manager! trust-store) |
| 823 | + |
| 824 | + (some? ssl-provider) |
| 825 | + (.sslProvider (coerce-ssl-provider ssl-provider)) |
| 826 | + |
| 827 | + (some? ciphers) |
| 828 | + (.ciphers ciphers) |
| 829 | + |
| 830 | + (some? protocols) |
| 831 | + (.protocols ^"[Ljava.lang.String;" (into-array String protocols)) |
| 832 | + |
| 833 | + (some? session-cache-size) |
| 834 | + (.sessionCacheSize session-cache-size) |
| 835 | + |
| 836 | + (some? session-timeout) |
| 837 | + (.sessionTimeout session-timeout))] |
814 | 838 |
|
815 | 839 | (.build builder))))
|
816 | 840 |
|
|
832 | 856 | Note that if specified, the types of `private-key` and `certificate-chain` must be \"compatible\": either both input streams, both files, or a private key and an array of certificates."
|
833 | 857 | ([] (ssl-server-context {}))
|
834 | 858 | ([{:keys [private-key
|
835 |
| - private-key-password |
| 859 | + ^String private-key-password |
836 | 860 | certificate-chain
|
837 | 861 | trust-store
|
838 | 862 | ssl-provider
|
839 |
| - ciphers |
| 863 | + ^Iterable ciphers |
840 | 864 | protocols
|
841 |
| - session-cache-size |
842 |
| - session-timeout |
| 865 | + ^long session-cache-size |
| 866 | + ^long session-timeout |
843 | 867 | start-tls
|
844 | 868 | client-auth]}]
|
845 |
| - (let [certificate-chain' (if-not (sequential? certificate-chain) |
846 |
| - certificate-chain |
847 |
| - (into-array X509Certificate certificate-chain))] |
848 |
| - (check-ssl-args! private-key certificate-chain') |
849 |
| - (let [^SslContextBuilder |
850 |
| - b (cond-> (if (instance? cert-array-class certificate-chain') |
851 |
| - (SslContextBuilder/forServer private-key |
852 |
| - private-key-password |
853 |
| - certificate-chain') |
854 |
| - (SslContextBuilder/forServer certificate-chain' |
855 |
| - private-key |
856 |
| - private-key-password)) |
857 |
| - |
858 |
| - (some? trust-store) |
859 |
| - (.trustManager (if-not (sequential? trust-store) |
860 |
| - trust-store |
861 |
| - (into-array X509Certificate trust-store))) |
862 |
| - |
863 |
| - (some? ssl-provider) |
864 |
| - (.provider (coerce-ssl-provider ssl-provider)) |
865 |
| - |
866 |
| - (some? ciphers) |
867 |
| - (.ciphers ciphers) |
868 |
| - |
869 |
| - (some? protocols) |
870 |
| - (.protocols (into-array String protocols)) |
871 |
| - |
872 |
| - (some? session-cache-size) |
873 |
| - (.sessionCacheSize session-cache-size) |
874 |
| - |
875 |
| - (some? session-timeout) |
876 |
| - (.sessionTimeout session-timeout) |
877 |
| - |
878 |
| - (some? start-tls) |
879 |
| - (.startTls (boolean start-tls)) |
880 |
| - |
881 |
| - (some? client-auth) |
882 |
| - (.clientAuth (case client-auth |
883 |
| - :none ClientAuth/NONE |
884 |
| - :optional ClientAuth/OPTIONAL |
885 |
| - :require ClientAuth/REQUIRE)))] |
886 |
| - (.build b)))))) |
887 |
| - |
888 |
| -(set! *warn-on-reflection* true) |
| 869 | + (let [^SslContextBuilder |
| 870 | + b (cond (and (instance? File private-key) |
| 871 | + (instance? File certificate-chain)) |
| 872 | + (SslContextBuilder/forServer ^File certificate-chain |
| 873 | + ^File private-key |
| 874 | + private-key-password) |
| 875 | + (and (instance? InputStream private-key) |
| 876 | + (instance? InputStream certificate-chain)) |
| 877 | + (SslContextBuilder/forServer ^InputStream certificate-chain |
| 878 | + ^InputStream private-key |
| 879 | + private-key-password) |
| 880 | + (and (instance? PrivateKey private-key) |
| 881 | + (instance? cert-array-class certificate-chain)) |
| 882 | + (SslContextBuilder/forServer ^PrivateKey private-key |
| 883 | + private-key-password |
| 884 | + ^"[Ljava.security.cert.X509Certificate;" certificate-chain) |
| 885 | + (and (instance? PrivateKey private-key) |
| 886 | + (sequential? certificate-chain)) |
| 887 | + (let [^"[Ljava.security.cert.X509Certificate;" certificate-chain' (into-array X509Certificate certificate-chain)] |
| 888 | + (SslContextBuilder/forServer ^PrivateKey private-key |
| 889 | + private-key-password |
| 890 | + certificate-chain')) |
| 891 | + :else |
| 892 | + (throw |
| 893 | + (IllegalArgumentException. |
| 894 | + "ssl context arguments invalid"))) |
| 895 | + |
| 896 | + ^SslContextBuilder |
| 897 | + b (cond-> b |
| 898 | + (some? trust-store) |
| 899 | + (add-ssl-trust-manager! trust-store) |
| 900 | + |
| 901 | + (some? ssl-provider) |
| 902 | + (.sslProvider (coerce-ssl-provider ssl-provider)) |
| 903 | + |
| 904 | + (some? ciphers) |
| 905 | + (.ciphers ciphers) |
| 906 | + |
| 907 | + |
| 908 | + (some? protocols) |
| 909 | + (.protocols ^"[Ljava.lang.String;" (into-array String protocols)) |
| 910 | + |
| 911 | + |
| 912 | + (some? session-cache-size) |
| 913 | + (.sessionCacheSize session-cache-size) |
| 914 | + |
| 915 | + (some? session-timeout) |
| 916 | + (.sessionTimeout session-timeout) |
| 917 | + |
| 918 | + (some? start-tls) |
| 919 | + (.startTls (boolean start-tls)) |
| 920 | + |
| 921 | + (some? client-auth) |
| 922 | + (.clientAuth (case client-auth |
| 923 | + :none ClientAuth/NONE |
| 924 | + :optional ClientAuth/OPTIONAL |
| 925 | + :require ClientAuth/REQUIRE)))] |
| 926 | + (.build b))))) |
889 | 927 |
|
890 | 928 | (defn self-signed-ssl-context
|
891 | 929 | "A self-signed SSL context for servers."
|
|
0 commit comments