4 * This is free software; see Copyright file in the source
5 * distribution for preciese wording.
7 * Copyright.........................
17 #include <xmlsec/xmlsec.h>
18 #include <xmlsec/keys.h>
19 #include <xmlsec/transforms.h>
20 #include <xmlsec/errors.h>
22 #include <xmlsec/nss/crypto.h>
23 #include <xmlsec/nss/tokens.h>
24 #include <xmlsec/nss/akmngr.h>
25 #include <xmlsec/nss/pkikeys.h>
26 #include <xmlsec/nss/ciphers.h>
27 #include <xmlsec/nss/keysstore.h>
30 * xmlSecNssAppliedKeysMngrCreate:
31 * @slot: array of pointers to NSS PKCS#11 slot information.
32 * @cSlots: number of slots in the array
33 * @handler: the pointer to NSS certificate database.
35 * Create and load NSS crypto slot and certificate database into keys manager
37 * Returns keys manager pointer on success or NULL otherwise.
40 xmlSecNssAppliedKeysMngrCreate(
43 CERTCertDBHandle
* handler
45 xmlSecKeyDataStorePtr certStore
= NULL
;
46 xmlSecKeysMngrPtr keyMngr
= NULL
;
47 xmlSecKeyStorePtr keyStore
= NULL
;
49 keyStore
= xmlSecKeyStoreCreate( xmlSecNssKeysStoreId
) ;
50 if( keyStore
== NULL
) {
51 xmlSecError( XMLSEC_ERRORS_HERE
,
53 "xmlSecKeyStoreCreate" ,
54 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
55 XMLSEC_ERRORS_NO_MESSAGE
) ;
59 for (islot
= 0; islot
< cSlots
; islot
++)
61 xmlSecNssKeySlotPtr keySlot
;
63 /* Create a key slot */
64 keySlot
= xmlSecNssKeySlotCreate() ;
65 if( keySlot
== NULL
) {
66 xmlSecError( XMLSEC_ERRORS_HERE
,
67 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore
) ) ,
68 "xmlSecNssKeySlotCreate" ,
69 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
70 XMLSEC_ERRORS_NO_MESSAGE
) ;
72 xmlSecKeyStoreDestroy( keyStore
) ;
77 if( xmlSecNssKeySlotSetSlot( keySlot
, slots
[islot
] ) < 0 ) {
78 xmlSecError( XMLSEC_ERRORS_HERE
,
79 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore
) ) ,
80 "xmlSecNssKeySlotSetSlot" ,
81 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
82 XMLSEC_ERRORS_NO_MESSAGE
) ;
84 xmlSecKeyStoreDestroy( keyStore
) ;
85 xmlSecNssKeySlotDestroy( keySlot
) ;
90 if( xmlSecNssKeysStoreAdoptKeySlot( keyStore
, keySlot
) < 0 ) {
91 xmlSecError( XMLSEC_ERRORS_HERE
,
92 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore
) ) ,
93 "xmlSecNssKeysStoreAdoptKeySlot" ,
94 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
95 XMLSEC_ERRORS_NO_MESSAGE
) ;
97 xmlSecKeyStoreDestroy( keyStore
) ;
98 xmlSecNssKeySlotDestroy( keySlot
) ;
103 keyMngr
= xmlSecKeysMngrCreate() ;
104 if( keyMngr
== NULL
) {
105 xmlSecError( XMLSEC_ERRORS_HERE
,
107 "xmlSecKeysMngrCreate" ,
108 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
109 XMLSEC_ERRORS_NO_MESSAGE
) ;
111 xmlSecKeyStoreDestroy( keyStore
) ;
116 * Add key store to manager, from now on keys manager destroys the store if
119 if( xmlSecKeysMngrAdoptKeysStore( keyMngr
, keyStore
) < 0 ) {
120 xmlSecError( XMLSEC_ERRORS_HERE
,
121 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore
) ) ,
122 "xmlSecKeysMngrAdoptKeyStore" ,
123 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
124 XMLSEC_ERRORS_NO_MESSAGE
) ;
126 xmlSecKeyStoreDestroy( keyStore
) ;
127 xmlSecKeysMngrDestroy( keyMngr
) ;
132 * Initialize crypto library specific data in keys manager
134 if( xmlSecNssKeysMngrInit( keyMngr
) < 0 ) {
135 xmlSecError( XMLSEC_ERRORS_HERE
,
137 "xmlSecKeysMngrCreate" ,
138 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
139 XMLSEC_ERRORS_NO_MESSAGE
) ;
141 xmlSecKeysMngrDestroy( keyMngr
) ;
146 * Set certificate databse to X509 key data store
149 * Because Tej's implementation of certDB use the default DB, so I ignore
150 * the certDB handler at present. I'll modify the cert store sources to
151 * accept particular certDB instead of default ones.
152 certStore = xmlSecKeysMngrGetDataStore( keyMngr , xmlSecNssKeyDataStoreX509Id ) ;
153 if( certStore == NULL ) {
154 xmlSecError( XMLSEC_ERRORS_HERE ,
155 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) ,
156 "xmlSecKeysMngrGetDataStore" ,
157 XMLSEC_ERRORS_R_XMLSEC_FAILED ,
158 XMLSEC_ERRORS_NO_MESSAGE ) ;
160 xmlSecKeysMngrDestroy( keyMngr ) ;
164 if( xmlSecNssKeyDataStoreX509SetCertDb( certStore , handler ) < 0 ) {
165 xmlSecError( XMLSEC_ERRORS_HERE ,
166 xmlSecErrorsSafeString( xmlSecKeyStoreGetName( keyStore ) ) ,
167 "xmlSecNssKeyDataStoreX509SetCertDb" ,
168 XMLSEC_ERRORS_R_XMLSEC_FAILED ,
169 XMLSEC_ERRORS_NO_MESSAGE ) ;
171 xmlSecKeysMngrDestroy( keyMngr ) ;
177 * Set the getKey callback
179 keyMngr
->getKey
= xmlSecKeysMngrGetKey
;
185 xmlSecNssAppliedKeysMngrSymKeyLoad(
186 xmlSecKeysMngrPtr mngr
,
190 xmlSecKeyDataPtr data
;
191 xmlSecKeyStorePtr keyStore
;
193 xmlSecAssert2( mngr
!= NULL
, -1 ) ;
194 xmlSecAssert2( symKey
!= NULL
, -1 ) ;
196 keyStore
= xmlSecKeysMngrGetKeysStore( mngr
) ;
197 if( keyStore
== NULL
) {
198 xmlSecError( XMLSEC_ERRORS_HERE
,
200 "xmlSecKeysMngrGetKeysStore" ,
201 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
202 XMLSEC_ERRORS_NO_MESSAGE
) ;
205 xmlSecAssert2( xmlSecKeyStoreCheckId( keyStore
, xmlSecNssKeysStoreId
) , -1 ) ;
207 data
= xmlSecNssSymKeyDataKeyAdopt( symKey
) ;
209 xmlSecError( XMLSEC_ERRORS_HERE
,
211 "xmlSecNssSymKeyDataKeyAdopt" ,
212 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
213 XMLSEC_ERRORS_NO_MESSAGE
) ;
217 key
= xmlSecKeyCreate() ;
219 xmlSecError( XMLSEC_ERRORS_HERE
,
221 "xmlSecNssSymKeyDataKeyAdopt" ,
222 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
223 XMLSEC_ERRORS_NO_MESSAGE
) ;
224 xmlSecKeyDataDestroy( data
) ;
228 if( xmlSecKeySetValue( key
, data
) < 0 ) {
229 xmlSecError( XMLSEC_ERRORS_HERE
,
231 "xmlSecNssSymKeyDataKeyAdopt" ,
232 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
233 XMLSEC_ERRORS_NO_MESSAGE
) ;
234 xmlSecKeyDataDestroy( data
) ;
238 if( xmlSecNssKeysStoreAdoptKey( keyStore
, key
) < 0 ) {
239 xmlSecError( XMLSEC_ERRORS_HERE
,
241 "xmlSecNssSymKeyDataKeyAdopt" ,
242 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
243 XMLSEC_ERRORS_NO_MESSAGE
) ;
244 xmlSecKeyDestroy( key
) ;
252 xmlSecNssAppliedKeysMngrPubKeyLoad(
253 xmlSecKeysMngrPtr mngr
,
254 SECKEYPublicKey
* pubKey
257 xmlSecKeyDataPtr data
;
258 xmlSecKeyStorePtr keyStore
;
260 xmlSecAssert2( mngr
!= NULL
, -1 ) ;
261 xmlSecAssert2( pubKey
!= NULL
, -1 ) ;
263 keyStore
= xmlSecKeysMngrGetKeysStore( mngr
) ;
264 if( keyStore
== NULL
) {
265 xmlSecError( XMLSEC_ERRORS_HERE
,
267 "xmlSecKeysMngrGetKeysStore" ,
268 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
269 XMLSEC_ERRORS_NO_MESSAGE
) ;
272 xmlSecAssert2( xmlSecKeyStoreCheckId( keyStore
, xmlSecNssKeysStoreId
) , -1 ) ;
274 data
= xmlSecNssPKIAdoptKey( NULL
, pubKey
) ;
276 xmlSecError( XMLSEC_ERRORS_HERE
,
278 "xmlSecNssPKIAdoptKey" ,
279 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
280 XMLSEC_ERRORS_NO_MESSAGE
) ;
284 key
= xmlSecKeyCreate() ;
286 xmlSecError( XMLSEC_ERRORS_HERE
,
288 "xmlSecNssSymKeyDataKeyAdopt" ,
289 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
290 XMLSEC_ERRORS_NO_MESSAGE
) ;
291 xmlSecKeyDataDestroy( data
) ;
295 if( xmlSecKeySetValue( key
, data
) < 0 ) {
296 xmlSecError( XMLSEC_ERRORS_HERE
,
298 "xmlSecNssSymKeyDataKeyAdopt" ,
299 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
300 XMLSEC_ERRORS_NO_MESSAGE
) ;
301 xmlSecKeyDataDestroy( data
) ;
305 if( xmlSecNssKeysStoreAdoptKey( keyStore
, key
) < 0 ) {
306 xmlSecError( XMLSEC_ERRORS_HERE
,
308 "xmlSecNssSymKeyDataKeyAdopt" ,
309 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
310 XMLSEC_ERRORS_NO_MESSAGE
) ;
311 xmlSecKeyDestroy( key
) ;
319 xmlSecNssAppliedKeysMngrPriKeyLoad(
320 xmlSecKeysMngrPtr mngr
,
321 SECKEYPrivateKey
* priKey
324 xmlSecKeyDataPtr data
;
325 xmlSecKeyStorePtr keyStore
;
327 xmlSecAssert2( mngr
!= NULL
, -1 ) ;
328 xmlSecAssert2( priKey
!= NULL
, -1 ) ;
330 keyStore
= xmlSecKeysMngrGetKeysStore( mngr
) ;
331 if( keyStore
== NULL
) {
332 xmlSecError( XMLSEC_ERRORS_HERE
,
334 "xmlSecKeysMngrGetKeysStore" ,
335 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
336 XMLSEC_ERRORS_NO_MESSAGE
) ;
339 xmlSecAssert2( xmlSecKeyStoreCheckId( keyStore
, xmlSecNssKeysStoreId
) , -1 ) ;
341 data
= xmlSecNssPKIAdoptKey( priKey
, NULL
) ;
343 xmlSecError( XMLSEC_ERRORS_HERE
,
345 "xmlSecNssPKIAdoptKey" ,
346 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
347 XMLSEC_ERRORS_NO_MESSAGE
) ;
351 key
= xmlSecKeyCreate() ;
353 xmlSecError( XMLSEC_ERRORS_HERE
,
355 "xmlSecNssSymKeyDataKeyAdopt" ,
356 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
357 XMLSEC_ERRORS_NO_MESSAGE
) ;
358 xmlSecKeyDataDestroy( data
) ;
362 if( xmlSecKeySetValue( key
, data
) < 0 ) {
363 xmlSecError( XMLSEC_ERRORS_HERE
,
365 "xmlSecNssSymKeyDataKeyAdopt" ,
366 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
367 XMLSEC_ERRORS_NO_MESSAGE
) ;
368 xmlSecKeyDataDestroy( data
) ;
372 if( xmlSecNssKeysStoreAdoptKey( keyStore
, key
) < 0 ) {
373 xmlSecError( XMLSEC_ERRORS_HERE
,
375 "xmlSecNssSymKeyDataKeyAdopt" ,
376 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
377 XMLSEC_ERRORS_NO_MESSAGE
) ;
378 xmlSecKeyDestroy( key
) ;