VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Token.cpp
blob: 17fce78b5222c0ad686626865212184c6b4adb31 (plain)
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
#include "Token.h"
#include "Platform/Finally.h"
#include "Platform/ForEach.h"

#if !defined(TC_WINDOWS) || defined(TC_PROTOTYPE)
#include "Platform/SerializerFactory.h"
#include "Platform/StringConverter.h"
#include "Platform/SystemException.h"
#else
#include "Dictionary.h"
#include "Language.h"
#endif

#include <vector>
#include <algorithm>
#include <memory>

#include "SecurityToken.h"
#include "EMVToken.h"
#include "PCSCException.h"
#include "iostream"

using namespace std;

namespace VeraCrypt
{
	vector<shared_ptr<TokenKeyfile>> Token::GetAvailableKeyfiles(bool isEMVSupportEnabled)
	{
		vector<shared_ptr<TokenKeyfile>> availableKeyfiles;
		bool securityTokenLibraryInitialized = true;
		bool scardLibraryInitialized = true;

		try
		{
			foreach (SecurityTokenKeyfile k, SecurityToken::GetAvailableKeyfiles())
			{
				availableKeyfiles.push_back(shared_ptr<TokenKeyfile>(new SecurityTokenKeyfile(k)));
			}
		}
		catch (SecurityTokenLibraryNotInitialized&)
		{
			securityTokenLibraryInitialized = false;
		}
		
		if (isEMVSupportEnabled)
		{
			try
			{
				foreach (EMVTokenKeyfile k, EMVToken::GetAvailableKeyfiles())
				{
					availableKeyfiles.push_back(shared_ptr<TokenKeyfile>(new EMVTokenKeyfile(k)));
				}
			}
			catch (ScardLibraryInitializationFailed&)
			{
				scardLibraryInitialized = false;
			}
		}

		if (availableKeyfiles.size() == 0)
		{
			if (!securityTokenLibraryInitialized)
			{
				throw SecurityTokenLibraryNotInitialized();
			}
			else if (!scardLibraryInitialized)
			{
				throw ScardLibraryInitializationFailed();
			}
		}

		return availableKeyfiles;
	}

	bool Token::IsKeyfilePathValid(const wstring& tokenKeyfilePath, bool isEMVSupportEnabled)
	{
		if (isEMVSupportEnabled)
		{
			return SecurityToken::IsKeyfilePathValid(tokenKeyfilePath) || EMVToken::IsKeyfilePathValid(tokenKeyfilePath);
		}
		return SecurityToken::IsKeyfilePathValid(tokenKeyfilePath);
	}

	list <shared_ptr<TokenInfo>> Token::GetAvailableTokens()
	{
		list <shared_ptr<TokenInfo>> availableTokens;

		foreach(SecurityTokenInfo securityToken, SecurityToken::GetAvailableTokens())
		{
			availableTokens.push_back(shared_ptr<TokenInfo>(new SecurityTokenInfo(std::move(securityToken))));
		}

		return availableTokens ;
	}

	shared_ptr<TokenKeyfile> Token::getTokenKeyfile(const TokenKeyfilePath& path)
	{
		shared_ptr<TokenKeyfile> tokenKeyfile;

		if (SecurityToken::IsKeyfilePathValid(path))
		{
			tokenKeyfile = shared_ptr<TokenKeyfile>(new SecurityTokenKeyfile(path));
		}
		else 
		{
			if (EMVToken::IsKeyfilePathValid(path))
			{
				tokenKeyfile = shared_ptr<TokenKeyfile>(new EMVTokenKeyfile(path));
			}		
		}

		return tokenKeyfile;
	}
}