1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <xmlsec/xmlsec.h>
22 #include <xmlsec/keys.h>
23 #include <xmlsec/keysmngr.h>
24 #include <xmlsec/transforms.h>
25 #include <xmlsec/errors.h>
27 #include <xmlsec/mscrypto/crypto.h>
28 #include <xmlsec/mscrypto/keysstore.h>
29 #include <xmlsec/mscrypto/x509.h>
32 * xmlSecMSCryptoAppliedKeysMngrCreate:
34 * Create and load key store and certificate database into keys manager
36 * Returns keys manager pointer on success or NULL otherwise.
38 xmlSecKeysMngrPtr
xmlSecMSCryptoAppliedKeysMngrCreate()
40 xmlSecKeysMngrPtr keyMngr
= nullptr ;
41 xmlSecKeyStorePtr keyStore
= nullptr ;
43 keyStore
= xmlSecKeyStoreCreate(xmlSecMSCryptoKeysStoreId
) ;
44 if (keyStore
== nullptr)
46 xmlSecError(XMLSEC_ERRORS_HERE
,
48 "xmlSecKeyStoreCreate",
49 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
50 XMLSEC_ERRORS_NO_MESSAGE
) ;
55 * At present, MS Crypto engine do not provide a way to setup a key store.
57 if (keyStore
!= nullptr)
59 /*TODO: binding key store.*/
62 keyMngr
= xmlSecKeysMngrCreate() ;
63 if (keyMngr
== nullptr)
65 xmlSecError(XMLSEC_ERRORS_HERE
,
67 "xmlSecKeysMngrCreate",
68 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
69 XMLSEC_ERRORS_NO_MESSAGE
) ;
71 xmlSecKeyStoreDestroy(keyStore
) ;
76 * Add key store to manager, from now on keys manager destroys the store if
79 if (xmlSecKeysMngrAdoptKeysStore(keyMngr
, keyStore
) < 0)
81 xmlSecError(XMLSEC_ERRORS_HERE
,
82 xmlSecErrorsSafeString(xmlSecKeyStoreGetName(keyStore
)),
83 "xmlSecKeysMngrAdoptKeyStore",
84 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
85 XMLSEC_ERRORS_NO_MESSAGE
) ;
87 xmlSecKeyStoreDestroy(keyStore
) ;
88 xmlSecKeysMngrDestroy(keyMngr
) ;
93 * Initialize crypto library specific data in keys manager
95 if (xmlSecMSCryptoKeysMngrInit(keyMngr
) < 0)
97 xmlSecError(XMLSEC_ERRORS_HERE
,
99 "xmlSecMSCryptoKeysMngrInit",
100 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
101 XMLSEC_ERRORS_NO_MESSAGE
) ;
103 xmlSecKeysMngrDestroy(keyMngr
) ;
108 * Set certificate database to X509 key data store
111 * At present, MS Crypto engine do not provide a way to setup a cert store.
115 * Set the getKey callback
117 keyMngr
->getKey
= xmlSecKeysMngrGetKey
;
123 xmlSecMSCryptoAppliedKeysMngrAdoptKeyStore(
124 xmlSecKeysMngrPtr mngr
,
128 xmlSecKeyDataStorePtr x509Store
;
130 xmlSecAssert2(mngr
!= nullptr, -1) ;
131 xmlSecAssert2(keyStore
!= nullptr, -1) ;
133 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
134 if (x509Store
== nullptr)
136 xmlSecError(XMLSEC_ERRORS_HERE
,
138 "xmlSecKeysMngrGetDataStore",
139 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
140 XMLSEC_ERRORS_NO_MESSAGE
) ;
144 if (xmlSecMSCryptoX509StoreAdoptKeyStore(x509Store
, keyStore
) < 0)
146 xmlSecError(XMLSEC_ERRORS_HERE
,
147 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
148 "xmlSecMSCryptoX509StoreAdoptKeyStore",
149 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
150 XMLSEC_ERRORS_NO_MESSAGE
) ;
158 xmlSecMSCryptoAppliedKeysMngrAdoptTrustedStore(
159 xmlSecKeysMngrPtr mngr
,
160 HCERTSTORE trustedStore
163 xmlSecKeyDataStorePtr x509Store
;
165 xmlSecAssert2(mngr
!= nullptr, -1) ;
166 xmlSecAssert2(trustedStore
!= nullptr, -1) ;
168 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
169 if (x509Store
== nullptr)
171 xmlSecError(XMLSEC_ERRORS_HERE
,
173 "xmlSecKeysMngrGetDataStore",
174 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
175 XMLSEC_ERRORS_NO_MESSAGE
) ;
179 if (xmlSecMSCryptoX509StoreAdoptTrustedStore(x509Store
, trustedStore
) < 0)
181 xmlSecError(XMLSEC_ERRORS_HERE
,
182 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
183 "xmlSecMSCryptoX509StoreAdoptKeyStore",
184 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
185 XMLSEC_ERRORS_NO_MESSAGE
) ;
193 xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore(
194 xmlSecKeysMngrPtr mngr
,
195 HCERTSTORE untrustedStore
198 xmlSecKeyDataStorePtr x509Store
;
200 xmlSecAssert2(mngr
!= nullptr, -1) ;
201 xmlSecAssert2(untrustedStore
!= nullptr, -1) ;
203 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
204 if (x509Store
== nullptr)
206 xmlSecError(XMLSEC_ERRORS_HERE
,
208 "xmlSecKeysMngrGetDataStore",
209 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
210 XMLSEC_ERRORS_NO_MESSAGE
) ;
214 if (xmlSecMSCryptoX509StoreAdoptUntrustedStore(x509Store
, untrustedStore
) < 0)
216 xmlSecError(XMLSEC_ERRORS_HERE
,
217 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
218 "xmlSecMSCryptoX509StoreAdoptKeyStore",
219 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
220 XMLSEC_ERRORS_NO_MESSAGE
) ;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */