src/sign.py
changeset 3321 52e8eec3014c
parent 3194 185fd0ebde38
child 3339 c88573eb98ea
equal deleted inserted replaced
3320:f727edff50bd 3321:52e8eec3014c
    19 #
    19 #
    20 # CDDL HEADER END
    20 # CDDL HEADER END
    21 #
    21 #
    22 
    22 
    23 #
    23 #
    24 # Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
    24 # Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
    25 #
    25 #
    26 
    26 
    27 import getopt
    27 import getopt
    28 import gettext
    28 import gettext
    29 import hashlib
    29 import hashlib
    31 import os
    31 import os
    32 import shutil
    32 import shutil
    33 import sys
    33 import sys
    34 import tempfile
    34 import tempfile
    35 import traceback
    35 import traceback
       
    36 
       
    37 from cryptography import x509
       
    38 from cryptography.hazmat.backends import default_backend
       
    39 from cryptography.hazmat.primitives import serialization
    36 from imp import reload
    40 from imp import reload
    37 
    41 
    38 import pkg
    42 import pkg
    39 import pkg.actions as actions
    43 import pkg.actions as actions
    40 import pkg.client.api_errors as api_errors
    44 import pkg.client.api_errors as api_errors
    45 import pkg.misc as misc
    49 import pkg.misc as misc
    46 import pkg.publish.transaction as trans
    50 import pkg.publish.transaction as trans
    47 from pkg.client import global_settings
    51 from pkg.client import global_settings
    48 from pkg.client.debugvalues import DebugValues
    52 from pkg.client.debugvalues import DebugValues
    49 from pkg.misc import emsg, msg, PipeError
    53 from pkg.misc import emsg, msg, PipeError
    50 import M2Crypto as m2
       
    51 
    54 
    52 PKG_CLIENT_NAME = "pkgsign"
    55 PKG_CLIENT_NAME = "pkgsign"
    53 
    56 
    54 # pkg exit codes
    57 # pkg exit codes
    55 EXIT_OK      = 0
    58 EXIT_OK      = 0
   105 
   108 
   106         return src_pub.catalog
   109         return src_pub.catalog
   107 
   110 
   108 def __make_tmp_cert(d, pth):
   111 def __make_tmp_cert(d, pth):
   109         try:
   112         try:
   110                 cert = m2.X509.load_cert(pth)
   113                 with open(pth, "rb") as f:
   111         except m2.X509.X509Error as e:
   114                         cert = x509.load_pem_x509_certificate(f.read(),
       
   115                             default_backend())
       
   116         except (ValueError, IOError) as e:
   112                 raise api_errors.BadFileFormat(_("The file {0} was expected to "
   117                 raise api_errors.BadFileFormat(_("The file {0} was expected to "
   113                     "be a PEM certificate but it could not be read.").format(
   118                     "be a PEM certificate but it could not be read.").format(
   114                     pth))
   119                     pth))
   115         fd, fp = tempfile.mkstemp(dir=d)
   120         fd, fp = tempfile.mkstemp(dir=d)
   116         with os.fdopen(fd, "wb") as fh:
   121         with os.fdopen(fd, "wb") as fh:
   117                 fh.write(cert.as_pem())
   122                 fh.write(cert.public_bytes(serialization.Encoding.PEM))
   118         return fp
   123         return fp
   119 
   124 
   120 def main_func():
   125 def main_func():
   121         misc.setlocale(locale.LC_ALL, "", error)
   126         misc.setlocale(locale.LC_ALL, "", error)
   122         gettext.install("pkg", "/usr/share/locale",
   127         gettext.install("pkg", "/usr/share/locale",