LibreOffice Module xmlsecurity (master) 1
akmngr.cxx
Go to the documentation of this file.
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.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21#include <xmlsec-wrapper.h>
22
23#include "akmngr.hxx"
24
25#include <xmlsec/xmlsec.h>
26#include <xmlsec/keys.h>
27#include <xmlsec/keysmngr.h>
28#include <xmlsec/transforms.h>
29#include <xmlsec/errors.h>
30
31#include <xmlsec/mscng/crypto.h>
32#include <xmlsec/mscng/keysstore.h>
33#include <xmlsec/mscng/x509.h>
34#include <svl/cryptosign.hxx>
35
36namespace xmlsecurity
37{
38
47{
48 xmlSecKeysMngrPtr keyMngr = nullptr ;
49 xmlSecKeyStorePtr keyStore = nullptr ;
50
51 keyStore = xmlSecKeyStoreCreate(xmlSecMSCngKeysStoreId);
52 if (keyStore == nullptr)
53 {
54 xmlSecError(XMLSEC_ERRORS_HERE,
55 nullptr,
56 "xmlSecKeyStoreCreate",
57 XMLSEC_ERRORS_R_XMLSEC_FAILED,
58 XMLSEC_ERRORS_NO_MESSAGE) ;
59 return nullptr ;
60 }
61
62 keyMngr = xmlSecKeysMngrCreate() ;
63 if (keyMngr == nullptr)
64 {
65 xmlSecError(XMLSEC_ERRORS_HERE,
66 nullptr,
67 "xmlSecKeysMngrCreate",
68 XMLSEC_ERRORS_R_XMLSEC_FAILED,
69 XMLSEC_ERRORS_NO_MESSAGE) ;
70
71 xmlSecKeyStoreDestroy(keyStore) ;
72 return nullptr ;
73 }
74
75 /*-
76 * Add key store to manager, from now on keys manager destroys the store if
77 * needed
78 */
79 if (xmlSecKeysMngrAdoptKeysStore(keyMngr, keyStore) < 0)
80 {
81 xmlSecError(XMLSEC_ERRORS_HERE,
82 xmlSecErrorsSafeString(xmlSecKeyStoreGetName(keyStore)),
83 "xmlSecKeysMngrAdoptKeyStore",
84 XMLSEC_ERRORS_R_XMLSEC_FAILED,
85 XMLSEC_ERRORS_NO_MESSAGE) ;
86
87 xmlSecKeyStoreDestroy(keyStore) ;
88 xmlSecKeysMngrDestroy(keyMngr) ;
89 return nullptr ;
90 }
91
92 /*-
93 * Initialize crypto library specific data in keys manager
94 */
95 if (xmlSecMSCngKeysMngrInit(keyMngr) < 0)
96 {
97 xmlSecError(XMLSEC_ERRORS_HERE,
98 nullptr,
99 "xmlSecMSCngKeysMngrInit",
100 XMLSEC_ERRORS_R_XMLSEC_FAILED,
101 XMLSEC_ERRORS_NO_MESSAGE);
102
103 xmlSecKeysMngrDestroy(keyMngr);
104 return nullptr;
105 }
106
107 /*-
108 * Set certificate database to X509 key data store
109 */
110 /*-
111 * At present, MS Crypto engine do not provide a way to setup a cert store.
112 */
113
114 /*-
115 * Set the getKey callback
116 */
117 keyMngr->getKey = xmlSecKeysMngrGetKey ;
118
119 return keyMngr ;
120}
121
122int
124 xmlSecKeysMngrPtr mngr,
125 HCERTSTORE keyStore
126)
127{
128 xmlSecKeyDataStorePtr x509Store ;
129
130 xmlSecAssert2(mngr != nullptr, -1) ;
131 xmlSecAssert2(keyStore != nullptr, -1) ;
132
133 x509Store = xmlSecKeysMngrGetDataStore(mngr, xmlSecMSCngX509StoreId);
134 if (x509Store == nullptr)
135 {
136 xmlSecError(XMLSEC_ERRORS_HERE,
137 nullptr,
138 "xmlSecKeysMngrGetDataStore",
139 XMLSEC_ERRORS_R_XMLSEC_FAILED,
140 XMLSEC_ERRORS_NO_MESSAGE) ;
141 return -1 ;
142 }
143
144 if (xmlSecMSCngX509StoreAdoptKeyStore(x509Store, keyStore) < 0)
145 {
146 xmlSecError(XMLSEC_ERRORS_HERE,
147 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store)),
148 "xmlSecMSCngX509StoreAdoptKeyStore",
149 XMLSEC_ERRORS_R_XMLSEC_FAILED,
150 XMLSEC_ERRORS_NO_MESSAGE);
151 return -1;
152 }
153
154 return 0 ;
155}
156
157int
159 xmlSecKeysMngrPtr mngr,
160 HCERTSTORE trustedStore
161)
162{
163 xmlSecKeyDataStorePtr x509Store ;
164
165 xmlSecAssert2(mngr != nullptr, -1) ;
166 xmlSecAssert2(trustedStore != nullptr, -1) ;
167
168 x509Store = xmlSecKeysMngrGetDataStore(mngr, xmlSecMSCngX509StoreId);
169 if (x509Store == nullptr)
170 {
171 xmlSecError(XMLSEC_ERRORS_HERE,
172 nullptr,
173 "xmlSecKeysMngrGetDataStore",
174 XMLSEC_ERRORS_R_XMLSEC_FAILED,
175 XMLSEC_ERRORS_NO_MESSAGE) ;
176 return -1 ;
177 }
178
179 if (xmlSecMSCngX509StoreAdoptTrustedStore(x509Store, trustedStore) < 0)
180 {
181 xmlSecError(XMLSEC_ERRORS_HERE,
182 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store)),
183 "xmlSecMSCngX509StoreAdoptKeyStore",
184 XMLSEC_ERRORS_R_XMLSEC_FAILED,
185 XMLSEC_ERRORS_NO_MESSAGE);
186 return -1;
187 }
188
189 return 0 ;
190}
191
192int
194 xmlSecKeysMngrPtr mngr,
195 HCERTSTORE untrustedStore
196)
197{
198 xmlSecKeyDataStorePtr x509Store ;
199
200 xmlSecAssert2(mngr != nullptr, -1) ;
201 xmlSecAssert2(untrustedStore != nullptr, -1) ;
202
203 x509Store = xmlSecKeysMngrGetDataStore(mngr, xmlSecMSCngX509StoreId);
204 if (x509Store == nullptr)
205 {
206 xmlSecError(XMLSEC_ERRORS_HERE,
207 nullptr,
208 "xmlSecKeysMngrGetDataStore",
209 XMLSEC_ERRORS_R_XMLSEC_FAILED,
210 XMLSEC_ERRORS_NO_MESSAGE) ;
211 return -1 ;
212 }
213
214 if (xmlSecMSCngX509StoreAdoptUntrustedStore(x509Store, untrustedStore) < 0)
215 {
216 xmlSecError(XMLSEC_ERRORS_HERE,
217 xmlSecErrorsSafeString(xmlSecKeyDataStoreGetName(x509Store)),
218 "xmlSecMSCngX509StoreAdoptKeyStore",
219 XMLSEC_ERRORS_R_XMLSEC_FAILED,
220 XMLSEC_ERRORS_NO_MESSAGE);
221 return -1;
222 }
223
224 return 0 ;
225}
226
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
xmlSecKeysMngrPtr MSCryptoAppliedKeysMngrCreate()
MSCryptoAppliedKeysMngrCreate:
Definition: akmngr.cxx:46
int MSCryptoAppliedKeysMngrAdoptKeyStore(xmlSecKeysMngrPtr mngr, HCERTSTORE keyStore)
Definition: akmngr.cxx:123
int MSCryptoAppliedKeysMngrAdoptUntrustedStore(xmlSecKeysMngrPtr mngr, HCERTSTORE untrustedStore)
Definition: akmngr.cxx:193
int MSCryptoAppliedKeysMngrAdoptTrustedStore(xmlSecKeysMngrPtr mngr, HCERTSTORE trustedStore)
Definition: akmngr.cxx:158