~ubuntu-branches/debian/experimental/sks/experimental

« back to all changes in this revision

Viewing changes to armor.ml

  • Committer: Package Import Robot
  • Author(s): Daniel Kahn Gillmor
  • Date: 2013-06-27 16:39:02 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130627163902-qqic4va2187boeji
Tags: 1.1.4-1
* New Upstream Release (Closes: #690135)
* added myself to Uploaders.
* convert to dh 9
* Standards-Version: bump to 3.9.4 (no changes needed)
* debian/rules: clean up
* refresh and clean up debian/patches
* switch packaging vcs to git
* avoid trying to upgrade DB_CONFIG (Closes: #709322)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(************************************************************************)
2
 
(* This file is part of SKS.  SKS is free software; you can
3
 
   redistribute it and/or modify it under the terms of the GNU General
4
 
   Public License as published by the Free Software Foundation; either
5
 
   version 2 of the License, or (at your option) any later version.
6
 
 
7
 
   This program is distributed in the hope that it will be useful, but
8
 
   WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 
   General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
15
 
   USA *)
16
 
(***********************************************************************)
17
 
 
18
 
(** Conversion to and from ASCII armor *)
 
1
(***********************************************************************)
 
2
(* armor.ml- Conversion to and from ASCII armor                        *)
 
3
(*                                                                     *)
 
4
(* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, *)
 
5
(*               2011, 2012  Yaron Minsky and Contributors             *)
 
6
(*                                                                     *)
 
7
(* This file is part of SKS.  SKS is free software; you can            *)
 
8
(* redistribute it and/or modify it under the terms of the GNU General *)
 
9
(* Public License as published by the Free Software Foundation; either *)
 
10
(* version 2 of the License, or (at your option) any later version.    *)
 
11
(*                                                                     *)
 
12
(* This program is distributed in the hope that it will be useful, but *)
 
13
(* WITHOUT ANY WARRANTY; without even the implied warranty of          *)
 
14
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *)
 
15
(* General Public License for more details.                            *)
 
16
(*                                                                     *)
 
17
(* You should have received a copy of the GNU General Public License   *)
 
18
(* along with this program; if not, write to the Free Software         *)
 
19
(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)
 
20
(* USA or see <http://www.gnu.org/licenses/>.                          *)
 
21
(***********************************************************************)
 
22
 
19
23
open StdLabels
20
24
open MoreLabels
21
25
open Printf
22
26
 
23
27
external crc_of_string : string -> int = "caml_crc_octets"
24
28
 
25
 
let base64crc input = 
 
29
let base64crc input =
26
30
  let encoder = Cryptokit.Base64.encode_multiline () in
27
31
  encoder#put_string input;
28
32
  encoder#finish;
33
37
  encoder#put_char (char_of_int ((crc lsr 8) land 0xFF));
34
38
  encoder#put_char (char_of_int (crc land 0xFF));
35
39
  encoder#finish;
36
 
  let base64 = 
37
 
    if base64.[String.length base64 - 1] <> '\n' 
 
40
  let base64 =
 
41
    if base64.[String.length base64 - 1] <> '\n'
38
42
    then base64 ^ "\n" else base64 in
39
43
  base64 ^ "=" ^ encoder#get_string
40
44
 
41
 
let pubkey_armor_header = "-----BEGIN PGP PUBLIC KEY BLOCK-----" 
42
 
let pubkey_armor_tail = "-----END PGP PUBLIC KEY BLOCK-----" 
 
45
let pubkey_armor_header = "-----BEGIN PGP PUBLIC KEY BLOCK-----"
 
46
let pubkey_armor_tail = "-----END PGP PUBLIC KEY BLOCK-----"
43
47
 
44
48
(* pubkey *)
45
 
let encode_pubkey key = 
 
49
let encode_pubkey key =
46
50
  let armor_header = pubkey_armor_header
47
51
  and armor_tail = pubkey_armor_tail
48
 
  and version = (sprintf "Version: SKS %s" Common.version)
 
52
  and version = (sprintf "Version: SKS %s%s" Common.version Common.version_suffix)
 
53
  and hostname = (sprintf "Comment: Hostname: %s" (if String.length !Settings.hostname > 53 then String.sub !Settings.hostname 0 53 else !Settings.hostname))
49
54
  in
50
55
  let input = Key.to_string key in
51
56
  armor_header ^ "\n" ^
52
 
  version ^ "\n\n" ^
 
57
  version ^ "\n" ^
 
58
  hostname ^ "\n\n" ^
53
59
  base64crc input ^ "\n" ^
54
60
  armor_tail
55
 
    
56
 
let encode_pubkey_string keystr = 
 
61
 
 
62
let encode_pubkey_string keystr =
57
63
  let armor_header = pubkey_armor_header
58
64
  and armor_tail = pubkey_armor_tail
59
 
  and version = (sprintf "Version: SKS %s" Common.version)
 
65
  and version = (sprintf "Version: SKS %s%s" Common.version Common.version_suffix)
 
66
  and hostname = (sprintf "Comment: Hostname: %s" (if String.length !Settings.hostname > 53 then String.sub !Settings.hostname 0 53 else !Settings.hostname))
60
67
  in
61
68
  let input = keystr in
62
69
  armor_header ^ "\n" ^
63
 
  version ^ "\n\n" ^
 
70
  version ^ "\n" ^
 
71
  hostname ^ "\n\n" ^
64
72
  base64crc input ^ "\n" ^
65
73
  armor_tail
66
74
 
67
 
let decode_crc s = 
 
75
let decode_crc s =
68
76
  let decoder = Cryptokit.Base64.decode () in
69
77
  decoder#put_string s;
70
78
  decoder#finish;
71
79
  let b1 = decoder#get_byte in
72
80
  let b2 = decoder#get_byte in
73
81
  let b3 = decoder#get_byte in
74
 
  b1 lsl 16 + b2 lsl 8 + b3 
 
82
  b1 lsl 16 + b2 lsl 8 + b3
75
83
 
76
84
let eol = Str.regexp "[ \t]*\r?\n"
77
85
 
81
89
  let rec read_adata lines = match lines with
82
90
      [] -> failwith "Error while decoding ascii-armored key: text terminated before reaching CRC sum"
83
91
    | line::tl ->
84
 
        if line.[0] = '=' 
85
 
        then ( (* close the decoder and return the CRC string *)
86
 
          decoder#finish;
87
 
          let crc = decode_crc (String.sub ~pos:1 
88
 
                                  ~len:(String.length line - 1) line)
89
 
          and data = decoder#get_string in
90
 
          (data,crc)
91
 
        )
92
 
        else (
93
 
          decoder#put_string line;
94
 
          read_adata tl
95
 
        )
 
92
        if line.[0] = '='
 
93
        then ( (* close the decoder and return the CRC string *)
 
94
          decoder#finish;
 
95
          let crc = decode_crc (String.sub ~pos:1
 
96
                                  ~len:(String.length line - 1) line)
 
97
          and data = decoder#get_string in
 
98
          (data,crc)
 
99
        )
 
100
        else (
 
101
          decoder#put_string line;
 
102
          read_adata tl
 
103
        )
96
104
  and read_full lines = match lines with
97
105
      [] -> failwith "Error while decoding ascii-armored key:  text terminated before reaching PGP public key header line"
98
106
    | line::tl ->
99
 
        if line = pubkey_armor_header then read_block tl
100
 
        else read_full tl
 
107
        if line = pubkey_armor_header then read_block tl
 
108
        else read_full tl
101
109
  and read_block lines = match lines with
102
110
      [] -> failwith "Error while decoding ascii-armored key: text terminated before beginning of ascii block"
103
111
    | line::tl ->
104
 
        if line = "" then read_adata tl
105
 
        else read_block tl
 
112
        if line = "" then read_adata tl
 
113
        else read_block tl
106
114
  in
107
115
  let (data,crc) = read_full lines in
108
116
  let data_crc = crc_of_string data in
109
117
  assert (data_crc = crc);
110
118
  Key.of_string_multiple data
111
119
 
112
 
 
 
120