Import 1.9b4 NSS tag from cvs
[mozilla-nss.git] / security / nss / lib / certhigh / crlv2.c
blob308215d49c33e1df1be56f80e869d4606f459b29
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.6 2007/10/12 01:44:41 julien.pierre.boogz%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,
116 SEC_ASN1_GET(SEC_IntegerTemplate),
117 tmpItem);
118 } else {
119 rv = SECFailure;
122 PORT_Free (encodedExtenValue.data);
123 if (rv == SECFailure) {
124 PORT_ArenaRelease(arena, mark);
125 } else {
126 PORT_ArenaUnmark(arena, mark);
128 return (rv);
131 SECStatus CERT_FindCRLEntryReasonExten (CERTCrlEntry *crlEntry,
132 CERTCRLEntryReasonCode *value)
134 SECItem wrapperItem = {siBuffer,0};
135 SECItem tmpItem = {siBuffer,0};
136 SECStatus rv;
137 PRArenaPool *arena = NULL;
139 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
140 if ( ! arena ) {
141 return(SECFailure);
144 rv = cert_FindExtension(crlEntry->extensions, SEC_OID_X509_REASON_CODE,
145 &wrapperItem);
146 if ( rv != SECSuccess ) {
147 goto loser;
150 rv = SEC_QuickDERDecodeItem(arena, &tmpItem,
151 SEC_ASN1_GET(SEC_EnumeratedTemplate),
152 &wrapperItem);
154 if ( rv != SECSuccess ) {
155 goto loser;
158 *value = (CERTCRLEntryReasonCode) DER_GetInteger(&tmpItem);
160 loser:
161 if ( arena ) {
162 PORT_FreeArena(arena, PR_FALSE);
165 if ( wrapperItem.data ) {
166 PORT_Free(wrapperItem.data);
169 return (rv);
172 SECStatus CERT_FindInvalidDateExten (CERTCrl *crl, int64 *value)
174 SECItem encodedExtenValue;
175 SECItem decodedExtenValue = {siBuffer,0};
176 SECStatus rv;
178 encodedExtenValue.data = decodedExtenValue.data = NULL;
179 encodedExtenValue.len = decodedExtenValue.len = 0;
181 rv = cert_FindExtension
182 (crl->extensions, SEC_OID_X509_INVALID_DATE, &encodedExtenValue);
183 if ( rv != SECSuccess )
184 return (rv);
186 rv = SEC_ASN1DecodeItem (NULL, &decodedExtenValue,
187 SEC_ASN1_GET(SEC_GeneralizedTimeTemplate),
188 &encodedExtenValue);
189 if (rv == SECSuccess)
190 rv = DER_GeneralizedTimeToTime(value, &encodedExtenValue);
191 PORT_Free (decodedExtenValue.data);
192 PORT_Free (encodedExtenValue.data);
193 return (rv);