3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 Reads a specification from stdin or a file and outputs a PKCS12
9 file with the desired properties.
11 The input format currently consists of a pycert certificate
12 specification (see pycert.py).
13 Currently, keys other than the default key are not supported.
14 The password that is used to encrypt and authenticate the file
28 from mozfile
import NamedTemporaryFile
31 class Error(Exception):
32 """Base class for exceptions in this module."""
37 class OpenSSLError(Error
):
38 """Class for handling errors when calling OpenSSL."""
40 def __init__(self
, status
):
41 super(OpenSSLError
, self
).__init
__()
45 return "Error running openssl: %s " % self
.status
48 def runUtil(util
, args
):
49 env
= os
.environ
.copy()
50 if mozinfo
.os
== "linux":
51 pathvar
= "LD_LIBRARY_PATH"
52 app_path
= os
.path
.dirname(util
)
54 env
[pathvar
] = "%s%s%s" % (app_path
, os
.pathsep
, env
[pathvar
])
56 env
[pathvar
] = app_path
57 proc
= subprocess
.run(
60 universal_newlines
=True,
62 return proc
.returncode
66 """Utility class for reading a specification and generating
69 def __init__(self
, paramStream
):
70 self
.cert
= pycert
.Certificate(paramStream
)
71 self
.key
= pykey
.keyFromSpecification("default")
74 with
NamedTemporaryFile(mode
="wt+") as certTmp
, NamedTemporaryFile(
76 ) as keyTmp
, NamedTemporaryFile(mode
="rb+") as pkcs12Tmp
:
77 certTmp
.write(self
.cert
.toPEM())
79 keyTmp
.write(self
.key
.toPEM())
81 openssl
= shutil
.which("openssl")
98 raise OpenSSLError(status
)
99 return pkcs12Tmp
.read()
102 output
= "-----BEGIN PKCS12-----"
104 b64
= six
.ensure_text(base64
.b64encode(der
))
106 output
+= "\n" + b64
[:64]
108 output
+= "\n-----END PKCS12-----"
112 # The build harness will call this function with an output
113 # file-like object and a path to a file containing a
114 # specification. This will read the specification and output
116 def main(output
, inputPath
):
117 with
open(inputPath
) as configStream
:
118 output
.write(PKCS12(configStream
).toDER())
121 # When run as a standalone program, this will read a specification from
122 # stdin and output the PKCS12 file as PEM to stdout.
123 if __name__
== "__main__":
124 print(PKCS12(sys
.stdin
).toPEM())