PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/certdb/xauthkid.c

http://github.com/zpao/v8monkey
C | 160 lines | 92 code | 20 blank | 48 comment | 23 complexity | 53e699ebdad1b3e048f5885f23f8e694 MD5 | raw file
  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 Netscape security libraries.
 15 *
 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.
 20 *
 21 * Contributor(s):
 22 *
 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.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37/*
 38 * X.509 v3 Subject Key Usage Extension 
 39 *
 40 */
 41
 42#include "prtypes.h"
 43#include "seccomon.h"
 44#include "secdert.h"
 45#include "secoidt.h"
 46#include "secasn1t.h"
 47#include "secasn1.h"
 48#include "secport.h"
 49#include "certt.h"  
 50#include "genname.h"
 51#include "secerr.h"
 52
 53SEC_ASN1_MKSUB(SEC_IntegerTemplate)
 54SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
 55
 56const SEC_ASN1Template CERTAuthKeyIDTemplate[] = {
 57    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTAuthKeyID) },
 58    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
 59	  offsetof(CERTAuthKeyID,keyID), SEC_ASN1_SUB(SEC_OctetStringTemplate)},
 60    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC  | 1,
 61          offsetof(CERTAuthKeyID, DERAuthCertIssuer), CERT_GeneralNamesTemplate},
 62    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2,
 63	  offsetof(CERTAuthKeyID,authCertSerialNumber),
 64          SEC_ASN1_SUB(SEC_IntegerTemplate) },
 65    { 0 }
 66};
 67
 68
 69
 70SECStatus CERT_EncodeAuthKeyID (PRArenaPool *arena, CERTAuthKeyID *value, SECItem *encodedValue)
 71{
 72    SECStatus rv = SECFailure;
 73 
 74    PORT_Assert (value);
 75    PORT_Assert (arena);
 76    PORT_Assert (value->DERAuthCertIssuer == NULL);
 77    PORT_Assert (encodedValue);
 78
 79    do {
 80	
 81	/* If both of the authCertIssuer and the serial number exist, encode
 82	   the name first.  Otherwise, it is an error if one exist and the other
 83	   is not.
 84	 */
 85	if (value->authCertIssuer) {
 86	    if (!value->authCertSerialNumber.data) {
 87		PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
 88		break;
 89	    }
 90
 91	    value->DERAuthCertIssuer = cert_EncodeGeneralNames
 92		(arena, value->authCertIssuer);
 93	    if (!value->DERAuthCertIssuer) {
 94		PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
 95		break;
 96	    }
 97	}
 98	else if (value->authCertSerialNumber.data) {
 99		PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
100		break;
101	}
102
103	if (SEC_ASN1EncodeItem (arena, encodedValue, value,
104				CERTAuthKeyIDTemplate) == NULL)
105	    break;
106	rv = SECSuccess;
107
108    } while (0);
109     return(rv);
110}
111
112CERTAuthKeyID *
113CERT_DecodeAuthKeyID (PRArenaPool *arena, SECItem *encodedValue)
114{
115    CERTAuthKeyID * value = NULL;
116    SECStatus       rv    = SECFailure;
117    void *          mark;
118    SECItem         newEncodedValue;
119
120    PORT_Assert (arena);
121   
122    do {
123	mark = PORT_ArenaMark (arena);
124	value = (CERTAuthKeyID*)PORT_ArenaZAlloc (arena, sizeof (*value));
125	if (value == NULL)
126	    break;
127	value->DERAuthCertIssuer = NULL;
128        /* copy the DER into the arena, since Quick DER returns data that points
129           into the DER input, which may get freed by the caller */
130        rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue);
131        if ( rv != SECSuccess ) {
132	    break;
133        }
134
135        rv = SEC_QuickDERDecodeItem
136	     (arena, value, CERTAuthKeyIDTemplate, &newEncodedValue);
137	if (rv != SECSuccess)
138	    break;
139
140        value->authCertIssuer = cert_DecodeGeneralNames (arena, value->DERAuthCertIssuer);
141	if (value->authCertIssuer == NULL)
142	    break;
143	
144	/* what if the general name contains other format but not URI ?
145	   hl
146	 */
147	if ((value->authCertSerialNumber.data && !value->authCertIssuer) ||
148	    (!value->authCertSerialNumber.data && value->authCertIssuer)){
149	    PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
150	    break;
151	}
152    } while (0);
153
154    if (rv != SECSuccess) {
155	PORT_ArenaRelease (arena, mark);
156	return ((CERTAuthKeyID *)NULL);	    
157    } 
158    PORT_ArenaUnmark(arena, mark);
159    return (value);
160}