Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 144 lines (128 sloc) 5.357 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 module CL = CryptoLib
19 module C = Cryptokit
20
21 type license_version = int
22 type license_id = string
23 type customer_info_data = string StringMap.t
24 type product_bool_info_data = bool StringMap.t
25 type product_int_info_data = int StringMap.t
26 type license = { (* 'a license ? *)
27 license_version : license_version;
28 license_id : license_id;
29 customer_info_data : customer_info_data;
30 product_bool_info_data : product_bool_info_data;
31 product_int_info_data : product_int_info_data;
32 license_signature : string;
33 }
34
35 exception InvalidLicense
36
37 let get_customer_info license_opt key default = try
38 let license = Option.get license_opt in
39 StringMap.find key license.customer_info_data
40 with Failure "Option.get" | Not_found -> default
41
42 let get_product_bool_info license_opt key default = try
43 let license = Option.get license_opt in
44 StringMap.find key license.product_bool_info_data
45 with Failure "Option.get" | Not_found -> default
46
47 let get_product_int_info license_opt key default = try
48 let license = Option.get license_opt in
49 StringMap.find key license.product_int_info_data
50 with Failure "Option.get" | Not_found -> default
51
52 let make_license license_version license_id customer_info_data product_bool_info_data product_int_info_data signature = {
53 license_version = license_version;
54 license_id = license_id;
55 customer_info_data = customer_info_data;
56 product_bool_info_data = product_bool_info_data;
57 product_int_info_data = product_int_info_data;
58 license_signature = signature;
59 }
60
61 let serialize_license license =
62 let ci_data = StringMap.fold (
63 fun k v acc ->
64 Printf.sprintf "%s=%s,%s" k (CryptoLib.to_base64 v) acc
65 ) license.customer_info_data "" in
66 let pbi_data = StringMap.fold (
67 fun k v acc ->
68 Printf.sprintf "%s=%s,%s" k (string_of_bool v) acc
69 ) license.product_bool_info_data "" in
70 let pii_data = StringMap.fold (
71 fun k v acc ->
72 Printf.sprintf "%s=%s,%s" k (string_of_int v) acc
73 ) license.product_int_info_data "" in
74 let data = Printf.sprintf "%d;%s;%s;%s;%s" license.license_version license.license_id ci_data pbi_data pii_data in
75 data
76
77 let sign_serialized_license data sym_key rsa_sig_privkey=
78 let to_sign = CL.hash data in
79 let signature = CL.to_hex (CL.sign rsa_sig_privkey to_sign) in
80 let final = Printf.sprintf "%s\n%s" data signature in
81 CL.to_base64_m (CL.symetric_encrypt (CL.from_hex sym_key) final)
82
83 let check_license_data license_data sym_key rsa_sig_pubkey = try
84 let license_content = CL.symetric_decrypt (CL.from_hex sym_key) (CL.from_base64 license_data) in
85 let p1, p2 =
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
86 let l = Base.String.slice '\n' license_content in
fccc685 Initial open-source release
MLstate authored
87 if List.length l = 2 then
88 let p1 = List.nth l 0
89 and p2 = List.nth l 1 in
90 p1, p2
91 else raise InvalidLicense in
92 if CL.hash p1 = Base.String.ltrim ~is_space:((=) '\000') (CL.unsign rsa_sig_pubkey (CL.from_hex p2)) then Some (p1, p2)
93 else None
94 with C.Error _ | InvalidLicense -> None
95
96 let recover_license serialized_license signature =
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
97 let l = Base.String.slice ';' serialized_license in
fccc685 Initial open-source release
MLstate authored
98 if List.length l = 5 then
99 let lv = List.nth l 0
100 and lid = List.nth l 1
101 and ci = List.nth l 2
102 and pbi = List.nth l 3
103 and pii = List.nth l 4 in
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
104 let cl = Base.String.slice ',' ci
105 and pbl = Base.String.slice ',' pbi
106 and pil = Base.String.slice ',' pii in
fccc685 Initial open-source release
MLstate authored
107 let customer_info_data =
108 List.fold_left (
109 fun acc elt ->
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
110 let kv = Base.String.slice '=' elt in
fccc685 Initial open-source release
MLstate authored
111 let k = List.nth kv 0
112 and v = (CryptoLib.from_base64 (List.nth kv 1)) in
113 StringMap.add k v acc
114 ) StringMap.empty cl
115 and product_bool_info_data =
116 List.fold_left (
117 fun acc elt ->
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
118 let kv = Base.String.slice '=' elt in
fccc685 Initial open-source release
MLstate authored
119 let k = List.nth kv 0
120 and v = List.nth kv 1 in
121 StringMap.add k (bool_of_string v) acc
122 ) StringMap.empty pbl
123 and product_int_info_data =
124 List.fold_left (
125 fun acc elt ->
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
126 let kv = Base.String.slice '=' elt in
fccc685 Initial open-source release
MLstate authored
127 let k = List.nth kv 0
128 and v = List.nth kv 1 in
129 StringMap.add k (int_of_string v) acc
130 ) StringMap.empty pil in
131 make_license (int_of_string lv) lid customer_info_data product_bool_info_data product_int_info_data signature
132 else raise InvalidLicense
133
134 let system_uuid () = try
135 let sysprof = "/usr/sbin/system_profiler" in
136 if Sys.file_exists sysprof then
137 let ic = Unix.open_process_in (Printf.sprintf "%s -detailLevel full SPHardwareDataType | grep UUID" sysprof) in
138 let huuid = input_line ic in
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
139 let l = Base.String.slice ':' huuid in
fccc685 Initial open-source release
MLstate authored
140 let uuid = Base.String.trim (List.nth l 1) in
141 uuid
142 else ""
143 with End_of_file | Invalid_argument "List.nth" | Failure "nth" -> ""
Something went wrong with that request. Please try again.