summaryrefslogtreecommitdiffstats
path: root/base/tps/src/selftests/TPSSystemCertsVerification.cpp
blob: a89d18d04e7102806a4bbe1ddd1fbce8fc69adcd (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA 
// 
// Copyright (C) 2010 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---


#ifdef XP_WIN32
#define TPS_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define TPS_PUBLIC
#endif /* !XP_WIN32 */

#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "prmem.h"
#include "prsystem.h"
#include "plstr.h"
#include "prio.h"

#include "cert.h"
#include "certt.h"

#ifdef __cplusplus
}   
#endif

#include "engine/RA.h"
#include "main/ConfigStore.h"
#include "selftests/TPSSystemCertsVerification.h"


int  TPSSystemCertsVerification::initialized = 0;
bool TPSSystemCertsVerification::startupEnabled = false;
bool TPSSystemCertsVerification::onDemandEnabled = false;
bool TPSSystemCertsVerification::startupCritical = false;
bool TPSSystemCertsVerification::onDemandCritical = false;
const char *TPSSystemCertsVerification::CRITICAL_TEST_NAME = "TPSSystemCertsVerification:critical";
const char *TPSSystemCertsVerification::TEST_NAME = "TPSSystemCertsVerification";
// for testing if system is initialized
const char *TPSSystemCertsVerification::UNINITIALIZED_NICKNAME = "[HSM_LABEL][NICKNAME]";
const char *TPSSystemCertsVerification::SUBSYSTEM_NICKNAME= "tps.cert.subsystem.nickname";


//default constructor
TPSSystemCertsVerification::TPSSystemCertsVerification()
{
}

TPSSystemCertsVerification::~TPSSystemCertsVerification()
{
}

void TPSSystemCertsVerification::Initialize (ConfigStore *cfg)
{
    if (TPSSystemCertsVerification::initialized == 0) {
        TPSSystemCertsVerification::initialized = 1;
        const char* s = cfg->GetConfigAsString(CFG_SELFTEST_STARTUP);
        if (s != NULL) {
            if (PL_strstr (s, TPSSystemCertsVerification::CRITICAL_TEST_NAME) != NULL) {
                startupCritical = true;
                startupEnabled = true;
            } else if (PL_strstr (s, TPSSystemCertsVerification::TEST_NAME) != NULL) {
                startupEnabled = true;
            }
        }
        const char* d = cfg->GetConfigAsString(CFG_SELFTEST_ONDEMAND);
        if (d != NULL) {
            if (PL_strstr (d, TPSSystemCertsVerification::CRITICAL_TEST_NAME) != NULL) {
                onDemandCritical = true;
                onDemandEnabled = true;
            } else if (PL_strstr (d, TPSSystemCertsVerification::TEST_NAME) != NULL) {
                onDemandEnabled = true;
            }
        }
        char* n = (char*)(cfg->GetConfigAsString(TPSSystemCertsVerification::SUBSYSTEM_NICKNAME));
        if (n != NULL && PL_strlen(n) > 0) {
            if (PL_strstr (n, TPSSystemCertsVerification::UNINITIALIZED_NICKNAME) != NULL) {
                TPSSystemCertsVerification::initialized = 0;
            }
        }
        if (TPSSystemCertsVerification::initialized == 1) {
            TPSSystemCertsVerification::initialized = 2;
        }
    }
    RA::SelfTestLog("TPSSystemCertsVerification::Initialize", "%s", ((initialized==2)?"successfully completed":"failed"));
}

// Error codes:
//   -1 -  failed system certs verification
// critical errors are negative

int TPSSystemCertsVerification::runSelfTest ()
{
    int rc = 0;

    if (TPSSystemCertsVerification::initialized == 2) {
        rc = RA::verifySystemCerts();
        if (rc == true) {
            return 0;
        } else {
            rc = -1;
        }
    }

    return rc;
}

bool TPSSystemCertsVerification::isStartupEnabled ()
{
    return startupEnabled;
}

bool TPSSystemCertsVerification::isOnDemandEnabled ()
{
    return onDemandEnabled;
}

bool TPSSystemCertsVerification::isStartupCritical ()
{
    return startupCritical;
}

bool TPSSystemCertsVerification::isOnDemandCritical ()
{
    return onDemandCritical;
}