File: DataSealerTest.h

package info (click to toggle)
xmltooling 3.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,016 kB
  • sloc: cpp: 23,286; sh: 4,446; xml: 1,493; makefile: 351; ansic: 27; python: 14
file content (130 lines) | stat: -rw-r--r-- 5,102 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */

#include "XMLObjectBaseTestCase.h"

#include <xmltooling/exceptions.h>
#include <xmltooling/security/DataSealer.h>

#include <fstream>
#include <xercesc/util/Base64.hpp>
#include <xsec/utils/XSECPlatformUtils.hpp>

class DataSealerTest : public CxxTest::TestSuite {
public:
    void testStaticDataSealer() {
#ifdef XSEC_OPENSSL_HAVE_GCM
        unsigned char keybuf[32];
        TS_ASSERT_EQUALS(sizeof(keybuf), XSECPlatformUtils::g_cryptoProvider->getRandom(keybuf, sizeof(keybuf)));

        XMLSize_t x;
        XMLByte* encoded = Base64::encode(keybuf, sizeof(keybuf), &x);
        TS_ASSERT_LESS_THAN(0, x);
        auto_ptr_XMLCh widekey((char*)encoded);
        XMLString::release((char**)&encoded);

        DOMDocument* doc = XMLToolingConfig::getConfig().getParser().newDocument();
        Janitor<DOMDocument> jdoc(doc);

        static const XMLCh _key[] = UNICODE_LITERAL_3(k,e,y);
        DOMElement* root = doc->createElementNS(nullptr, _key);
        root->setAttributeNS(nullptr, _key, widekey.get());
        doc->appendChild(root);

        auto_ptr<DataSealerKeyStrategy> keyStrategy(
            XMLToolingConfig::getConfig().DataSealerKeyStrategyManager.newPlugin(
                STATIC_DATA_SEALER_KEY_STRATEGY, doc->getDocumentElement(), false
                )
            );

		keyStrategy->lock();
        pair<string,const XSECCryptoSymmetricKey*> key = keyStrategy->getDefaultKey();
        TS_ASSERT_EQUALS("static", key.first);
        TSM_ASSERT_EQUALS("Wrong key type", key.second->getSymmetricKeyType(), XSECCryptoSymmetricKey::KEY_AES_256);
		keyStrategy->unlock();

        scoped_ptr<DataSealer> sealer(new DataSealer(keyStrategy.get()));
		keyStrategy.release();

        string data = "this is a test";

        string wrapped = sealer->wrap(data.c_str(), time(nullptr) + 500);
        string unwrapped = sealer->unwrap(wrapped.c_str());
            
        TSM_ASSERT_EQUALS("DataSealer output did not match.", data, unwrapped);

        wrapped = sealer->wrap(data.c_str(), time(nullptr) - 500);
        TSM_ASSERT_THROWS("DataSealer did not throw on expired data.", sealer->unwrap(wrapped.c_str()), IOException);

		wrapped = sealer->wrap(data.c_str(), time(nullptr) - 500);
		wrapped.insert(0, "invalid");
		TSM_ASSERT_THROWS("DataSealer did not throw on wrong key label.", sealer->unwrap(wrapped.c_str()), IOException);
#endif
    }

	void testVersionedDataSealer() {
#ifdef XSEC_OPENSSL_HAVE_GCM
		DOMDocument* doc = XMLToolingConfig::getConfig().getParser().newDocument();
		Janitor<DOMDocument> jdoc(doc);

		static const XMLCh _path[] = UNICODE_LITERAL_4(p, a, t, h);
		DOMElement* root = doc->createElementNS(nullptr, _path);
        string sealerpath = data_path + "sealer.keys";
		auto_ptr_XMLCh widepath(sealerpath.c_str());
		root->setAttributeNS(nullptr, _path, widepath.get());
		doc->appendChild(root);

		auto_ptr<DataSealerKeyStrategy> keyStrategy(
			XMLToolingConfig::getConfig().DataSealerKeyStrategyManager.newPlugin(
				VERSIONED_DATA_SEALER_KEY_STRATEGY, doc->getDocumentElement(), false
			)
		);

		keyStrategy->lock();
		
		pair<string, const XSECCryptoSymmetricKey*> key = keyStrategy->getDefaultKey();
		TS_ASSERT_EQUALS("4", key.first);
		TSM_ASSERT_EQUALS("Wrong key type", key.second->getSymmetricKeyType(), XSECCryptoSymmetricKey::KEY_AES_128);

		key.second = keyStrategy->getKey("1");
		TS_ASSERT(key.second != nullptr);
		TSM_ASSERT_EQUALS("Wrong key type", key.second->getSymmetricKeyType(), XSECCryptoSymmetricKey::KEY_AES_128);

		keyStrategy->unlock();

		scoped_ptr<DataSealer> sealer(new DataSealer(keyStrategy.get()));
		keyStrategy.release();

		string data = "this is a test";

		string wrapped = sealer->wrap(data.c_str(), time(nullptr) + 500);
		string unwrapped = sealer->unwrap(wrapped.c_str());

		TSM_ASSERT_EQUALS("DataSealer output did not match.", data, unwrapped);

		wrapped = sealer->wrap(data.c_str(), time(nullptr) - 500);
		TSM_ASSERT_THROWS("DataSealer did not throw on expired data.", sealer->unwrap(wrapped.c_str()), IOException);

		wrapped = sealer->wrap(data.c_str(), time(nullptr) - 500);
		wrapped.insert(0, "invalid");
		TSM_ASSERT_THROWS("DataSealer did not throw on wrong key label.", sealer->unwrap(wrapped.c_str()), IOException);
#endif
	}
};