Module: Dualcone

Defined in:
lib/dualcone/version.rb,
ext/dualcone/dualcone.c

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.encrypt(path) ⇒ nil

Encrypts (and overwrites!) the file specified by path.

If successful, this method returns nil. Otherwise, it raises a fatal error.

Returns:

  • (nil)

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/dualcone/dualcone.c', line 160

static VALUE
rb_dualcone_encrypt(VALUE _self, VALUE path) {
  int result = 0;
  int errno_sv = 0;

  /* Check args */
  rb_check_type(path, T_STRING);

  /* Dualcone private memory allocations */
  DualconeContext ctx = {0};
  rb_dualcone_get_key(&ctx);

  /* The path of the input file (ruby code, plaintext) */
  /* This value is modified by dirname(), so strdup() is necessary */
  ctx.input_path = strdup(StringValuePtr(path));
  if (RB_UNLIKELY(ctx.input_path == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for input path: %s", strerror(errno_sv));
  }

  /* Check if input file is readable */
  int plaintext_fd = open(ctx.input_path, O_RDONLY);
  if (plaintext_fd == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to read input file '%s': %s", ctx.input_path, strerror(errno_sv));
  }

  /* Allocate memory for temporary path template */
  ctx.output_path = calloc(1, PATH_MAX);
  if (RB_UNLIKELY(ctx.output_path == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for output path: %s", strerror(errno_sv));
  }

  /* Construct path template for temporary file */
  char *output_dir = dirname(ctx.input_path);
  strncat(ctx.output_path, output_dir, PATH_MAX - strlen(ctx.output_path) - 1);
  strncat(ctx.output_path, "/.dualcone.XXXXXX", PATH_MAX - strlen(ctx.output_path) - 1);

  /* Create temporary file */
  int output_fd = mkstemp(ctx.output_path);
  if (RB_UNLIKELY(output_fd == -1)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to create temporary file: %s", strerror(errno_sv));
  }

  /* Get input file length */
  struct stat plaintext_stat = {0};
  result = fstat(plaintext_fd, &plaintext_stat);
  if (result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to determine length of input file: %s", strerror(errno_sv));
  }

  /* Allocate buffer for input file */
  ctx.plaintext_len = plaintext_stat.st_size;
  ctx.plaintext = calloc(1, ctx.plaintext_len);
  if (RB_UNLIKELY(ctx.plaintext == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for input data: %s", strerror(errno_sv));
  }

  /* Read entire input file */
  ssize_t read_result = read(plaintext_fd, ctx.plaintext, ctx.plaintext_len);
  if (read_result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to read ruby code: %s", strerror(errno_sv));
  }
  close(plaintext_fd);

  /* Allocate memory for encryption result */
  ctx.ciphertext_len = hydro_secretbox_HEADERBYTES + ctx.plaintext_len;
  ctx.ciphertext = calloc(1, ctx.ciphertext_len);
  if (RB_UNLIKELY(ctx.ciphertext == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for encryption: %s", strerror(errno_sv));
  }

  /* Encrypt data! */
  result = hydro_secretbox_encrypt(ctx.ciphertext, ctx.plaintext, ctx.plaintext_len, 0, DUALCONE_CONTEXT, ctx.binary_key);
  if (result != 0) {
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to encrypt ruby code");
  }

  /* Allocate memory for hex-encoded encrypted data */
  ctx.ciphertext_hex_len = ctx.ciphertext_len * 2 + 1;
  ctx.ciphertext_hex = calloc(1, ctx.ciphertext_hex_len);
  if (RB_UNLIKELY(ctx.ciphertext_hex == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for hex-encoding: %s", strerror(errno_sv));
  }

  /* Hex encode encrypted data */
  hydro_bin2hex(ctx.ciphertext_hex, ctx.ciphertext_hex_len, ctx.ciphertext, ctx.ciphertext_len);

  /* Write preamble */
  ssize_t write_result = 0;
  write_result = write(output_fd, DUALCONE_PREAMBLE, sizeof(DUALCONE_PREAMBLE) - 1);
  if (write_result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to write to temporary file: %s", strerror(errno_sv));
  }

  /* Write hex-encoded data */
  write_result = write(output_fd, ctx.ciphertext_hex, ctx.ciphertext_hex_len - 1);
  if (write_result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to write to temporary file: %s", strerror(errno_sv));
  }

  /* Write postamble */
  write_result = write(output_fd, DUALCONE_POSTAMBLE, sizeof(DUALCONE_POSTAMBLE) - 1);
  if (write_result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to write to temporary file: %s", strerror(errno_sv));
  }

  /* Rename tempfile over original */
  close(output_fd);
  char *plaintext_path = StringValuePtr(path);
  result = rename(ctx.output_path, plaintext_path);
  if (result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to rename temporary file: %s", strerror(errno_sv));
  }

  /* Done */
  rb_dualcone_cleanup(&ctx);
  return Qnil;
}

.generate_keyString

Returns a new secret encryption key in hex-encoded format.

Returns:

  • (String)

136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/dualcone/dualcone.c', line 136

static VALUE
rb_dualcone_generate_key(VALUE _self) {
  uint8_t key[hydro_secretbox_KEYBYTES];
  char hex[hydro_secretbox_KEYBYTES * 2 + 1];

  /* Generate key */
  hydro_secretbox_keygen(key);
  char *retval = hydro_bin2hex(hex, hydro_secretbox_KEYBYTES * 2 + 1, key, hydro_secretbox_KEYBYTES);
  if (retval == NULL) {
    rb_raise(rb_eFatal, "unable to generate key");
  }

  return rb_str_new_cstr(hex);
}

.run(path) ⇒ nil

Runs the encrypted file specified by path.

If successful, this method returns nil. Otherwise, it raises a fatal error.

Returns:

  • (nil)

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
# File 'ext/dualcone/dualcone.c', line 63

static VALUE
rb_dualcone_run(VALUE _self, VALUE code) {
  int result = 0;
  int errno_sv = 0;

  /* Check args */
  rb_check_type(code, T_STRING);

  /* Dualcone private memory allocations */
  DualconeContext ctx = {0};
  rb_dualcone_get_key(&ctx);

  /* Encrypted ruby code (hex-encoded) */
  char *hex_code = StringValuePtr(code);
  long hex_code_len = RSTRING_LEN(code);

  /* Check message length */
  if (hex_code_len < DUALCONE_MIN_HEX_LEN) {
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to run code: too short (got %ld chars, expected at least %d chars)", hex_code_len, DUALCONE_MIN_HEX_LEN);
  }

  /* Allocate memory for ciphertext */
  ctx.ciphertext_len = hex_code_len / 2;
  ctx.ciphertext = calloc(1, ctx.ciphertext_len);
  if (RB_UNLIKELY(ctx.ciphertext == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for ciphertext: %s", strerror(errno_sv));
  }

  /* Convert code (hex encoded) to binary */
  result = hydro_hex2bin(ctx.ciphertext, ctx.ciphertext_len, hex_code, hex_code_len, NULL, NULL);
  if (result == -1) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to hex-decode ruby code: %s", strerror(errno_sv));
  }

  /* Allocate memory for plaintext */
  ctx.plaintext_len = ctx.ciphertext_len - hydro_secretbox_HEADERBYTES + 1; // Null byte
  ctx.plaintext = calloc(1, ctx.plaintext_len);
  if (RB_UNLIKELY(ctx.plaintext == NULL)) {
    errno_sv = errno;
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to allocate memory for plaintext: %s", strerror(errno_sv));
  }

  /* Decrypt binary code to plaintext code */
  result = hydro_secretbox_decrypt(ctx.plaintext, ctx.ciphertext, ctx.ciphertext_len, 0, DUALCONE_CONTEXT, ctx.binary_key);
  if (result == -1) {
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to decrypt ruby code");
  }

  /* Evaluate the plaintext code */
  rb_eval_string_protect(ctx.plaintext, &result);
  if (result != 0) {
    rb_dualcone_cleanup(&ctx);
    rb_raise(rb_eFatal, "unable to evaluate ruby code");
  }

  /* Done */
  rb_dualcone_cleanup(&ctx);
  return Qnil;
}