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 .
20 #include <sal/config.h>
24 #include <xmlsec/xmlsec.h>
25 #include <xmlsec/keys.h>
26 #include <xmlsec/keysmngr.h>
27 #include <xmlsec/transforms.h>
28 #include <xmlsec/errors.h>
30 #include <xmlsec/mscng/crypto.h>
31 #include <xmlsec/mscng/keysstore.h>
32 #include <xmlsec/mscng/x509.h>
33 #include <svl/cryptosign.hxx>
39 * MSCryptoAppliedKeysMngrCreate:
41 * Create and load key store and certificate database into keys manager
43 * Returns keys manager pointer on success or NULL otherwise.
45 xmlSecKeysMngrPtr
MSCryptoAppliedKeysMngrCreate()
47 xmlSecKeysMngrPtr keyMngr
= nullptr ;
48 xmlSecKeyStorePtr keyStore
= nullptr ;
50 keyStore
= xmlSecKeyStoreCreate(xmlSecMSCngKeysStoreId
);
51 if (keyStore
== nullptr)
53 xmlSecError(XMLSEC_ERRORS_HERE
,
55 "xmlSecKeyStoreCreate",
56 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
57 XMLSEC_ERRORS_NO_MESSAGE
) ;
61 keyMngr
= xmlSecKeysMngrCreate() ;
62 if (keyMngr
== nullptr)
64 xmlSecError(XMLSEC_ERRORS_HERE
,
66 "xmlSecKeysMngrCreate",
67 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
68 XMLSEC_ERRORS_NO_MESSAGE
) ;
70 xmlSecKeyStoreDestroy(keyStore
) ;
75 * Add key store to manager, from now on keys manager destroys the store if
78 if (xmlSecKeysMngrAdoptKeysStore(keyMngr
, keyStore
) < 0)
80 xmlSecError(XMLSEC_ERRORS_HERE
,
81 xmlSecErrorsSafeString(xmlSecKeyStoreGetName(keyStore
)),
82 "xmlSecKeysMngrAdoptKeyStore",
83 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
84 XMLSEC_ERRORS_NO_MESSAGE
) ;
86 xmlSecKeyStoreDestroy(keyStore
) ;
87 xmlSecKeysMngrDestroy(keyMngr
) ;
92 * Initialize crypto library specific data in keys manager
94 if (xmlSecMSCngKeysMngrInit(keyMngr
) < 0)
96 xmlSecError(XMLSEC_ERRORS_HERE
,
98 "xmlSecMSCngKeysMngrInit",
99 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
100 XMLSEC_ERRORS_NO_MESSAGE
);
102 xmlSecKeysMngrDestroy(keyMngr
);
107 * Set certificate database to X509 key data store
110 * At present, MS Crypto engine do not provide a way to setup a cert store.
114 * Set the getKey callback
116 keyMngr
->getKey
= xmlSecKeysMngrGetKey
;
122 MSCryptoAppliedKeysMngrAdoptKeyStore(
123 xmlSecKeysMngrPtr mngr
,
127 xmlSecKeyDataStorePtr x509Store
;
129 xmlSecAssert2(mngr
!= nullptr, -1) ;
130 xmlSecAssert2(keyStore
!= nullptr, -1) ;
132 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCngX509StoreId
);
133 if (x509Store
== nullptr)
135 xmlSecError(XMLSEC_ERRORS_HERE
,
137 "xmlSecKeysMngrGetDataStore",
138 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
139 XMLSEC_ERRORS_NO_MESSAGE
) ;
143 if (xmlSecMSCngX509StoreAdoptKeyStore(x509Store
, keyStore
) < 0)
145 xmlSecError(XMLSEC_ERRORS_HERE
,
146 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
147 "xmlSecMSCngX509StoreAdoptKeyStore",
148 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
149 XMLSEC_ERRORS_NO_MESSAGE
);
157 MSCryptoAppliedKeysMngrAdoptTrustedStore(
158 xmlSecKeysMngrPtr mngr
,
159 HCERTSTORE trustedStore
162 xmlSecKeyDataStorePtr x509Store
;
164 xmlSecAssert2(mngr
!= nullptr, -1) ;
165 xmlSecAssert2(trustedStore
!= nullptr, -1) ;
167 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCngX509StoreId
);
168 if (x509Store
== nullptr)
170 xmlSecError(XMLSEC_ERRORS_HERE
,
172 "xmlSecKeysMngrGetDataStore",
173 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
174 XMLSEC_ERRORS_NO_MESSAGE
) ;
178 if (xmlSecMSCngX509StoreAdoptTrustedStore(x509Store
, trustedStore
) < 0)
180 xmlSecError(XMLSEC_ERRORS_HERE
,
181 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
182 "xmlSecMSCngX509StoreAdoptKeyStore",
183 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
184 XMLSEC_ERRORS_NO_MESSAGE
);
192 MSCryptoAppliedKeysMngrAdoptUntrustedStore(
193 xmlSecKeysMngrPtr mngr
,
194 HCERTSTORE untrustedStore
197 xmlSecKeyDataStorePtr x509Store
;
199 xmlSecAssert2(mngr
!= nullptr, -1) ;
200 xmlSecAssert2(untrustedStore
!= nullptr, -1) ;
202 x509Store
= xmlSecKeysMngrGetDataStore(mngr
, xmlSecMSCngX509StoreId
);
203 if (x509Store
== nullptr)
205 xmlSecError(XMLSEC_ERRORS_HERE
,
207 "xmlSecKeysMngrGetDataStore",
208 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
209 XMLSEC_ERRORS_NO_MESSAGE
) ;
213 if (xmlSecMSCngX509StoreAdoptUntrustedStore(x509Store
, untrustedStore
) < 0)
215 xmlSecError(XMLSEC_ERRORS_HERE
,
216 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store
)),
217 "xmlSecMSCngX509StoreAdoptKeyStore",
218 XMLSEC_ERRORS_R_XMLSEC_FAILED
,
219 XMLSEC_ERRORS_NO_MESSAGE
);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */