Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / certhigh / crlv2.c
blobda848b094bd82e237ca92c3f794d670ecdc5282b
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * Code for dealing with x.509 v3 crl and crl entries extensions.
40 * $Id: crlv2.c,v 1.5 2007/05/25 07:28:32 alexei.volkov.bugs%sun.com Exp $
43 #include "cert.h"
44 #include "secitem.h"
45 #include "secoid.h"
46 #include "secoidt.h"
47 #include "secder.h"
48 #include "secasn1.h"
49 #include "certxutl.h"
51 SECStatus
52 CERT_FindCRLExtensionByOID(CERTCrl *crl, SECItem *oid, SECItem *value)
54 return (cert_FindExtensionByOID (crl->extensions, oid, value));
58 SECStatus
59 CERT_FindCRLExtension(CERTCrl *crl, int tag, SECItem *value)
61 return (cert_FindExtension (crl->extensions, tag, value));
65 /* Callback to set extensions and adjust verison */
66 static void
67 SetCrlExts(void *object, CERTCertExtension **exts)
69 CERTCrl *crl = (CERTCrl *)object;
71 crl->extensions = exts;
72 DER_SetUInteger (crl->arena, &crl->version, SEC_CRL_VERSION_2);
75 void *
76 CERT_StartCRLExtensions(CERTCrl *crl)
78 return (cert_StartExtensions ((void *)crl, crl->arena, SetCrlExts));
81 static void
82 SetCrlEntryExts(void *object, CERTCertExtension **exts)
84 CERTCrlEntry *crlEntry = (CERTCrlEntry *)object;
86 crlEntry->extensions = exts;
89 void *
90 CERT_StartCRLEntryExtensions(CERTCrl *crl, CERTCrlEntry *entry)
92 return (cert_StartExtensions (entry, crl->arena, SetCrlEntryExts));
95 SECStatus CERT_FindCRLNumberExten (PRArenaPool *arena, CERTCrl *crl,
96 SECItem *value)
98 SECItem encodedExtenValue;
99 SECItem *tmpItem = NULL;
100 SECStatus rv;
101 void *mark = NULL;
103 encodedExtenValue.data = NULL;
104 encodedExtenValue.len = 0;
106 rv = cert_FindExtension(crl->extensions, SEC_OID_X509_CRL_NUMBER,
107 &encodedExtenValue);
108 if ( rv != SECSuccess )
109 return (rv);
111 mark = PORT_ArenaMark(arena);
113 tmpItem = SECITEM_ArenaDupItem(arena, &encodedExtenValue);
114 if (tmpItem) {
115 rv = SEC_QuickDERDecodeItem (arena, value, SEC_IntegerTemplate,
116 tmpItem);
117 } else {
118 rv = SECFailure;
121 PORT_Free (encodedExtenValue.data);
122 if (rv == SECFailure) {
123 PORT_ArenaRelease(arena, mark);
124 } else {
125 PORT_ArenaUnmark(arena, mark);
127 return (rv);
130 SECStatus CERT_FindCRLEntryReasonExten (CERTCrlEntry *crlEntry,
131 CERTCRLEntryReasonCode *value)
133 SECItem wrapperItem = {siBuffer,0};
134 SECItem tmpItem = {siBuffer,0};
135 SECStatus rv;
136 PRArenaPool *arena = NULL;
138 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
139 if ( ! arena ) {
140 return(SECFailure);
143 rv = cert_FindExtension(crlEntry->extensions, SEC_OID_X509_REASON_CODE,
144 &wrapperItem);
145 if ( rv != SECSuccess ) {
146 goto loser;
149 rv = SEC_QuickDERDecodeItem(arena, &tmpItem, SEC_EnumeratedTemplate,
150 &wrapperItem);
152 if ( rv != SECSuccess ) {
153 goto loser;
156 *value = (CERTCRLEntryReasonCode) DER_GetInteger(&tmpItem);
158 loser:
159 if ( arena ) {
160 PORT_FreeArena(arena, PR_FALSE);
163 if ( wrapperItem.data ) {
164 PORT_Free(wrapperItem.data);
167 return (rv);
170 SECStatus CERT_FindInvalidDateExten (CERTCrl *crl, int64 *value)
172 SECItem encodedExtenValue;
173 SECItem decodedExtenValue = {siBuffer,0};
174 SECStatus rv;
176 encodedExtenValue.data = decodedExtenValue.data = NULL;
177 encodedExtenValue.len = decodedExtenValue.len = 0;
179 rv = cert_FindExtension
180 (crl->extensions, SEC_OID_X509_INVALID_DATE, &encodedExtenValue);
181 if ( rv != SECSuccess )
182 return (rv);
184 rv = SEC_ASN1DecodeItem (NULL, &decodedExtenValue,
185 SEC_GeneralizedTimeTemplate, &encodedExtenValue);
186 if (rv == SECSuccess)
187 rv = DER_GeneralizedTimeToTime(value, &encodedExtenValue);
188 PORT_Free (decodedExtenValue.data);
189 PORT_Free (encodedExtenValue.data);
190 return (rv);