Bump version to 6.0-36
[LibreOffice.git] / xmlsecurity / source / xmlsec / mscrypt / akmngr.cxx
blob4f0edd4df43ae7d4cd3838f6afad5218aa91c799
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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 .
19 #include "akmngr.hxx"
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>
31 /**
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,
47 nullptr,
48 "xmlSecKeyStoreCreate",
49 XMLSEC_ERRORS_R_XMLSEC_FAILED,
50 XMLSEC_ERRORS_NO_MESSAGE) ;
51 return nullptr ;
54 /*-
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,
66 nullptr,
67 "xmlSecKeysMngrCreate",
68 XMLSEC_ERRORS_R_XMLSEC_FAILED,
69 XMLSEC_ERRORS_NO_MESSAGE) ;
71 xmlSecKeyStoreDestroy(keyStore) ;
72 return nullptr ;
75 /*-
76 * Add key store to manager, from now on keys manager destroys the store if
77 * needed
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) ;
89 return nullptr ;
92 /*-
93 * Initialize crypto library specific data in keys manager
95 if (xmlSecMSCryptoKeysMngrInit(keyMngr) < 0)
97 xmlSecError(XMLSEC_ERRORS_HERE,
98 nullptr,
99 "xmlSecMSCryptoKeysMngrInit",
100 XMLSEC_ERRORS_R_XMLSEC_FAILED,
101 XMLSEC_ERRORS_NO_MESSAGE) ;
103 xmlSecKeysMngrDestroy(keyMngr) ;
104 return nullptr ;
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 ;
119 return keyMngr ;
123 xmlSecMSCryptoAppliedKeysMngrAdoptKeyStore(
124 xmlSecKeysMngrPtr mngr,
125 HCERTSTORE keyStore
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,
137 nullptr,
138 "xmlSecKeysMngrGetDataStore",
139 XMLSEC_ERRORS_R_XMLSEC_FAILED,
140 XMLSEC_ERRORS_NO_MESSAGE) ;
141 return (-1) ;
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) ;
151 return (-1) ;
154 return (0) ;
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,
172 nullptr,
173 "xmlSecKeysMngrGetDataStore",
174 XMLSEC_ERRORS_R_XMLSEC_FAILED,
175 XMLSEC_ERRORS_NO_MESSAGE) ;
176 return (-1) ;
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) ;
186 return (-1) ;
189 return (0) ;
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,
207 nullptr,
208 "xmlSecKeysMngrGetDataStore",
209 XMLSEC_ERRORS_R_XMLSEC_FAILED,
210 XMLSEC_ERRORS_NO_MESSAGE) ;
211 return (-1) ;
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) ;
221 return (-1) ;
224 return (0) ;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */