]> git.saurik.com Git - apple/security.git/blob - libsecurity_cssm/lib/oidsalg.c
Security-55471.14.4.tar.gz
[apple/security.git] / libsecurity_cssm / lib / oidsalg.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 /*
26 * oidsalg.c - OIDs defining crypto algorithms
27 */
28
29 #include <Security/oidsbase.h>
30 #include <Security/cssmtype.h>
31 #include "cssmapple.h"
32 #include <Security/oidsalg.h>
33 #include <string.h>
34
35 #pragma mark ----- CSSM_OID <--> CSSM_ALGORITHMS -----
36
37 typedef struct {
38 const CSSM_OID *oid;
39 CSSM_ALGORITHMS alg;
40 } OidToAlgEnt;
41
42 static const OidToAlgEnt oidToAlgMap[] =
43 {
44 {&CSSMOID_RSA, CSSM_ALGID_RSA },
45 {&CSSMOID_MD2WithRSA, CSSM_ALGID_MD2WithRSA },
46 {&CSSMOID_MD5WithRSA, CSSM_ALGID_MD5WithRSA },
47 {&CSSMOID_SHA1WithRSA, CSSM_ALGID_SHA1WithRSA },
48 {&CSSMOID_SHA1WithRSA_OIW, CSSM_ALGID_SHA1WithRSA },
49 {&CSSMOID_SHA1, CSSM_ALGID_SHA1},
50 {&CSSMOID_MD5, CSSM_ALGID_MD5 },
51 /*
52 * These OIDs have three variants - one for BSAFE, CMS, and JDK 1.1.
53 * On the oid-to-alg map, we'll handle either one, mapping to
54 * the same CSSM alg. When we map from alg to OID, we'll use
55 * the CMS variant (being first in the list).
56 */
57 {&CSSMOID_DSA_CMS, CSSM_ALGID_DSA },
58 {&CSSMOID_DSA, CSSM_ALGID_DSA },
59 {&CSSMOID_DSA_JDK, CSSM_ALGID_DSA },
60 {&CSSMOID_SHA1WithDSA_CMS, CSSM_ALGID_SHA1WithDSA },
61 {&CSSMOID_SHA1WithDSA, CSSM_ALGID_SHA1WithDSA },
62 {&CSSMOID_SHA1WithDSA_JDK, CSSM_ALGID_SHA1WithDSA },
63 /*
64 * Multiple entries for Diffie-Hellman. We favor the PKCS3 version for
65 * mapping alg to OID.
66 */
67 {&CSSMOID_DH, CSSM_ALGID_DH},
68 {&CSSMOID_ANSI_DH_PUB_NUMBER, CSSM_ALGID_DH},
69 {&CSSMOID_ANSI_DH_STATIC, CSSM_ALGID_DH},
70 {&CSSMOID_ANSI_DH_ONE_FLOW, CSSM_ALGID_DH},
71 {&CSSMOID_ANSI_DH_EPHEM, CSSM_ALGID_DH},
72 {&CSSMOID_ANSI_DH_HYBRID1, CSSM_ALGID_DH},
73 {&CSSMOID_ANSI_DH_HYBRID2, CSSM_ALGID_DH},
74 {&CSSMOID_ANSI_DH_HYBRID_ONEFLOW, CSSM_ALGID_DH},
75 {&CSSMOID_APPLE_FEE, CSSM_ALGID_FEE },
76 {&CSSMOID_APPLE_ASC, CSSM_ALGID_ASC },
77 {&CSSMOID_APPLE_FEE_MD5, CSSM_ALGID_FEE_MD5 },
78 {&CSSMOID_APPLE_FEE_SHA1, CSSM_ALGID_FEE_SHA1 },
79 {&CSSMOID_APPLE_FEED, CSSM_ALGID_FEED },
80 {&CSSMOID_APPLE_FEEDEXP, CSSM_ALGID_FEEDEXP },
81 /* the current valid alg --> OID mapping */
82 {&CSSMOID_ECDSA_WithSHA1, CSSM_ALGID_SHA1WithECDSA},
83 /* for backwards compatibility */
84 {&CSSMOID_APPLE_ECDSA, CSSM_ALGID_SHA1WithECDSA },
85 {&CSSMOID_SHA224, CSSM_ALGID_SHA224},
86 {&CSSMOID_SHA256, CSSM_ALGID_SHA256},
87 {&CSSMOID_SHA384, CSSM_ALGID_SHA384},
88 {&CSSMOID_SHA512, CSSM_ALGID_SHA512},
89 {&CSSMOID_SHA224WithRSA, CSSM_ALGID_SHA224WithRSA },
90 {&CSSMOID_SHA256WithRSA, CSSM_ALGID_SHA256WithRSA },
91 {&CSSMOID_SHA384WithRSA, CSSM_ALGID_SHA384WithRSA },
92 {&CSSMOID_SHA512WithRSA, CSSM_ALGID_SHA512WithRSA },
93 {&CSSMOID_RSAWithOAEP, CSSM_ALGMODE_PKCS1_EME_OAEP },
94 {&CSSMOID_ECDSA_WithSHA224, CSSM_ALGID_SHA224WithECDSA },
95 {&CSSMOID_ECDSA_WithSHA256, CSSM_ALGID_SHA256WithECDSA },
96 {&CSSMOID_ECDSA_WithSHA384, CSSM_ALGID_SHA384WithECDSA },
97 {&CSSMOID_ECDSA_WithSHA512, CSSM_ALGID_SHA512WithECDSA },
98 /* AlgId.algorithm for ECDSA public key */
99 {&CSSMOID_ecPublicKey, CSSM_ALGID_ECDSA },
100 /* This OID is accompanied by an additional digest OID in AlgId.parameters */
101 {&CSSMOID_ECDSA_WithSpecified, CSSM_ALGID_ECDSA_SPECIFIED },
102 {NULL, 0}
103 };
104
105 #define NUM_OID_TO_ALGS (sizeof(oidToAlgMap) / sizeof(oidToAlgMap[0]))
106
107 /*
108 * Compare two CSSM_DATAs (or two CSSM_OIDs), return true if identical.
109 */
110 static bool compareCssmData(
111 const CSSM_DATA *data1,
112 const CSSM_DATA *data2)
113 {
114 if((data1 == NULL) || (data1->Data == NULL) ||
115 (data2 == NULL) || (data2->Data == NULL) ||
116 (data1->Length != data2->Length)) {
117 return false;
118 }
119 if(data1->Length != data2->Length) {
120 return false;
121 }
122 if(memcmp(data1->Data, data2->Data, data1->Length) == 0) {
123 return true;
124 }
125 else {
126 return false;
127 }
128 }
129
130 bool cssmOidToAlg(
131 const CSSM_OID *oid,
132 CSSM_ALGORITHMS *alg) // RETURNED
133 {
134 const OidToAlgEnt *ent;
135
136 for(ent=oidToAlgMap; ent->oid; ent++) {
137 if(compareCssmData(ent->oid, oid)) {
138 *alg = ent->alg;
139 return true;
140 }
141 }
142 return false;
143 }
144
145 const CSSM_OID *cssmAlgToOid(
146 CSSM_ALGORITHMS algId)
147 {
148 const OidToAlgEnt *ent;
149
150 for(ent=oidToAlgMap; ent->oid; ent++) {
151 if(ent->alg == algId) {
152 return ent->oid;
153 }
154 }
155 return NULL;
156 }
157
158