pluto and scepclient use private and public key plugins of libstrongswan
[strongswan.git] / src / libstrongswan / credentials / keys / public_key.h
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 /**
17 * @defgroup public_key public_key
18 * @{ @ingroup keys
19 */
20
21 #ifndef PUBLIC_KEY_H_
22 #define PUBLIC_KEY_H_
23
24 typedef struct public_key_t public_key_t;
25 typedef enum key_type_t key_type_t;
26 typedef enum key_id_type_t key_id_type_t;
27 typedef enum signature_scheme_t signature_scheme_t;
28
29 #include <library.h>
30 #include <utils/identification.h>
31
32 /**
33 * Type of a key pair, the used crypto system
34 */
35 enum key_type_t {
36 /** key type wildcard */
37 KEY_ANY = 0,
38 /** RSA crypto system as in PKCS#1 */
39 KEY_RSA = 1,
40 /** ECDSA as in ANSI X9.62 */
41 KEY_ECDSA = 2,
42 /** DSA */
43 KEY_DSA = 3,
44 /** ElGamal, ... */
45 };
46
47 /**
48 * Enum names for key_type_t
49 */
50 extern enum_name_t *key_type_names;
51
52 /**
53 * Signature scheme for signature creation
54 *
55 * EMSA-PKCS1 signatures are defined in PKCS#1 standard.
56 * A prepended ASN.1 encoded digestInfo field contains the
57 * OID of the used hash algorithm. The ASN.1 type of the PKCS#7
58 * variants is OCTET_STRING instead of the default BIT_STRING.
59 */
60 enum signature_scheme_t {
61 /** Default scheme of the underlying crypto system */
62 SIGN_DEFAULT,
63 /** EMSA-PKCS1_v1.5 signature over digest without digestInfo */
64 SIGN_RSA_EMSA_PKCS1_NULL,
65 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and MD5 */
66 SIGN_RSA_EMSA_PKCS1_MD5,
67 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-1 */
68 SIGN_RSA_EMSA_PKCS1_SHA1,
69 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-256 */
70 SIGN_RSA_EMSA_PKCS1_SHA256,
71 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-384 */
72 SIGN_RSA_EMSA_PKCS1_SHA384,
73 /** EMSA-PKCS1_v1.5 signature as in PKCS#1 using RSA and SHA-512 */
74 SIGN_RSA_EMSA_PKCS1_SHA512,
75 /** ECDSA with SHA-1 */
76 SIGN_ECDSA_WITH_SHA1,
77 /** ECDSA on the P-256 curve with SHA-256 as in RFC 4754 */
78 SIGN_ECDSA_256,
79 /** ECDSA on the P-384 curve with SHA-384 as in RFC 4754 */
80 SIGN_ECDSA_384,
81 /** ECDSA on the P-521 curve with SHA-512 as in RFC 4754 */
82 SIGN_ECDSA_521,
83 };
84
85 /**
86 * Enum names for signature_scheme_t
87 */
88 extern enum_name_t *signature_scheme_names;
89
90 /**
91 * Abstract interface of a public key.
92 */
93 struct public_key_t {
94
95 /**
96 * Get the key type.
97 *
98 * @return type of the key
99 */
100 key_type_t (*get_type)(public_key_t *this);
101
102 /**
103 * Verifies a signature against a chunk of data.
104 *
105 * @param scheme signature scheme to use for verification, may be default
106 * @param data data to check signature against
107 * @param signature signature to check
108 * @return TRUE if signature matches
109 */
110 bool (*verify)(public_key_t *this, signature_scheme_t scheme,
111 chunk_t data, chunk_t signature);
112
113 /**
114 * Encrypt a chunk of data.
115 *
116 * @param plain chunk containing plaintext data
117 * @param crypto where to allocate encrypted data
118 * @return TRUE if data successfully encrypted
119 */
120 bool (*encrypt)(public_key_t *this, chunk_t plain, chunk_t *crypto);
121
122 /**
123 * Check if two public keys are equal.
124 *
125 * @param other other public key
126 * @return TRUE, if equality
127 */
128 bool (*equals)(public_key_t *this, public_key_t *other);
129
130 /**
131 * Get the strength of the key in bytes.
132 *
133 * @return strength of the key in bytes
134 */
135 size_t (*get_keysize) (public_key_t *this);
136
137 /**
138 * Get a unique key identifier, such as a hash over the key.
139 *
140 * @param type type of the key ID to get
141 * @return unique ID of the key as identification_t, or NULL
142 */
143 identification_t* (*get_id) (public_key_t *this, id_type_t type);
144
145 /**
146 * Get an encoded form of the key.
147 *
148 * @todo Do we need a encoding type specification?
149 *
150 * @return allocated chunk containing encoded key
151 */
152 chunk_t (*get_encoding)(public_key_t *this);
153
154 /**
155 * Increase the refcount of the key.
156 *
157 * @return this with an increased refcount
158 */
159 public_key_t* (*get_ref)(public_key_t *this);
160
161 /**
162 * Destroy a public_key instance.
163 */
164 void (*destroy)(public_key_t *this);
165 };
166
167 #endif /** PUBLIC_KEY_H_ @}*/