#include "nssAppUtils.h"
#include "common.h"
#include "cspwrap.h"
#include <Security/SecAsn1Coder.h>
#include <Security/osKeyTemplates.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
CSSM_RETURN extractDsaPartial(
CSSM_CSP_HANDLE cspHand,
const CSSM_KEY *pubKey,
CSSM_KEY_PTR pubKeyPartial)
{
const CSSM_KEY *thePubKey = pubKey;
CSSM_KEY rawPubKey;
CSSM_RETURN crtn;
if(pubKey->KeyHeader.BlobType == CSSM_KEYBLOB_REFERENCE) {
crtn = cspRefKeyToRaw(cspHand, pubKey, &rawPubKey);
if(crtn) {
return crtn;
}
thePubKey = &rawPubKey;
}
NSS_DSAPublicKeyX509 nssPub;
SecAsn1CoderRef coder;
OSStatus ortn = SecAsn1CoderCreate(&coder);
if(ortn) {
cssmPerror("SecAsn1CoderCreate", ortn);
return ortn;
}
memset(&nssPub, 0, sizeof(nssPub));
if(SecAsn1DecodeData(coder, &thePubKey->KeyData, kSecAsn1DSAPublicKeyX509Template,
&nssPub)) {
printf("***Error decoding DSA public key. Aborting.\n");
return 1;
}
nssPub.dsaAlg.params = NULL;
CSSM_DATA newKey = {0, NULL};
if(SecAsn1EncodeItem(coder, &nssPub, kSecAsn1DSAPublicKeyX509Template,
&newKey)) {
printf("***Error reencoding DSA pub key\n");
return 1;
}
*pubKeyPartial = *thePubKey;
appCopyCssmData(&newKey, &pubKeyPartial->KeyData);
if(pubKey->KeyHeader.BlobType == CSSM_KEYBLOB_REFERENCE) {
CSSM_FREE(thePubKey->KeyData.Data);
pubKeyPartial->KeyHeader.BlobType = CSSM_KEYBLOB_RAW;
}
pubKeyPartial->KeyHeader.KeyAttr |= CSSM_KEYATTR_PARTIAL;
SecAsn1CoderRelease(coder);
return CSSM_OK;
}