2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
17 * @defgroup cred_encoding cred_encoding
18 * @{ @ingroup credentials
21 #ifndef CRED_ENCODING_H_
22 #define CRED_ENCODING_H_
24 typedef struct cred_encoding_t cred_encoding_t
;
25 typedef enum cred_encoding_type_t cred_encoding_type_t
;
26 typedef enum cred_encoding_part_t cred_encoding_part_t
;
31 * Credential encoder function implementing encoding/fingerprinting.
33 * The variable argument list takes cred_encoding_part_t, followed by part
34 * specific arguments, terminated by KEY_PART_END.
36 * @param type format to encode the credential to
37 * @param args list of (cred_encoding_part_t, data)
38 * @param encoding encoding result, allocated
39 * @return TRUE if encoding successful
41 typedef bool (*cred_encoder_t
)(cred_encoding_type_t type
, chunk_t
*encoding
,
45 * Helper function for cred_encoder_t implementations to parse argument list.
47 * Credential encoder functions get a variable argument list to parse. To
48 * simplify the job, this function reads the arguments and returns chunks for
50 * The argument list of this function takes a cred_encoding_part_t, followed
51 * by a data pointer receiving the value, terminated by CRED_PART_END.
53 * @param args argument list passed to credential encoder function
54 * @param ... list of (cred_encoding_part_t, data*)
55 * @return TRUE if all parts found, FALSE otherwise
57 bool cred_encoding_args(va_list args
, ...);
60 * Encoding type of a fingerprint/credential.
62 * Fingerprints have the KEYID_*, public keys the PUBKEY_* and
63 * private keys the PRIVKEY_* prefix.
65 enum cred_encoding_type_t
{
66 /** SHA1 fingerprint over subjectPublicKeyInfo */
67 KEYID_PUBKEY_INFO_SHA1
= 0,
68 /** SHA1 fingerprint over subjectPublicKey */
70 /** PGPv3 fingerprint */
72 /** PGPv4 fingerprint */
77 /** PKCS#1 and similar ASN.1 key encoding */
80 /** subjectPublicKeyInfo encoding */
82 /** PEM encoded PKCS#1 key */
85 /** PGP key encoding */
89 /** ASN.1 DER encoded certificate */
91 /** PEM encoded certificate */
93 /** PGP Packet encoded certificate */
100 * Parts of a credential to encode.
102 enum cred_encoding_part_t
{
103 /** modulus of a RSA key, n */
104 CRED_PART_RSA_MODULUS
,
105 /** public exponent of a RSA key, e */
106 CRED_PART_RSA_PUB_EXP
,
107 /** private exponent of a RSA key, d */
108 CRED_PART_RSA_PRIV_EXP
,
109 /** prime1 a RSA key, p */
110 CRED_PART_RSA_PRIME1
,
111 /** prime2 a RSA key, q */
112 CRED_PART_RSA_PRIME2
,
113 /** exponent1 a RSA key, exp1 */
115 /** exponent1 a RSA key, exp2 */
117 /** coefficient of RSA key, coeff */
119 /** a DER encoded RSA public key */
120 CRED_PART_RSA_PUB_ASN1_DER
,
121 /** a DER encoded RSA private key */
122 CRED_PART_RSA_PRIV_ASN1_DER
,
123 /** a DER encoded ECDSA public key */
124 CRED_PART_ECDSA_PUB_ASN1_DER
,
125 /** a DER encoded ECDSA private key */
126 CRED_PART_ECDSA_PRIV_ASN1_DER
,
127 /** a DER encoded X509 certificate */
128 CRED_PART_X509_ASN1_DER
,
129 /** a DER encoded X509 CRL */
130 CRED_PART_X509_CRL_ASN1_DER
,
131 /** a DER encoded X509 OCSP request */
132 CRED_PART_X509_OCSP_REQ_ASN1_DER
,
133 /** a DER encoded X509 OCSP response */
134 CRED_PART_X509_OCSP_RES_ASN1_DER
,
135 /** a DER encoded X509 attribute certificate */
136 CRED_PART_X509_AC_ASN1_DER
,
137 /** a DER encoded PKCS10 certificate request */
138 CRED_PART_PKCS10_ASN1_DER
,
139 /** a PGP encoded certificate */
146 * Credential encoding and fingerprinting facility.
148 struct cred_encoding_t
{
151 * Encode a credential in a format using several parts, optional caching.
153 * The variable argument list takes cred_encoding_part_t, followed by part
154 * specific arguments, terminated by CRED_PART_END.
155 * If a cache key is given, the returned encoding points to internal data:
156 * do not free or modify. If no cache key is given, the encoding is
157 * allocated and must be freed by the caller.
159 * @param type format the credential should be encoded to
160 * @param cache key to use for caching, NULL to not cache
161 * @param encoding encoding result, allocated if caching disabled
162 * @param ... list of (cred_encoding_part_t, data)
163 * @return TRUE if encoding successful
165 bool (*encode
)(cred_encoding_t
*this, cred_encoding_type_t type
, void *cache
,
166 chunk_t
*encoding
, ...);
169 * Clear all cached encodings of a given cache key.
171 * @param cache key used in encode() for caching
173 void (*clear_cache
)(cred_encoding_t
*this, void *cache
);
176 * Check for a cached encoding.
178 * @param type format of the credential encoding
179 * @param cache key to use for caching, as given to encode()
180 * @param encoding encoding result, internal data
181 * @return TRUE if cache entry found
183 bool (*get_cache
)(cred_encoding_t
*this, cred_encoding_type_t type
,
184 void *cache
, chunk_t
*encoding
);
187 * Cache a credential encoding created externally.
189 * After calling cache(), the passed encoding is owned by the cred encoding
192 * @param type format of the credential encoding
193 * @param cache key to use for caching, as given to encode()
194 * @param encoding encoding to cache, gets owned by this
196 void (*cache
)(cred_encoding_t
*this, cred_encoding_type_t type
, void *cache
,
200 * Register a credential encoder function.
202 * @param encoder credential encoder function to add
204 void (*add_encoder
)(cred_encoding_t
*this, cred_encoder_t encoder
);
207 * Unregister a previously registered credential encoder function.
209 * @param encoder credential encoder function to remove
211 void (*remove_encoder
)(cred_encoding_t
*this, cred_encoder_t encoder
);
214 * Destroy a cred_encoding_t.
216 void (*destroy
)(cred_encoding_t
*this);
220 * Create a cred_encoding instance.
222 cred_encoding_t
*cred_encoding_create();
224 #endif /** CRED_ENCODING_H_ @}*/