summary refs log blame commit diff stats
path: root/win32/deps/openssl/mk-ca-bundle.pl
blob: 3b4e16ae7cbf0ba9d914014c4f5aaa15d950e3a9 (plain) (tree)


































































































































































































                                                                                                                      
#!/usr/bin/perl -w
# ***************************************************************************
# *                                  _   _ ____  _
# *  Project                     ___| | | |  _ \| |
# *                             / __| | | | |_) | |
# *                            | (__| |_| |  _ <| |___
# *                             \___|\___/|_| \_\_____|
# *
# * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
# *
# * This software is licensed as described in the file COPYING, which
# * you should have received as part of this distribution. The terms
# * are also available at http://curl.haxx.se/docs/copyright.html.
# *
# * You may opt to use, copy, modify, merge, publish, distribute and/or sell
# * copies of the Software, and permit persons to whom the Software is
# * furnished to do so, under the terms of the COPYING file.
# *
# * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# * KIND, either express or implied.
# *
# * $Id: mk-ca-bundle.pl,v 1.10 2008/02/15 00:41:54 gknauf Exp $
# ***************************************************************************
# This Perl script creates a fresh ca-bundle.crt file for use with libcurl. 
# It downloads certdata.txt from Mozilla's source tree (see URL below),
# then parses certdata.txt and extracts CA Root Certificates into PEM format.
# These are then processed with the OpenSSL commandline tool to produce the
# final ca-bundle.crt file.
# The script is based on the parse-certs script written by Roland Krikava.
# This Perl script works on almost any platform since its only external
# dependency is the OpenSSL commandline tool for optional text listing.
# Hacked by Guenter Knauf.
#
# Hacked again by Cameron Kaiser to use cURL *instead* of LWP.
#
use Getopt::Std;
use MIME::Base64;
#use LWP::UserAgent;
use strict;
use vars qw($opt_b $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);  

# more up-to-date revisions can always be found at http://hg.mozilla.org/mozilla-central/
# in the security/nss/lib/ckfw/builtins folder
my $url = 'http://mxr.mozilla.org/seamonkey/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
# If the OpenSSL commandline is not in search path you can configure it here!
my $openssl = 'openssl';

my $version = $1 if ('$Revision: 1.10 $' =~ /\s(\d+\.\d+)\s/);

getopts('bhilnqtuv');

if ($opt_i) {
  print ("=" x 78 . "\n");
  print "Script Version            : $version\n";
  print "Perl Version              : $]\n";
  print "Operating System Name     : $^O\n";
  print "Getopt::Std.pm Version    : ${Getopt::Std::VERSION}\n";
  print "MIME::Base64.pm Version   : ${MIME::Base64::VERSION}\n";
#  print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
#  print "LWP.pm Version            : ${LWP::VERSION}\n";
  print "This version uses cURL. As it should.\n";
  print `curl --version`;
  print ("=" x 78 . "\n");
}

$0 =~ s/\\/\//g;
$0 = substr($0, rindex($0, '/') + 1);
if ($opt_h) {
  printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [<outputfile>]\n", $0);
  print "\t-b\tbackup an existing version of ca-bundle.crt\n";
  print "\t-i\tprint version info about used modules\n";
  print "\t-l\tprint license info about certdata.txt\n";
  print "\t-n\tno download of certdata.txt (to use existing)\n";
  print "\t-q\tbe really quiet (no progress output at all)\n";
  print "\t-t\tinclude plain text listing of certificates\n";
  print "\t-u\tunlink (remove) certdata.txt after processing\n";
  print "\t-v\tbe verbose and print out processed CAs\n";
  exit;
}

#my $crt = $ARGV[0] || 'ca-bundle.crt';
my $crt = $ARGV[0] || 'cert.pem';
my $txt = substr($url, rindex($url, '/') + 1);
$txt =~ s/\?.*//;

if (!$opt_n || !-e $txt) {
  print "Downloading '$txt' ...\n" if (!$opt_q);
  #my $ua  = new LWP::UserAgent(agent => "$0/$version");
  #my $req = new HTTP::Request('GET', $url);
  #my $res = $ua->request($req);
  #if ($res->is_success) {
  #  open(TXT,">$txt") or die "Couldn't open $txt: $!";
  #  print TXT $res->content . "\n";
  #  close(TXT) or die "Couldn't close $txt: $!";
  #} else {
  #  die $res->status_line;
  #}
  system("curl $url > $txt");
}

if ($opt_b && -e $crt) {
  my $bk = 1;
  while (-e "$crt.~${bk}~") {
    $bk++;
  }
  rename $crt, "$crt.~${bk}~";
}

my $format = $opt_t ? "plain text and " : "";
my $currentdate = scalar gmtime() . " UTC";
open(CRT,">$crt") or die "Couldn't open $crt: $!";
print CRT <<EOT;
##
## $crt -- Bundle of CA Root Certificates
##
## Converted at: ${currentdate}
##
## This is a bundle of X.509 certificates of public Certificate Authorities
## (CA). These were automatically extracted from Mozilla's root certificates
## file (certdata.txt).  This file can be found in the mozilla source tree:
## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'
##
## It contains the certificates in ${format}PEM format and therefore
## can be directly used with curl / libcurl / php_curl, or with
## an Apache+mod_ssl webserver for SSL client authentication.
## Just configure this file as the SSLCACertificateFile.
##

EOT

close(CRT) or die "Couldn't close $crt: $!";

print "Processing  '$txt' ...\n" if (!$opt_q);
my $caname;
my $certnum = 0;
open(TXT,"$txt") or die "Couldn't open $txt: $!";
while (<TXT>) {
  if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
    open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
    print CRT;
    print if ($opt_l);
    while (<TXT>) {
      print CRT;
      print if ($opt_l);
      last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
    }
    close(CRT) or die "Couldn't close $crt: $!";
  }
  next if /^#|^\s*$/;
  chomp;
  if (/^CVS_ID\s+\"(.*)\"/) {
    open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
    print CRT "# $1\n";
    close(CRT) or die "Couldn't close $crt: $!";
  }
  if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
    $caname = $1;
  }
  if (/^CKA_VALUE MULTILINE_OCTAL/) {
    my $data;
    while (<TXT>) {
      last if (/^END/);
      chomp;
      my @octets = split(/\\/);
      shift @octets;
      for (@octets) {
        $data .= chr(oct);
      }
    }
    my $pem = "-----BEGIN CERTIFICATE-----\n"
            . MIME::Base64::encode($data)
            . "-----END CERTIFICATE-----\n";
    open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
    print CRT "\n$caname\n";
    print CRT ("=" x length($caname) . "\n");
    if (!$opt_t) {
      print CRT $pem;
    }
    close(CRT) or die "Couldn't close $crt: $!";
    if ($opt_t) {
      open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!";
      print TMP $pem;
      close(TMP) or die "Couldn't close openssl pipe: $!";
    }
    print "Parsing: $caname\n" if ($opt_v);
    $certnum ++;
  }
}
close(TXT) or die "Couldn't close $txt: $!";
unlink $txt if ($opt_u);
print "Done ($certnum CA certs processed).\n" if (!$opt_q);

exit;
span> ^
f6dd61cb ^
08e5778b ^
f6dd61cb ^

08e5778b ^
f6dd61cb ^

08e5778b ^
f6dd61cb ^
08e5778b ^
f6dd61cb ^






08e5778b ^
f6dd61cb ^









08e5778b ^
f6dd61cb ^
08e5778b ^
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226



















                                                                            
 

                   
                           
                    
                   



                                  

                                         

           





                                                    
 



                                      
 

                                                        
 
                                                    
         



                                                                            

         


                                                                           


           





                                                     
 



                                      
 

                                                         
 
                                                    
         



                                                                          

         


                                                                           

 



                                             
 


                                 
 



                                                                                   
 
                                
         









                                                                                   

         
                          
         

                                                               
         

 






                             
 


                                                             
 


                                                              
 




                                                             

                                                          
                                                              



                                                                       
                                                                
                                                                        
                                                                
 


                                                                           
 






                       
 

                                                  
 

                                            
 

                              


   












                                                                                                      
 
                      
 

                                                  
 

                                                   
 
                                          
 






                                                                               
 









                                                                                       
 
                           
 
/* gtkspell - a spell-checking addon for GTK's TextView widget
* Copyright (c) 2013 Sandro Mani
*
* Based on gtkhtml-editor-spell-language.c code which is
* Copyright (C) 2008 Novell, Inc.
*
*    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, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "config.h"

#include "sexy-iso-codes.h"
#include <libintl.h>
#include <string.h>

#define ISO_639_DOMAIN	"iso_639"
#define ISO_3166_DOMAIN	"iso_3166"

static GHashTable *iso_639_table = NULL;
static GHashTable *iso_3166_table = NULL;

static void
iso_639_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer data,
GError **error)
{
	GHashTable *hash_table = data;
	const gchar *name = NULL;
	const gchar *code = NULL;
	int i;

	if (strcmp (element_name, "iso_639_entry") != 0)
		return;

	for (i = 0; attribute_names[i] != NULL; i++)
	{
		if (strcmp (attribute_names[i], "name") == 0)
			name = attribute_values[i];
		else if (strcmp (attribute_names[i], "iso_639_1_code") == 0)
			code = attribute_values[i];
	}

	if (code != NULL && *code != '\0' && name != NULL && *name != '\0')
		g_hash_table_insert (hash_table, g_strdup (code),
		g_strdup (dgettext (ISO_639_DOMAIN, name)));
}

static void
iso_3166_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer data,
GError **error)
{
	GHashTable *hash_table = data;
	const gchar *name = NULL;
	const gchar *code = NULL;
	int i;

	if (strcmp (element_name, "iso_3166_entry") != 0)
		return;

	for (i = 0; attribute_names[i] != NULL; i++)
	{
		if (strcmp (attribute_names[i], "name") == 0)
			name = attribute_values[i];
		else if (strcmp (attribute_names[i], "alpha_2_code") == 0)
			code = attribute_values[i];
	}

	if (code != NULL && *code != '\0' && name != NULL && *name != '\0')
		g_hash_table_insert (hash_table, g_strdup (code),
		g_strdup (dgettext (ISO_3166_DOMAIN, name)));
}

static void
iso_codes_parse (const GMarkupParser *parser,
const gchar *basename,
GHashTable *hash_table)
{
	GMappedFile *mapped_file;
	gchar *filename;
	GError *error = NULL;

	filename = g_build_filename (ISO_CODES_PREFIX, "share", "xml", "iso-codes",
		basename, NULL);
	mapped_file = g_mapped_file_new (filename, FALSE, &error);
	g_free (filename);

	if (mapped_file != NULL)
	{
		GMarkupParseContext *context;
		const gchar *contents;
		gsize length;

		context = g_markup_parse_context_new (parser, 0, hash_table, NULL);
		contents = g_mapped_file_get_contents (mapped_file);
		length = g_mapped_file_get_length (mapped_file);
		g_markup_parse_context_parse (context, contents, length, &error);
		g_markup_parse_context_free (context);
		g_mapped_file_unref (mapped_file);
	}

	if (error != NULL)
	{
		g_warning ("%s: %s", basename, error->message);
		g_error_free (error);
	}
}

/**
* codetable_init:
*
* Initializes the code table.
*/
void
codetable_init (void)
{
	GMarkupParser iso_639_parser = {
		iso_639_start_element, NULL, NULL, NULL, NULL
	};

	GMarkupParser iso_3166_parser = {
		iso_3166_start_element, NULL, NULL, NULL, NULL
	};

	g_return_if_fail (iso_639_table == NULL);
	g_return_if_fail (iso_3166_table == NULL);

#ifdef ENABLE_NLS
	bindtextdomain (ISO_639_DOMAIN, ISO_CODES_LOCALEDIR);
	bind_textdomain_codeset (ISO_639_DOMAIN, "UTF-8");

	bindtextdomain (ISO_3166_DOMAIN, ISO_CODES_LOCALEDIR);
	bind_textdomain_codeset (ISO_3166_DOMAIN, "UTF-8");
#endif

	iso_639_table = g_hash_table_new_full (g_str_hash, g_str_equal,
		(GDestroyNotify)g_free, (GDestroyNotify)g_free);
	iso_3166_table = g_hash_table_new_full (g_str_hash, g_str_equal,
		(GDestroyNotify)g_free, (GDestroyNotify)g_free);

	iso_codes_parse (&iso_639_parser, "iso_639.xml", iso_639_table);
	iso_codes_parse (&iso_3166_parser, "iso_3166.xml", iso_3166_table);
}

/**
* codetable_free:
*
* Frees the code table.
*/
void
codetable_free (void)
{
	g_return_if_fail (iso_639_table != NULL);
	g_return_if_fail (iso_3166_table != NULL);

	g_hash_table_unref (iso_639_table);
	g_hash_table_unref (iso_3166_table);

	iso_639_table = NULL;
	iso_3166_table = NULL;
}

/**
* codetable_lookup:
* @language_code: A language code (i.e. "en_US")
* @language_name: (out) (transfer none): Pointer to the name of the language.
* This pointer is owned by the code table and must not be freed.
* @country_name: (out) (transfer none): Pointer to the name of the country.
* This pointer is owned by the code table and must not be freed.
*
* Looks up the language and country name for the specified language code.
* If no matching entries are found, language_name and country_name will
* simply contain the parts of the language code (i.e. "en" and "US").
*/
void
codetable_lookup (const gchar *language_code, const gchar **language_name, const gchar** country_name)
{
	gchar **parts;

	g_return_if_fail (iso_639_table != NULL);
	g_return_if_fail (iso_3166_table != NULL);

	/* Split language code into parts. */
	parts = g_strsplit (language_code, "_", 2);

	g_return_if_fail (*parts != NULL);

	*language_name = g_hash_table_lookup (iso_639_table, parts[0]);
	if (*language_name == NULL)
	{
		g_hash_table_insert (iso_639_table, g_strdup (parts[0]),
			g_strdup (parts[0]));
		*language_name = g_hash_table_lookup (iso_639_table, parts[0]);
	}

	if (g_strv_length (parts) == 2)
	{
		*country_name = g_hash_table_lookup (iso_3166_table, parts[1]);
		if (*country_name == NULL)
		{
			g_hash_table_insert (iso_3166_table, g_strdup (parts[1]),
				g_strdup (parts[1]));
			*country_name = g_hash_table_lookup (iso_3166_table, parts[1]);
		}
	}

	g_strfreev (parts);
}