2 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "dso_local.h"
12 #ifdef OPENSSL_SYS_VMS
14 # pragma message disable DOLLARID
17 # include <lib$routines.h>
18 # include <libfisdef.h>
22 # include "../vms_rms.h"
24 /* Some compiler options may mask the declaration of "_malloc32". */
25 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
26 # if __INITIAL_POINTER_SIZE == 64
27 # pragma pointer_size save
28 # pragma pointer_size 32
29 void *_malloc32(__size_t);
30 static void *dso_malloc(__size_t num, const char *file, int line)
32 void *ret = _malloc32(num);
33 if (ret == NULL && (file != NULL || line != 0)) {
35 ERR_set_debug(file, line, NULL);
36 ERR_set_error(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE, NULL);
40 # define DSO_MALLOC(num) dso_malloc((num), OPENSSL_FILE, OPENSSL_LINE)
41 # pragma pointer_size restore
42 # else /* __INITIAL_POINTER_SIZE == 64 */
43 # define DSO_MALLOC OPENSSL_malloc
44 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
45 # endif /* __INITIAL_POINTER_SIZE && defined
48 # pragma message disable DOLLARID
50 static int vms_load(DSO *dso);
51 static int vms_unload(DSO *dso);
52 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
53 static char *vms_name_converter(DSO *dso, const char *filename);
54 static char *vms_merger(DSO *dso, const char *filespec1,
55 const char *filespec2);
57 static DSO_METHOD dso_meth_vms = {
58 "OpenSSL 'VMS' shared library method",
67 NULL, /* pathbyaddr */
68 NULL /* globallookup */
72 * On VMS, the only "handle" is the file name. LIB$FIND_IMAGE_SYMBOL depends
73 * on the reference to the file name being the same for all calls regarding
74 * one shared image, so we'll just store it in an instance of the following
75 * structure and put a pointer to that instance in the meth_data stack.
77 typedef struct dso_internal_st {
79 * This should contain the name only, no directory, no extension, nothing
82 struct dsc$descriptor_s filename_dsc;
83 char filename[NAMX_MAXRSS + 1];
85 * This contains whatever is not in filename, if needed. Normally not
88 struct dsc$descriptor_s imagename_dsc;
89 char imagename[NAMX_MAXRSS + 1];
92 DSO_METHOD *DSO_METHOD_openssl(void)
97 static int vms_load(DSO *dso)
100 /* See applicable comments in dso_dl.c */
101 char *filename = DSO_convert_filename(dso, NULL);
103 /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
104 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
105 # if __INITIAL_POINTER_SIZE == 64
106 # pragma pointer_size save
107 # pragma pointer_size 32
108 # endif /* __INITIAL_POINTER_SIZE == 64 */
110 DSO_VMS_INTERNAL *p = NULL;
112 # if __INITIAL_POINTER_SIZE == 64
113 # pragma pointer_size restore
114 # endif /* __INITIAL_POINTER_SIZE == 64 */
115 # endif /* __INITIAL_POINTER_SIZE && defined
118 const char *sp1, *sp2; /* Search result */
119 const char *ext = NULL; /* possible extension to add */
121 if (filename == NULL) {
122 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
127 * A file specification may look like this:
129 * node::dev:[dir-spec]name.type;ver
131 * or (for compatibility with TOPS-20):
133 * node::dev:<dir-spec>name.type;ver
135 * and the dir-spec uses '.' as separator. Also, a dir-spec
136 * may consist of several parts, with mixed use of [] and <>:
140 * We need to split the file specification into the name and
141 * the rest (both before and after the name itself).
144 * Start with trying to find the end of a dir-spec, and save the position
145 * of the byte after in sp1
147 sp1 = strrchr(filename, ']');
148 sp2 = strrchr(filename, '>');
151 if (sp2 != NULL && sp2 > sp1)
154 sp1 = strrchr(filename, ':');
158 sp1++; /* The byte after the found character */
159 /* Now, let's see if there's a type, and save the position in sp2 */
160 sp2 = strchr(sp1, '.');
162 * If there is a period and the next character is a semi-colon,
163 * we need to add an extension
165 if (sp2 != NULL && sp2[1] == ';')
168 * If we found it, that's where we'll cut. Otherwise, look for a version
169 * number and save the position in sp2
172 sp2 = strchr(sp1, ';');
176 * If there was still nothing to find, set sp2 to point at the end of the
180 sp2 = sp1 + strlen(sp1);
182 /* Check that we won't get buffer overflows */
183 if (sp2 - sp1 > FILENAME_MAX
184 || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
185 ERR_raise(ERR_LIB_DSO, DSO_R_FILENAME_TOO_BIG);
189 p = DSO_MALLOC(sizeof(*p));
193 strncpy(p->filename, sp1, sp2 - sp1);
194 p->filename[sp2 - sp1] = '\0';
196 strncpy(p->imagename, filename, sp1 - filename);
197 p->imagename[sp1 - filename] = '\0';
199 strcat(p->imagename, ext);
203 strcat(p->imagename, sp2);
205 p->filename_dsc.dsc$w_length = strlen(p->filename);
206 p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
207 p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
208 p->filename_dsc.dsc$a_pointer = p->filename;
209 p->imagename_dsc.dsc$w_length = strlen(p->imagename);
210 p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
211 p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
212 p->imagename_dsc.dsc$a_pointer = p->imagename;
214 if (!sk_void_push(dso->meth_data, (char *)p)) {
215 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
219 /* Success (for now, we lie. We actually do not know...) */
220 dso->loaded_filename = filename;
225 OPENSSL_free(filename);
230 * Note that this doesn't actually unload the shared image, as there is no
231 * such thing in VMS. Next time it get loaded again, a new copy will
232 * actually be loaded.
234 static int vms_unload(DSO *dso)
238 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
241 if (sk_void_num(dso->meth_data) < 1)
243 p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
245 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
254 * We must do this in a separate function because of the way the exception
255 * handler works (it makes this function return
257 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
258 struct dsc$descriptor_s *symname_dsc, void **sym,
262 * Make sure that signals are caught and returned instead of aborting the
263 * program. The exception handler gets unestablished automatically on
264 * return from this function.
266 lib$establish(lib$sig_to_ret);
268 if (ptr->imagename_dsc.dsc$w_length)
269 return lib$find_image_symbol(&ptr->filename_dsc,
271 &ptr->imagename_dsc, flags);
273 return lib$find_image_symbol(&ptr->filename_dsc,
274 symname_dsc, sym, 0, flags);
277 # ifndef LIB$M_FIS_MIXEDCASE
278 # define LIB$M_FIS_MIXEDCASE (1 << 4);
280 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
282 DSO_VMS_INTERNAL *ptr;
284 struct dsc$descriptor_s symname_dsc;
286 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
287 # if __INITIAL_POINTER_SIZE == 64
288 # define SYMNAME symname_32p
289 # pragma pointer_size save
290 # pragma pointer_size 32
292 # pragma pointer_size restore
293 char symname_32[NAMX_MAXRSS + 1];
294 # else /* __INITIAL_POINTER_SIZE == 64 */
295 # define SYMNAME ((char *) symname)
296 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
300 if ((dso == NULL) || (symname == NULL)) {
301 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
304 # if __INITIAL_POINTER_SIZE == 64
305 /* Copy the symbol name to storage with a 32-bit pointer. */
306 symname_32p = symname_32;
307 strcpy(symname_32p, symname);
308 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
310 symname_dsc.dsc$w_length = strlen(SYMNAME);
311 symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
312 symname_dsc.dsc$b_class = DSC$K_CLASS_S;
313 symname_dsc.dsc$a_pointer = SYMNAME;
315 if (sk_void_num(dso->meth_data) < 1) {
316 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
319 ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
320 sk_void_num(dso->meth_data) - 1);
322 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
326 status = do_find_symbol(ptr, &symname_dsc, sym, LIB$M_FIS_MIXEDCASE);
328 if (!$VMS_STATUS_SUCCESS(status))
329 status = do_find_symbol(ptr, &symname_dsc, sym, 0);
331 if (!$VMS_STATUS_SUCCESS(status)) {
332 unsigned short length;
334 struct dsc$descriptor_s errstring_dsc;
336 errstring_dsc.dsc$w_length = sizeof(errstring);
337 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
338 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
339 errstring_dsc.dsc$a_pointer = errstring;
343 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
345 if (!$VMS_STATUS_SUCCESS(status))
346 lib$signal(status); /* This is really bad. Abort! */
348 errstring[length] = '\0';
350 if (ptr->imagename_dsc.dsc$w_length)
351 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
352 "Symbol %s in %s (%s): %s",
353 symname, ptr->filename, ptr->imagename,
356 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
357 "Symbol %s in %s: %s",
358 symname, ptr->filename, errstring);
365 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
367 DSO_FUNC_TYPE sym = 0;
368 vms_bind_sym(dso, symname, (void **)&sym);
372 static char *vms_merger(DSO *dso, const char *filespec1,
373 const char *filespec2)
376 int filespec1len, filespec2len;
378 struct NAMX_STRUCT nam;
379 char esa[NAMX_MAXRSS + 1];
382 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
383 # if __INITIAL_POINTER_SIZE == 64
384 # define FILESPEC1 filespec1_32p;
385 # define FILESPEC2 filespec2_32p;
386 # pragma pointer_size save
387 # pragma pointer_size 32
390 # pragma pointer_size restore
391 char filespec1_32[NAMX_MAXRSS + 1];
392 char filespec2_32[NAMX_MAXRSS + 1];
393 # else /* __INITIAL_POINTER_SIZE == 64 */
394 # define FILESPEC1 ((char *) filespec1)
395 # define FILESPEC2 ((char *) filespec2)
396 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
402 filespec1len = strlen(filespec1);
403 filespec2len = strlen(filespec2);
405 # if __INITIAL_POINTER_SIZE == 64
406 /* Copy the file names to storage with a 32-bit pointer. */
407 filespec1_32p = filespec1_32;
408 filespec2_32p = filespec2_32;
409 strcpy(filespec1_32p, filespec1);
410 strcpy(filespec2_32p, filespec2);
411 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
416 FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
417 FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
418 FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
419 FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
420 NAMX_DNA_FNA_SET(fab)
423 nam.NAMX_ESS = NAMX_MAXRSS;
424 nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
425 SET_NAMX_NO_SHORT_UPCASE(nam);
429 status = sys$parse(&fab, 0, 0);
431 if (!$VMS_STATUS_SUCCESS(status)) {
432 unsigned short length;
434 struct dsc$descriptor_s errstring_dsc;
436 errstring_dsc.dsc$w_length = sizeof(errstring);
437 errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
438 errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
439 errstring_dsc.dsc$a_pointer = errstring;
441 status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
443 if (!$VMS_STATUS_SUCCESS(status))
444 lib$signal(status); /* This is really bad. Abort! */
446 errstring[length] = '\0';
448 ERR_raise_data(ERR_LIB_DSO, DSO_R_FAILURE,
449 "filespec \"%s\", default \"%s\": %s",
450 filespec1, filespec2, errstring);
455 merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
458 strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
459 merged[nam.NAMX_ESL] = '\0';
463 static char *vms_name_converter(DSO *dso, const char *filename)
469 len = strlen(filename);
471 p = strchr(filename, ':');
476 transform = (strrchr(p, '>') == NULL && strrchr(p, ']') == NULL);
480 int rsize = len + sizeof(DSO_EXTENSION);
482 if ((translated = OPENSSL_malloc(rsize)) != NULL) {
483 p = strrchr(filename, ';');
486 strncpy(translated, filename, len);
487 translated[len] = '\0';
488 strcat(translated, DSO_EXTENSION);
490 strcat(translated, p);
493 translated = OPENSSL_strdup(filename);
498 #endif /* OPENSSL_SYS_VMS */