2  * Copyright (c) 2003-2004 Apple Computer, 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  
   7  * obtain a copy of the License at http://www.apple.com/publicsource and  
   8  * read it before using this file. 
  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  
  12  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,  
  13  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,  
  14  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.  
  15  * Please see the License for the specific language governing rights and  
  16  * limitations under the License. 
  22 #include "nssAppUtils.h" 
  25 #include <Security/SecAsn1Coder.h> 
  26 #include <Security/osKeyTemplates.h>     
  32  * Create pubKeyPartial as copy of pubKey without the DSA params. 
  33  * Returned partial key is RAW. Incoming key can be raw or ref. 
  35 CSSM_RETURN 
extractDsaPartial( 
  36         CSSM_CSP_HANDLE cspHand
, 
  37         const CSSM_KEY 
*pubKey
,  
  38         CSSM_KEY_PTR pubKeyPartial
) 
  40         const CSSM_KEY 
*thePubKey 
= pubKey
; 
  44         if(pubKey
->KeyHeader
.BlobType 
== CSSM_KEYBLOB_REFERENCE
) { 
  45                 /* first get this in raw form */ 
  46                 crtn 
= cspRefKeyToRaw(cspHand
, pubKey
, &rawPubKey
); 
  50                 thePubKey 
= &rawPubKey
; 
  53         /* decode raw public key */ 
  54         NSS_DSAPublicKeyX509 nssPub
; 
  55         SecAsn1CoderRef coder
; 
  57         OSStatus ortn 
= SecAsn1CoderCreate(&coder
); 
  59                 cssmPerror("SecAsn1CoderCreate", ortn
); 
  62         memset(&nssPub
, 0, sizeof(nssPub
)); 
  63         if(SecAsn1DecodeData(coder
, &thePubKey
->KeyData
, kSecAsn1DSAPublicKeyX509Template
, 
  65                 printf("***Error decoding DSA public key. Aborting.\n"); 
  69         /* zero out the params and reencode */ 
  70         nssPub
.dsaAlg
.params 
= NULL
; 
  71         CSSM_DATA newKey 
= {0, NULL
}; 
  72         if(SecAsn1EncodeItem(coder
, &nssPub
, kSecAsn1DSAPublicKeyX509Template
, 
  74                 printf("***Error reencoding DSA pub key\n"); 
  78         /* copy - newKey is in coder space */ 
  79         *pubKeyPartial 
= *thePubKey
; 
  80         appCopyCssmData(&newKey
, &pubKeyPartial
->KeyData
); 
  82         if(pubKey
->KeyHeader
.BlobType 
== CSSM_KEYBLOB_REFERENCE
) { 
  83                 /* free the KeyData mallocd by cspRefKeyToRaw */ 
  84                 CSSM_FREE(thePubKey
->KeyData
.Data
); 
  85                 pubKeyPartial
->KeyHeader
.BlobType 
= CSSM_KEYBLOB_RAW
; 
  87         pubKeyPartial
->KeyHeader
.KeyAttr 
|= CSSM_KEYATTR_PARTIAL
; 
  88         SecAsn1CoderRelease(coder
);