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 # define DSO_MALLOC OPENSSL_malloc
26 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
27 # if __INITIAL_POINTER_SIZE == 64
28 # pragma pointer_size save
29 # pragma pointer_size 32
30 void *_malloc32(__size_t
);
31 static void *dso_malloc(__size_t num
, const char *file
, int line
)
33 void *ret
= _malloc32(num
);
34 if (ret
== NULL
&& (file
!= NULL
|| line
!= 0)) {
36 ERR_set_debug(file
, line
, NULL
);
37 ERR_set_error(ERR_LIB_DSO
, ERR_R_MALLOC_FAILURE
, NULL
);
42 # define DSO_MALLOC(num) dso_malloc((num), OPENSSL_FILE, OPENSSL_LINE)
43 # pragma pointer_size restore
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 */
109 # endif /* __INITIAL_POINTER_SIZE && defined
112 DSO_VMS_INTERNAL
*p
= NULL
;
114 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
115 # if __INITIAL_POINTER_SIZE == 64
116 # pragma pointer_size restore
117 # endif /* __INITIAL_POINTER_SIZE == 64 */
118 # endif /* __INITIAL_POINTER_SIZE && defined
121 const char *sp1
, *sp2
; /* Search result */
122 const char *ext
= NULL
; /* possible extension to add */
124 if (filename
== NULL
) {
125 ERR_raise(ERR_LIB_DSO
, DSO_R_NO_FILENAME
);
130 * A file specification may look like this:
132 * node::dev:[dir-spec]name.type;ver
134 * or (for compatibility with TOPS-20):
136 * node::dev:<dir-spec>name.type;ver
138 * and the dir-spec uses '.' as separator. Also, a dir-spec
139 * may consist of several parts, with mixed use of [] and <>:
143 * We need to split the file specification into the name and
144 * the rest (both before and after the name itself).
147 * Start with trying to find the end of a dir-spec, and save the position
148 * of the byte after in sp1
150 sp1
= strrchr(filename
, ']');
151 sp2
= strrchr(filename
, '>');
154 if (sp2
!= NULL
&& sp2
> sp1
)
157 sp1
= strrchr(filename
, ':');
161 sp1
++; /* The byte after the found character */
162 /* Now, let's see if there's a type, and save the position in sp2 */
163 sp2
= strchr(sp1
, '.');
165 * If there is a period and the next character is a semi-colon,
166 * we need to add an extension
168 if (sp2
!= NULL
&& sp2
[1] == ';')
171 * If we found it, that's where we'll cut. Otherwise, look for a version
172 * number and save the position in sp2
175 sp2
= strchr(sp1
, ';');
179 * If there was still nothing to find, set sp2 to point at the end of the
183 sp2
= sp1
+ strlen(sp1
);
185 /* Check that we won't get buffer overflows */
186 if (sp2
- sp1
> FILENAME_MAX
187 || (sp1
- filename
) + strlen(sp2
) > FILENAME_MAX
) {
188 ERR_raise(ERR_LIB_DSO
, DSO_R_FILENAME_TOO_BIG
);
192 p
= DSO_MALLOC(sizeof(*p
));
196 strncpy(p
->filename
, sp1
, sp2
- sp1
);
197 p
->filename
[sp2
- sp1
] = '\0';
199 strncpy(p
->imagename
, filename
, sp1
- filename
);
200 p
->imagename
[sp1
- filename
] = '\0';
202 strcat(p
->imagename
, ext
);
206 strcat(p
->imagename
, sp2
);
208 p
->filename_dsc
.dsc$w_length
= strlen(p
->filename
);
209 p
->filename_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
210 p
->filename_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
211 p
->filename_dsc
.dsc$a_pointer
= p
->filename
;
212 p
->imagename_dsc
.dsc$w_length
= strlen(p
->imagename
);
213 p
->imagename_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
214 p
->imagename_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
215 p
->imagename_dsc
.dsc$a_pointer
= p
->imagename
;
217 if (!sk_void_push(dso
->meth_data
, (char *)p
)) {
218 ERR_raise(ERR_LIB_DSO
, DSO_R_STACK_ERROR
);
222 /* Success (for now, we lie. We actually do not know...) */
223 dso
->loaded_filename
= filename
;
228 OPENSSL_free(filename
);
233 * Note that this doesn't actually unload the shared image, as there is no
234 * such thing in VMS. Next time it get loaded again, a new copy will
235 * actually be loaded.
237 static int vms_unload(DSO
*dso
)
241 ERR_raise(ERR_LIB_DSO
, ERR_R_PASSED_NULL_PARAMETER
);
244 if (sk_void_num(dso
->meth_data
) < 1)
246 p
= (DSO_VMS_INTERNAL
*)sk_void_pop(dso
->meth_data
);
248 ERR_raise(ERR_LIB_DSO
, DSO_R_NULL_HANDLE
);
257 * We must do this in a separate function because of the way the exception
258 * handler works (it makes this function return
260 static int do_find_symbol(DSO_VMS_INTERNAL
*ptr
,
261 struct dsc$descriptor_s
*symname_dsc
, void **sym
,
265 * Make sure that signals are caught and returned instead of aborting the
266 * program. The exception handler gets unestablished automatically on
267 * return from this function.
269 lib$
establish(lib$sig_to_ret
);
271 if (ptr
->imagename_dsc
.dsc$w_length
)
272 return lib$
find_image_symbol(&ptr
->filename_dsc
,
274 &ptr
->imagename_dsc
, flags
);
276 return lib$
find_image_symbol(&ptr
->filename_dsc
,
277 symname_dsc
, sym
, 0, flags
);
280 # ifndef LIB$M_FIS_MIXEDCASE
281 # define LIB$M_FIS_MIXEDCASE (1 << 4);
283 void vms_bind_sym(DSO
*dso
, const char *symname
, void **sym
)
285 DSO_VMS_INTERNAL
*ptr
;
287 struct dsc$descriptor_s symname_dsc
;
289 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
290 # if __INITIAL_POINTER_SIZE == 64
291 # define SYMNAME symname_32p
292 # pragma pointer_size save
293 # pragma pointer_size 32
295 # pragma pointer_size restore
296 char symname_32
[NAMX_MAXRSS
+ 1];
297 # else /* __INITIAL_POINTER_SIZE == 64 */
298 # define SYMNAME ((char *) symname)
299 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
303 if ((dso
== NULL
) || (symname
== NULL
)) {
304 ERR_raise(ERR_LIB_DSO
, ERR_R_PASSED_NULL_PARAMETER
);
307 # if __INITIAL_POINTER_SIZE == 64
308 /* Copy the symbol name to storage with a 32-bit pointer. */
309 symname_32p
= symname_32
;
310 strcpy(symname_32p
, symname
);
311 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
313 symname_dsc
.dsc$w_length
= strlen(SYMNAME
);
314 symname_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
315 symname_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
316 symname_dsc
.dsc$a_pointer
= SYMNAME
;
318 if (sk_void_num(dso
->meth_data
) < 1) {
319 ERR_raise(ERR_LIB_DSO
, DSO_R_STACK_ERROR
);
322 ptr
= (DSO_VMS_INTERNAL
*)sk_void_value(dso
->meth_data
,
323 sk_void_num(dso
->meth_data
) - 1);
325 ERR_raise(ERR_LIB_DSO
, DSO_R_NULL_HANDLE
);
329 status
= do_find_symbol(ptr
, &symname_dsc
, sym
, LIB$M_FIS_MIXEDCASE
);
331 if (!$
VMS_STATUS_SUCCESS(status
))
332 status
= do_find_symbol(ptr
, &symname_dsc
, sym
, 0);
334 if (!$
VMS_STATUS_SUCCESS(status
)) {
335 unsigned short length
;
337 struct dsc$descriptor_s errstring_dsc
;
339 errstring_dsc
.dsc$w_length
= sizeof(errstring
);
340 errstring_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
341 errstring_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
342 errstring_dsc
.dsc$a_pointer
= errstring
;
346 status
= sys$
getmsg(status
, &length
, &errstring_dsc
, 1, 0);
348 if (!$
VMS_STATUS_SUCCESS(status
))
349 lib$
signal(status
); /* This is really bad. Abort! */
351 errstring
[length
] = '\0';
353 if (ptr
->imagename_dsc
.dsc$w_length
)
354 ERR_raise_data(ERR_LIB_DSO
, DSO_R_SYM_FAILURE
,
355 "Symbol %s in %s (%s): %s",
356 symname
, ptr
->filename
, ptr
->imagename
,
359 ERR_raise_data(ERR_LIB_DSO
, DSO_R_SYM_FAILURE
,
360 "Symbol %s in %s: %s",
361 symname
, ptr
->filename
, errstring
);
368 static DSO_FUNC_TYPE
vms_bind_func(DSO
*dso
, const char *symname
)
370 DSO_FUNC_TYPE sym
= 0;
371 vms_bind_sym(dso
, symname
, (void **)&sym
);
375 static char *vms_merger(DSO
*dso
, const char *filespec1
,
376 const char *filespec2
)
379 int filespec1len
, filespec2len
;
381 struct NAMX_STRUCT nam
;
382 char esa
[NAMX_MAXRSS
+ 1];
385 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
386 # if __INITIAL_POINTER_SIZE == 64
387 # define FILESPEC1 filespec1_32p;
388 # define FILESPEC2 filespec2_32p;
389 # pragma pointer_size save
390 # pragma pointer_size 32
393 # pragma pointer_size restore
394 char filespec1_32
[NAMX_MAXRSS
+ 1];
395 char filespec2_32
[NAMX_MAXRSS
+ 1];
396 # else /* __INITIAL_POINTER_SIZE == 64 */
397 # define FILESPEC1 ((char *) filespec1)
398 # define FILESPEC2 ((char *) filespec2)
399 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
405 filespec1len
= strlen(filespec1
);
406 filespec2len
= strlen(filespec2
);
408 # if __INITIAL_POINTER_SIZE == 64
409 /* Copy the file names to storage with a 32-bit pointer. */
410 filespec1_32p
= filespec1_32
;
411 filespec2_32p
= filespec2_32
;
412 strcpy(filespec1_32p
, filespec1
);
413 strcpy(filespec2_32p
, filespec2
);
414 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
419 FAB_OR_NAML(fab
, nam
).FAB_OR_NAML_FNA
= FILESPEC1
;
420 FAB_OR_NAML(fab
, nam
).FAB_OR_NAML_FNS
= filespec1len
;
421 FAB_OR_NAML(fab
, nam
).FAB_OR_NAML_DNA
= FILESPEC2
;
422 FAB_OR_NAML(fab
, nam
).FAB_OR_NAML_DNS
= filespec2len
;
423 NAMX_DNA_FNA_SET(fab
)
426 nam
.NAMX_ESS
= NAMX_MAXRSS
;
427 nam
.NAMX_NOP
= NAM$M_SYNCHK
| NAM$M_PWD
;
428 SET_NAMX_NO_SHORT_UPCASE(nam
);
432 status
= sys$
parse(&fab
, 0, 0);
434 if (!$
VMS_STATUS_SUCCESS(status
)) {
435 unsigned short length
;
437 struct dsc$descriptor_s errstring_dsc
;
439 errstring_dsc
.dsc$w_length
= sizeof(errstring
);
440 errstring_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
441 errstring_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
442 errstring_dsc
.dsc$a_pointer
= errstring
;
444 status
= sys$
getmsg(status
, &length
, &errstring_dsc
, 1, 0);
446 if (!$
VMS_STATUS_SUCCESS(status
))
447 lib$
signal(status
); /* This is really bad. Abort! */
449 errstring
[length
] = '\0';
451 ERR_raise_data(ERR_LIB_DSO
, DSO_R_FAILURE
,
452 "filespec \"%s\", default \"%s\": %s",
453 filespec1
, filespec2
, errstring
);
458 merged
= OPENSSL_malloc(nam
.NAMX_ESL
+ 1);
461 strncpy(merged
, nam
.NAMX_ESA
, nam
.NAMX_ESL
);
462 merged
[nam
.NAMX_ESL
] = '\0';
466 static char *vms_name_converter(DSO
*dso
, const char *filename
)
472 len
= strlen(filename
);
474 p
= strchr(filename
, ':');
479 transform
= (strrchr(p
, '>') == NULL
&& strrchr(p
, ']') == NULL
);
483 int rsize
= len
+ sizeof(DSO_EXTENSION
);
485 if ((translated
= OPENSSL_malloc(rsize
)) != NULL
) {
486 p
= strrchr(filename
, ';');
489 strncpy(translated
, filename
, len
);
490 translated
[len
] = '\0';
491 strcat(translated
, DSO_EXTENSION
);
493 strcat(translated
, p
);
496 translated
= OPENSSL_strdup(filename
);
501 #endif /* OPENSSL_SYS_VMS */