summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/libpkix/pkix_pl_nss/module/pkix_pl_nsscontext.c
blob: 171427558be2150079f147244ebdb61f60f3811e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * pkix_pl_nsscontext.c
 *
 * NSSContext Function Definitions
 *
 */


#include "pkix_pl_nsscontext.h"

#define PKIX_DEFAULT_MAX_RESPONSE_LENGTH               64 * 1024
#define PKIX_DEFAULT_COMM_TIMEOUT_SECONDS              60

#define PKIX_DEFAULT_CRL_RELOAD_DELAY_SECONDS        6 * 24 * 60 * 60
#define PKIX_DEFAULT_BAD_CRL_RELOAD_DELAY_SECONDS    60 * 60

/* --Public-NSSContext-Functions--------------------------- */

/*
 * FUNCTION: PKIX_PL_NssContext_Create
 * (see comments in pkix_samples_modules.h)
 */
PKIX_Error *
PKIX_PL_NssContext_Create(
        PKIX_UInt32 certificateUsage,
        PKIX_Boolean useNssArena,
        void *wincx,
        void **pNssContext)
{
        PKIX_PL_NssContext *context = NULL;
        PLArenaPool *arena = NULL;
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_Create");
        PKIX_NULLCHECK_ONE(pNssContext);

        PKIX_CHECK(PKIX_PL_Malloc
                   (sizeof(PKIX_PL_NssContext), (void **)&context, NULL),
                   PKIX_MALLOCFAILED);

        if (useNssArena == PKIX_TRUE) {
                PKIX_CONTEXT_DEBUG("\t\tCalling PORT_NewArena\n");
                arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
        }
        
        context->arena = arena;
        context->certificateUsage = (SECCertificateUsage)certificateUsage;
        context->wincx = wincx;
        context->timeoutSeconds = PKIX_DEFAULT_COMM_TIMEOUT_SECONDS;
        context->maxResponseLength = PKIX_DEFAULT_MAX_RESPONSE_LENGTH;
        context->crlReloadDelay = PKIX_DEFAULT_CRL_RELOAD_DELAY_SECONDS;
        context->badDerCrlReloadDelay =
                             PKIX_DEFAULT_BAD_CRL_RELOAD_DELAY_SECONDS;
        context->chainVerifyCallback.isChainValid = NULL;
        context->chainVerifyCallback.isChainValidArg = NULL;
        *pNssContext = context;

cleanup:

        PKIX_RETURN(CONTEXT);
}


/*
 * FUNCTION: PKIX_PL_NssContext_Destroy
 * (see comments in pkix_samples_modules.h)
 */
PKIX_Error *
PKIX_PL_NssContext_Destroy(
        void *nssContext)
{
        void *plContext = NULL;
        PKIX_PL_NssContext *context = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_Destroy");
        PKIX_NULLCHECK_ONE(nssContext);

        context = (PKIX_PL_NssContext*)nssContext;

        if (context->arena != NULL) {
                PKIX_CONTEXT_DEBUG("\t\tCalling PORT_FreeArena\n");
                PORT_FreeArena(context->arena, PKIX_FALSE);
        }

        PKIX_PL_Free(nssContext, NULL);

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: pkix_pl_NssContext_GetCertUsage
 * DESCRIPTION:
 *
 *  This function obtains the platform-dependent SECCertificateUsage  parameter
 *  from the context object pointed to by "nssContext", storing the result at
 *  "pCertUsage".
 *
 * PARAMETERS:
 *  "nssContext"
 *      The address of the context object whose wincx parameter is to be
 *      obtained. Must be non-NULL.
 *  "pCertUsage"
 *      The address where the result is stored. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_NssContext_GetCertUsage(
        PKIX_PL_NssContext *nssContext,
        SECCertificateUsage *pCertUsage)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "pkix_pl_NssContext_GetCertUsage");
        PKIX_NULLCHECK_TWO(nssContext, pCertUsage);

        *pCertUsage = nssContext->certificateUsage;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: pkix_pl_NssContext_SetCertUsage
 * DESCRIPTION:
 *
 *  This function sets the platform-dependent SECCertificateUsage parameter in
 *  the context object pointed to by "nssContext" to the value provided in
 *  "certUsage".
 *
 * PARAMETERS:
 *  "certUsage"
 *      Platform-dependent value to be stored.
 *  "nssContext"
 *      The address of the context object whose wincx parameter is to be
 *      obtained. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_NssContext_SetCertUsage(
        SECCertificateUsage certUsage,
        PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "pkix_pl_NssContext_SetCertUsage");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->certificateUsage = certUsage;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: pkix_pl_NssContext_GetWincx
 * DESCRIPTION:
 *
 *  This function obtains the platform-dependent wincx parameter from the
 *  context object pointed to by "nssContext", storing the result at "pWincx".
 *
 * PARAMETERS:
 *  "nssContext"
 *      The address of the context object whose wincx parameter is to be
 *      obtained. Must be non-NULL.
 *  "pWincx"
 *      The address where the result is stored. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_NssContext_GetWincx(
        PKIX_PL_NssContext *nssContext,
        void **pWincx)
{
        void *plContext = NULL;
        PKIX_PL_NssContext *context = NULL;

        PKIX_ENTER(CONTEXT, "pkix_pl_NssContext_GetWincx");
        PKIX_NULLCHECK_TWO(nssContext, pWincx);

        context = (PKIX_PL_NssContext *)nssContext;

        *pWincx = context->wincx;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: pkix_pl_NssContext_SetWincx
 * DESCRIPTION:
 *
 *  This function sets the platform-dependent wincx parameter in the context
 *  object pointed to by "nssContext" to the value provided in "wincx".
 *
 * PARAMETERS:
 *  "wincx"
 *      Platform-dependent value to be stored.
 *  "nssContext"
 *      The address of the context object whose wincx parameter is to be
 *      obtained. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns NULL if the function succeeds.
 *  Returns a Fatal Error if the function fails in an unrecoverable way.
 */
PKIX_Error *
pkix_pl_NssContext_SetWincx(
        void *wincx,
        PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "pkix_pl_NssContext_SetWincx");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->wincx = wincx;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: PKIX_PL_NssContext_SetTimeout
 * DESCRIPTION:
 *
 * Sets user defined socket timeout for the validation
 * session. Default is 60 seconds.
 *
 */
PKIX_Error *
PKIX_PL_NssContext_SetTimeout(PKIX_UInt32 timeout,
                              PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_SetTimeout");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->timeoutSeconds = timeout;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: PKIX_PL_NssContext_SetMaxResponseLen
 * DESCRIPTION:
 *
 * Sets user defined maximum transmission length of a message.
 *
 */
PKIX_Error *
PKIX_PL_NssContext_SetMaxResponseLen(PKIX_UInt32 len,
                                     PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_SetMaxResponseLen");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->maxResponseLength = len;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: PKIX_PL_NssContext_SetCrlReloadDelay
 * DESCRIPTION:
 *
 * Sets user defined delay between attempts to load crl using
 * CRLDP.
 *
 */
PKIX_Error *
PKIX_PL_NssContext_SetCrlReloadDelay(PKIX_UInt32 delay,
                                     PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_SetCrlReloadDelay");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->crlReloadDelay = delay;

        PKIX_RETURN(CONTEXT);
}

/*
 * FUNCTION: PKIX_PL_NssContext_SetBadDerCrlReloadDelay
 * DESCRIPTION:
 *
 * Sets user defined delay between attempts to load crl that
 * failed to decode.
 *
 */
PKIX_Error *
PKIX_PL_NssContext_SetBadDerCrlReloadDelay(PKIX_UInt32 delay,
                                             PKIX_PL_NssContext *nssContext)
{
        void *plContext = NULL;

        PKIX_ENTER(CONTEXT, "PKIX_PL_NssContext_SetBadDerCrlReloadDelay");
        PKIX_NULLCHECK_ONE(nssContext);

        nssContext->badDerCrlReloadDelay = delay;

        PKIX_RETURN(CONTEXT);
}