summaryrefslogtreecommitdiffstats
path: root/ipaserver/secrets/handlers/nsscert.py
blob: e72927ace85281aa533b2b82b2744a885656395b (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
#
# Copyright (C) 2019  IPA Project Contributors, see COPYING for license
#
"""Export / import cert and key from NSS DB as PKCS#12 data
"""
import base64
import json
import os

from ipaplatform.paths import paths
from ipapython import ipautil
from ipapython.certdb import NSSDatabase
from . import common


def export_key(args, tmpdir):
    """Export key and certificate from the NSS DB to a PKCS#12 file.

    The PKCS#12 file is encrypted with a password.
    """
    pk12file = os.path.join(tmpdir, 'export.p12')

    password = ipautil.ipa_generate_password()
    pk12pk12pwfile = os.path.join(tmpdir, 'passwd')
    with open(pk12pk12pwfile, 'w') as f:
        f.write(password)

    nssdb = NSSDatabase(args.nssdb_path)
    nssdb.run_pk12util([
        "-o", pk12file,
        "-n", args.nickname,
        "-k", args.nssdb_pwdfile,
        "-w", pk12pk12pwfile,
    ])

    with open(pk12file, 'rb') as f:
        p12data = f.read()

    data = {
        'export password': password,
        'pkcs12 data': p12data,
    }
    common.json_dump(data, args.exportfile)


def import_key(args, tmpdir):
    """Import key and certificate from a PKCS#12 file to a NSS DB.
    """
    data = json.load(args.importfile)
    password = data['export password']
    p12data = base64.b64decode(data['pkcs12 data'])

    pk12pwfile = os.path.join(tmpdir, 'passwd')
    with open(pk12pwfile, 'w') as f:
        f.write(password)

    pk12file = os.path.join(tmpdir, 'import.p12')
    with open(pk12file, 'wb') as f:
        f.write(p12data)

    nssdb = NSSDatabase(args.nssdb_path)
    nssdb.run_pk12util([
        "-i", pk12file,
        "-n", args.nickname,
        "-k", args.nssdb_pwdfile,
        "-w", pk12pwfile,
    ])


def default_parser():
    """Generic interface
    """
    parser = common.mkparser(
        description='ipa-custodia NSS cert handler'
    )
    parser.add_argument(
        '--nssdb',
        dest='nssdb_path',
        help='path to NSS DB',
        required=True
    )
    parser.add_argument(
        '--pwdfile',
        dest='nssdb_pwdfile',
        help='path to password file for NSS DB',
        required=True
    )
    parser.add_argument(
        '--nickname',
        help='nick name of certificate',
        required=True
    )
    return parser


def pki_tomcat_parser():
    """Hard-code Dogtag's NSSDB and its password file
    """
    parser = common.mkparser(
        description='ipa-custodia pki-tomcat NSS cert handler'
    )
    parser.add_argument(
        '--nickname',
        help='nick name of certificate',
        required=True
    )
    parser.set_defaults(
        nssdb_path=paths.PKI_TOMCAT_ALIAS_DIR,
        nssdb_pwdfile=paths.PKI_TOMCAT_ALIAS_PWDFILE_TXT,
    )
    return parser


def main(parser=None):
    if parser is None:
        parser = default_parser()

    common.main(parser, export_key, import_key)


if __name__ == '__main__':
    main()