1/*
2 * Copyright (c) 1995-2000 Intel Corporation. All rights reserved.
3 */
4/*
5 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
6 * Use is subject to license terms.
7 */
8
9#include <stdlib.h>
10#include <string.h>
11
12#include <kmfapiP.h>
13
14typedef struct {
15	KMF_OID * AlgOID;
16	KMF_ALGORITHM_INDEX AlgID;
17} KMF_OID_ID;
18
19/*
20 * The following table defines the mapping of AlgOID's to AlgID's.
21 */
22static KMF_OID_ID ALGOID_ID_Table[] = {
23	{(KMF_OID *)&KMFOID_X9CM_DSA, KMF_ALGID_DSA},
24	{(KMF_OID *)&KMFOID_X9CM_DSAWithSHA1, KMF_ALGID_SHA1WithDSA},
25	{(KMF_OID *)&KMFOID_SHA1, KMF_ALGID_SHA1},
26	{(KMF_OID *)&KMFOID_RSA, KMF_ALGID_RSA},
27	{(KMF_OID *)&KMFOID_DSA, KMF_ALGID_DSA},
28	{(KMF_OID *)&KMFOID_MD5WithRSA, KMF_ALGID_MD5WithRSA},
29	{(KMF_OID *)&KMFOID_SHA1WithRSA, KMF_ALGID_SHA1WithRSA},
30	{(KMF_OID *)&KMFOID_SHA1WithDSA, KMF_ALGID_SHA1WithDSA},
31	{(KMF_OID *)&KMFOID_SHA256WithDSA, KMF_ALGID_SHA256WithDSA},
32	{(KMF_OID *)&KMFOID_SHA1WithECDSA, KMF_ALGID_SHA1WithECDSA},
33	{(KMF_OID *)&KMFOID_SHA256WithECDSA, KMF_ALGID_SHA256WithECDSA},
34	{(KMF_OID *)&KMFOID_SHA256WithRSA, KMF_ALGID_SHA256WithRSA},
35	{(KMF_OID *)&KMFOID_SHA384WithECDSA, KMF_ALGID_SHA384WithECDSA},
36	{(KMF_OID *)&KMFOID_SHA384WithRSA, KMF_ALGID_SHA384WithRSA},
37	{(KMF_OID *)&KMFOID_SHA512WithECDSA, KMF_ALGID_SHA512WithECDSA},
38	{(KMF_OID *)&KMFOID_SHA512WithRSA, KMF_ALGID_SHA512WithRSA},
39	{(KMF_OID *)&KMFOID_EC_PUBLIC_KEY, KMF_ALGID_ECDSA}
40};
41
42#define	NUM_ALGOIDS ((sizeof (ALGOID_ID_Table))/(sizeof (ALGOID_ID_Table[0])))
43
44/*
45 * Name: x509_algid_to_algoid
46 *
47 * Description:
48 * This function maps the specified AlgID to the corresponding
49 * Algorithm OID.
50 *
51 * Parameters:
52 * alg_int - AlgID to be mapped.
53 *
54 * Return value:
55 * Pointer to OID structure and NULL in case of failure.
56 *
57 */
58KMF_OID *
59x509_algid_to_algoid(KMF_ALGORITHM_INDEX alg_int)
60{
61	int i;
62
63	switch (alg_int) {
64		case KMF_ALGID_NONE:
65			return (NULL);
66
67		default:
68			for (i = 0; i < NUM_ALGOIDS; i++) {
69				if (ALGOID_ID_Table[i].AlgID == alg_int)
70					return (ALGOID_ID_Table[i].AlgOID);
71			}
72			break;
73	}
74
75	return (NULL);
76}
77
78/*
79 * Name: x509_algoid_to_algid
80 *
81 * Description:
82 * This function maps the specified Algorithm OID to the corresponding
83 * AlgID.
84 *
85 * Parameters:
86 * Oid - OID to be mapped.
87 *
88 * Return value:
89 * Algorithm ID and KMF_ALGID_NONE in case of failures.
90 */
91KMF_ALGORITHM_INDEX
92x509_algoid_to_algid(KMF_OID * Oid)
93{
94	int i;
95
96	if ((Oid == NULL) || (Oid->Data == NULL) || (Oid->Length == 0)) {
97		return (KMF_ALGID_NONE);
98	}
99
100	for (i = 0; i < NUM_ALGOIDS; i++) {
101		if (IsEqualOid(ALGOID_ID_Table[i].AlgOID, Oid))
102			return (ALGOID_ID_Table[i].AlgID);
103	}
104
105	return (KMF_ALGID_NONE);
106}
107