Back to index

lightning-sunbird  0.9+nobinonly
cmmfasn1.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8 -*-*/
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape security libraries.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "cmmf.h"
00039 #include "cmmfi.h"
00040 #include "secasn1.h"
00041 #include "secitem.h"
00042 
00043 SEC_ASN1_MKSUB(SEC_SignedCertificateTemplate)
00044 
00045 static const SEC_ASN1Template CMMFSequenceOfCertifiedKeyPairsTemplate[] = {
00046     { SEC_ASN1_SEQUENCE_OF, 0, CMMFCertifiedKeyPairTemplate}
00047 };
00048 
00049 static const SEC_ASN1Template CMMFKeyRecRepContentTemplate[] = {
00050     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CMMFKeyRecRepContent)},
00051     { SEC_ASN1_INLINE, offsetof(CMMFKeyRecRepContent, status), 
00052       CMMFPKIStatusInfoTemplate},
00053     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_POINTER | 
00054               SEC_ASN1_XTRN | 0,
00055       offsetof(CMMFKeyRecRepContent, newSigCert),
00056       SEC_ASN1_SUB(SEC_SignedCertificateTemplate)},
00057     { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 1,
00058       offsetof(CMMFKeyRecRepContent, caCerts),
00059       CMMFSequenceOfCertsTemplate},
00060     { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2,
00061       offsetof(CMMFKeyRecRepContent, keyPairHist),
00062       CMMFSequenceOfCertifiedKeyPairsTemplate},
00063     { 0 }
00064 };
00065 
00066 SECStatus
00067 CMMF_EncodeCertRepContent (CMMFCertRepContent        *inCertRepContent,
00068                         CRMFEncoderOutputCallback  inCallback,
00069                         void                      *inArg)
00070 {
00071     return cmmf_user_encode(inCertRepContent, inCallback, inArg,
00072                          CMMFCertRepContentTemplate);
00073 }
00074 
00075 SECStatus
00076 CMMF_EncodePOPODecKeyChallContent(CMMFPOPODecKeyChallContent *inDecKeyChall,
00077                               CRMFEncoderOutputCallback inCallback,
00078                               void                     *inArg)
00079 {
00080     return cmmf_user_encode(inDecKeyChall, inCallback, inArg,
00081                          CMMFPOPODecKeyChallContentTemplate);
00082 }
00083 
00084 CMMFPOPODecKeyRespContent*
00085 CMMF_CreatePOPODecKeyRespContentFromDER(const char *buf, long len)
00086 {
00087     PRArenaPool               *poolp;
00088     CMMFPOPODecKeyRespContent *decKeyResp;
00089     SECStatus                  rv;
00090 
00091     poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
00092     if (poolp == NULL) {
00093         return NULL;
00094     }
00095     decKeyResp = PORT_ArenaZNew(poolp, CMMFPOPODecKeyRespContent);
00096     if (decKeyResp == NULL) {
00097         goto loser;
00098     }
00099     decKeyResp->poolp = poolp;
00100     rv = SEC_ASN1Decode(poolp, decKeyResp, CMMFPOPODecKeyRespContentTemplate,
00101                      buf, len);
00102     if (rv != SECSuccess) {
00103         goto loser;
00104     }
00105     return decKeyResp;
00106     
00107  loser:
00108     if (poolp != NULL) {
00109         PORT_FreeArena(poolp, PR_FALSE);
00110     }
00111     return NULL;
00112 }
00113 
00114 SECStatus
00115 CMMF_EncodeKeyRecRepContent(CMMFKeyRecRepContent      *inKeyRecRep,
00116                          CRMFEncoderOutputCallback  inCallback,
00117                          void                      *inArg)
00118 {
00119     return cmmf_user_encode(inKeyRecRep, inCallback, inArg,
00120                          CMMFKeyRecRepContentTemplate);
00121 }
00122 
00123 CMMFKeyRecRepContent* 
00124 CMMF_CreateKeyRecRepContentFromDER(CERTCertDBHandle *db, const char *buf, 
00125                                long len)
00126 {
00127     PRArenaPool          *poolp;
00128     CMMFKeyRecRepContent *keyRecContent;
00129     SECStatus             rv;
00130 
00131     poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
00132     if (poolp == NULL) {
00133         return NULL;
00134     }
00135     keyRecContent = PORT_ArenaZNew(poolp, CMMFKeyRecRepContent);
00136     if (keyRecContent == NULL) {
00137         goto loser;
00138     }
00139     keyRecContent->poolp = poolp;
00140     rv = SEC_ASN1Decode(poolp, keyRecContent, CMMFKeyRecRepContentTemplate,
00141                      buf, len);
00142     if (rv != SECSuccess) {
00143         goto loser;
00144     }
00145     if (keyRecContent->keyPairHist != NULL) {
00146         while(keyRecContent->keyPairHist[keyRecContent->numKeyPairs] != NULL) {
00147            rv = cmmf_decode_process_certified_key_pair(poolp, db,
00148                      keyRecContent->keyPairHist[keyRecContent->numKeyPairs]);
00149            if (rv != SECSuccess) {
00150                goto loser;
00151            }
00152            keyRecContent->numKeyPairs++;
00153        }
00154        keyRecContent->allocKeyPairs = keyRecContent->numKeyPairs;
00155     }
00156     keyRecContent->isDecoded = PR_TRUE;
00157     return keyRecContent;
00158  loser:
00159     if (poolp != NULL) {
00160         PORT_FreeArena(poolp, PR_FALSE);
00161     }
00162     return NULL;
00163 }
00164