Skip to content

Commit

Permalink
MSCNG: add keys store support (lsh123#173)
Browse files Browse the repository at this point in the history
* mscng: add keysstore support

This also requires changes in the x509 key data class. It's no longer safe to
call CertDeleteCertificateFromStore() for no longer needed certificates, as the
store may be a non-in-memory one, and the user expects that the system store is
only read by xmlsec. Non-deleting also means that we can't assert anymore that
the in-memory cert store is empty when we delete it.

We don't leak memory with this, as the refcount of the certificate contexts is
still properly decremented.

As a side effect 6 TODOs added earlier are now resolved.

* mscng: add initial documentation

Also fix the inconsistency that email address is sometimes stated, sometimes
not. Now it's omitted everywhere and the AUTHORS file has it at a single place.

* mscng: add keysstore test

A manual test is not ideal, but it's better than nothing.
  • Loading branch information
vmiklos authored and lsh123 committed May 14, 2018
1 parent 3489911 commit 1bcd4ed
Show file tree
Hide file tree
Showing 20 changed files with 696 additions and 50 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ Windows port: Igor Zlatkovic <[email protected]>
Debian port: John Belmonte <[email protected]>
xmlsec-nss: Tej Arora <[email protected]>, AOL Inc.
xmlsec-mscrypto: Wouter Ketting <[email protected]>, Cordys R&D BV
xmlsec-mscng: Miklos Vajna <[email protected]>
GOST support: Dmitry Belyavsky <[email protected]>, Cryptocom LTD (http://www.cryptocom.ru)

3 changes: 2 additions & 1 deletion include/xmlsec/mscng/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_APP_H__
#define __XMLSEC_MSCNG_APP_H__
Expand All @@ -25,6 +25,7 @@ extern "C" {
********************************************************************/
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngAppInit (const char* config);
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngAppShutdown (void);
XMLSEC_CRYPTO_EXPORT LPCTSTR xmlSecMSCngAppGetCertStoreName (void);

/********************************************************************
*
Expand Down
2 changes: 1 addition & 1 deletion include/xmlsec/mscng/certkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_CERTKEYS_H__
#define __XMLSEC_MSCNG_CERTKEYS_H__
Expand Down
2 changes: 1 addition & 1 deletion include/xmlsec/mscng/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_CRYPTO_H__
#define __XMLSEC_MSCNG_CRYPTO_H__
Expand Down
41 changes: 41 additions & 0 deletions include/xmlsec/mscng/keysstore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_KEYSSTORE_H__
#define __XMLSEC_MSCNG_KEYSSTORE_H__

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <xmlsec/xmlsec.h>

/**
* xmlSecMSCngKeysStoreId:
*
* A MSCng keys store klass id.
*/
#define xmlSecMSCngKeysStoreId xmlSecMSCngKeysStoreGetKlass()

XMLSEC_CRYPTO_EXPORT xmlSecKeyStoreId xmlSecMSCngKeysStoreGetKlass(void);
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngKeysStoreAdoptKey(xmlSecKeyStorePtr store,
xmlSecKeyPtr key);
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngKeysStoreLoad (xmlSecKeyStorePtr store,
const char *uri,
xmlSecKeysMngrPtr keysMngr);
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngKeysStoreSave (xmlSecKeyStorePtr store,
const char *filename,
xmlSecKeyDataType type);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __XMLSEC_MSCNG_PCCERT_CONTEXT_H__ */


2 changes: 1 addition & 1 deletion include/xmlsec/mscng/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_SYMBOLS_H__
#define __XMLSEC_MSCNG_SYMBOLS_H__
Expand Down
4 changes: 3 additions & 1 deletion include/xmlsec/mscng/x509.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_MSCNG_X509_H__
#define __XMLSEC_MSCNG_X509_H__
Expand Down Expand Up @@ -55,6 +55,8 @@ XMLSEC_CRYPTO_EXPORT int xmlSecMSCngKeyDataX509AdoptCert (xm
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngX509StoreAdoptCert (xmlSecKeyDataStorePtr store,
PCCERT_CONTEXT cert,
xmlSecKeyDataType type);
XMLSEC_CRYPTO_EXPORT int xmlSecMSCngX509StoreAdoptKeyStore (xmlSecKeyDataStorePtr store,
HCERTSTORE keyStore);
XMLSEC_CRYPTO_EXPORT PCCERT_CONTEXT xmlSecMSCngX509StoreVerify (xmlSecKeyDataStorePtr store,
HCERTSTORE certs,
xmlSecKeyInfoCtx* keyInfoCtx);
Expand Down
16 changes: 16 additions & 0 deletions src/mscng/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
What version of Windows?
------------------------------------------------------------------------

The Microsoft CNG API is a set of BCrypt* and NCrypt* functions. Taking
BCryptOpenAlgorithmProvider() as a representative example, the minimum
supported client is Windows Vista and the minimum supported server is Windows
Server 2008.

Keys manager with MS Certificate store support.
------------------------------------------------------------------------

Similarly to the nss and mscrypto backends, the xmlsec-mscng keys manager is
based on the simple keys store from xmlsec core. If keys are not found in the
simple keys store, then the MS Certificate store (the "MY" store by default,
visible as Personal -> Certificates in certmgr.msc) is used to look up keys.
The certificate store from the OS is a read-only store.
86 changes: 53 additions & 33 deletions src/mscng/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#include "globals.h"

Expand All @@ -23,6 +23,10 @@
#include <xmlsec/mscng/symbols.h>
#include <xmlsec/mscng/x509.h>
#include <xmlsec/mscng/certkeys.h>
#include <xmlsec/mscng/keysstore.h>

/* config info for the mscng keysstore */
static LPTSTR gXmlSecMSCngAppCertStoreName = NULL;

/**
* xmlSecMSCngAppInit:
Expand All @@ -36,8 +40,26 @@
*/
int
xmlSecMSCngAppInit(const char* config) {
UNREFERENCED_PARAMETER(config);
/* TODO: initialize MSCng crypto engine */
/* initialize MSCng crypto engine */

/* config parameter is an ms cert store name */
if(config != NULL && strlen(config) > 0) {
if(gXmlSecMSCngAppCertStoreName != NULL) {
/* deny double initialization */
xmlSecOtherError2(XMLSEC_ERRORS_R_INVALID_CONFIG, NULL,
"config=%s, config already set",
xmlSecErrorsSafeString(config));
return(-1);
}

gXmlSecMSCngAppCertStoreName = xmlSecMSCngConvertUtf8ToUnicode((const xmlChar*)config);
if(gXmlSecMSCngAppCertStoreName == NULL) {
xmlSecInternalError2("xmlSecMSCngConvertUtf8ToUnicode", NULL,
"config=%s", xmlSecErrorsSafeString(config));
return(-1);
}
}

return(0);
}

Expand All @@ -52,11 +74,26 @@ xmlSecMSCngAppInit(const char* config) {
*/
int
xmlSecMSCngAppShutdown(void) {
/* TODO: shutdown MSCng crypto engine */

/* shutdown MSCng crypto engine */
if(gXmlSecMSCngAppCertStoreName != NULL) {
xmlFree(gXmlSecMSCngAppCertStoreName);
gXmlSecMSCngAppCertStoreName = NULL;
}
return(0);
}

/**
* xmlSecMSCngAppGetCertStoreName:
*
* Gets the MS Cng certs store name set by @xmlSecMSCngAppInit function.
*
* Returns: the MS Cng certs name used by xmlsec-mscng.
*/
LPCTSTR
xmlSecMSCngAppGetCertStoreName(void) {
return(gXmlSecMSCngAppCertStoreName);
}

/**
* xmlSecMSCngAppKeyLoad:
* @filename: the key filename.
Expand Down Expand Up @@ -524,17 +561,13 @@ xmlSecMSCngAppDefaultKeysMngrInit(xmlSecKeysMngrPtr mngr) {

xmlSecAssert2(mngr != NULL, -1);

/* TODO: if MSCng crypto engine has another default
* keys storage then use it!
*/

/* create simple keys store if needed */
/* create MSCng keys store if needed */
if(xmlSecKeysMngrGetKeysStore(mngr) == NULL) {
xmlSecKeyStorePtr keysStore;

keysStore = xmlSecKeyStoreCreate(xmlSecSimpleKeysStoreId);
keysStore = xmlSecKeyStoreCreate(xmlSecMSCngKeysStoreId);
if(keysStore == NULL) {
xmlSecInternalError("xmlSecKeyStoreCreate(xmlSecSimpleKeysStoreId)", NULL);
xmlSecInternalError("xmlSecKeyStoreCreate(xmlSecMSCngKeysStoreId)", NULL);
return(-1);
}

Expand Down Expand Up @@ -574,19 +607,15 @@ xmlSecMSCngAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr mngr, xmlSecKeyPtr key)
xmlSecAssert2(mngr != NULL, -1);
xmlSecAssert2(key != NULL, -1);

/* TODO: if MSCng crypto engine has another default
* keys storage then use it!
*/

store = xmlSecKeysMngrGetKeysStore(mngr);
if(store == NULL) {
xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
return(-1);
}

ret = xmlSecSimpleKeysStoreAdoptKey(store, key);
ret = xmlSecMSCngKeysStoreAdoptKey(store, key);
if(ret < 0) {
xmlSecInternalError("xmlSecSimpleKeysStoreAdoptKey", NULL);
xmlSecInternalError("xmlSecMSCngKeysStoreAdoptKey", NULL);
return(-1);
}

Expand All @@ -611,20 +640,16 @@ xmlSecMSCngAppDefaultKeysMngrLoad(xmlSecKeysMngrPtr mngr, const char* uri) {
xmlSecAssert2(mngr != NULL, -1);
xmlSecAssert2(uri != NULL, -1);

/* TODO: if MSCng crypto engine has another default
* keys storage then use it!
*/

store = xmlSecKeysMngrGetKeysStore(mngr);
if(store == NULL) {
xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
return(-1);
}

ret = xmlSecSimpleKeysStoreLoad(store, uri, mngr);
ret = xmlSecMSCngKeysStoreLoad(store, uri, mngr);
if(ret < 0) {
xmlSecInternalError2("xmlSecSimpleKeysStoreLoad", NULL,
"uri=%s", xmlSecErrorsSafeString(uri));
xmlSecInternalError2("xmlSecMSCngKeysStoreLoad", NULL, "uri=%s",
xmlSecErrorsSafeString(uri));
return(-1);
}

Expand All @@ -649,21 +674,16 @@ xmlSecMSCngAppDefaultKeysMngrSave(xmlSecKeysMngrPtr mngr, const char* filename,
xmlSecAssert2(mngr != NULL, -1);
xmlSecAssert2(filename != NULL, -1);

/* TODO: if MSCng crypto engine has another default
* keys storage then use it!
*/

store = xmlSecKeysMngrGetKeysStore(mngr);
if(store == NULL) {
xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
return(-1);
}

ret = xmlSecSimpleKeysStoreSave(store, filename, type);
ret = xmlSecMSCngKeysStoreSave(store, filename, type);
if(ret < 0) {
xmlSecInternalError2("xmlSecSimpleKeysStoreSave", NULL,
"filename=%s",
xmlSecErrorsSafeString(filename));
xmlSecInternalError2("xmlSecMSCngKeysStoreSave", NULL, "filename%s",
xmlSecErrorsSafeString(filename));
return(-1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mscng/certkeys.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#include "globals.h"

Expand Down
2 changes: 1 addition & 1 deletion src/mscng/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#include "globals.h"

Expand Down
2 changes: 1 addition & 1 deletion src/mscng/digests.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#include "globals.h"

Expand Down
2 changes: 1 addition & 1 deletion src/mscng/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2018 Miklos Vajna <[email protected]>. All Rights Reserved.
* Copyright (C) 2018 Miklos Vajna. All Rights Reserved.
*/
#ifndef __XMLSEC_GLOBALS_H__
#define __XMLSEC_GLOBALS_H__
Expand Down
Loading

0 comments on commit 1bcd4ed

Please sign in to comment.