Debian Codegraph @

stubs/nss_stubs.c

raw
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Netscape security libraries.
15 *
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * either the GNU General Public License Version 2 or later (the "GPL"), or
23 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24 * in which case the provisions of the GPL or the LGPL are applicable instead
25 * of those above. If you wish to allow use of your version of this file only
26 * under the terms of either the GPL or the LGPL, and not to allow others to
27 * use your version of this file under the terms of the MPL, indicate your
28 * decision by deleting the provisions above and replace them with the notice
29 * and other provisions required by the GPL or the LGPL. If you do not delete
30 * the provisions above, a recipient may use your version of this file under
31 * the terms of any one of the MPL, the GPL or the LGPL.
32 *
33 * This file was adapted from nss/mozilla/security/nss/cmd/pwdecrypt.c for
34 * use with Objective Caml.
35 * Copyright (C) 2010 Stephane Glondu <[email protected]>.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 #define NO_NSPR_10_SUPPORT
40
41 #include <nspr/nspr.h>
42 #include <string.h>
43 #include <nss/nss.h>
44 #include <nss/cert.h>
45 #include <nss/pk11func.h>
46 #include <nss/nssb64.h>
47
48 #include <nss/pk11sdr.h>
49
50 #include <caml/mlvalues.h>
51 #include <caml/memory.h>
52 #include <caml/alloc.h>
53 #include <caml/fail.h>
54 #include <caml/callback.h>
55
56 #include <assert.h>
57
58 char *password_func(PK11SlotInfo *slot, PRBool retry, void *arg) {
59 CAMLparam0();
60 CAMLlocal4(ml_arg, ml_retry, ml_res, ml_exn);
61 ml_arg = *(value *)arg;
62 /* ml_arg : (bool -> string) * exn option */
63 assert(Is_block(ml_arg));
64 ml_retry = retry ? Val_true : Val_false;
65 ml_res = caml_callback_exn(Field(ml_arg, 0), ml_retry);
66 if (Is_exception_result(ml_res)) {
67 /* put (Some exn) in exn slot of arg */
68 ml_exn = caml_alloc_tuple(1);
69 Store_field(ml_exn, 0, Extract_exception(ml_res));
70 Store_field(ml_arg, 1, ml_exn);
71 CAMLreturnT(char *, NULL);
72 } else {
73 CAMLreturnT(char *, PL_strdup(String_val(ml_res)));
74 }
75 }
76
77 CAMLprim value caml_nss_cleanup(value unit) {
78 CAMLparam1(unit);
79 if (NSS_Shutdown() != SECSuccess) {
80 caml_raise_constant(*caml_named_value("NSS_cleanup_failed"));
81 }
82 PR_Cleanup ();
83 CAMLreturn(Val_unit);
84 }
85
86 CAMLprim value caml_nss_init(value path) {
87 CAMLparam1(path);
88 int rv;
89
90 PK11_SetPasswordFunc(password_func);
91 rv = NSS_Init(String_val(path));
92 if (rv != SECSuccess) {
93 caml_raise_constant(*caml_named_value("NSS_init_failed"));
94 }
95 CAMLreturn(Val_unit);
96 }
97
98 CAMLprim value caml_do_decrypt(value decrypt_callback, value data) {
99 CAMLparam2(decrypt_callback, data);
100 CAMLlocal3(res, exn, cb_data);
101 const char *dataString = String_val(data);
102 int strLen = caml_string_length(data);
103 SECItem *decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString, strLen);
104 SECStatus rv;
105 SECItem result = { siBuffer, NULL, 0 };
106
107 if ((decoded == NULL) || (decoded->len == 0)) {
108 /* Base64 decoding failed */
109 res = Val_int(PORT_GetError());
110 if (decoded) {
111 SECITEM_FreeItem(decoded, PR_TRUE);
112 }
113 {
114 value args[] = { data, res };
115 caml_raise_with_args(*caml_named_value("NSS_base64_decode_failed"), 2, args);
116 }
117 }
118 /* Base64 decoding succeeded */
119 /* Build the argument to password_func ((bool -> string) * exn option) */
120 cb_data = caml_alloc_tuple(2);
121 Store_field(cb_data, 0, decrypt_callback);
122 Store_field(cb_data, 1, Val_unit); /* None */
123 /* Decrypt */
124 rv = PK11SDR_Decrypt(decoded, &result, &cb_data);
125 SECITEM_ZfreeItem(decoded, PR_TRUE);
126 if (rv == SECSuccess) {
127 res = caml_alloc_string(result.len);
128 memcpy(Bytes_val(res), result.data, result.len);
129 SECITEM_ZfreeItem(&result, PR_FALSE);
130 CAMLreturn(res);
131 }
132 /* decryption failed */
133 res = Val_int(PORT_GetError());
134 exn = Field(cb_data, 1);
135 {
136 value args[] = { data, res, exn };
137 caml_raise_with_args(*caml_named_value("NSS_decrypt_failed"), 3, args);
138 }
139 }