]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/utilLib/nssAppUtils.cpp
Security-57740.31.2.tar.gz
[apple/security.git] / SecurityTests / cspxutils / utilLib / nssAppUtils.cpp
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
3 *
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.
9 *
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.
17 */
18 /*
19 * nssAppUtils.cpp
20 */
21
22 #include "nssAppUtils.h"
23 #include "common.h"
24 #include "cspwrap.h"
25 #include <Security/SecAsn1Coder.h>
26 #include <Security/osKeyTemplates.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <strings.h>
30
31 /*
32 * Create pubKeyPartial as copy of pubKey without the DSA params.
33 * Returned partial key is RAW. Incoming key can be raw or ref.
34 */
35 CSSM_RETURN extractDsaPartial(
36 CSSM_CSP_HANDLE cspHand,
37 const CSSM_KEY *pubKey,
38 CSSM_KEY_PTR pubKeyPartial)
39 {
40 const CSSM_KEY *thePubKey = pubKey;
41 CSSM_KEY rawPubKey;
42 CSSM_RETURN crtn;
43
44 if(pubKey->KeyHeader.BlobType == CSSM_KEYBLOB_REFERENCE) {
45 /* first get this in raw form */
46 crtn = cspRefKeyToRaw(cspHand, pubKey, &rawPubKey);
47 if(crtn) {
48 return crtn;
49 }
50 thePubKey = &rawPubKey;
51 }
52
53 /* decode raw public key */
54 NSS_DSAPublicKeyX509 nssPub;
55 SecAsn1CoderRef coder;
56
57 OSStatus ortn = SecAsn1CoderCreate(&coder);
58 if(ortn) {
59 cssmPerror("SecAsn1CoderCreate", ortn);
60 return ortn;
61 }
62 memset(&nssPub, 0, sizeof(nssPub));
63 if(SecAsn1DecodeData(coder, &thePubKey->KeyData, kSecAsn1DSAPublicKeyX509Template,
64 &nssPub)) {
65 printf("***Error decoding DSA public key. Aborting.\n");
66 return 1;
67 }
68
69 /* zero out the params and reencode */
70 nssPub.dsaAlg.params = NULL;
71 CSSM_DATA newKey = {0, NULL};
72 if(SecAsn1EncodeItem(coder, &nssPub, kSecAsn1DSAPublicKeyX509Template,
73 &newKey)) {
74 printf("***Error reencoding DSA pub key\n");
75 return 1;
76 }
77
78 /* copy - newKey is in coder space */
79 *pubKeyPartial = *thePubKey;
80 appCopyCssmData(&newKey, &pubKeyPartial->KeyData);
81
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;
86 }
87 pubKeyPartial->KeyHeader.KeyAttr |= CSSM_KEYATTR_PARTIAL;
88 SecAsn1CoderRelease(coder);
89 return CSSM_OK;
90 }