src/java.base/share/classes/sun/security/ssl/StatusResponseManager.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 51398 3c389a284345
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     4
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    10
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    15
 * accompanied this code).
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    16
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    20
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    23
 * questions.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    24
 */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    25
package sun.security.ssl;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    26
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    27
import java.io.IOException;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    28
import java.net.URI;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    29
import java.net.URISyntaxException;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    30
import java.security.AccessController;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    31
import java.security.cert.Extension;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    33
import java.util.ArrayList;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    34
import java.util.Collections;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    35
import java.util.Date;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    36
import java.util.HashMap;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    37
import java.util.List;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    38
import java.util.Map;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    39
import java.util.Objects;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    40
import java.util.concurrent.Callable;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    41
import java.util.concurrent.ExecutionException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    42
import java.util.concurrent.Executors;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    43
import java.util.concurrent.Future;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    44
import java.util.concurrent.ScheduledThreadPoolExecutor;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    45
import java.util.concurrent.ThreadFactory;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    46
import java.util.concurrent.ThreadPoolExecutor;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    47
import java.util.concurrent.TimeUnit;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    48
import sun.security.action.GetBooleanAction;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    49
import sun.security.action.GetIntegerAction;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    50
import sun.security.action.GetPropertyAction;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    51
import sun.security.provider.certpath.CertId;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    52
import sun.security.provider.certpath.OCSP;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    53
import sun.security.provider.certpath.OCSPResponse;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    54
import sun.security.provider.certpath.ResponderId;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    55
import sun.security.util.Cache;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    56
import sun.security.x509.PKIXExtensions;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    57
import sun.security.x509.SerialNumber;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    58
import sun.security.ssl.X509Authentication.X509Possession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    59
import static sun.security.ssl.CertStatusExtension.*;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    60
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    61
final class StatusResponseManager {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    62
    private static final int DEFAULT_CORE_THREADS = 8;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    63
    private static final int DEFAULT_CACHE_SIZE = 256;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    64
    private static final int DEFAULT_CACHE_LIFETIME = 3600;     // seconds
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    65
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    66
    private final ScheduledThreadPoolExecutor threadMgr;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    67
    private final Cache<CertId, ResponseCacheEntry> responseCache;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    68
    private final URI defaultResponder;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    69
    private final boolean respOverride;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    70
    private final int cacheCapacity;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    71
    private final int cacheLifetime;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    72
    private final boolean ignoreExtensions;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    73
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    74
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    75
     * Create a StatusResponseManager with default parameters.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    76
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    77
    StatusResponseManager() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    78
        int cap = AccessController.doPrivileged(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    79
                new GetIntegerAction("jdk.tls.stapling.cacheSize",
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    80
                    DEFAULT_CACHE_SIZE));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    81
        cacheCapacity = cap > 0 ? cap : 0;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    82
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    83
        int life = AccessController.doPrivileged(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    84
                new GetIntegerAction("jdk.tls.stapling.cacheLifetime",
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    85
                    DEFAULT_CACHE_LIFETIME));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    86
        cacheLifetime = life > 0 ? life : 0;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    87
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    88
        String uriStr = GetPropertyAction
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    89
                .privilegedGetProperty("jdk.tls.stapling.responderURI");
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    90
        URI tmpURI;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    91
        try {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    92
            tmpURI = ((uriStr != null && !uriStr.isEmpty()) ?
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    93
                    new URI(uriStr) : null);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    94
        } catch (URISyntaxException urise) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    95
            tmpURI = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    96
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    97
        defaultResponder = tmpURI;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    98
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
    99
        respOverride = AccessController.doPrivileged(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   100
                new GetBooleanAction("jdk.tls.stapling.responderOverride"));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   101
        ignoreExtensions = AccessController.doPrivileged(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   102
                new GetBooleanAction("jdk.tls.stapling.ignoreExtensions"));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   103
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   104
        threadMgr = new ScheduledThreadPoolExecutor(DEFAULT_CORE_THREADS,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   105
                new ThreadFactory() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   106
            @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   107
            public Thread newThread(Runnable r) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   108
                Thread t = Executors.defaultThreadFactory().newThread(r);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   109
                t.setDaemon(true);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   110
                return t;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   111
            }
39317
fbda4d400372 8143302: javax/net/ssl/Stapling/SSLSocketWithStapling.java fails intermittently: Server died
jnimeh
parents: 37781
diff changeset
   112
        }, new ThreadPoolExecutor.DiscardPolicy());
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   113
        threadMgr.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   114
        threadMgr.setContinueExistingPeriodicTasksAfterShutdownPolicy(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   115
                false);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   116
        threadMgr.setKeepAliveTime(5000, TimeUnit.MILLISECONDS);
39317
fbda4d400372 8143302: javax/net/ssl/Stapling/SSLSocketWithStapling.java fails intermittently: Server died
jnimeh
parents: 37781
diff changeset
   117
        threadMgr.allowCoreThreadTimeOut(true);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   118
        responseCache = Cache.newSoftMemoryCache(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   119
                cacheCapacity, cacheLifetime);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   120
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   121
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   122
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   123
     * Get the current cache lifetime setting
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   124
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   125
     * @return the current cache lifetime value
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   126
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   127
    int getCacheLifetime() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   128
        return cacheLifetime;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   129
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   130
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   131
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   132
     * Get the current maximum cache size.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   133
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   134
     * @return the current maximum cache size
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   135
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   136
    int getCacheCapacity() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   137
        return cacheCapacity;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   138
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   139
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   140
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   141
     * Get the default OCSP responder URI, if previously set.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   142
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   143
     * @return the current default OCSP responder URI, or {@code null} if
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   144
     *      it has not been set.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   145
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   146
    URI getDefaultResponder() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   147
        return defaultResponder;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   148
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   149
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   150
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   151
     * Get the URI override setting
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   152
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   153
     * @return {@code true} if URI override has been set, {@code false}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   154
     * otherwise.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   155
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   156
    boolean getURIOverride() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   157
        return respOverride;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   158
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   159
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   160
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   161
     * Get the ignore extensions setting.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   162
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   163
     * @return {@code true} if the {@code StatusResponseManager} will not
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   164
     * pass OCSP Extensions in the TLS {@code status_request[_v2]}
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   165
     * extensions, {@code false} if extensions will be passed (the default).
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   166
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   167
    boolean getIgnoreExtensions() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   168
        return ignoreExtensions;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   169
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   170
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   171
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   172
     * Clear the status response cache
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   173
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   174
    void clear() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   175
        if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   176
            SSLLogger.fine("Clearing response cache");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   177
        }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   178
        responseCache.clear();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   179
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   180
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   181
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   182
     * Returns the number of currently valid objects in the response cache.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   183
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   184
     * @return the number of valid objects in the response cache.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   185
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   186
    int size() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   187
        return responseCache.size();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   188
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   189
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   190
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   191
     * Obtain the URI use by the {@code StatusResponseManager} during
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   192
     * lookups.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   193
     *
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   194
     * This method takes into account not only the AIA extension from a
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   195
     * certificate to be checked, but also any default URI and possible
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   196
     * override settings for the response manager.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   197
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   198
     * @param cert the subject to get the responder URI from
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   199
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   200
     * @return a {@code URI} containing the address to the OCSP responder,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   201
     *      or {@code null} if no AIA extension exists in the certificate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   202
     *      and no default responder has been configured.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   203
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   204
     * @throws NullPointerException if {@code cert} is {@code null}.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   205
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   206
    URI getURI(X509Certificate cert) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   207
        Objects.requireNonNull(cert);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   208
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   209
        if (cert.getExtensionValue(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   210
                PKIXExtensions.OCSPNoCheck_Id.toString()) != null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   211
            if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   212
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   213
                    "OCSP NoCheck extension found.  OCSP will be skipped");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   214
            }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   215
            return null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   216
        } else if (defaultResponder != null && respOverride) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   217
            if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   218
              SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   219
                    "Responder override: URI is " + defaultResponder);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   220
            }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   221
            return defaultResponder;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   222
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   223
            URI certURI = OCSP.getResponderURI(cert);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   224
            return (certURI != null ? certURI : defaultResponder);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   225
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   226
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   227
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   228
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   229
     * Shutdown the thread pool
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   230
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   231
    void shutdown() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   232
        if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   233
            SSLLogger.fine("Shutting down " + threadMgr.getActiveCount() +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   234
                    " active threads");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   235
        }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   236
        threadMgr.shutdown();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   237
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   238
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   239
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   240
     * Get a list of responses for a chain of certificates.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   241
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   242
     * This will find OCSP responses from the cache, or failing that,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   243
     * directly contact the OCSP responder.  It is assumed that the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   244
     * certificates in the provided chain are in their proper order
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   245
     * (from end-entity to trust anchor).
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   246
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   247
     * @param type the type of request being made of the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   248
     *      {@code StatusResponseManager}
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   249
     * @param request the {@code CertStatusRequest} from the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   250
     *      status_request or status_request_v2 ClientHello extension.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   251
     *      A value of {@code null} is interpreted as providing no
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   252
     *      responder IDs or extensions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   253
     * @param chain an array of 2 or more certificates.  Each certificate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   254
     *      must be issued by the next certificate in the chain.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   255
     * @param delay the number of time units to delay before returning
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   256
     *      responses.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   257
     * @param unit the unit of time applied to the {@code delay} parameter
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   258
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   259
     * @return an unmodifiable {@code Map} containing the certificate and
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   260
     *      its usually
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   261
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   262
     * @throws SSLHandshakeException if an unsupported
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   263
     *      {@code CertStatusRequest} is provided.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   264
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   265
    Map<X509Certificate, byte[]> get(CertStatusRequestType type,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   266
            CertStatusRequest request, X509Certificate[] chain, long delay,
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   267
            TimeUnit unit) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   268
        Map<X509Certificate, byte[]> responseMap = new HashMap<>();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   269
        List<OCSPFetchCall> requestList = new ArrayList<>();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   270
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   271
        if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   272
            SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   273
                "Beginning check: Type = " + type + ", Chain length = " +
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   274
                chain.length);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   275
        }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   276
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   277
        // It is assumed that the caller has ordered the certs in the chain
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   278
        // in the proper order (each certificate is issued by the next entry
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   279
        // in the provided chain).
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   280
        if (chain.length < 2) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   281
            return Collections.emptyMap();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   282
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   283
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   284
        if (type == CertStatusRequestType.OCSP) {
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   285
            try {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   286
                // For type OCSP, we only check the end-entity certificate
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   287
                OCSPStatusRequest ocspReq = (OCSPStatusRequest)request;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   288
                CertId cid = new CertId(chain[1],
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   289
                        new SerialNumber(chain[0].getSerialNumber()));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   290
                ResponseCacheEntry cacheEntry = getFromCache(cid, ocspReq);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   291
                if (cacheEntry != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   292
                    responseMap.put(chain[0], cacheEntry.ocspBytes);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   293
                } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   294
                    StatusInfo sInfo = new StatusInfo(chain[0], cid);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   295
                    requestList.add(new OCSPFetchCall(sInfo, ocspReq));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   296
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   297
            } catch (IOException exc) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   298
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   299
                    SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   300
                        "Exception during CertId creation: ", exc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   301
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   302
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   303
        } else if (type == CertStatusRequestType.OCSP_MULTI) {
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   304
            // For type OCSP_MULTI, we check every cert in the chain that
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   305
            // has a direct issuer at the next index.  We won't have an
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   306
            // issuer certificate for the last certificate in the chain
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   307
            // and will not be able to create a CertId because of that.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   308
            OCSPStatusRequest ocspReq = (OCSPStatusRequest)request;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   309
            int ctr;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   310
            for (ctr = 0; ctr < chain.length - 1; ctr++) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   311
                try {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   312
                    // The cert at "ctr" is the subject cert, "ctr + 1"
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   313
                    // is the issuer certificate.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   314
                    CertId cid = new CertId(chain[ctr + 1],
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   315
                        new SerialNumber(chain[ctr].getSerialNumber()));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   316
                    ResponseCacheEntry cacheEntry =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   317
                        getFromCache(cid, ocspReq);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   318
                    if (cacheEntry != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   319
                        responseMap.put(chain[ctr], cacheEntry.ocspBytes);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   320
                    } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   321
                        StatusInfo sInfo = new StatusInfo(chain[ctr], cid);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   322
                        requestList.add(new OCSPFetchCall(sInfo, ocspReq));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   323
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   324
                } catch (IOException exc) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   325
                    if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   326
                        SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   327
                            "Exception during CertId creation: ", exc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   328
                    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   329
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   330
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   331
        } else {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   332
            if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   333
                SSLLogger.fine("Unsupported status request type: " + type);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   334
            }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   335
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   336
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   337
        // If we were able to create one or more Fetches, go and run all
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   338
        // of them in separate threads.  For all the threads that completed
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   339
        // in the allotted time, put those status responses into the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   340
        // returned Map.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   341
        if (!requestList.isEmpty()) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   342
            try {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   343
                // Set a bunch of threads to go do the fetching
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   344
                List<Future<StatusInfo>> resultList =
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   345
                        threadMgr.invokeAll(requestList, delay, unit);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   346
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   347
                // Go through the Futures and from any non-cancelled task,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   348
                // get the bytes and attach them to the responseMap.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   349
                for (Future<StatusInfo> task : resultList) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   350
                    if (!task.isDone()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   351
                        continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   352
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   353
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   354
                    if (!task.isCancelled()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   355
                        StatusInfo info = task.get();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   356
                        if (info != null && info.responseData != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   357
                            responseMap.put(info.cert,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   358
                                    info.responseData.ocspBytes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   359
                        } else if (SSLLogger.isOn &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   360
                                SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   361
                            SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   362
                                "Completed task had no response data");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   363
                        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   364
                    } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   365
                        if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   366
                            SSLLogger.fine("Found cancelled task");
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   367
                        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   368
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   369
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   370
            } catch (InterruptedException | ExecutionException exc) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   371
                // Not sure what else to do here
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   372
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   373
                    SSLLogger.fine("Exception when getting data: ", exc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   374
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   375
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   376
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   377
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   378
        return Collections.unmodifiableMap(responseMap);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   379
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   380
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   381
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   382
     * Check the cache for a given {@code CertId}.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   383
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   384
     * @param cid the CertId of the response to look up
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   385
     * @param ocspRequest the OCSP request structure sent by the client
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   386
     *      in the TLS status_request[_v2] hello extension.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   387
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   388
     * @return the {@code ResponseCacheEntry} for a specific CertId, or
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   389
     *      {@code null} if it is not found or a nonce extension has been
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   390
     *      requested by the caller.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   391
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   392
    private ResponseCacheEntry getFromCache(CertId cid,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   393
            OCSPStatusRequest ocspRequest) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   394
        // Determine if the nonce extension is present in the request.  If
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   395
        // so, then do not attempt to retrieve the response from the cache.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   396
        for (Extension ext : ocspRequest.extensions) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   397
            if (ext.getId().equals(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   398
                    PKIXExtensions.OCSPNonce_Id.toString())) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   399
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   400
                    SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   401
                            "Nonce extension found, skipping cache check");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   402
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   403
                return null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   404
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   405
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   406
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   407
        ResponseCacheEntry respEntry = responseCache.get(cid);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   408
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   409
        // If the response entry has a nextUpdate and it has expired
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   410
        // before the cache expiration, purge it from the cache
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   411
        // and do not return it as a cache hit.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   412
        if (respEntry != null && respEntry.nextUpdate != null &&
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   413
                respEntry.nextUpdate.before(new Date())) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   414
            if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   415
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   416
                    "nextUpdate threshold exceeded, purging from cache");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   417
            }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   418
            respEntry = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   419
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   420
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   421
        if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   422
            SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   423
                    "Check cache for SN" + cid.getSerialNumber() + ": " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   424
                    (respEntry != null ? "HIT" : "MISS"));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   425
        }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   426
        return respEntry;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   427
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   428
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   429
    @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   430
    public String toString() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   431
        StringBuilder sb = new StringBuilder("StatusResponseManager: ");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   432
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   433
        sb.append("Core threads: ").append(threadMgr.getCorePoolSize());
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   434
        sb.append(", Cache timeout: ");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   435
        if (cacheLifetime > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   436
            sb.append(cacheLifetime).append(" seconds");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   437
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   438
            sb.append(" indefinite");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   439
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   440
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   441
        sb.append(", Cache MaxSize: ");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   442
        if (cacheCapacity > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   443
            sb.append(cacheCapacity).append(" items");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   444
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   445
            sb.append(" unbounded");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   446
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   447
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   448
        sb.append(", Default URI: ");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   449
        if (defaultResponder != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   450
            sb.append(defaultResponder);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   451
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   452
            sb.append("NONE");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   453
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   454
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   455
        return sb.toString();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   456
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   457
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   458
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   459
     * Inner class used to group request and response data.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   460
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   461
    class StatusInfo {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   462
        final X509Certificate cert;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   463
        final CertId cid;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   464
        final URI responder;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   465
        ResponseCacheEntry responseData;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   466
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   467
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   468
         * Create a StatusInfo object from certificate data.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   469
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   470
         * @param subjectCert the certificate to be checked for revocation
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   471
         * @param issuerCert the issuer of the {@code subjectCert}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   472
         *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   473
         * @throws IOException if CertId creation from the certificate fails
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   474
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   475
        StatusInfo(X509Certificate subjectCert, X509Certificate issuerCert)
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   476
                throws IOException {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   477
            this(subjectCert, new CertId(issuerCert,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   478
                    new SerialNumber(subjectCert.getSerialNumber())));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   479
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   480
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   481
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   482
         * Create a StatusInfo object from an existing subject certificate
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   483
         * and its corresponding CertId.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   484
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   485
         * @param subjectCert the certificate to be checked for revocation
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   486
         * @param cid the CertId for {@code subjectCert}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   487
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   488
        StatusInfo(X509Certificate subjectCert, CertId certId) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   489
            cert = subjectCert;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   490
            cid = certId;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   491
            responder = getURI(cert);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   492
            responseData = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   493
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   494
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   495
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   496
         * Copy constructor (used primarily for rescheduling).
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   497
         * This will do a member-wise copy with the exception of the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   498
         * responseData and extensions fields, which should not persist
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   499
         * in a rescheduled fetch.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   500
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   501
         * @param orig the original {@code StatusInfo}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   502
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   503
        StatusInfo(StatusInfo orig) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   504
            this.cert = orig.cert;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   505
            this.cid = orig.cid;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   506
            this.responder = orig.responder;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   507
            this.responseData = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   508
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   509
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   510
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   511
         * Return a String representation of the {@code StatusInfo}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   512
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   513
         * @return a {@code String} representation of this object
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   514
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   515
        @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   516
        public String toString() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   517
            StringBuilder sb = new StringBuilder("StatusInfo:");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   518
            sb.append("\n\tCert: ").append(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   519
                    this.cert.getSubjectX500Principal());
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   520
            sb.append("\n\tSerial: ").append(this.cert.getSerialNumber());
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   521
            sb.append("\n\tResponder: ").append(this.responder);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   522
            sb.append("\n\tResponse data: ").append(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   523
                    this.responseData != null ?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   524
                        (this.responseData.ocspBytes.length + " bytes") :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   525
                        "<NULL>");
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   526
            return sb.toString();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   527
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   528
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   529
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   530
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   531
     * Static nested class used as the data kept in the response cache.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   532
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   533
    class ResponseCacheEntry {
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   534
        final OCSPResponse.ResponseStatus status;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   535
        final byte[] ocspBytes;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   536
        final Date nextUpdate;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   537
        final OCSPResponse.SingleResponse singleResp;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   538
        final ResponderId respId;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   539
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   540
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   541
         * Create a new cache entry from the raw bytes of the response
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   542
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   543
         * @param responseBytes the DER encoding for the OCSP response
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   544
         *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   545
         * @throws IOException if an {@code OCSPResponse} cannot be
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   546
         *         created from the encoded bytes.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   547
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   548
        ResponseCacheEntry(byte[] responseBytes, CertId cid)
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   549
                throws IOException {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   550
            Objects.requireNonNull(responseBytes,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   551
                    "Non-null responseBytes required");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   552
            Objects.requireNonNull(cid, "Non-null Cert ID required");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   553
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   554
            ocspBytes = responseBytes.clone();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   555
            OCSPResponse oResp = new OCSPResponse(ocspBytes);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   556
            status = oResp.getResponseStatus();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   557
            respId = oResp.getResponderId();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   558
            singleResp = oResp.getSingleResponse(cid);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   559
            if (status == OCSPResponse.ResponseStatus.SUCCESSFUL) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   560
                if (singleResp != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   561
                    // Pull out the nextUpdate field in advance because the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   562
                    // Date is cloned.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   563
                    nextUpdate = singleResp.getNextUpdate();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   564
                } else {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   565
                    throw new IOException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   566
                            "Unable to find SingleResponse for SN " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   567
                            cid.getSerialNumber());
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   568
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   569
            } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   570
                nextUpdate = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   571
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   572
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   573
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   574
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   575
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   576
     * Inner Callable class that does the actual work of looking up OCSP
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   577
     * responses, first looking at the cache and doing OCSP requests if
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   578
     * a cache miss occurs.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   579
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   580
    class OCSPFetchCall implements Callable<StatusInfo> {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   581
        StatusInfo statInfo;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   582
        OCSPStatusRequest ocspRequest;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   583
        List<Extension> extensions;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   584
        List<ResponderId> responderIds;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   585
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   586
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   587
         * A constructor that builds the OCSPFetchCall from the provided
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   588
         * StatusInfo and information from the status_request[_v2]
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   589
         * extension.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   590
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   591
         * @param info the {@code StatusInfo} containing the subject
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   592
         * certificate, CertId, and other supplemental info.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   593
         * @param request the {@code OCSPStatusRequest} containing any
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   594
         * responder IDs and extensions.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   595
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   596
        public OCSPFetchCall(StatusInfo info, OCSPStatusRequest request) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   597
            statInfo = Objects.requireNonNull(info,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   598
                    "Null StatusInfo not allowed");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   599
            ocspRequest = Objects.requireNonNull(request,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   600
                    "Null OCSPStatusRequest not allowed");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   601
            extensions = ocspRequest.extensions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   602
            responderIds = ocspRequest.responderIds;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   603
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   604
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   605
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   606
         * Get an OCSP response, either from the cache or from a responder.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   607
         *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   608
         * @return The StatusInfo object passed into the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   609
         *         {@code OCSPFetchCall} constructor, with the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   610
         *         {@code responseData} field filled in with the response
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   611
         *         or {@code null} if no response can be obtained.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   612
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   613
        @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   614
        public StatusInfo call() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   615
            if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   616
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   617
                    "Starting fetch for SN " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   618
                    statInfo.cid.getSerialNumber());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   619
            }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   620
            try {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   621
                ResponseCacheEntry cacheEntry;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   622
                List<Extension> extsToSend;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   623
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   624
                if (statInfo.responder == null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   625
                    // If we have no URI then there's nothing to do
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   626
                    // but return.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   627
                    if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   628
                        SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   629
                            "Null URI detected, OCSP fetch aborted");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   630
                    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   631
                    return statInfo;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   632
                } else {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   633
                    if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   634
                        SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   635
                            "Attempting fetch from " + statInfo.responder);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   636
                    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   637
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   638
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   639
                // If the StatusResponseManager has been configured to not
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   640
                // forward extensions, then set extensions to an empty
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   641
                // list.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   642
                //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   643
                // We will forward the extensions unless one of two
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   644
                // conditions occur:
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   645
                // (1) The jdk.tls.stapling.ignoreExtensions property is
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   646
                //     true, or
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   647
                // (2) There is a non-empty ResponderId list.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   648
                //
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   649
                // ResponderId selection is a feature that will be
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   650
                // supported in the future.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   651
                extsToSend = (ignoreExtensions || !responderIds.isEmpty()) ?
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   652
                        Collections.emptyList() : extensions;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   653
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   654
                byte[] respBytes = OCSP.getOCSPBytes(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   655
                        Collections.singletonList(statInfo.cid),
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   656
                        statInfo.responder, extsToSend);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   657
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   658
                if (respBytes != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   659
                    // Place the data into the response cache
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   660
                    cacheEntry = new ResponseCacheEntry(respBytes,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   661
                            statInfo.cid);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   662
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   663
                    // Get the response status and act on it appropriately
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   664
                    if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   665
                        SSLLogger.fine("OCSP Status: " + cacheEntry.status +
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   666
                            " (" + respBytes.length + " bytes)");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   667
                    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   668
                    if (cacheEntry.status ==
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   669
                            OCSPResponse.ResponseStatus.SUCCESSFUL) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   670
                        // Set the response in the returned StatusInfo
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   671
                        statInfo.responseData = cacheEntry;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   672
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   673
                        // Add the response to the cache (if applicable)
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   674
                        addToCache(statInfo.cid, cacheEntry);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   675
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   676
                } else {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   677
                    if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   678
                        SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   679
                            "No data returned from OCSP Responder");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   680
                    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   681
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   682
            } catch (IOException ioe) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   683
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   684
                    SSLLogger.fine("Caught exception: ", ioe);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   685
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   686
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   687
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   688
            return statInfo;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   689
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   690
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   691
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   692
         * Add a response to the cache.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   693
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   694
         * @param certId The {@code CertId} for the OCSP response
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   695
         * @param entry A cache entry containing the response bytes and
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   696
         *      the {@code OCSPResponse} built from those bytes.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   697
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   698
        private void addToCache(CertId certId, ResponseCacheEntry entry) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   699
            // If no cache lifetime has been set on entries then
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   700
            // don't cache this response if there is no nextUpdate field
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   701
            if (entry.nextUpdate == null && cacheLifetime == 0) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   702
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   703
                    SSLLogger.fine("Not caching this OCSP response");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   704
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   705
            } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   706
                responseCache.put(certId, entry);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   707
                if (SSLLogger.isOn && SSLLogger.isOn("respmgr")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   708
                    SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   709
                        "Added response for SN " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   710
                        certId.getSerialNumber() +
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   711
                        " to cache");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   712
                }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   713
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   714
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   715
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   716
        /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   717
         * Determine the delay to use when scheduling the task that will
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   718
         * update the OCSP response.  This is the shorter time between the
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   719
         * cache lifetime and the nextUpdate.  If no nextUpdate is present
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   720
         * in the response, then only the cache lifetime is used.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   721
         * If cache timeouts are disabled (a zero value) and there's no
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   722
         * nextUpdate, then the entry is not cached and no rescheduling
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   723
         * will take place.
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   724
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   725
         * @param nextUpdate a {@code Date} object corresponding to the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   726
         *      next update time from a SingleResponse.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   727
         *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   728
         * @return the number of seconds of delay before the next fetch
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   729
         *      should be executed.  A zero value means that the fetch
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   730
         *      should happen immediately, while a value less than zero
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   731
         *      indicates no rescheduling should be done.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   732
         */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   733
        private long getNextTaskDelay(Date nextUpdate) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   734
            long delaySec;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   735
            int lifetime = getCacheLifetime();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   736
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   737
            if (nextUpdate != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   738
                long nuDiffSec = (nextUpdate.getTime() -
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   739
                        System.currentTimeMillis()) / 1000;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   740
                delaySec = lifetime > 0 ? Long.min(nuDiffSec, lifetime) :
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   741
                        nuDiffSec;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   742
            } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   743
                delaySec = lifetime > 0 ? lifetime : -1;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   744
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   745
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   746
            return delaySec;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   747
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   748
    }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   749
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   750
    static final StaplingParameters processStapling(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   751
            ServerHandshakeContext shc) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   752
        StaplingParameters params = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   753
        SSLExtension ext = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   754
        CertStatusRequestType type = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   755
        CertStatusRequest req = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   756
        Map<X509Certificate, byte[]> responses;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   757
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   758
        // If this feature has not been enabled, then no more processing
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   759
        // is necessary.  Also we will only staple if we're doing a full
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   760
        // handshake.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   761
        if (!shc.sslContext.isStaplingEnabled(false) || shc.isResumption) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   762
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   763
                SSLLogger.fine("Staping disabled or is a resumed session");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   764
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   765
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   766
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   767
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   768
        // Check if the client has asserted the status_request[_v2] extension(s)
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   769
        Map<SSLExtension, SSLExtension.SSLExtensionSpec> exts =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   770
                shc.handshakeExtensions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   771
        CertStatusRequestSpec statReq = (CertStatusRequestSpec)exts.get(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   772
                SSLExtension.CH_STATUS_REQUEST);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   773
        CertStatusRequestV2Spec statReqV2 = (CertStatusRequestV2Spec)
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   774
                exts.get(SSLExtension.CH_STATUS_REQUEST_V2);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   775
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   776
        // Determine which type of stapling we are doing and assert the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   777
        // proper extension in the server hello.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   778
        // Favor status_request_v2 over status_request and ocsp_multi
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   779
        // over ocsp.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   780
        // If multiple ocsp or ocsp_multi types exist, select the first
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   781
        // instance of a given type.  Also since we don't support ResponderId
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   782
        // selection yet, only accept a request if the ResponderId field
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   783
        // is empty.  Finally, we'll only do this in (D)TLS 1.2 or earlier.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   784
        if (statReqV2 != null && !shc.negotiatedProtocol.useTLS13PlusSpec()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   785
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake,verbose")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   786
                SSLLogger.fine("SH Processing status_request_v2 extension");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   787
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   788
            // RFC 6961 stapling
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   789
            ext = SSLExtension.CH_STATUS_REQUEST_V2;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   790
            int ocspIdx = -1;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   791
            int ocspMultiIdx = -1;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   792
            CertStatusRequest[] reqItems = statReqV2.certStatusRequests;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   793
            for (int pos = 0; (pos < reqItems.length &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   794
                    (ocspIdx == -1 || ocspMultiIdx == -1)); pos++) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   795
                CertStatusRequest item = reqItems[pos];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   796
                CertStatusRequestType curType =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   797
                        CertStatusRequestType.valueOf(item.statusType);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   798
                if (ocspIdx < 0 && curType == CertStatusRequestType.OCSP) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   799
                    OCSPStatusRequest ocspReq = (OCSPStatusRequest)item;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   800
                    // We currently only accept empty responder ID lists
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   801
                    // but may support them in the future
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   802
                    if (ocspReq.responderIds.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   803
                        ocspIdx = pos;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   804
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   805
                } else if (ocspMultiIdx < 0 &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   806
                        curType == CertStatusRequestType.OCSP_MULTI) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   807
                    OCSPStatusRequest ocspReq = (OCSPStatusRequest)item;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   808
                    // We currently only accept empty responder ID lists
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   809
                    // but may support them in the future
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   810
                    if (ocspReq.responderIds.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   811
                        ocspMultiIdx = pos;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   812
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   813
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   814
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   815
            if (ocspMultiIdx >= 0) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   816
                req = reqItems[ocspMultiIdx];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   817
                type = CertStatusRequestType.valueOf(req.statusType);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   818
            } else if (ocspIdx >= 0) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   819
                req = reqItems[ocspIdx];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   820
                type = CertStatusRequestType.valueOf(req.statusType);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   821
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   822
                if (SSLLogger.isOn &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   823
                        SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   824
                    SSLLogger.finest("Warning: No suitable request " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   825
                            "found in the status_request_v2 extension.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   826
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   827
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   828
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   829
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   830
        // Only attempt to process a status_request extension if:
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   831
        // * The status_request extension is set AND
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   832
        // * either the status_request_v2 extension is not present OR
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   833
        // * none of the underlying OCSPStatusRequest structures is
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   834
        // suitable for stapling.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   835
        // If either of the latter two bullet items is true the ext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   836
        // type and req variables should all be null.  If any are null
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   837
        // we will try processing an asserted status_request.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   838
        if ((statReq != null) &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   839
                (ext == null || type == null || req == null)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   840
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake,verbose")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   841
                SSLLogger.fine("SH Processing status_request extension");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   842
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   843
            ext = SSLExtension.CH_STATUS_REQUEST;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   844
            type = CertStatusRequestType.valueOf(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   845
                    statReq.statusRequest.statusType);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   846
            if (type == CertStatusRequestType.OCSP) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   847
                // If the type is OCSP, then the request is guaranteed
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   848
                // to be OCSPStatusRequest
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   849
                OCSPStatusRequest ocspReq =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   850
                        (OCSPStatusRequest)statReq.statusRequest;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   851
                if (ocspReq.responderIds.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   852
                    req = ocspReq;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   853
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   854
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   855
                        SSLLogger.finest("Warning: No suitable request " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   856
                            "found in the status_request extension.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   857
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   858
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   859
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   860
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   861
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   862
        // If, after walking through the extensions we were unable to
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   863
        // find a suitable StatusRequest, then stapling is disabled.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   864
        // The ext, type and req variables must have been set to continue.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   865
        if (type == null || req == null || ext == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   866
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   867
                SSLLogger.fine("No suitable status_request or " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   868
                        "status_request_v2, stapling is disabled");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   869
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   870
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   871
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   872
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   873
        // Get the cert chain since we'll need it for OCSP checking
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   874
        X509Possession x509Possession = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   875
        for (SSLPossession possession : shc.handshakePossessions) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   876
            if (possession instanceof X509Possession) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   877
                x509Possession = (X509Possession)possession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   878
                break;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   879
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   880
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   881
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   882
        if (x509Possession == null) {       // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   883
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   884
                SSLLogger.finest("Warning: no X.509 certificates found.  " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   885
                        "Stapling is disabled.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   886
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   887
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   888
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   889
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   890
        // Get the OCSP responses from the StatusResponseManager
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   891
        X509Certificate[] certs = x509Possession.popCerts;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   892
        StatusResponseManager statRespMgr =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   893
                shc.sslContext.getStatusResponseManager();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   894
        if (statRespMgr != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   895
            // For the purposes of the fetch from the SRM, override the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   896
            // type when it is TLS 1.3 so it always gets responses for
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   897
            // all certs it can.  This should not change the type field
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   898
            // in the StaplingParameters though.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   899
            CertStatusRequestType fetchType =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   900
                    shc.negotiatedProtocol.useTLS13PlusSpec() ?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   901
                    CertStatusRequestType.OCSP_MULTI : type;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   902
            responses = statRespMgr.get(fetchType, req, certs,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   903
                    shc.statusRespTimeout, TimeUnit.MILLISECONDS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   904
            if (!responses.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   905
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   906
                    SSLLogger.finest("Response manager returned " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   907
                            responses.size() + " entries.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   908
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   909
                // If this RFC 6066-style stapling (SSL cert only) then the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   910
                // response cannot be zero length
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   911
                if (type == CertStatusRequestType.OCSP) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   912
                    byte[] respDER = responses.get(certs[0]);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   913
                    if (respDER == null || respDER.length <= 0) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   914
                        if (SSLLogger.isOn &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   915
                                SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   916
                            SSLLogger.finest("Warning: Null or zero-length " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   917
                                    "response found for leaf certificate. " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   918
                                    "Stapling is disabled.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   919
                        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   920
                        return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   921
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   922
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   923
                params = new StaplingParameters(ext, type, req, responses);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   924
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   925
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   926
                    SSLLogger.finest("Warning: no OCSP responses obtained.  " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   927
                            "Stapling is disabled.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   928
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   929
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   930
        } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   931
            // This should not happen, but if lazy initialization of the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   932
            // StatusResponseManager doesn't occur we should turn off stapling.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   933
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   934
                SSLLogger.finest("Warning: lazy initialization " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   935
                        "of the StatusResponseManager failed.  " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   936
                        "Stapling is disabled.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   937
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   938
            params = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   939
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   940
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   941
        return params;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   942
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   943
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   944
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   945
     * Inner class used to hold stapling parameters needed by the handshaker
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   946
     * when stapling is active.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   947
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   948
    static final class StaplingParameters {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   949
        final SSLExtension statusRespExt;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   950
        final CertStatusRequestType statReqType;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   951
        final CertStatusRequest statReqData;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   952
        final Map<X509Certificate, byte[]> responseMap;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   953
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   954
        StaplingParameters(SSLExtension ext, CertStatusRequestType type,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   955
                CertStatusRequest req, Map<X509Certificate, byte[]> responses) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   956
            statusRespExt = ext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   957
            statReqType = type;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   958
            statReqData = req;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   959
            responseMap = responses;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   960
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   961
    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents:
diff changeset
   962
}
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   963