2  * Copyright (c) 2002,2011,2014 Apple Inc. All Rights Reserved. 
   4  * The contents of this file constitute Original Code as defined in and are 
   5  * subject to the Apple Public Source License Version 1.2 (the 'License'). 
   6  * You may not use this file except in compliance with the License. Please obtain 
   7  * a copy of the License at http://www.apple.com/publicsource and read it before 
  10  * This Original Code and all software distributed under the License are 
  11  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS 
  12  * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT 
  13  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  14  * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the 
  15  * specific language governing rights and limitations under the License. 
  20  * DecodedCrl.cpp - object representing a decoded CRL, in NSS format, 
  21  * with extensions parsed and decoded (still in NSS format). 
  25 #include "DecodedCrl.h" 
  26 #include "cldebugging.h" 
  27 #include "AppleX509CLSession.h" 
  28 #include "CSPAttacher.h" 
  29 #include <Security/cssmapple.h> 
  31 DecodedCrl::DecodedCrl( 
  32         AppleX509CLSession      
&session
) 
  33         : DecodedItem(session
) 
  35         memset(&mCrl
, 0, sizeof(mCrl
)); 
  38 /* one-shot constructor, decoding from DER-encoded data */ 
  39 DecodedCrl::DecodedCrl( 
  40         AppleX509CLSession      
&session
, 
  41         const CssmData          
&encodedCrl
) 
  42         : DecodedItem(session
) 
  44         memset(&mCrl
, 0, sizeof(mCrl
)); 
  45         PRErrorCode prtn 
= mCoder
.decode(encodedCrl
.data(), encodedCrl
.length(),  
  46                 kSecAsn1SignedCrlTemplate
, &mCrl
); 
  48                 CssmError::throwMe(CSSMERR_CL_UNKNOWN_FORMAT
); 
  50         mDecodedExtensions
.decodeFromNss(mCrl
.tbs
.extensions
); 
  51         mState 
= IS_DecodedAll
; 
  54 DecodedCrl::~DecodedCrl() 
  58 /* decode mCrl.tbs and its extensions */ 
  59 void DecodedCrl::decodeCts( 
  60         const CssmData  
&encodedCts
) 
  62         assert(mState 
== IS_Empty
); 
  63         memset(&mCrl
, 0, sizeof(mCrl
)); 
  64         PRErrorCode prtn 
= mCoder
.decode(encodedCts
.data(), encodedCts
.length(),  
  65                 kSecAsn1TBSCrlTemplate
, &mCrl
.tbs
); 
  67                 CssmError::throwMe(CSSMERR_CL_UNKNOWN_FORMAT
); 
  69         mDecodedExtensions
.decodeFromNss(mCrl
.tbs
.extensions
); 
  70         mState 
= IS_DecodedTBS
; 
  73 void DecodedCrl::encodeExtensions() 
  75         NSS_TBSCrl 
&tbs 
= mCrl
.tbs
; 
  76         assert(mState 
== IS_Building
); 
  77         assert(tbs
.extensions 
== NULL
); 
  79         if(mDecodedExtensions
.numExtensions() == 0) { 
  80                 /* no extensions, no error */ 
  83         mDecodedExtensions
.encodeToNss(tbs
.extensions
); 
  87  * FIXME : how to determine max encoding size at run time!? 
  89 #define MAX_TEMPLATE_SIZE       (16 * 1024) 
  91 /* encode TBS component; only called from CrlCreateTemplate */ 
  92 void DecodedCrl::encodeCts( 
  93         CssmOwnedData   
&encodedCts
) 
  96         assert(mState 
== IS_Building
); 
  98         /* enforce required fields - could go deeper, maybe we should */ 
  99         NSS_TBSCrl 
&tbs 
= mCrl
.tbs
; 
 100         if((tbs
.signature
.algorithm
.Data 
== NULL
) || 
 101            (tbs
.issuer
.rdns 
== NULL
)) { 
 102                 clErrorLog("DecodedCrl::encodeTbs: incomplete TBS"); 
 103                 /* an odd, undocumented error return */ 
 104                 CssmError::throwMe(CSSMERR_CL_NO_FIELD_VALUES
); 
 108         prtn 
= SecNssEncodeItemOdata(&tbs
, kSecAsn1TBSCrlTemplate
, 
 111                 CssmError::throwMe(CSSMERR_CL_MEMORY_ERROR
);