Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use both hostname and ip address for debug service certificate #1667

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ Thanks so much to everyone [who has contributed](https://github.com/codefori/vsc
* [@richardm90](https://github.com/richardm90)
* [@ThePrez](https://github.com/ThePrez)
* [@BoykaZhu](https://github.com/BoykaZhu)
* [@krka01](https://github.com/krka01)
* [@krka01](https://github.com/krka01)
* [@william-xiang](https://github.com/william-xiang)
51 changes: 50 additions & 1 deletion src/api/debug/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from "path";
import {promises as fs} from "fs";
import * as os from "os";
import IBMi from "../IBMi";
import * as dns from 'dns';
import {window} from "vscode";

const serverCertName = `debug_service.pfx`;
const clientCertName = `debug_service.crt`;
Expand All @@ -10,6 +12,51 @@ function getRemoteCertDirectory(connection: IBMi) {
return connection.config?.debugCertDirectory!;
}

function resolveHostnameToIP(hostName: string): Promise<string | undefined> {
return new Promise<string | undefined>((resolve) => {
dns.lookup(hostName, (err, res) => {
if (err) {
resolve(undefined);
} else {
resolve(res);
}
});
});
}

async function getExtFileConent(host: string, connection: IBMi) {
const ipRegexExp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi;
let hostname = undefined;
let ipAddr = undefined;

if (ipRegexExp.test(host)) {
ipAddr = host;
const hostnameResult = await connection.sendCommand({
command: `hostname`
});

if (hostnameResult.stdout) {
hostname = hostnameResult.stdout;
} else {
window.showWarningMessage(`Hostname cannot be retrieved from IBM i, certificate will be created only using the IP address!`);
}
} else {
hostname = host;
ipAddr = await resolveHostnameToIP(host);
}

let extFileContent;
if (hostname && ipAddr) {
extFileContent = `subjectAltName=DNS:${hostname},IP:${ipAddr}`;
} else if (hostname) {
extFileContent = `subjectAltName=DNS:${hostname}`;
} else {
extFileContent = `subjectAltName=IP:${ipAddr}`;
}

return extFileContent;
}

export function getRemoteServerCertPath(connection: IBMi) {
return path.posix.join(getRemoteCertDirectory(connection), serverCertName);
}
Expand Down Expand Up @@ -47,12 +94,14 @@ export async function remoteClientCertExists(connection: IBMi) {
*/
export async function setup(connection: IBMi) {
const host = connection.currentHost;
const extFileContent = await getExtFileConent(host, connection);

const commands = [
`openssl genrsa -out debug_service_ca.key 2048`,
`openssl req -x509 -new -nodes -key debug_service_ca.key -sha256 -days 1825 -out debug_service_ca.pem -subj '/CN=${host}'`,
`openssl genrsa -out debug_service.key 2048`,
`openssl req -new -key debug_service.key -out debug_service.csr -subj '/CN=${host}'`,
`openssl x509 -req -in debug_service.csr -CA debug_service_ca.pem -CAkey debug_service_ca.key -CAcreateserial -out debug_service.crt -days 1095 -sha256`,
`openssl x509 -req -in debug_service.csr -CA debug_service_ca.pem -CAkey debug_service_ca.key -CAcreateserial -out debug_service.crt -days 1095 -sha256 -sha256 -req -extfile <(printf "${extFileContent}")`,
`openssl pkcs12 -export -out debug_service.pfx -inkey debug_service.key -in debug_service.crt -password pass:${host}`
];

Expand Down