Back to index

lightning-sunbird  0.9+nobinonly
xauthkid.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * X.509 v3 Subject Key Usage Extension 
00039  *
00040  */
00041 
00042 #include "prtypes.h"
00043 #include "mcom_db.h"
00044 #include "seccomon.h"
00045 #include "secdert.h"
00046 #include "secoidt.h"
00047 #include "secasn1t.h"
00048 #include "secasn1.h"
00049 #include "secport.h"
00050 #include "certt.h"  
00051 #include "genname.h"
00052 #include "secerr.h"
00053 
00054    
00055 const SEC_ASN1Template CERTAuthKeyIDTemplate[] = {
00056     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTAuthKeyID) },
00057     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 0,
00058          offsetof(CERTAuthKeyID,keyID), SEC_OctetStringTemplate},
00059     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC  | 1,
00060           offsetof(CERTAuthKeyID, DERAuthCertIssuer), CERT_GeneralNamesTemplate},
00061     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2,
00062          offsetof(CERTAuthKeyID,authCertSerialNumber), SEC_IntegerTemplate},
00063     { 0 }
00064 };
00065 
00066 
00067 
00068 SECStatus CERT_EncodeAuthKeyID (PRArenaPool *arena, CERTAuthKeyID *value, SECItem *encodedValue)
00069 {
00070     SECStatus rv = SECFailure;
00071  
00072     PORT_Assert (value);
00073     PORT_Assert (arena);
00074     PORT_Assert (value->DERAuthCertIssuer == NULL);
00075     PORT_Assert (encodedValue);
00076 
00077     do {
00078        
00079        /* If both of the authCertIssuer and the serial number exist, encode
00080           the name first.  Otherwise, it is an error if one exist and the other
00081           is not.
00082         */
00083        if (value->authCertIssuer) {
00084            if (!value->authCertSerialNumber.data) {
00085               PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
00086               break;
00087            }
00088 
00089            value->DERAuthCertIssuer = cert_EncodeGeneralNames
00090               (arena, value->authCertIssuer);
00091            if (!value->DERAuthCertIssuer) {
00092               PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
00093               break;
00094            }
00095        }
00096        else if (value->authCertSerialNumber.data) {
00097               PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
00098               break;
00099        }
00100 
00101        if (SEC_ASN1EncodeItem (arena, encodedValue, value,
00102                             CERTAuthKeyIDTemplate) == NULL)
00103            break;
00104        rv = SECSuccess;
00105 
00106     } while (0);
00107      return(rv);
00108 }
00109 
00110 CERTAuthKeyID *
00111 CERT_DecodeAuthKeyID (PRArenaPool *arena, SECItem *encodedValue)
00112 {
00113     CERTAuthKeyID * value = NULL;
00114     SECStatus       rv    = SECFailure;
00115     void *          mark;
00116     SECItem         newEncodedValue;
00117 
00118     PORT_Assert (arena);
00119    
00120     do {
00121        mark = PORT_ArenaMark (arena);
00122        value = (CERTAuthKeyID*)PORT_ArenaZAlloc (arena, sizeof (*value));
00123        if (value == NULL)
00124            break;
00125        value->DERAuthCertIssuer = NULL;
00126         /* copy the DER into the arena, since Quick DER returns data that points
00127            into the DER input, which may get freed by the caller */
00128         rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue);
00129         if ( rv != SECSuccess ) {
00130            break;
00131         }
00132 
00133         rv = SEC_QuickDERDecodeItem
00134             (arena, value, CERTAuthKeyIDTemplate, &newEncodedValue);
00135        if (rv != SECSuccess)
00136            break;
00137 
00138         value->authCertIssuer = cert_DecodeGeneralNames (arena, value->DERAuthCertIssuer);
00139        if (value->authCertIssuer == NULL)
00140            break;
00141        
00142        /* what if the general name contains other format but not URI ?
00143           hl
00144         */
00145        if ((value->authCertSerialNumber.data && !value->authCertIssuer) ||
00146            (!value->authCertSerialNumber.data && value->authCertIssuer)){
00147            PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
00148            break;
00149        }
00150     } while (0);
00151 
00152     if (rv != SECSuccess) {
00153        PORT_ArenaRelease (arena, mark);
00154        return ((CERTAuthKeyID *)NULL);        
00155     } 
00156     PORT_ArenaUnmark(arena, mark);
00157     return (value);
00158 }