Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / libpkix / pkix / checker / pkix_expirationchecker.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the PKIX-C library.
15  *
16  * The Initial Developer of the Original Code is
17  * Sun Microsystems, Inc.
18  * Portions created by the Initial Developer are
19  * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Sun Microsystems, Inc.
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 /*
38  * pkix_expirationchecker.c
39  *
40  * Functions for expiration validation
41  *
42  */
43
44
45 #include "pkix_expirationchecker.h"
46
47 /* --Private-Functions-------------------------------------------- */
48
49 /*
50  * FUNCTION: pkix_ExpirationChecker_Check
51  * (see comments for PKIX_CertChainChecker_CheckCallback in pkix_checker.h)
52  */
53 PKIX_Error *
54 pkix_ExpirationChecker_Check(
55         PKIX_CertChainChecker *checker,
56         PKIX_PL_Cert *cert,
57         PKIX_List *unresolvedCriticalExtensions,
58         void **pNBIOContext,
59         void *plContext)
60 {
61         PKIX_PL_Date *testDate = NULL;
62
63         PKIX_ENTER(CERTCHAINCHECKER, "pkix_ExpirationChecker_Check");
64         PKIX_NULLCHECK_THREE(checker, cert, pNBIOContext);
65
66         *pNBIOContext = NULL; /* we never block on pending I/O */
67
68         PKIX_CHECK(PKIX_CertChainChecker_GetCertChainCheckerState
69                     (checker, (PKIX_PL_Object **)&testDate, plContext),
70                     PKIX_CERTCHAINCHECKERGETCERTCHAINCHECKERSTATEFAILED);
71
72         PKIX_CHECK(PKIX_PL_Cert_CheckValidity(cert, testDate, plContext),
73                     PKIX_CERTCHECKVALIDITYFAILED);
74
75 cleanup:
76
77         PKIX_DECREF(testDate);
78
79         PKIX_RETURN(CERTCHAINCHECKER);
80
81 }
82
83 /*
84  * FUNCTION: pkix_ExpirationChecker_Initialize
85  * DESCRIPTION:
86  *
87  *  Creates a new CertChainChecker and stores it at "pChecker", where it will
88  *  used by pkix_ExpirationChecker_Check to check that the certificate has not
89  *  expired with respect to the Date pointed to by "testDate." If "testDate"
90  *  is NULL, then the CertChainChecker will check that a certificate has not
91  *  expired with respect to the current date and time.
92  *
93  * PARAMETERS:
94  *  "testDate"
95  *      Address of Date representing the point in time at which the cert is to
96  *      be validated. If "testDate" is NULL, the current date and time is used.
97  *  "pChecker"
98  *      Address where object pointer will be stored. Must be non-NULL.
99  *  "plContext"
100  *      Platform-specific context pointer.
101  * THREAD SAFETY:
102  *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
103  * RETURNS:
104  *  Returns NULL if the function succeeds.
105  *  Returns a CertChainChecker Error if the function fails in a non-fatal way.
106  *  Returns a Fatal Error if the function fails in an unrecoverable way.
107  */
108 PKIX_Error *
109 pkix_ExpirationChecker_Initialize(
110         PKIX_PL_Date *testDate,
111         PKIX_CertChainChecker **pChecker,
112         void *plContext)
113 {
114         PKIX_PL_Date *myDate = NULL;
115         PKIX_PL_Date *nowDate = NULL;
116
117         PKIX_ENTER(CERTCHAINCHECKER, "pkix_ExpirationChecker_Initialize");
118         PKIX_NULLCHECK_ONE(pChecker);
119
120         /* if testDate is NULL, we use the current time */
121         if (!testDate){
122                 PKIX_CHECK(PKIX_PL_Date_Create_UTCTime
123                             (NULL, &nowDate, plContext),
124                             PKIX_DATECREATEUTCTIMEFAILED);
125                 myDate = nowDate;
126         } else {
127                 myDate = testDate;
128         }
129
130         PKIX_CHECK(PKIX_CertChainChecker_Create
131                     (pkix_ExpirationChecker_Check,
132                     PKIX_TRUE,
133                     PKIX_FALSE,
134                     NULL,
135                     (PKIX_PL_Object *)myDate,
136                     pChecker,
137                     plContext),
138                     PKIX_CERTCHAINCHECKERCREATEFAILED);
139
140 cleanup:
141
142         PKIX_DECREF(nowDate);
143
144         PKIX_RETURN(CERTCHAINCHECKER);
145
146 }