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>
35 * MSCryptoAppliedKeysMngrCreate:
37 * Create and load key store and certificate database into keys manager
39 * Returns keys manager pointer on success or NULL otherwise.
41 xmlSecKeysMngrPtr
MSCryptoAppliedKeysMngrCreate()
43 xmlSecKeysMngrPtr keyMngr
= nullptr ;
44 xmlSecKeyStorePtr keyStore
= nullptr ;
46 keyStore
= xmlSecKeyStoreCreate(xmlSecMSCryptoKeysStoreId
) ;
47 if (keyStore
== nullptr)
49 xmlSecError(XMLSEC_ERRORS_HERE
,
51 "xmlSecKeyStoreCreate",
52 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
53 XMLSEC_ERRORS_NO_MESSAGE
) ;
58 * At present, MS Crypto engine do not provide a way to setup a key store.
60 if (keyStore
!= nullptr)
62 /*TODO: binding key store.*/
65 keyMngr
= xmlSecKeysMngrCreate() ;
66 if (keyMngr
== nullptr)
68 xmlSecError(XMLSEC_ERRORS_HERE
,
70 "xmlSecKeysMngrCreate",
71 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
72 XMLSEC_ERRORS_NO_MESSAGE
) ;
74 xmlSecKeyStoreDestroy(keyStore
) ;
79 * Add key store to manager, from now on keys manager destroys the store if
82 if (xmlSecKeysMngrAdoptKeysStore(keyMngr
, keyStore
) < 0)
84 xmlSecError(XMLSEC_ERRORS_HERE
,
85 xmlSecErrorsSafeString(xmlSecKeyStoreGetName(keyStore
)),
86 "xmlSecKeysMngrAdoptKeyStore",
87 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
88 XMLSEC_ERRORS_NO_MESSAGE
) ;
90 xmlSecKeyStoreDestroy(keyStore
) ;
91 xmlSecKeysMngrDestroy(keyMngr
) ;
96 * Initialize crypto library specific data in keys manager
98 if (xmlSecMSCryptoKeysMngrInit(keyMngr
) < 0)
100 xmlSecError(XMLSEC_ERRORS_HERE
,
102 "xmlSecMSCryptoKeysMngrInit",
103 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
104 XMLSEC_ERRORS_NO_MESSAGE
) ;
106 xmlSecKeysMngrDestroy(keyMngr
) ;
111 * Set certificate database to X509 key data store
114 * At present, MS Crypto engine do not provide a way to setup a cert store.
118 * Set the getKey callback
120 keyMngr
->getKey
= xmlSecKeysMngrGetKey
;
126 MSCryptoAppliedKeysMngrAdoptKeyStore(
127 xmlSecKeysMngrPtr mngr
,
131 xmlSecKeyDataStorePtr x509Store
;
133 xmlSecAssert2(mngr
!= nullptr, -1) ;
134 xmlSecAssert2(keyStore
!= nullptr, -1) ;
136 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
137 if (x509Store
== nullptr)
139 xmlSecError(XMLSEC_ERRORS_HERE
,
141 "xmlSecKeysMngrGetDataStore",
142 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
143 XMLSEC_ERRORS_NO_MESSAGE
) ;
147 if (xmlSecMSCryptoX509StoreAdoptKeyStore(x509Store
, keyStore
) < 0)
149 xmlSecError(XMLSEC_ERRORS_HERE
,
150 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
151 "xmlSecMSCryptoX509StoreAdoptKeyStore",
152 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
153 XMLSEC_ERRORS_NO_MESSAGE
) ;
161 MSCryptoAppliedKeysMngrAdoptTrustedStore(
162 xmlSecKeysMngrPtr mngr
,
163 HCERTSTORE trustedStore
166 xmlSecKeyDataStorePtr x509Store
;
168 xmlSecAssert2(mngr
!= nullptr, -1) ;
169 xmlSecAssert2(trustedStore
!= nullptr, -1) ;
171 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
172 if (x509Store
== nullptr)
174 xmlSecError(XMLSEC_ERRORS_HERE
,
176 "xmlSecKeysMngrGetDataStore",
177 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
178 XMLSEC_ERRORS_NO_MESSAGE
) ;
182 if (xmlSecMSCryptoX509StoreAdoptTrustedStore(x509Store
, trustedStore
) < 0)
184 xmlSecError(XMLSEC_ERRORS_HERE
,
185 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
186 "xmlSecMSCryptoX509StoreAdoptKeyStore",
187 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
188 XMLSEC_ERRORS_NO_MESSAGE
) ;
196 MSCryptoAppliedKeysMngrAdoptUntrustedStore(
197 xmlSecKeysMngrPtr mngr
,
198 HCERTSTORE untrustedStore
201 xmlSecKeyDataStorePtr x509Store
;
203 xmlSecAssert2(mngr
!= nullptr, -1) ;
204 xmlSecAssert2(untrustedStore
!= nullptr, -1) ;
206 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCryptoX509StoreId
) ;
207 if (x509Store
== nullptr)
209 xmlSecError(XMLSEC_ERRORS_HERE
,
211 "xmlSecKeysMngrGetDataStore",
212 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
213 XMLSEC_ERRORS_NO_MESSAGE
) ;
217 if (xmlSecMSCryptoX509StoreAdoptUntrustedStore(x509Store
, untrustedStore
) < 0)
219 xmlSecError(XMLSEC_ERRORS_HERE
,
220 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
221 "xmlSecMSCryptoX509StoreAdoptKeyStore",
222 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
223 XMLSEC_ERRORS_NO_MESSAGE
) ;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */