1 /*
2  * XML Security Library (http://www.aleksey.com/xmlsec).
3  *
4  * Keys.
5  *
6  * This is free software; see Copyright file in the source
7  * distribution for preciese wording.
8  *
9  * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
10  */
11 #ifndef __XMLSEC_KEYS_H__
12 #define __XMLSEC_KEYS_H__
13 
14 #include <time.h>
15 
16 #include <xmlsec/xmlsec.h>
17 #include <xmlsec/list.h>
18 #include <xmlsec/keysdata.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif /* __cplusplus */
23 
24 /**
25  * xmlSecKeyUsage:
26  *
27  * The key usage.
28  */
29 typedef unsigned int                    xmlSecKeyUsage;
30 
31 /**
32  * xmlSecKeyUsageSign:
33  *
34  * Key for signing.
35  */
36 #define xmlSecKeyUsageSign              0x00000001
37 
38 /**
39  * xmlSecKeyUsageVerify:
40  *
41  * Key for signature verification.
42  */
43 #define xmlSecKeyUsageVerify            0x00000002
44 
45 /**
46  * xmlSecKeyUsageEncrypt:
47  *
48  * An encryption key.
49  */
50 #define xmlSecKeyUsageEncrypt           0x00000004
51 
52 /**
53  * xmlSecKeyUsageDecrypt:
54  *
55  * A decryption key.
56  */
57 #define xmlSecKeyUsageDecrypt           0x00000008
58 
59 /**
60  * xmlSecKeyUsageKeyExchange:
61  *
62  * The key is used for key exchange.
63  */
64 #define xmlSecKeyUsageKeyExchange       0x00000010
65 
66 /**
67  * xmlSecKeyUsageAny:
68  *
69  * Key can be used in any way.
70  */
71 #define xmlSecKeyUsageAny               0xFFFFFFFF
72 
73 
74 /**************************************************************************
75  *
76  * xmlSecKeyUseWith
77  *
78  *************************************************************************/
79 typedef struct _xmlSecKeyUseWith                xmlSecKeyUseWith, *xmlSecKeyUseWithPtr;
80 XMLSEC_EXPORT int       xmlSecKeyUseWithInitialize              (xmlSecKeyUseWithPtr keyUseWith);
81 XMLSEC_EXPORT void      xmlSecKeyUseWithFinalize                (xmlSecKeyUseWithPtr keyUseWith);
82 XMLSEC_EXPORT void      xmlSecKeyUseWithReset                   (xmlSecKeyUseWithPtr keyUseWith);
83 XMLSEC_EXPORT int       xmlSecKeyUseWithCopy                    (xmlSecKeyUseWithPtr dst,
84                                                                  xmlSecKeyUseWithPtr src);
85 XMLSEC_EXPORT xmlSecKeyUseWithPtr xmlSecKeyUseWithCreate        (const xmlChar* application,
86                                                                  const xmlChar* identifier);
87 XMLSEC_EXPORT xmlSecKeyUseWithPtr xmlSecKeyUseWithDuplicate     (xmlSecKeyUseWithPtr keyUseWith);
88 XMLSEC_EXPORT void      xmlSecKeyUseWithDestroy                 (xmlSecKeyUseWithPtr keyUseWith);
89 XMLSEC_EXPORT int       xmlSecKeyUseWithSet                     (xmlSecKeyUseWithPtr keyUseWith,
90                                                                  const xmlChar* application,
91                                                                  const xmlChar* identifier);
92 XMLSEC_EXPORT void      xmlSecKeyUseWithDebugDump               (xmlSecKeyUseWithPtr keyUseWith,
93                                                                  FILE* output);
94 XMLSEC_EXPORT void      xmlSecKeyUseWithDebugXmlDump            (xmlSecKeyUseWithPtr keyUseWith,
95                                                                  FILE* output);
96 
97 /**
98  * xmlSecKeyUseWith:
99  * @application:        the application.
100  * @identifier:         the identifier.
101  * @reserved1:          reserved for future use.
102  * @reserved2:          reserved for future use.
103  *
104  * Information about application and user of the key.
105  */
106 struct _xmlSecKeyUseWith {
107     xmlChar*                    application;
108     xmlChar*                    identifier;
109 
110     void*                       reserved1;
111     void*                       reserved2;
112 };
113 
114 /**
115  * xmlSecKeyUseWithPtrListId:
116  *
117  * The keys list klass.
118  */
119 #define xmlSecKeyUseWithPtrListId       xmlSecKeyUseWithPtrListGetKlass()
120 XMLSEC_EXPORT xmlSecPtrListId   xmlSecKeyUseWithPtrListGetKlass (void);
121 
122 /**************************************************************************
123  *
124  * xmlSecKeyReq - what key are we looking for?
125  *
126  *************************************************************************/
127 typedef struct _xmlSecKeyReq                    xmlSecKeyReq, *xmlSecKeyReqPtr;
128 
129 /**
130  * xmlSecKeyReq:
131  * @keyId:              the desired key value klass.
132  * @keyType:            the desired key type.
133  * @keyUsage:           the desired key usage.
134  * @keyBitsSize:        the desired key size (in bits!).
135  * @keyUseWithList:     the desired key use with application/identifier information.
136  * @reserved1:          reserved for future use.
137  * @reserved2:          reserved for future use.
138  *
139  * The key requirements information.
140  */
141 struct _xmlSecKeyReq {
142     xmlSecKeyDataId             keyId;
143     xmlSecKeyDataType           keyType;
144     xmlSecKeyUsage              keyUsage;
145     xmlSecSize                  keyBitsSize;
146     xmlSecPtrList               keyUseWithList;
147 
148     void*                       reserved1;
149     void*                       reserved2;
150 };
151 
152 XMLSEC_EXPORT int       xmlSecKeyReqInitialize                  (xmlSecKeyReqPtr keyReq);
153 XMLSEC_EXPORT void      xmlSecKeyReqFinalize                    (xmlSecKeyReqPtr keyReq);
154 XMLSEC_EXPORT void      xmlSecKeyReqReset                       (xmlSecKeyReqPtr keyReq);
155 XMLSEC_EXPORT int       xmlSecKeyReqCopy                        (xmlSecKeyReqPtr dst,
156                                                                  xmlSecKeyReqPtr src);
157 XMLSEC_EXPORT int       xmlSecKeyReqMatchKey                    (xmlSecKeyReqPtr keyReq,
158                                                                  xmlSecKeyPtr key);
159 XMLSEC_EXPORT int       xmlSecKeyReqMatchKeyValue               (xmlSecKeyReqPtr keyReq,
160                                                                  xmlSecKeyDataPtr value);
161 XMLSEC_EXPORT void      xmlSecKeyReqDebugDump                   (xmlSecKeyReqPtr keyReq,
162                                                                  FILE* output);
163 XMLSEC_EXPORT void      xmlSecKeyReqDebugXmlDump                (xmlSecKeyReqPtr keyReq,
164                                                                  FILE* output);
165 
166 /**
167  * xmlSecKey:
168  * @name:               the key name.
169  * @value:              the key value.
170  * @dataList:           the key data list.
171  * @usage:              the key usage.
172  * @notValidBefore:     the start key validity interval.
173  * @notValidAfter:      the end key validity interval.
174  *
175  * The key.
176  */
177 struct _xmlSecKey {
178     xmlChar*                            name;
179     xmlSecKeyDataPtr                    value;
180     xmlSecPtrListPtr                    dataList;
181     xmlSecKeyUsage                      usage;
182     time_t                              notValidBefore;
183     time_t                              notValidAfter;
184 };
185 
186 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyCreate         (void);
187 XMLSEC_EXPORT void              xmlSecKeyDestroy        (xmlSecKeyPtr key);
188 XMLSEC_EXPORT void              xmlSecKeyEmpty          (xmlSecKeyPtr key);
189 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyDuplicate      (xmlSecKeyPtr key);
190 XMLSEC_EXPORT int               xmlSecKeyCopy           (xmlSecKeyPtr keyDst,
191                                                          xmlSecKeyPtr keySrc);
192 
193 XMLSEC_EXPORT const xmlChar*    xmlSecKeyGetName        (xmlSecKeyPtr key);
194 XMLSEC_EXPORT int               xmlSecKeySetName        (xmlSecKeyPtr key,
195                                                          const xmlChar* name);
196 
197 XMLSEC_EXPORT xmlSecKeyDataType xmlSecKeyGetType        (xmlSecKeyPtr key);
198 
199 XMLSEC_EXPORT xmlSecKeyDataPtr  xmlSecKeyGetValue       (xmlSecKeyPtr key);
200 XMLSEC_EXPORT int               xmlSecKeySetValue       (xmlSecKeyPtr key,
201                                                          xmlSecKeyDataPtr value);
202 
203 XMLSEC_EXPORT xmlSecKeyDataPtr  xmlSecKeyGetData        (xmlSecKeyPtr key,
204                                                          xmlSecKeyDataId dataId);
205 XMLSEC_EXPORT xmlSecKeyDataPtr  xmlSecKeyEnsureData     (xmlSecKeyPtr key,
206                                                          xmlSecKeyDataId dataId);
207 XMLSEC_EXPORT int               xmlSecKeyAdoptData      (xmlSecKeyPtr key,
208                                                          xmlSecKeyDataPtr data);
209 
210 XMLSEC_EXPORT void              xmlSecKeyDebugDump      (xmlSecKeyPtr key,
211                                                          FILE *output);
212 XMLSEC_EXPORT void              xmlSecKeyDebugXmlDump   (xmlSecKeyPtr key,
213                                                          FILE *output);
214 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyGenerate       (xmlSecKeyDataId dataId,
215                                                          xmlSecSize sizeBits,
216                                                          xmlSecKeyDataType type);
217 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyGenerateByName (const xmlChar* name,
218                                                          xmlSecSize sizeBits,
219                                                          xmlSecKeyDataType type);
220 
221 
222 XMLSEC_EXPORT int               xmlSecKeyMatch          (xmlSecKeyPtr key,
223                                                          const xmlChar *name,
224                                                          xmlSecKeyReqPtr keyReq);
225 
226 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyReadBuffer     (xmlSecKeyDataId dataId,
227                                                          xmlSecBuffer* buffer);
228 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyReadBinaryFile (xmlSecKeyDataId dataId,
229                                                          const char* filename);
230 XMLSEC_EXPORT xmlSecKeyPtr      xmlSecKeyReadMemory     (xmlSecKeyDataId dataId,
231                                                          const xmlSecByte* data,
232                                                          xmlSecSize dataSize);
233 
234 
235 /**
236  * xmlSecKeyIsValid:
237  * @key: the pointer to key.
238  *
239  * Macro. Returns 1 if @key is not NULL and @key->id is not NULL
240  * or 0 otherwise.
241  */
242 #define xmlSecKeyIsValid(key) \
243         ((( key ) != NULL) && \
244          (( key )->value != NULL) && \
245          ((( key )->value->id) != NULL))
246 /**
247  * xmlSecKeyCheckId:
248  * @key: the pointer to key.
249  * @keyId: the key Id.
250  *
251  * Macro. Returns 1 if @key is valid and @key's id is equal to @keyId.
252  */
253 #define xmlSecKeyCheckId(key, keyId) \
254         (xmlSecKeyIsValid(( key )) && \
255         ((( key )->value->id) == ( keyId )))
256 
257 
258 /***********************************************************************
259  *
260  * Keys list
261  *
262  **********************************************************************/
263 /**
264  * xmlSecKeyPtrListId:
265  *
266  * The keys list klass.
267  */
268 #define xmlSecKeyPtrListId      xmlSecKeyPtrListGetKlass()
269 XMLSEC_EXPORT xmlSecPtrListId   xmlSecKeyPtrListGetKlass                (void);
270 
271 #ifdef __cplusplus
272 }
273 #endif /* __cplusplus */
274 
275 #endif /* __XMLSEC_KEYS_H__ */
276 
277