xref: /aosp_15_r20/external/curl/lib/psl.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker 
25*6236dae4SAndroid Build Coastguard Worker #include "curl_setup.h"
26*6236dae4SAndroid Build Coastguard Worker 
27*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #ifdef USE_LIBPSL
30*6236dae4SAndroid Build Coastguard Worker 
31*6236dae4SAndroid Build Coastguard Worker #include "psl.h"
32*6236dae4SAndroid Build Coastguard Worker #include "share.h"
33*6236dae4SAndroid Build Coastguard Worker 
34*6236dae4SAndroid Build Coastguard Worker /* The last 3 #include files should be in this order */
35*6236dae4SAndroid Build Coastguard Worker #include "curl_printf.h"
36*6236dae4SAndroid Build Coastguard Worker #include "curl_memory.h"
37*6236dae4SAndroid Build Coastguard Worker #include "memdebug.h"
38*6236dae4SAndroid Build Coastguard Worker 
Curl_psl_destroy(struct PslCache * pslcache)39*6236dae4SAndroid Build Coastguard Worker void Curl_psl_destroy(struct PslCache *pslcache)
40*6236dae4SAndroid Build Coastguard Worker {
41*6236dae4SAndroid Build Coastguard Worker   if(pslcache->psl) {
42*6236dae4SAndroid Build Coastguard Worker     if(pslcache->dynamic)
43*6236dae4SAndroid Build Coastguard Worker       psl_free((psl_ctx_t *) pslcache->psl);
44*6236dae4SAndroid Build Coastguard Worker     pslcache->psl = NULL;
45*6236dae4SAndroid Build Coastguard Worker     pslcache->dynamic = FALSE;
46*6236dae4SAndroid Build Coastguard Worker   }
47*6236dae4SAndroid Build Coastguard Worker }
48*6236dae4SAndroid Build Coastguard Worker 
now_seconds(void)49*6236dae4SAndroid Build Coastguard Worker static time_t now_seconds(void)
50*6236dae4SAndroid Build Coastguard Worker {
51*6236dae4SAndroid Build Coastguard Worker   struct curltime now = Curl_now();
52*6236dae4SAndroid Build Coastguard Worker 
53*6236dae4SAndroid Build Coastguard Worker   return now.tv_sec;
54*6236dae4SAndroid Build Coastguard Worker }
55*6236dae4SAndroid Build Coastguard Worker 
Curl_psl_use(struct Curl_easy * easy)56*6236dae4SAndroid Build Coastguard Worker const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
57*6236dae4SAndroid Build Coastguard Worker {
58*6236dae4SAndroid Build Coastguard Worker   struct PslCache *pslcache = easy->psl;
59*6236dae4SAndroid Build Coastguard Worker   const psl_ctx_t *psl;
60*6236dae4SAndroid Build Coastguard Worker   time_t now;
61*6236dae4SAndroid Build Coastguard Worker 
62*6236dae4SAndroid Build Coastguard Worker   if(!pslcache)
63*6236dae4SAndroid Build Coastguard Worker     return NULL;
64*6236dae4SAndroid Build Coastguard Worker 
65*6236dae4SAndroid Build Coastguard Worker   Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
66*6236dae4SAndroid Build Coastguard Worker   now = now_seconds();
67*6236dae4SAndroid Build Coastguard Worker   if(!pslcache->psl || pslcache->expires <= now) {
68*6236dae4SAndroid Build Coastguard Worker     /* Let a chance to other threads to do the job: avoids deadlock. */
69*6236dae4SAndroid Build Coastguard Worker     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
70*6236dae4SAndroid Build Coastguard Worker 
71*6236dae4SAndroid Build Coastguard Worker     /* Update cache: this needs an exclusive lock. */
72*6236dae4SAndroid Build Coastguard Worker     Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
73*6236dae4SAndroid Build Coastguard Worker 
74*6236dae4SAndroid Build Coastguard Worker     /* Recheck in case another thread did the job. */
75*6236dae4SAndroid Build Coastguard Worker     now = now_seconds();
76*6236dae4SAndroid Build Coastguard Worker     if(!pslcache->psl || pslcache->expires <= now) {
77*6236dae4SAndroid Build Coastguard Worker       bool dynamic = FALSE;
78*6236dae4SAndroid Build Coastguard Worker       time_t expires = TIME_T_MAX;
79*6236dae4SAndroid Build Coastguard Worker 
80*6236dae4SAndroid Build Coastguard Worker #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
81*6236dae4SAndroid Build Coastguard Worker       psl = psl_latest(NULL);
82*6236dae4SAndroid Build Coastguard Worker       dynamic = psl != NULL;
83*6236dae4SAndroid Build Coastguard Worker       /* Take care of possible time computation overflow. */
84*6236dae4SAndroid Build Coastguard Worker       expires = now < TIME_T_MAX - PSL_TTL ? now + PSL_TTL : TIME_T_MAX;
85*6236dae4SAndroid Build Coastguard Worker 
86*6236dae4SAndroid Build Coastguard Worker       /* Only get the built-in PSL if we do not already have the "latest". */
87*6236dae4SAndroid Build Coastguard Worker       if(!psl && !pslcache->dynamic)
88*6236dae4SAndroid Build Coastguard Worker #endif
89*6236dae4SAndroid Build Coastguard Worker 
90*6236dae4SAndroid Build Coastguard Worker         psl = psl_builtin();
91*6236dae4SAndroid Build Coastguard Worker 
92*6236dae4SAndroid Build Coastguard Worker       if(psl) {
93*6236dae4SAndroid Build Coastguard Worker         Curl_psl_destroy(pslcache);
94*6236dae4SAndroid Build Coastguard Worker         pslcache->psl = psl;
95*6236dae4SAndroid Build Coastguard Worker         pslcache->dynamic = dynamic;
96*6236dae4SAndroid Build Coastguard Worker         pslcache->expires = expires;
97*6236dae4SAndroid Build Coastguard Worker       }
98*6236dae4SAndroid Build Coastguard Worker     }
99*6236dae4SAndroid Build Coastguard Worker     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);  /* Release exclusive lock. */
100*6236dae4SAndroid Build Coastguard Worker     Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
101*6236dae4SAndroid Build Coastguard Worker   }
102*6236dae4SAndroid Build Coastguard Worker   psl = pslcache->psl;
103*6236dae4SAndroid Build Coastguard Worker   if(!psl)
104*6236dae4SAndroid Build Coastguard Worker     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
105*6236dae4SAndroid Build Coastguard Worker   return psl;
106*6236dae4SAndroid Build Coastguard Worker }
107*6236dae4SAndroid Build Coastguard Worker 
Curl_psl_release(struct Curl_easy * easy)108*6236dae4SAndroid Build Coastguard Worker void Curl_psl_release(struct Curl_easy *easy)
109*6236dae4SAndroid Build Coastguard Worker {
110*6236dae4SAndroid Build Coastguard Worker   Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
111*6236dae4SAndroid Build Coastguard Worker }
112*6236dae4SAndroid Build Coastguard Worker 
113*6236dae4SAndroid Build Coastguard Worker #endif /* USE_LIBPSL */
114