summaryrefslogtreecommitdiffstats
path: root/storage/devicelibs/crypto.py
blob: 0872ed052c73d45bb849a7360b3301f850c1118a (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
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
#
# crypto.py
#
# Copyright (C) 2009  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Dave Lehman <dlehman@redhat.com>
#

import os

import iutil
from ..errors import *

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

def is_luks(device):
    rc = iutil.execWithRedirect("cryptsetup",
                                ["isLuks", device],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)
    if rc:
        return False
    else:
        return True

def luks_uuid(device):
    uuid = iutil.execWithCapture("cryptsetup",
                                 ["luksUUID", device],
                                 stderr="/dev/null")
    return uuid.strip()

def luks_status(name):
    """0 means active, 1 means inactive (or non-existent)"""
    rc = iutil.execWithRedirect("cryptsetup",
                                ["status", name],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)
    return rc

def luks_format(device,
                passphrase=None, key_file=None,
                cipher=None, key_size=None):
    p = os.pipe()
    argv = ["-q"]
    os.close(p[1])

    if cipher:
        argv.extend(["--cipher", cipher])

    if key_size:
        argv.append("--key-size=%d" % key_size)

    argv.extend(["luksFormat", device])
        
    if passphrase:
        os.write(p[1], "%s\n" % passphrase)
    elif key_file and os.path.isfile(key_file):
        argv.append(key_file)
    else:
        raise ValueError("luks_format requires either a passphrase or a key file")

    rc = iutil.execWithRedirect("cryptsetup",
                                argv,
                                stdin = p[0],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)

    os.close(p[0])
    if rc:
        raise CryptoError("luks_format failed for '%s'" % device)

def luks_open(device, name, passphrase=None, key_file=None):
    p = os.pipe()
    if passphrase:
        os.write(p[1], "%s\n" % passphrase)
        argv = ["luksOpen", device, name]
    elif key_file and os.path.isfile(key_file):
        argv = ["luksOpen", "--key-file", key_file, device, name]
    else:
        raise ValueError("luks_open requires either a passphrase or a key file")

    os.close(p[1])
    rc = iutil.execWithRedirect("cryptsetup",
                                argv,
                                stdin = p[0],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)

    os.close(p[0])
    if rc:
        raise CryptoError("luks_open failed for %s (%s)" % (device, name))

def luks_close(name):
    rc = iutil.execWithRedirect("cryptsetup",
                                ["luksClose", name],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)

    if rc:
        raise CryptoError("luks_close failed for %s" % name)

def luks_add_key(device,
                 new_passphrase=None, new_key_file=None,
                 passphrase=None, key_file=None):
    p = os.pipe()
    if passphrase:
        os.write(p[1], "%s\n" % passphrase)
        key_spec = ""
    elif key_file and os.path.isfile(key_file):
        key_spec = "--key-file %s" % key_file
    else:
        raise ValueError("luks_add_key requires either a passphrase or a key file")

    if new_passphrase:
        os.write(p[1], "%s\n" % new_passphrase)
        new_key_spec = ""
    elif new_key_file and os.path.isfile(new_key_file):
        new_key_spec = "%s" % new_key_file
    else:
        raise ValueError("luks_add_key requires either a passphrase or a key file to add")

    os.close(p[1])

    rc = iutil.execWithRedirect("cryptsetup",
                                ["-q",
                                 key_spec,
                                 "luksAddKey",
                                 device,
                                 new_key_spec],
                                stdin = p[0],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)

    os.close(p[0])
    if rc:
        raise CryptoError("luks add key failed")

def luks_remove_key(device,
                    del_passphrase=None, del_key_file=None,
                    passphrase=None, key_file=None):
    p = os.pipe()
    if passphrase:
        os.write(p[1], "%s\n" % passphrase)
        key_spec = ""
    elif key_file and os.path.isfile(key_file):
        key_spec = "--key-file %s" % key_file
    else:
        raise ValueError("luks_remove_key requires either a passphrase or a key file")

    if del_passphrase:
        os.write(p[1], "%s\n" % del_passphrase)
        del_key_spec = ""
    elif del_key_file and os.path.isfile(del_key_file):
        del_key_spec = "%s" % del_key_file
    else:
        raise ValueError("luks_remove_key requires either a passphrase or a key file to remove")

    os.close(p[1])

    rc = iutil.execWithRedirect("cryptsetup",
                                ["-q",
                                 key_spec,
                                 "luksRemoveKey",
                                 device,
                                 del_key_spec],
                                stdin = p[0],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)

    os.close(p[0])
    if rc:
        raise CryptoError("luks_remove_key failed")