Newer
Older
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Change this variable if another host is used for CAPIF
const CAPIF_HOSTNAME = 'capifcore';
const express = require('express'),
app = express(),
fs = require('fs'),
shell = require('shelljs'),
folderPath = './Responses/',
bodyParser = require('body-parser'),
path = require('path');
const { exec } = require('child_process');
// Create the folder path in case it doesn't exist
shell.mkdir('-p', folderPath);
// Change the limits according to your response size
app.use(bodyParser.json({limit: '50mb', extended: true}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
var opensslCommand = ''
if (CAPIF_HOSTNAME.includes(':')){
opensslCommand = `openssl s_client -connect ${CAPIF_HOSTNAME} | openssl x509 -text > ./Responses/cert_server.pem`;
}
else{
opensslCommand = `openssl s_client -connect ${CAPIF_HOSTNAME}:443 | openssl x509 -text > ./Responses/cert_server.pem`;
}
exec(opensslCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error generating CSR: ${stderr}`);
}
});
fs.writeFileSync('./Responses/client_cert.crt', '');
fs.writeFileSync('./Responses/client_key.key', '');
app.get('/', (req, res) => res.send('Hello, I write data to file. Send them requests!'));
app.post('/generate_csr', (req, res) => {
console.log(req.body);
const csrFilePath = 'Responses/'+req.body.apiProvFuncRole+'_csr.pem';
const privateKeyFilePath = 'Responses/'+req.body.apiProvFuncRole+'_key.key';
const subjectInfo = {
country: 'ES',
state: 'Madrid',
locality: 'Madrid',
organization: 'Telefonica I+D',
organizationalUnit: 'IT Department',
emailAddress: 'admin@example.com',
};
const opensslCommand = `openssl req -newkey rsa:2048 -nodes -keyout ${privateKeyFilePath} -out ${csrFilePath} -subj "/C=${subjectInfo.country}/ST=${subjectInfo.state}/L=${subjectInfo.locality}/O=${subjectInfo.organization}/OU=${subjectInfo.organizationalUnit}/emailAddress=${subjectInfo.emailAddress}"`;
exec(opensslCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error generating CSR: ${stderr}`);
} else {
console.log('CSR generated successfully:');
fs.readFile(csrFilePath, 'utf8', (readError, csrContent) => {
if (readError) {
console.error(`Error reading CSR: ${readError}`);
res.status(500).send('Error reading CSR');
} else {
console.log('CSR read successfully:');
// Send the CSR content in the response
fs.readFile(privateKeyFilePath, 'utf8', (readError, keyContent) => {
if (readError) {
console.error(`Error reading KEY: ${readError}`);
res.status(500).send('Error reading KEY');
} else {
console.log('KEY read successfully:');
// Send the CSR content in the response
fs.unlink(csrFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${err.message}`);
}
});
fs.unlink(privateKeyFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${err.message}`);
}
});
res.send({csr: csrContent, key: keyContent});
}
});
}
});
}
});
});
app.post('/generate_csr_invoker', (req, res) => {
console.log(req.body);
const csrFilePath = 'Responses/invoker_csr.pem';
const privateKeyFilePath = 'Responses/invoker_key.key';
const subjectInfo = {
country: 'ES',
state: 'Madrid',
locality: 'Madrid',
organization: 'Telefonica I+D',
organizationalUnit: 'IT Department',
emailAddress: 'admin@example.com',
};
const opensslCommand = `openssl req -newkey rsa:2048 -nodes -keyout ${privateKeyFilePath} -out ${csrFilePath} -subj "/C=${subjectInfo.country}/ST=${subjectInfo.state}/L=${subjectInfo.locality}/O=${subjectInfo.organization}/OU=${subjectInfo.organizationalUnit}/emailAddress=${subjectInfo.emailAddress}"`;
exec(opensslCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error generating CSR: ${stderr}`);
} else {
console.log('CSR generated successfully:');
fs.readFile(csrFilePath, 'utf8', (readError, csrContent) => {
if (readError) {
console.error(`Error reading CSR: ${readError}`);
res.status(500).send('Error reading CSR');
} else {
console.log('CSR read successfuly:');
// Send the CSR content in the response
fs.readFile(privateKeyFilePath, 'utf8', (readError, keyContent) => {
if (readError) {
console.error(`Error reading KEY: ${readError}`);
res.status(500).send('Error reading KEY');
} else {
console.log('KEY read successfully:');
// Send the CSR content in the response
fs.unlink(csrFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${err.message}`);
}
});
fs.unlink(privateKeyFilePath, (err) => {
if (err) {
console.error(`Error deleting file: ${err.message}`);
}
});
res.send({csr: csrContent, key: keyContent});
}
});
}
});
}
});
});
app.post('/write_cert', (req, res) => {
let extension = 'crt',
fsMode = 'writeFile',
filename = "client_cert",
filePath = `${path.join(folderPath, filename)}.${extension}`,
options = {encoding: 'binary'};
fs[fsMode](filePath, req.body.cert, options, (err) => {
if (err) {
console.log(err);
res.send('Error');
}
});
extension = 'key';
filename = "client_key";
filePath = `${path.join(folderPath, filename)}.${extension}`;
fs[fsMode](filePath, req.body.key, options, (err) => {
if (err) {
console.log(err);
res.send('Error');
}
else {
res.send('Success');
}
});
});
app.post('/write_ca', (req, res) => {
let extension = 'pem',
fsMode = 'writeFile',
filename = "ca_cert",
filePath = `${path.join(folderPath, filename)}.${extension}`,
options = {encoding: 'binary'};
fs[fsMode](filePath, req.body.ca_root, options, (err) => {
if (err) {
console.log(err);
res.send('Error');
}
else {
res.send('Success');
}
});
});
app.listen(3010, () => {
console.log('Listener API running.');
console.log(`Data is being stored at location: ${path.join(process.cwd(), folderPath)}`);
});