|
16 | 16 | # You should have received a copy of the GNU General Public License |
17 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | | -import os |
| 19 | +import os, platform |
20 | 20 | import datetime |
21 | 21 | from pathlib import Path |
22 | 22 |
|
|
33 | 33 | ) |
34 | 34 | from cryptography.x509.oid import ExtendedKeyUsageOID |
35 | 35 |
|
| 36 | +from .log import logger |
| 37 | +logger = logger.getChild("certmanager") |
36 | 38 | from . import setting |
37 | 39 | from .setting import basepath |
38 | 40 |
|
39 | | -if setting.config["importca"]: |
40 | | - certpath = os.path.join(basepath, "CERT") |
41 | | -else: |
42 | | - certpath = "CERT" |
43 | | -if not os.path.exists(certpath): |
| 41 | + |
| 42 | +def decide_state_path_legacy(): |
| 43 | + if setting.config["importca"]: |
| 44 | + return Path(basepath) |
| 45 | + else: |
| 46 | + return Path() |
| 47 | + |
| 48 | + |
| 49 | +def decide_state_path_unix_like(): |
| 50 | + if os.geteuid() == 0: |
| 51 | + logger.warn("Running Accesser as the root user carries certain risks; see pull #245") |
| 52 | + return Path("/var/lib") / "accesser" |
| 53 | + |
| 54 | + state_path = os.getenv("XDG_STATE_HOME", None) |
| 55 | + if state_path is not None: |
| 56 | + state_path = Path(state_path) / "accesser" |
| 57 | + else: |
| 58 | + state_path = Path.home() / ".local/state" / "accesser" |
| 59 | + return state_path |
| 60 | + |
| 61 | + |
| 62 | +def decide_certpath(): |
| 63 | + certpath = None |
| 64 | + # 人为指定最优先 |
| 65 | + #if setting.config["state_dir"]: |
| 66 | + #return Path(setting.config["state_dir"]) / "cert" |
| 67 | + match platform.system(): |
| 68 | + case 'Linux' | 'FreeBSD': |
| 69 | + deprecated_path = decide_state_path_legacy() / "CERT" |
| 70 | + # 暂仅在 *nix 上视为已废弃 |
| 71 | + if deprecated_path.exists(): |
| 72 | + logger.warn("deprecated path, see pull #245") |
| 73 | + return deprecated_path |
| 74 | + certpath = decide_state_path_unix_like() / "cert" |
| 75 | + case _: |
| 76 | + # windows,mac,android ... |
| 77 | + certpath = decide_state_path_legacy() / "CERT" |
| 78 | + return certpath |
| 79 | + |
| 80 | + |
| 81 | +certpath = decide_certpath() |
| 82 | +if not certpath.exists(): |
44 | 83 | os.makedirs(certpath, exist_ok=True) |
45 | 84 |
|
46 | 85 |
|
@@ -92,28 +131,28 @@ def create_root_ca(): |
92 | 131 | .sign(key, hashes.SHA256()) |
93 | 132 | ) |
94 | 133 |
|
95 | | - (Path(certpath) / "root.crt").write_bytes( |
| 134 | + (certpath / "root.crt").write_bytes( |
96 | 135 | cert.public_bytes(serialization.Encoding.PEM) |
97 | 136 | ) |
98 | 137 |
|
99 | | - (Path(certpath) / "root.key").write_bytes( |
| 138 | + (certpath / "root.key").write_bytes( |
100 | 139 | key.private_bytes( |
101 | 140 | encoding=serialization.Encoding.PEM, |
102 | 141 | format=serialization.PrivateFormat.PKCS8, |
103 | 142 | encryption_algorithm=serialization.NoEncryption(), |
104 | 143 | ) |
105 | 144 | ) |
106 | 145 |
|
107 | | - (Path(certpath) / "root.pfx").write_bytes( |
| 146 | + (certpath / "root.pfx").write_bytes( |
108 | 147 | serialization.pkcs12.serialize_key_and_certificates( |
109 | 148 | b"Accesser", key, cert, None, serialization.NoEncryption() |
110 | 149 | ) |
111 | 150 | ) |
112 | 151 |
|
113 | 152 |
|
114 | 153 | def create_certificate(server_name): |
115 | | - rootpem = (Path(certpath) / "root.crt").read_bytes() |
116 | | - rootkey = (Path(certpath) / "root.key").read_bytes() |
| 154 | + rootpem = (certpath / "root.crt").read_bytes() |
| 155 | + rootkey = (certpath / "root.key").read_bytes() |
117 | 156 | ca_cert = x509.load_pem_x509_certificate(rootpem) |
118 | 157 | pkey = serialization.load_pem_private_key(rootkey, password=None) |
119 | 158 |
|
@@ -180,7 +219,7 @@ def create_certificate(server_name): |
180 | 219 | .sign(pkey, hashes.SHA256()) |
181 | 220 | ) |
182 | 221 |
|
183 | | - (Path(certpath) / f"{server_name}.crt").write_bytes( |
| 222 | + (certpath / f"{server_name}.crt").write_bytes( |
184 | 223 | cert.public_bytes(serialization.Encoding.PEM) |
185 | 224 | + pkey.private_bytes( |
186 | 225 | encoding=serialization.Encoding.PEM, |
|
0 commit comments