1#!/usr/bin/python 2 3# Orthanc - A Lightweight, RESTful DICOM Store 4# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 5# Department, University Hospital of Liege, Belgium 6# Copyright (C) 2017-2021 Osimis S.A., Belgium 7# 8# This program is free software: you can redistribute it and/or 9# modify it under the terms of the GNU Lesser General Public License 10# as published by the Free Software Foundation, either version 3 of 11# the License, or (at your option) any later version. 12# 13# This program is distributed in the hope that it will be useful, but 14# WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16# Lesser General Public License for more details. 17# 18# You should have received a copy of the GNU Lesser General Public 19# License along with this program. If not, see 20# <http://www.gnu.org/licenses/>. 21 22 23import re 24import sys 25import subprocess 26import urllib2 27 28 29if len(sys.argv) <= 2: 30 print('Download a set of CA certificates, convert them to PEM, then format them as a C macro') 31 print('Usage: %s [Macro] [Certificate1] <Certificate2>...' % sys.argv[0]) 32 print('') 33 print('Example: %s BITBUCKET_CERTIFICATES https://www.digicert.com/CACerts/DigiCertHighAssuranceEVRootCA.crt' % sys.argv[0]) 34 print('') 35 sys.exit(-1) 36 37MACRO = sys.argv[1] 38 39sys.stdout.write('#define %s ' % MACRO) 40 41for url in sys.argv[2:]: 42 # Download the certificate from the CA authority, in the DES format 43 des = urllib2.urlopen(url).read() 44 45 # Convert DES to PEM 46 p = subprocess.Popen([ 'openssl', 'x509', '-inform', 'DES', '-outform', 'PEM' ], 47 stdin = subprocess.PIPE, 48 stdout = subprocess.PIPE) 49 pem = p.communicate(input = des)[0] 50 pem = re.sub(r'\r', '', pem) # Remove any carriage return 51 pem = re.sub(r'\\', r'\\\\', pem) # Escape any backslash 52 pem = re.sub(r'"', r'\\"', pem) # Escape any quote 53 54 # Write the PEM data into the macro 55 for line in pem.split('\n'): 56 sys.stdout.write(' \\\n') 57 sys.stdout.write('"%s\\n" ' % line) 58 59sys.stdout.write('\n') 60sys.stderr.write('Done!\n') 61