1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.http.ssl; 29 30 import java.security.KeyManagementException; 31 import java.security.NoSuchAlgorithmException; 32 33 import javax.net.ssl.SSLContext; 34 35 /** 36 * {@link javax.net.ssl.SSLContext} factory methods. 37 * 38 * @since 4.4 39 */ 40 public class SSLContexts { 41 42 /** 43 * Creates default factory based on the standard JSSE trust material 44 * ({@code cacerts} file in the security properties directory). System properties 45 * are not taken into consideration. 46 * 47 * @return the default SSL socket factory 48 */ 49 public static SSLContext createDefault() throws SSLInitializationException { 50 try { 51 final SSLContext sslContext = SSLContext.getInstance(SSLContextBuilder.TLS); 52 sslContext.init(null, null, null); 53 return sslContext; 54 } catch (final NoSuchAlgorithmException ex) { 55 throw new SSLInitializationException(ex.getMessage(), ex); 56 } catch (final KeyManagementException ex) { 57 throw new SSLInitializationException(ex.getMessage(), ex); 58 } 59 } 60 61 /** 62 * Creates default SSL context based on system properties. This method obtains 63 * default SSL context by calling {@code SSLContext.getInstance("Default")}. 64 * Please note that {@code Default} algorithm is supported as of Java 6. 65 * This method will fall back onto {@link #createDefault()} when 66 * {@code Default} algorithm is not available. 67 * 68 * @return default system SSL context 69 */ 70 public static SSLContext createSystemDefault() throws SSLInitializationException { 71 try { 72 return SSLContext.getDefault(); 73 } catch (final NoSuchAlgorithmException ex) { 74 return createDefault(); 75 } 76 } 77 78 /** 79 * Creates custom SSL context. 80 * 81 * @return default system SSL context 82 */ 83 public static SSLContextBuilder custom() { 84 return SSLContextBuilder.create(); 85 } 86 87 }