Public/Private keys implement a has_fingerprint() method
[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_RSA, KEY_DSA,
21 "RSA",
22 "ECDSA",
23 "DSA"
24 );
25
26 ENUM(signature_scheme_names, SIGN_UNKNOWN, SIGN_ECDSA_521,
27 "UNKNOWN",
28 "RSA_EMSA_PKCS1_NULL",
29 "RSA_EMSA_PKCS1_MD5",
30 "RSA_EMSA_PKCS1_SHA1",
31 "RSA_EMSA_PKCS1_SHA224",
32 "RSA_EMSA_PKCS1_SHA256",
33 "RSA_EMSA_PKCS1_SHA384",
34 "RSA_EMSA_PKCS1_SHA512",
35 "ECDSA_WITH_SHA1_DER",
36 "ECDSA_WITH_SHA256_DER",
37 "ECDSA_WITH_SHA384_DER",
38 "ECDSA_WITH_SHA512_DER",
39 "ECDSA_WITH_NULL",
40 "ECDSA-256",
41 "ECDSA-384",
42 "ECDSA-521",
43 );
44
45 /**
46 * See header.
47 */
48 bool public_key_equals(public_key_t *this, public_key_t *other)
49 {
50 key_encoding_type_t type;
51 chunk_t a, b;
52
53 if (this == other)
54 {
55 return TRUE;
56 }
57
58 for (type = 0; type < KEY_ENCODING_MAX; type++)
59 {
60 if (this->get_fingerprint(this, type, &a) &&
61 other->get_fingerprint(other, type, &b))
62 {
63 return chunk_equals(a, b);
64 }
65 }
66 return FALSE;
67 }
68
69 /**
70 * See header.
71 */
72 bool public_key_has_fingerprint(public_key_t *public, chunk_t fingerprint)
73 {
74 key_encoding_type_t type;
75 chunk_t current;
76
77 for (type = 0; type < KEY_ID_MAX; type++)
78 {
79 if (public->get_fingerprint(public, type, &current) &&
80 chunk_equals(current, fingerprint))
81 {
82 return TRUE;
83 }
84 }
85 return FALSE;
86 }
87
88 /*
89 * Defined in header.
90 */
91 signature_scheme_t signature_scheme_from_oid(int oid)
92 {
93 switch (oid)
94 {
95 case OID_MD5_WITH_RSA:
96 case OID_MD5:
97 return SIGN_RSA_EMSA_PKCS1_MD5;
98 case OID_SHA1_WITH_RSA:
99 case OID_SHA1:
100 return SIGN_RSA_EMSA_PKCS1_SHA1;
101 case OID_SHA224_WITH_RSA:
102 case OID_SHA224:
103 return SIGN_RSA_EMSA_PKCS1_SHA224;
104 case OID_SHA256_WITH_RSA:
105 case OID_SHA256:
106 return SIGN_RSA_EMSA_PKCS1_SHA256;
107 case OID_SHA384_WITH_RSA:
108 case OID_SHA384:
109 return SIGN_RSA_EMSA_PKCS1_SHA384;
110 case OID_SHA512_WITH_RSA:
111 case OID_SHA512:
112 return SIGN_RSA_EMSA_PKCS1_SHA512;
113 case OID_ECDSA_WITH_SHA1:
114 case OID_EC_PUBLICKEY:
115 return SIGN_ECDSA_WITH_SHA1_DER;
116 case OID_ECDSA_WITH_SHA256:
117 return SIGN_ECDSA_WITH_SHA256_DER;
118 case OID_ECDSA_WITH_SHA384:
119 return SIGN_ECDSA_WITH_SHA384_DER;
120 case OID_ECDSA_WITH_SHA512:
121 return SIGN_ECDSA_WITH_SHA512_DER;
122 default:
123 return SIGN_UNKNOWN;
124 }
125 }
126