37bba77d197df1d6388512b5afd3aff0551e3570
[strongswan.git] / src / libstrongswan / credentials / keys / public_key.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <asn1/oid.h>
17
18 #include "public_key.h"
19
20 ENUM(key_type_names, KEY_ANY, KEY_DSA,
21 "ANY",
22 "RSA",
23 "ECDSA",
24 "DSA"
25 );
26
27 ENUM(signature_scheme_names, SIGN_UNKNOWN, SIGN_ECDSA_521,
28 "UNKNOWN",
29 "RSA_EMSA_PKCS1_NULL",
30 "RSA_EMSA_PKCS1_MD5",
31 "RSA_EMSA_PKCS1_SHA1",
32 "RSA_EMSA_PKCS1_SHA224",
33 "RSA_EMSA_PKCS1_SHA256",
34 "RSA_EMSA_PKCS1_SHA384",
35 "RSA_EMSA_PKCS1_SHA512",
36 "ECDSA_WITH_SHA1_DER",
37 "ECDSA_WITH_SHA256_DER",
38 "ECDSA_WITH_SHA384_DER",
39 "ECDSA_WITH_SHA512_DER",
40 "ECDSA_WITH_NULL",
41 "ECDSA-256",
42 "ECDSA-384",
43 "ECDSA-521",
44 );
45
46 ENUM(encryption_scheme_names, ENCRYPT_UNKNOWN, ENCRYPT_RSA_OAEP_SHA512,
47 "ENCRYPT_UNKNOWN",
48 "ENCRYPT_RSA_PKCS1",
49 "ENCRYPT_RSA_OAEP_SHA1",
50 "ENCRYPT_RSA_OAEP_SHA224",
51 "ENCRYPT_RSA_OAEP_SHA256",
52 "ENCRYPT_RSA_OAEP_SHA384",
53 "ENCRYPT_RSA_OAEP_SHA512",
54 );
55
56 /**
57 * See header.
58 */
59 bool public_key_equals(public_key_t *this, public_key_t *other)
60 {
61 cred_encoding_type_t type;
62 chunk_t a, b;
63
64 if (this == other)
65 {
66 return TRUE;
67 }
68
69 for (type = 0; type < CRED_ENCODING_MAX; type++)
70 {
71 if (this->get_fingerprint(this, type, &a) &&
72 other->get_fingerprint(other, type, &b))
73 {
74 return chunk_equals(a, b);
75 }
76 }
77 return FALSE;
78 }
79
80 /**
81 * See header.
82 */
83 bool public_key_has_fingerprint(public_key_t *public, chunk_t fingerprint)
84 {
85 cred_encoding_type_t type;
86 chunk_t current;
87
88 for (type = 0; type < KEYID_MAX; type++)
89 {
90 if (public->get_fingerprint(public, type, &current) &&
91 chunk_equals(current, fingerprint))
92 {
93 return TRUE;
94 }
95 }
96 return FALSE;
97 }
98
99 /*
100 * Defined in header.
101 */
102 signature_scheme_t signature_scheme_from_oid(int oid)
103 {
104 switch (oid)
105 {
106 case OID_MD5_WITH_RSA:
107 case OID_MD5:
108 return SIGN_RSA_EMSA_PKCS1_MD5;
109 case OID_SHA1_WITH_RSA:
110 case OID_SHA1:
111 return SIGN_RSA_EMSA_PKCS1_SHA1;
112 case OID_SHA224_WITH_RSA:
113 case OID_SHA224:
114 return SIGN_RSA_EMSA_PKCS1_SHA224;
115 case OID_SHA256_WITH_RSA:
116 case OID_SHA256:
117 return SIGN_RSA_EMSA_PKCS1_SHA256;
118 case OID_SHA384_WITH_RSA:
119 case OID_SHA384:
120 return SIGN_RSA_EMSA_PKCS1_SHA384;
121 case OID_SHA512_WITH_RSA:
122 case OID_SHA512:
123 return SIGN_RSA_EMSA_PKCS1_SHA512;
124 case OID_ECDSA_WITH_SHA1:
125 case OID_EC_PUBLICKEY:
126 return SIGN_ECDSA_WITH_SHA1_DER;
127 case OID_ECDSA_WITH_SHA256:
128 return SIGN_ECDSA_WITH_SHA256_DER;
129 case OID_ECDSA_WITH_SHA384:
130 return SIGN_ECDSA_WITH_SHA384_DER;
131 case OID_ECDSA_WITH_SHA512:
132 return SIGN_ECDSA_WITH_SHA512_DER;
133 default:
134 return SIGN_UNKNOWN;
135 }
136 }
137