4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Joyent, Inc. All rights reserved.
26 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Gary Mills
28 * Copyright (c) 2016 Andrey Sokolov
32 * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
33 * associations, and display status. All the ioctls are private between
34 * lofi and lofiadm, and so are very simple - device information is
35 * communicated via a minor number.
38 #include <sys/types.h>
39 #include <sys/param.h>
42 #include <sys/sysmacros.h>
43 #include <sys/modctl.h>
44 #include <netinet/in.h>
54 #include <libdevinfo.h>
59 #include <security/cryptoki.h>
60 #include <cryptoutil.h>
61 #include <sys/crypto/ioctl.h>
62 #include <sys/crypto/ioctladmin.h>
64 #include <sys/mkdev.h>
68 /* Only need the IV len #defines out of these files, nothing else. */
69 #include <aes/aes_impl.h>
70 #include <des/des_impl.h>
71 #include <blowfish/blowfish_impl.h>
73 static const char USAGE
[] =
74 "Usage: %s [-r] [-l] -a file [ device ]\n"
75 " %s [-r] -c crypto_algorithm -a file [device]\n"
76 " %s [-r] -c crypto_algorithm -k raw_key_file -a file [device]\n"
77 " %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
79 " %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
80 "-k wrapped_key_file -a file [device]\n"
81 " %s [-r] -c crypto_algorithm -e -a file [device]\n"
82 " %s -d file | device\n"
83 " %s -C [gzip|gzip-6|gzip-9|lzma] [-s segment_size] file\n"
85 " %s [ file | device ]\n";
87 typedef struct token_spec
{
94 typedef struct mech_alias
{
96 CK_MECHANISM_TYPE type
;
97 char *name
; /* for ioctl */
98 char *iv_name
; /* for ioctl */
99 size_t iv_len
; /* for ioctl */
100 iv_method_t iv_type
; /* for ioctl */
101 size_t min_keysize
; /* in bytes */
102 size_t max_keysize
; /* in bytes */
107 static mech_alias_t mech_aliases
[] = {
108 /* Preferred one should always be listed first. */
109 { "aes-256-cbc", CKM_AES_CBC
, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN
,
110 IVM_ENC_BLKNO
, ULONG_MAX
, 0L, NULL
, (CK_SLOT_ID
) -1 },
111 { "aes-192-cbc", CKM_AES_CBC
, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN
,
112 IVM_ENC_BLKNO
, ULONG_MAX
, 0L, NULL
, (CK_SLOT_ID
) -1 },
113 { "aes-128-cbc", CKM_AES_CBC
, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN
,
114 IVM_ENC_BLKNO
, ULONG_MAX
, 0L, NULL
, (CK_SLOT_ID
) -1 },
115 { "des3-cbc", CKM_DES3_CBC
, "CKM_DES3_CBC", "CKM_DES3_ECB", DES_IV_LEN
,
116 IVM_ENC_BLKNO
, ULONG_MAX
, 0L, NULL
, (CK_SLOT_ID
)-1 },
117 { "blowfish-cbc", CKM_BLOWFISH_CBC
, "CKM_BLOWFISH_CBC",
118 "CKM_BLOWFISH_ECB", BLOWFISH_IV_LEN
, IVM_ENC_BLKNO
, ULONG_MAX
,
119 0L, NULL
, (CK_SLOT_ID
)-1 }
121 * A cipher without an iv requirement would look like this:
122 * { "aes-xex", CKM_AES_XEX, "CKM_AES_XEX", NULL, 0,
123 * IVM_NONE, ULONG_MAX, 0L, NULL, (CK_SLOT_ID)-1 }
127 int mech_aliases_count
= (sizeof (mech_aliases
) / sizeof (mech_alias_t
));
129 /* Preferred cipher, if one isn't specified on command line. */
130 #define DEFAULT_CIPHER (&mech_aliases[0])
132 #define DEFAULT_CIPHER_NUM 64 /* guess # kernel ciphers available */
133 #define DEFAULT_MECHINFO_NUM 16 /* guess # kernel mechs available */
134 #define MIN_PASSLEN 8 /* min acceptable passphrase size */
136 static int gzip_compress(void *src
, size_t srclen
, void *dst
,
137 size_t *destlen
, int level
);
138 static int lzma_compress(void *src
, size_t srclen
, void *dst
,
139 size_t *destlen
, int level
);
141 lofi_compress_info_t lofi_compress_table
[LOFI_COMPRESS_FUNCTIONS
] = {
142 {NULL
, gzip_compress
, 6, "gzip"}, /* default */
143 {NULL
, gzip_compress
, 6, "gzip-6"},
144 {NULL
, gzip_compress
, 9, "gzip-9"},
145 {NULL
, lzma_compress
, 0, "lzma"}
148 /* For displaying lofi mappings */
149 #define FORMAT "%-20s %-30s %s\n"
151 #define COMPRESS_ALGORITHM "gzip"
152 #define COMPRESS_THRESHOLD 2048
153 #define SEGSIZE 131072
154 #define BLOCK_SIZE 512
155 #define KILOBYTE 1024
156 #define MEGABYTE (KILOBYTE * KILOBYTE)
157 #define GIGABYTE (KILOBYTE * MEGABYTE)
158 #define LIBZ "libz.so.1"
160 const char lofi_crypto_magic
[6] = LOFI_CRYPTO_MAGIC
;
163 usage(const char *pname
)
165 (void) fprintf(stderr
, gettext(USAGE
), pname
, pname
, pname
,
166 pname
, pname
, pname
, pname
, pname
, pname
, pname
);
171 gzip_compress(void *src
, size_t srclen
, void *dst
, size_t *dstlen
, int level
)
173 static int (*compress2p
)(void *, ulong_t
*, void *, size_t, int) = NULL
;
174 void *libz_hdl
= NULL
;
177 * The first time we are called, attempt to dlopen()
178 * libz.so.1 and get a pointer to the compress2() function
180 if (compress2p
== NULL
) {
181 if ((libz_hdl
= openlib(LIBZ
)) == NULL
)
182 die(gettext("could not find %s. "
183 "gzip compression unavailable\n"), LIBZ
);
186 (int (*)(void *, ulong_t
*, void *, size_t, int))
187 dlsym(libz_hdl
, "compress2")) == NULL
) {
189 die(gettext("could not find the correct %s. "
190 "gzip compression unavailable\n"), LIBZ
);
194 if ((*compress2p
)(dst
, (ulong_t
*)dstlen
, src
, srclen
, level
) != 0)
201 *SzAlloc(void *p
, size_t size
)
203 return (malloc(size
));
208 SzFree(void *p
, void *address
, size_t size
)
213 static ISzAlloc g_Alloc
= {
218 #define LZMA_UNCOMPRESSED_SIZE 8
219 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + LZMA_UNCOMPRESSED_SIZE)
223 lzma_compress(void *src
, size_t srclen
, void *dst
,
224 size_t *dstlen
, int level
)
228 size_t outsizeprocessed
;
229 size_t outpropssize
= LZMA_PROPS_SIZE
;
237 LzmaEncProps_Init(&props
);
240 * The LZMA compressed file format is as follows -
242 * Offset Size(bytes) Description
243 * 0 1 LZMA properties (lc, lp, lp (encoded))
244 * 1 4 Dictionary size (little endian)
245 * 5 8 Uncompressed size (little endian)
249 /* set the dictionary size to be 8MB */
250 props
.dictSize
= 1 << 23;
252 if (*dstlen
< LZMA_HEADER_SIZE
)
253 return (SZ_ERROR_OUTPUT_EOF
);
258 * Set the uncompressed size in the LZMA header
259 * The LZMA properties (specified in 'props')
260 * will be set by the call to LzmaEncode()
262 for (i
= 0; i
< LZMA_UNCOMPRESSED_SIZE
; i
++, t
>>= 8) {
263 dstp
[LZMA_PROPS_SIZE
+ i
] = (Byte
)t
;
266 outsizeprocessed
= outsize2
- LZMA_HEADER_SIZE
;
267 res
= LzmaEncode(dstp
+ LZMA_HEADER_SIZE
, &outsizeprocessed
,
268 src
, srclen
, &props
, dstp
, &outpropssize
, 0, NULL
,
274 *dstlen
= outsizeprocessed
+ LZMA_HEADER_SIZE
;
279 * Translate a lofi device name to a minor number. We might be asked
280 * to do this when there is no association (such as when the user specifies
281 * a particular device), so we can only look at the string.
284 name_to_minor(const char *devicename
)
289 * If devicename does not exist, then devicename contains
290 * the name of the device to be created.
291 * Note we only allow non-labeled devices here.
293 if (stat(devicename
, &st
)) {
296 rv
= sscanf(devicename
, "/dev/" LOFI_BLOCK_NAME
"/%d", &minor
);
299 rv
= sscanf(devicename
, "/dev/" LOFI_CHAR_NAME
"/%d", &minor
);
307 * For disk devices we use modctl(MODGETNAME) to read driver name
310 if (st
.st_mode
& S_IFCHR
|| st
.st_mode
& S_IFBLK
) {
312 char mname
[MODMAXNAMELEN
];
314 maj
= major(st
.st_rdev
);
316 if (modctl(MODGETNAME
, mname
, MODMAXNAMELEN
, &maj
) == 0) {
317 if (strncmp(mname
, LOFI_DRIVER_NAME
,
318 sizeof (LOFI_DRIVER_NAME
)) == 0) {
319 return (LOFI_MINOR2ID(minor(st
.st_rdev
)));
328 * This might be the first time we've used this minor number. If so,
329 * it might also be that the /dev links are in the process of being created
330 * by devfsadmd (or that they'll be created "soon"). We cannot return
331 * until they're there or the invoker of lofiadm might try to use them
332 * and not find them. This can happen if a shell script is running on
335 static int sleeptime
= 2; /* number of seconds to sleep between stat's */
336 static int maxsleep
= 120; /* maximum number of seconds to sleep */
339 make_blkdevname(struct lofi_ioctl
*li
, char *path
, size_t len
)
344 if (li
->li_devpath
[0] == '\0') {
346 (void) strlcpy(path
, "unknown", len
);
348 (void) snprintf(path
, len
,
349 "/dev/" LOFI_BLOCK_NAME
"/%d", li
->li_id
);
352 (void) strlcpy(path
, li
->li_devpath
, len
);
353 r1
= strchr(path
, 'r');
355 r2
= strchr(li
->li_devpath
, 'r');
356 (void) strlcpy(r1
, r2
+1, len
- l1
);
358 if (li
->li_labeled
) {
359 (void) strlcat(path
, "p0", len
);
364 wait_until_dev_complete(struct lofi_ioctl
*li
)
368 char blkpath
[MAXPATHLEN
];
369 char charpath
[MAXPATHLEN
];
370 di_devlink_handle_t hdl
;
372 make_blkdevname(li
, blkpath
, sizeof (blkpath
));
373 (void) strlcpy(charpath
, li
->li_devpath
, sizeof (charpath
));
375 if (li
->li_labeled
) {
376 (void) strlcat(charpath
, "p0", sizeof (charpath
));
379 /* Check if links already present */
380 if (stat64(blkpath
, &buf
) == 0 && stat64(charpath
, &buf
) == 0)
383 /* First use di_devlink_init() */
384 if (hdl
= di_devlink_init("lofi", DI_MAKE_LINK
)) {
385 (void) di_devlink_fini(&hdl
);
390 * Under normal conditions, di_devlink_init(DI_MAKE_LINK) above will
391 * only fail if the caller is non-root. In that case, wait for
392 * link creation via sysevents.
394 for (cursleep
= 0; cursleep
< maxsleep
; cursleep
+= sleeptime
) {
395 if (stat64(blkpath
, &buf
) == 0 && stat64(charpath
, &buf
) == 0)
397 (void) sleep(sleeptime
);
402 if (stat64(blkpath
, &buf
) == -1) {
403 die(gettext("%s was not created"), blkpath
);
405 if (stat64(charpath
, &buf
) == -1) {
406 die(gettext("%s was not created"), charpath
);
411 * Map the file and return the minor number the driver picked for the file
412 * DO NOT use this function if the filename is actually the device name.
415 lofi_map_file(int lfd
, struct lofi_ioctl
*li
, const char *filename
)
420 (void) strlcpy(li
->li_filename
, filename
, sizeof (li
->li_filename
));
421 minor
= ioctl(lfd
, LOFI_MAP_FILE
, li
);
423 if (errno
== ENOTSUP
)
424 warn(gettext("encrypting compressed files is "
426 die(gettext("could not map file %s"), filename
);
428 wait_until_dev_complete(li
);
433 * Add a device association. If devicename is NULL, let the driver
437 add_mapping(int lfd
, const char *devicename
, const char *filename
,
438 mech_alias_t
*cipher
, const char *rkey
, size_t rksz
, boolean_t rdonly
,
441 struct lofi_ioctl li
;
443 bzero(&li
, sizeof (li
));
444 li
.li_readonly
= rdonly
;
445 li
.li_labeled
= label
;
447 li
.li_crypto_enabled
= B_FALSE
;
448 if (cipher
!= NULL
) {
449 /* set up encryption for mapped file */
450 li
.li_crypto_enabled
= B_TRUE
;
451 (void) strlcpy(li
.li_cipher
, cipher
->name
,
452 sizeof (li
.li_cipher
));
453 if (rksz
> sizeof (li
.li_key
)) {
454 die(gettext("key too large"));
456 bcopy(rkey
, li
.li_key
, rksz
);
457 li
.li_key_len
= rksz
<< 3; /* convert to bits */
459 li
.li_iv_type
= cipher
->iv_type
;
460 li
.li_iv_len
= cipher
->iv_len
; /* 0 when no iv needed */
461 switch (cipher
->iv_type
) {
463 (void) strlcpy(li
.li_iv_cipher
, cipher
->iv_name
,
464 sizeof (li
.li_iv_cipher
));
473 if (devicename
== NULL
) {
475 char path
[MAXPATHLEN
];
477 /* pick one via the driver */
478 minor
= lofi_map_file(lfd
, &li
, filename
);
480 make_blkdevname(&li
, path
, sizeof (path
));
482 /* if mapping succeeds, print the one picked */
483 (void) printf("%s\n", path
);
488 /* use device we were given */
489 li
.li_id
= name_to_minor(devicename
);
491 die(gettext("malformed device name %s\n"), devicename
);
493 (void) strlcpy(li
.li_filename
, filename
, sizeof (li
.li_filename
));
495 /* if device is already in use li.li_minor won't change */
496 if (ioctl(lfd
, LOFI_MAP_FILE_MINOR
, &li
) == -1) {
497 if (errno
== ENOTSUP
)
498 warn(gettext("encrypting compressed files is "
500 die(gettext("could not map file %s to %s"), filename
,
503 wait_until_dev_complete(&li
);
507 * Remove an association. Delete by device name if non-NULL, or by
508 * filename otherwise.
511 delete_mapping(int lfd
, const char *devicename
, const char *filename
,
514 struct lofi_ioctl li
;
517 li
.li_cleanup
= B_FALSE
;
519 if (devicename
== NULL
) {
520 /* delete by filename */
521 (void) strlcpy(li
.li_filename
, filename
,
522 sizeof (li
.li_filename
));
524 if (ioctl(lfd
, LOFI_UNMAP_FILE
, &li
) == -1) {
525 die(gettext("could not unmap file %s"), filename
);
530 /* delete by device */
531 li
.li_id
= name_to_minor(devicename
);
533 die(gettext("malformed device name %s\n"), devicename
);
535 if (ioctl(lfd
, LOFI_UNMAP_FILE_MINOR
, &li
) == -1) {
536 die(gettext("could not unmap device %s"), devicename
);
541 * Show filename given devicename, or devicename given filename.
544 print_one_mapping(int lfd
, const char *devicename
, const char *filename
)
546 struct lofi_ioctl li
;
547 char blkpath
[MAXPATHLEN
];
549 if (devicename
== NULL
) {
550 /* given filename, print devicename */
552 (void) strlcpy(li
.li_filename
, filename
,
553 sizeof (li
.li_filename
));
554 if (ioctl(lfd
, LOFI_GET_MINOR
, &li
) == -1) {
555 die(gettext("could not find device for %s"), filename
);
557 make_blkdevname(&li
, blkpath
, sizeof (blkpath
));
558 (void) printf("%s\n", blkpath
);
562 /* given devicename, print filename */
563 li
.li_id
= name_to_minor(devicename
);
565 die(gettext("malformed device name %s\n"), devicename
);
567 if (ioctl(lfd
, LOFI_GET_FILENAME
, &li
) == -1) {
568 die(gettext("could not find filename for %s"), devicename
);
570 (void) printf("%s\n", li
.li_filename
);
574 * Print the list of all the mappings, including a header.
577 print_mappings(int fd
)
579 struct lofi_ioctl li
;
582 char path
[MAXPATHLEN
];
583 char options
[MAXPATHLEN
] = { 0 };
586 if (ioctl(fd
, LOFI_GET_MAXMINOR
, &li
) == -1) {
591 (void) printf(FORMAT
, gettext("Block Device"), gettext("File"),
593 for (minor
= 1; minor
<= maxminor
; minor
++) {
595 if (ioctl(fd
, LOFI_GET_FILENAME
, &li
) == -1) {
601 make_blkdevname(&li
, path
, sizeof (path
));
606 * Encrypted lofi and compressed lofi are mutually exclusive.
608 if (li
.li_crypto_enabled
)
609 (void) snprintf(options
, sizeof (options
),
610 gettext("Encrypted"));
611 else if (li
.li_algorithm
[0] != '\0')
612 (void) snprintf(options
, sizeof (options
),
613 gettext("Compressed(%s)"), li
.li_algorithm
);
614 if (li
.li_readonly
) {
615 if (strlen(options
) != 0) {
616 (void) strlcat(options
, ",Readonly",
619 (void) snprintf(options
, sizeof (options
),
620 gettext("Readonly"));
624 if (strlen(options
) != 0) {
625 (void) strlcat(options
, ",Labeled",
628 (void) snprintf(options
, sizeof (options
),
632 if (strlen(options
) == 0)
633 (void) snprintf(options
, sizeof (options
), "-");
635 (void) printf(FORMAT
, path
, li
.li_filename
, options
);
640 * Verify the cipher selected by user.
642 static mech_alias_t
*
643 ciph2mech(const char *alias
)
647 for (i
= 0; i
< mech_aliases_count
; i
++) {
648 if (strcasecmp(alias
, mech_aliases
[i
].alias
) == 0)
649 return (&mech_aliases
[i
]);
655 * Verify user selected cipher is also available in kernel.
657 * While traversing kernel list of mechs, if the cipher is supported in the
658 * kernel for both encryption and decryption, it also picks up the min/max
662 kernel_cipher_check(mech_alias_t
*cipher
)
664 boolean_t ciph_ok
= B_FALSE
;
665 boolean_t iv_ok
= B_FALSE
;
668 crypto_get_mechanism_list_t
*kciphers
= NULL
;
669 crypto_get_all_mechanism_info_t
*kinfo
= NULL
;
674 /* if cipher doesn't need iv generating mech, bypass that check now */
675 if (cipher
->iv_name
== NULL
)
678 /* allocate some space for the list of kernel ciphers */
679 count
= DEFAULT_CIPHER_NUM
;
680 kciphers
= malloc(sizeof (crypto_get_mechanism_list_t
) +
681 sizeof (crypto_mech_name_t
) * (count
- 1));
682 if (kciphers
== NULL
)
683 die(gettext("failed to allocate memory for list of "
684 "kernel mechanisms"));
685 kciphers
->ml_count
= count
;
687 /* query crypto device to get list of kernel ciphers */
688 if ((fd
= open("/dev/crypto", O_RDWR
)) == -1) {
689 warn(gettext("failed to open %s"), "/dev/crypto");
693 if (ioctl(fd
, CRYPTO_GET_MECHANISM_LIST
, kciphers
) == -1) {
694 warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
698 if (kciphers
->ml_return_value
== CRYPTO_BUFFER_TOO_SMALL
) {
699 count
= kciphers
->ml_count
;
701 kciphers
= malloc(sizeof (crypto_get_mechanism_list_t
) +
702 sizeof (crypto_mech_name_t
) * (count
- 1));
703 if (kciphers
== NULL
) {
704 warn(gettext("failed to allocate memory for list of "
705 "kernel mechanisms"));
708 kciphers
->ml_count
= count
;
710 if (ioctl(fd
, CRYPTO_GET_MECHANISM_LIST
, kciphers
) == -1) {
711 warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
716 if (kciphers
->ml_return_value
!= CRYPTO_SUCCESS
) {
718 "CRYPTO_GET_MECHANISM_LIST ioctl return value = %d\n"),
719 kciphers
->ml_return_value
);
724 * scan list of kernel ciphers looking for the selected one and if
725 * it needs an iv generated using another cipher, also look for that
726 * additional cipher to be used for generating the iv
728 count
= kciphers
->ml_count
;
729 for (i
= 0; i
< count
&& !(ciph_ok
&& iv_ok
); i
++) {
731 strcasecmp(cipher
->name
, kciphers
->ml_list
[i
]) == 0)
734 strcasecmp(cipher
->iv_name
, kciphers
->ml_list
[i
]) == 0)
741 warn(gettext("%s mechanism not supported in kernel\n"),
744 warn(gettext("%s mechanism not supported in kernel\n"),
748 /* Get the details about the user selected cipher */
749 count
= DEFAULT_MECHINFO_NUM
;
750 kinfo
= malloc(sizeof (crypto_get_all_mechanism_info_t
) +
751 sizeof (crypto_mechanism_info_t
) * (count
- 1));
753 warn(gettext("failed to allocate memory for "
754 "kernel mechanism info"));
757 kinfo
->mi_count
= count
;
758 (void) strlcpy(kinfo
->mi_mechanism_name
, cipher
->name
,
759 CRYPTO_MAX_MECH_NAME
);
761 if (ioctl(fd
, CRYPTO_GET_ALL_MECHANISM_INFO
, kinfo
) == -1) {
763 "CRYPTO_GET_ALL_MECHANISM_INFO ioctl failed"));
767 if (kinfo
->mi_return_value
== CRYPTO_BUFFER_TOO_SMALL
) {
768 count
= kinfo
->mi_count
;
771 sizeof (crypto_get_all_mechanism_info_t
) +
772 sizeof (crypto_mechanism_info_t
) * (count
- 1));
774 warn(gettext("failed to allocate memory for "
775 "kernel mechanism info"));
778 kinfo
->mi_count
= count
;
779 (void) strlcpy(kinfo
->mi_mechanism_name
, cipher
->name
,
780 CRYPTO_MAX_MECH_NAME
);
782 if (ioctl(fd
, CRYPTO_GET_ALL_MECHANISM_INFO
, kinfo
) ==
784 warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO "
790 if (kinfo
->mi_return_value
!= CRYPTO_SUCCESS
) {
791 warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO ioctl "
792 "return value = %d\n"), kinfo
->mi_return_value
);
796 /* Set key min and max size */
797 count
= kinfo
->mi_count
;
800 keymin
= kinfo
->mi_list
[i
].mi_min_key_size
;
801 keymax
= kinfo
->mi_list
[i
].mi_max_key_size
;
802 if (kinfo
->mi_list
[i
].mi_keysize_unit
&
803 CRYPTO_KEYSIZE_UNIT_IN_BITS
) {
804 keymin
= CRYPTO_BITS2BYTES(keymin
);
805 keymax
= CRYPTO_BITS2BYTES(keymax
);
808 cipher
->min_keysize
= keymin
;
809 cipher
->max_keysize
= keymax
;
817 "failed to find usable %s kernel mechanism, "
818 "use \"cryptoadm list -m\" to find available "
824 /* Note: key min/max, unit size, usage for iv cipher are not checked. */
826 return (ciph_ok
&& iv_ok
);
831 if (kciphers
!= NULL
)
839 * Break up token spec into its components (non-destructive)
841 static token_spec_t
*
842 parsetoken(char *spec
)
849 #define nullfield(i) ((field[(i)+1] - field[(i)]) <= 1)
850 #define copyfield(fld, i) \
854 if ((n = (field[(i)+1] - field[(i)])) > 1) { \
855 if (((fld) = malloc(n)) != NULL) { \
856 (void) strncpy((fld), field[(i)], n); \
857 ((fld))[n - 1] = '\0'; \
863 char *field
[NFIELDS
+ 1]; /* +1 to catch extra delimiters */
864 token_spec_t
*ti
= NULL
;
870 * Correct format is "[name]:[manuf]:[serial]:key". Can't use
871 * strtok because it treats ":::key" and "key:::" and "key" all
872 * as the same thing, and we can't have the :s compressed away.
875 for (i
= 1; i
< NFIELDS
+ 1; i
++) {
876 field
[i
] = strchr(field
[i
-1], ':');
877 if (field
[i
] == NULL
)
881 if (i
< NFIELDS
) /* not enough fields */
883 if (field
[NFIELDS
] != NULL
) /* too many fields */
885 field
[NFIELDS
] = strchr(field
[NFIELDS
-1], '\0') + 1;
887 /* key label can't be empty */
888 if (nullfield(FLD_LABEL
))
891 ti
= malloc(sizeof (token_spec_t
));
895 copyfield(ti
->name
, FLD_NAME
);
896 copyfield(ti
->mfr
, FLD_MANUF
);
897 copyfield(ti
->serno
, FLD_SERIAL
);
898 copyfield(ti
->key
, FLD_LABEL
);
901 * If token specified and it only contains a key label, then
902 * search all tokens for the key, otherwise only those with
903 * matching name, mfr, and serno are used.
906 * That's how we'd like it to be, however, if only the key label
907 * is specified, default to using softtoken. It's easier.
909 if (ti
->name
== NULL
&& ti
->mfr
== NULL
&& ti
->serno
== NULL
)
910 ti
->name
= strdup(pkcs11_default_token());
915 * PBE the passphrase into a raw key
918 getkeyfromuser(mech_alias_t
*cipher
, char **raw_key
, size_t *raw_key_sz
,
919 boolean_t with_confirmation
)
921 CK_SESSION_HANDLE sess
;
925 void *salt
= NULL
; /* don't use NULL, see note on salt below */
931 /* did init_crypto find a slot that supports this cipher? */
932 if (cipher
->slot
== (CK_SLOT_ID
)-1 || cipher
->max_keysize
== 0) {
933 rv
= CKR_MECHANISM_INVALID
;
937 rv
= pkcs11_mech2keytype(cipher
->type
, &ktype
);
942 * use the passphrase to generate a PBE PKCS#5 secret key and
943 * retrieve the raw key data to eventually pass it to the kernel;
945 rv
= C_OpenSession(cipher
->slot
, CKF_SERIAL_SESSION
, NULL
, NULL
, &sess
);
949 /* get user passphrase with 8 byte minimum */
950 if (pkcs11_get_pass(NULL
, &pass
, &passlen
, MIN_PASSLEN
,
951 with_confirmation
) < 0) {
952 die(gettext("passphrases do not match\n"));
956 * salt should not be NULL, or else pkcs11_PasswdToKey() will
957 * complain about CKR_MECHANISM_PARAM_INVALID; the following is
958 * to make up for not having a salt until a proper one is used
963 klen
= cipher
->max_keysize
;
964 rv
= pkcs11_PasswdToKey(sess
, pass
, passlen
, salt
, saltlen
, ktype
,
965 cipher
->max_keysize
, &kvalue
, &klen
);
967 (void) C_CloseSession(sess
);
973 /* assert(klen == cipher->max_keysize); */
975 *raw_key
= (char *)kvalue
;
979 die(gettext("failed to generate %s key from passphrase: %s"),
980 cipher
->alias
, pkcs11_strerror(rv
));
984 * Read raw key from file; also handles ephemeral keys.
987 getkeyfromfile(const char *pathname
, mech_alias_t
*cipher
, char **key
,
992 boolean_t notplain
= B_FALSE
;
996 /* ephemeral keys are just random data */
997 if (pathname
== NULL
) {
998 *ksz
= cipher
->max_keysize
;
1001 die(gettext("failed to allocate memory for"
1003 if (pkcs11_get_urandom(*key
, *ksz
) < 0) {
1005 die(gettext("failed to get enough random data"));
1011 * If the remaining section of code didn't also check for secure keyfile
1012 * permissions and whether the key is within cipher min and max lengths,
1013 * (or, if those things moved out of this block), we could have had:
1014 * if (pkcs11_read_data(pathname, key, ksz) < 0)
1018 if ((fd
= open(pathname
, O_RDONLY
, 0)) == -1)
1019 die(gettext("open of keyfile (%s) failed"), pathname
);
1021 if (fstat(fd
, &sbuf
) == -1)
1022 die(gettext("fstat of keyfile (%s) failed"), pathname
);
1024 if (S_ISREG(sbuf
.st_mode
)) {
1025 if ((sbuf
.st_mode
& (S_IWGRP
| S_IWOTH
)) != 0)
1026 die(gettext("insecure permissions on keyfile %s\n"),
1029 *ksz
= sbuf
.st_size
;
1030 if (*ksz
< cipher
->min_keysize
|| cipher
->max_keysize
< *ksz
) {
1031 warn(gettext("%s: invalid keysize: %d\n"),
1032 pathname
, (int)*ksz
);
1033 die(gettext("\t%d <= keysize <= %d\n"),
1034 cipher
->min_keysize
, cipher
->max_keysize
);
1037 *ksz
= cipher
->max_keysize
;
1041 *key
= malloc(*ksz
);
1043 die(gettext("failed to allocate memory for key from file"));
1045 for (cursz
= 0, nread
= 0; cursz
< *ksz
; cursz
+= nread
) {
1046 nread
= read(fd
, *key
, *ksz
);
1050 * nread == 0. If it's not a regular file we were trying to
1051 * get the maximum keysize of data possible for this cipher.
1052 * But if we've got at least the minimum keysize of data,
1053 * round down to the nearest keysize unit and call it good.
1054 * If we haven't met the minimum keysize, that's an error.
1055 * If it's a regular file, nread = 0 is also an error.
1057 if (nread
== 0 && notplain
&& cursz
>= cipher
->min_keysize
) {
1058 *ksz
= (cursz
/ cipher
->min_keysize
) *
1059 cipher
->min_keysize
;
1062 die(gettext("%s: can't read all keybytes"), pathname
);
1068 * Read the raw key from token, or from a file that was wrapped with a
1072 getkeyfromtoken(CK_SESSION_HANDLE sess
,
1073 token_spec_t
*token
, const char *keyfile
, mech_alias_t
*cipher
,
1074 char **raw_key
, size_t *raw_key_sz
)
1077 CK_BBOOL trueval
= B_TRUE
;
1078 CK_OBJECT_CLASS kclass
; /* secret key or RSA private key */
1079 CK_KEY_TYPE ktype
; /* from selected cipher or CKK_RSA */
1080 CK_KEY_TYPE raw_ktype
; /* from selected cipher */
1081 CK_ATTRIBUTE key_tmpl
[] = {
1082 { CKA_CLASS
, NULL
, 0 }, /* re-used for token key and unwrap */
1083 { CKA_KEY_TYPE
, NULL
, 0 }, /* ditto */
1084 { CKA_LABEL
, NULL
, 0 },
1085 { CKA_TOKEN
, NULL
, 0 },
1086 { CKA_PRIVATE
, NULL
, 0 }
1088 CK_ULONG attrs
= sizeof (key_tmpl
) / sizeof (CK_ATTRIBUTE
);
1092 CK_OBJECT_HANDLE obj
, rawobj
;
1093 CK_ULONG num_objs
= 1; /* just want to find 1 token key */
1094 CK_MECHANISM unwrap
= { CKM_RSA_PKCS
, NULL
, 0 };
1098 if (token
== NULL
|| token
->key
== NULL
)
1101 /* did init_crypto find a slot that supports this cipher? */
1102 if (cipher
->slot
== (CK_SLOT_ID
)-1 || cipher
->max_keysize
== 0) {
1103 die(gettext("failed to find any cryptographic provider, "
1104 "use \"cryptoadm list -p\" to find providers: %s\n"),
1105 pkcs11_strerror(CKR_MECHANISM_INVALID
));
1108 if (pkcs11_get_pass(token
->name
, &pass
, &passlen
, 0, B_FALSE
) < 0)
1109 die(gettext("unable to get passphrase"));
1111 /* use passphrase to login to token */
1112 if (pass
!= NULL
&& passlen
> 0) {
1113 rv
= C_Login(sess
, CKU_USER
, (CK_UTF8CHAR_PTR
)pass
, passlen
);
1115 die(gettext("cannot login to the token %s: %s\n"),
1116 token
->name
, pkcs11_strerror(rv
));
1120 rv
= pkcs11_mech2keytype(cipher
->type
, &raw_ktype
);
1122 die(gettext("failed to get key type for cipher %s: %s\n"),
1123 cipher
->name
, pkcs11_strerror(rv
));
1127 * If no keyfile was given, then the token key is secret key to
1128 * be used for encryption/decryption. Otherwise, the keyfile
1129 * contains a wrapped secret key, and the token is actually the
1130 * unwrapping RSA private key.
1132 if (keyfile
== NULL
) {
1133 kclass
= CKO_SECRET_KEY
;
1136 kclass
= CKO_PRIVATE_KEY
;
1140 /* Find the key in the token first */
1141 for (i
= 0; i
< attrs
; i
++) {
1142 switch (key_tmpl
[i
].type
) {
1144 key_tmpl
[i
].pValue
= &kclass
;
1145 key_tmpl
[i
].ulValueLen
= sizeof (kclass
);
1148 key_tmpl
[i
].pValue
= &ktype
;
1149 key_tmpl
[i
].ulValueLen
= sizeof (ktype
);
1152 key_tmpl
[i
].pValue
= token
->key
;
1153 key_tmpl
[i
].ulValueLen
= strlen(token
->key
);
1156 key_tmpl
[i
].pValue
= &trueval
;
1157 key_tmpl
[i
].ulValueLen
= sizeof (trueval
);
1160 key_tmpl
[i
].pValue
= &trueval
;
1161 key_tmpl
[i
].ulValueLen
= sizeof (trueval
);
1167 rv
= C_FindObjectsInit(sess
, key_tmpl
, attrs
);
1169 die(gettext("cannot find key %s: %s\n"), token
->key
,
1170 pkcs11_strerror(rv
));
1171 rv
= C_FindObjects(sess
, &obj
, 1, &num_objs
);
1172 (void) C_FindObjectsFinal(sess
);
1174 if (num_objs
== 0) {
1175 die(gettext("cannot find key %s\n"), token
->key
);
1176 } else if (rv
!= CKR_OK
) {
1177 die(gettext("cannot find key %s: %s\n"), token
->key
,
1178 pkcs11_strerror(rv
));
1182 * No keyfile means when token key is found, convert it to raw key,
1183 * and done. Otherwise still need do an unwrap to create yet another
1184 * obj and that needs to be converted to raw key before we're done.
1186 if (keyfile
== NULL
) {
1187 /* obj contains raw key, extract it */
1188 rv
= pkcs11_ObjectToKey(sess
, obj
, (void **)&rkey
, &rksz
,
1191 die(gettext("failed to get key value for %s"
1192 " from token %s, %s\n"), token
->key
,
1193 token
->name
, pkcs11_strerror(rv
));
1196 getkeyfromfile(keyfile
, cipher
, &rkey
, &rksz
);
1199 * Got the wrapping RSA obj and the wrapped key from file.
1200 * Unwrap the key from file with RSA obj to get rawkey obj.
1203 /* re-use the first two attributes of key_tmpl */
1204 kclass
= CKO_SECRET_KEY
;
1207 rv
= C_UnwrapKey(sess
, &unwrap
, obj
, (CK_BYTE_PTR
)rkey
,
1208 rksz
, key_tmpl
, 2, &rawobj
);
1210 die(gettext("failed to unwrap key in keyfile %s,"
1211 " %s\n"), keyfile
, pkcs11_strerror(rv
));
1213 /* rawobj contains raw key, extract it */
1214 rv
= pkcs11_ObjectToKey(sess
, rawobj
, (void **)&rkey
, &rksz
,
1217 die(gettext("failed to get unwrapped key value for"
1218 " key in keyfile %s, %s\n"), keyfile
,
1219 pkcs11_strerror(rv
));
1223 /* validate raw key size */
1224 if (rksz
< cipher
->min_keysize
|| cipher
->max_keysize
< rksz
) {
1225 warn(gettext("%s: invalid keysize: %d\n"), keyfile
, (int)rksz
);
1226 die(gettext("\t%d <= keysize <= %d\n"), cipher
->min_keysize
,
1227 cipher
->max_keysize
);
1231 *raw_key
= (char *)rkey
;
1235 * Set up cipher key limits and verify PKCS#11 can be done
1236 * match_token_cipher is the function pointer used by
1237 * pkcs11_GetCriteriaSession() init_crypto.
1240 match_token_cipher(CK_SLOT_ID slot_id
, void *args
, CK_RV
*rv
)
1242 token_spec_t
*token
;
1243 mech_alias_t
*cipher
;
1244 CK_TOKEN_INFO tokinfo
;
1245 CK_MECHANISM_INFO mechinfo
;
1246 boolean_t token_match
;
1249 * While traversing slot list, pick up the following info per slot:
1250 * - if token specified, whether it matches this slot's token info
1251 * - if the slot supports the PKCS#5 PBKD2 cipher
1253 * If the user said on the command line
1254 * -T tok:mfr:ser:lab -k keyfile
1255 * -c cipher -T tok:mfr:ser:lab -k keyfile
1256 * the given cipher or the default cipher apply to keyfile,
1257 * If the user said instead
1258 * -T tok:mfr:ser:lab
1259 * -c cipher -T tok:mfr:ser:lab
1260 * the key named "lab" may or may not agree with the given
1261 * cipher or the default cipher. In those cases, cipher will
1262 * be overridden with the actual cipher type of the key "lab".
1264 *rv
= CKR_FUNCTION_FAILED
;
1270 cipher
= (mech_alias_t
*)args
;
1271 token
= cipher
->token
;
1273 if (C_GetMechanismInfo(slot_id
, cipher
->type
, &mechinfo
) != CKR_OK
) {
1277 if (token
== NULL
) {
1278 if (C_GetMechanismInfo(slot_id
, CKM_PKCS5_PBKD2
, &mechinfo
) !=
1285 /* does the token match the token spec? */
1286 if (token
->key
== NULL
|| (C_GetTokenInfo(slot_id
, &tokinfo
) != CKR_OK
))
1289 token_match
= B_TRUE
;
1291 if (token
->name
!= NULL
&& (token
->name
)[0] != '\0' &&
1292 strncmp((char *)token
->name
, (char *)tokinfo
.label
,
1293 TOKEN_LABEL_SIZE
) != 0)
1294 token_match
= B_FALSE
;
1295 if (token
->mfr
!= NULL
&& (token
->mfr
)[0] != '\0' &&
1296 strncmp((char *)token
->mfr
, (char *)tokinfo
.manufacturerID
,
1297 TOKEN_MANUFACTURER_SIZE
) != 0)
1298 token_match
= B_FALSE
;
1299 if (token
->serno
!= NULL
&& (token
->serno
)[0] != '\0' &&
1300 strncmp((char *)token
->serno
, (char *)tokinfo
.serialNumber
,
1301 TOKEN_SERIAL_SIZE
) != 0)
1302 token_match
= B_FALSE
;
1308 cipher
->slot
= slot_id
;
1313 * Clean up crypto loose ends
1316 end_crypto(CK_SESSION_HANDLE sess
)
1318 (void) C_CloseSession(sess
);
1319 (void) C_Finalize(NULL
);
1323 * Set up crypto, opening session on slot that matches token and cipher
1326 init_crypto(token_spec_t
*token
, mech_alias_t
*cipher
,
1327 CK_SESSION_HANDLE_PTR sess
)
1331 cipher
->token
= token
;
1333 /* Turn off Metaslot so that we can see actual tokens */
1334 if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
1335 die(gettext("could not disable Metaslot"));
1338 rv
= pkcs11_GetCriteriaSession(match_token_cipher
, (void *)cipher
,
1342 if (rv
== CKR_HOST_MEMORY
) {
1345 die(gettext("failed to find any cryptographic provider, "
1346 "use \"cryptoadm list -p\" to find providers: %s\n"),
1347 pkcs11_strerror(rv
));
1352 * Uncompress a file.
1354 * First map the file in to establish a device
1355 * association, then read from it. On-the-fly
1356 * decompression will automatically uncompress
1357 * the file if it's compressed
1359 * If the file is mapped and a device association
1360 * has been established, disallow uncompressing
1361 * the file until it is unmapped.
1364 lofi_uncompress(int lfd
, const char *filename
)
1366 struct lofi_ioctl li
;
1368 char devicename
[32];
1369 char tmpfilename
[MAXPATHLEN
];
1374 struct stat64 statbuf
;
1380 * Disallow uncompressing the file if it is
1383 li
.li_crypto_enabled
= B_FALSE
;
1385 (void) strlcpy(li
.li_filename
, filename
, sizeof (li
.li_filename
));
1386 if (ioctl(lfd
, LOFI_GET_MINOR
, &li
) != -1)
1387 die(gettext("%s must be unmapped before uncompressing"),
1390 /* Zero length files don't need to be uncompressed */
1391 if (stat64(filename
, &statbuf
) == -1)
1392 die(gettext("stat: %s"), filename
);
1393 if (statbuf
.st_size
== 0)
1396 minor
= lofi_map_file(lfd
, &li
, filename
);
1397 (void) snprintf(devicename
, sizeof (devicename
), "/dev/%s/%d",
1398 LOFI_BLOCK_NAME
, minor
);
1400 /* If the file isn't compressed, we just return */
1401 if ((ioctl(lfd
, LOFI_CHECK_COMPRESSED
, &li
) == -1) ||
1402 (li
.li_algorithm
[0] == '\0')) {
1403 delete_mapping(lfd
, devicename
, filename
, B_TRUE
);
1404 die("%s is not compressed\n", filename
);
1407 if ((compfd
= open64(devicename
, O_RDONLY
| O_NONBLOCK
)) == -1) {
1408 delete_mapping(lfd
, devicename
, filename
, B_TRUE
);
1409 die(gettext("open: %s"), filename
);
1411 /* Create a temp file in the same directory */
1412 x
= strdup(filename
);
1413 dir
= strdup(dirname(x
));
1415 x
= strdup(filename
);
1416 file
= strdup(basename(x
));
1418 (void) snprintf(tmpfilename
, sizeof (tmpfilename
),
1419 "%s/.%sXXXXXX", dir
, file
);
1423 if ((uncompfd
= mkstemp64(tmpfilename
)) == -1) {
1424 (void) close(compfd
);
1425 delete_mapping(lfd
, devicename
, filename
, B_TRUE
);
1426 die("%s could not be uncompressed\n", filename
);
1430 * Set the mode bits and the owner of this temporary
1431 * file to be that of the original uncompressed file
1433 (void) fchmod(uncompfd
, statbuf
.st_mode
);
1435 if (fchown(uncompfd
, statbuf
.st_uid
, statbuf
.st_gid
) == -1) {
1436 (void) close(compfd
);
1437 (void) close(uncompfd
);
1438 delete_mapping(lfd
, devicename
, filename
, B_TRUE
);
1439 die("%s could not be uncompressed\n", filename
);
1442 /* Now read from the device in MAXBSIZE-sized chunks */
1444 rbytes
= read(compfd
, buf
, sizeof (buf
));
1449 if (write(uncompfd
, buf
, rbytes
) != rbytes
) {
1455 (void) close(compfd
);
1456 (void) close(uncompfd
);
1458 /* Delete the mapping */
1459 delete_mapping(lfd
, devicename
, filename
, B_TRUE
);
1462 * If an error occured while reading or writing, rbytes will
1466 (void) unlink(tmpfilename
);
1467 die(gettext("could not read from %s"), filename
);
1470 /* Rename the temp file to the actual file */
1471 if (rename(tmpfilename
, filename
) == -1)
1472 (void) unlink(tmpfilename
);
1479 lofi_compress(int *lfd
, const char *filename
, int compress_index
,
1482 struct lofi_ioctl lic
;
1483 lofi_compress_info_t
*li
;
1485 char tmpfilename
[MAXPATHLEN
];
1486 char comp_filename
[MAXPATHLEN
];
1487 char algorithm
[MAXALGLEN
];
1489 char *dir
= NULL
, *file
= NULL
;
1490 uchar_t
*uncompressed_seg
= NULL
;
1491 uchar_t
*compressed_seg
= NULL
;
1492 uint32_t compressed_segsize
;
1493 uint32_t len_compressed
, count
;
1494 uint32_t index_entries
, index_sz
;
1495 uint64_t *index
= NULL
;
1497 size_t real_segsize
;
1498 struct stat64 statbuf
;
1499 int compfd
= -1, uncompfd
= -1;
1501 ssize_t rbytes
, wbytes
, lastread
;
1505 * Disallow compressing the file if it is
1509 (void) strlcpy(lic
.li_filename
, filename
, sizeof (lic
.li_filename
));
1510 if (ioctl(*lfd
, LOFI_GET_MINOR
, &lic
) != -1)
1511 die(gettext("%s must be unmapped before compressing"),
1515 * Close the control device so other operations
1521 li
= &lofi_compress_table
[compress_index
];
1524 * The size of the buffer to hold compressed data must
1525 * be slightly larger than the compressed segment size.
1527 * The compress functions use part of the buffer as
1528 * scratch space to do calculations.
1529 * Ref: http://www.zlib.net/manual.html#compress2
1531 compressed_segsize
= segsize
+ (segsize
>> 6);
1532 compressed_seg
= (uchar_t
*)malloc(compressed_segsize
+ SEGHDR
);
1533 uncompressed_seg
= (uchar_t
*)malloc(segsize
);
1535 if (compressed_seg
== NULL
|| uncompressed_seg
== NULL
)
1536 die(gettext("No memory"));
1538 if ((uncompfd
= open64(filename
, O_RDWR
|O_LARGEFILE
, 0)) == -1)
1539 die(gettext("open: %s"), filename
);
1541 lock
.l_type
= F_WRLCK
;
1542 lock
.l_whence
= SEEK_SET
;
1547 * Use an advisory lock to ensure that only a
1548 * single lofiadm process compresses a given
1549 * file at any given time
1551 * A close on the file descriptor automatically
1552 * closes all lock state on the file
1554 if (fcntl(uncompfd
, F_SETLKW
, &lock
) == -1)
1555 die(gettext("fcntl: %s"), filename
);
1557 if (fstat64(uncompfd
, &statbuf
) == -1) {
1558 (void) close(uncompfd
);
1559 die(gettext("fstat: %s"), filename
);
1562 /* Zero length files don't need to be compressed */
1563 if (statbuf
.st_size
== 0) {
1564 (void) close(uncompfd
);
1569 * Create temporary files in the same directory that
1570 * will hold the intermediate data
1572 x
= strdup(filename
);
1573 dir
= strdup(dirname(x
));
1575 x
= strdup(filename
);
1576 file
= strdup(basename(x
));
1578 (void) snprintf(tmpfilename
, sizeof (tmpfilename
),
1579 "%s/.%sXXXXXX", dir
, file
);
1580 (void) snprintf(comp_filename
, sizeof (comp_filename
),
1581 "%s/.%sXXXXXX", dir
, file
);
1585 if ((tfd
= mkstemp64(tmpfilename
)) == -1)
1588 if ((compfd
= mkstemp64(comp_filename
)) == -1)
1592 * Set the mode bits and owner of the compressed
1593 * file to be that of the original uncompressed file
1595 (void) fchmod(compfd
, statbuf
.st_mode
);
1597 if (fchown(compfd
, statbuf
.st_uid
, statbuf
.st_gid
) == -1)
1601 * Calculate the number of index entries required.
1602 * index entries are stored as an array. adding
1603 * a '2' here accounts for the fact that the last
1604 * segment may not be a multiple of the segment size
1606 index_sz
= (statbuf
.st_size
/ segsize
) + 2;
1607 index
= malloc(sizeof (*index
) * index_sz
);
1617 * Now read from the uncompressed file in 'segsize'
1618 * sized chunks, compress what was read in and
1619 * write it out to a temporary file
1622 rbytes
= read(uncompfd
, uncompressed_seg
, segsize
);
1627 if (lastread
< segsize
)
1631 * Account for the first byte that
1632 * indicates whether a segment is
1635 real_segsize
= segsize
- 1;
1636 (void) li
->l_compress(uncompressed_seg
, rbytes
,
1637 compressed_seg
+ SEGHDR
, &real_segsize
, li
->l_level
);
1640 * If the length of the compressed data is more
1641 * than a threshold then there isn't any benefit
1642 * to be had from compressing this segment - leave
1645 * NB. In case an error occurs during compression (above)
1646 * the 'real_segsize' isn't changed. The logic below
1647 * ensures that that segment is left uncompressed.
1649 len_compressed
= real_segsize
;
1650 if (segsize
<= COMPRESS_THRESHOLD
||
1651 real_segsize
> (segsize
- COMPRESS_THRESHOLD
)) {
1652 (void) memcpy(compressed_seg
+ SEGHDR
, uncompressed_seg
,
1654 type
= UNCOMPRESSED
;
1655 len_compressed
= rbytes
;
1661 * Set the first byte or the SEGHDR to
1662 * indicate if it's compressed or not
1664 *compressed_seg
= type
;
1665 wbytes
= write(tfd
, compressed_seg
, len_compressed
+ SEGHDR
);
1666 if (wbytes
!= (len_compressed
+ SEGHDR
)) {
1671 index
[count
] = BE_64(offset
);
1677 (void) close(uncompfd
);
1682 * The last index entry is a sentinel entry. It does not point to
1683 * an actual compressed segment but helps in computing the size of
1684 * the compressed segment. The size of each compressed segment is
1685 * computed by subtracting the current index value from the next
1686 * one (the compressed blocks are stored sequentially)
1688 index
[count
++] = BE_64(offset
);
1691 * Now write the compressed data along with the
1692 * header information to this file which will
1693 * later be renamed to the original uncompressed
1696 * The header is as follows -
1698 * Signature (name of the compression algorithm)
1699 * Compression segment size (a multiple of 512)
1700 * Number of index entries
1701 * Size of the last block
1702 * The array containing the index entries
1704 * the header is always stored in network byte
1707 (void) bzero(algorithm
, sizeof (algorithm
));
1708 (void) strlcpy(algorithm
, li
->l_name
, sizeof (algorithm
));
1709 if (write(compfd
, algorithm
, sizeof (algorithm
))
1710 != sizeof (algorithm
))
1713 segsize
= htonl(segsize
);
1714 if (write(compfd
, &segsize
, sizeof (segsize
)) != sizeof (segsize
))
1717 index_entries
= htonl(count
);
1718 if (write(compfd
, &index_entries
, sizeof (index_entries
)) !=
1719 sizeof (index_entries
))
1722 lastread
= htonl(lastread
);
1723 if (write(compfd
, &lastread
, sizeof (lastread
)) != sizeof (lastread
))
1726 for (i
= 0; i
< count
; i
++) {
1727 if (write(compfd
, index
+ i
, sizeof (*index
)) !=
1732 /* Header is written, now write the compressed data */
1733 if (lseek(tfd
, 0, SEEK_SET
) != 0)
1736 rbytes
= wbytes
= 0;
1739 rbytes
= read(tfd
, compressed_seg
, compressed_segsize
+ SEGHDR
);
1744 if (write(compfd
, compressed_seg
, rbytes
) != rbytes
)
1748 if (fstat64(compfd
, &statbuf
) == -1)
1752 * Round up the compressed file size to be a multiple of
1753 * DEV_BSIZE. lofi(7D) likes it that way.
1755 if ((offset
= statbuf
.st_size
% DEV_BSIZE
) > 0) {
1757 offset
= DEV_BSIZE
- offset
;
1759 for (i
= 0; i
< offset
; i
++)
1760 uncompressed_seg
[i
] = '\0';
1761 if (write(compfd
, uncompressed_seg
, offset
) != offset
)
1764 (void) close(compfd
);
1766 (void) unlink(tmpfilename
);
1770 (void) unlink(tmpfilename
);
1772 (void) unlink(comp_filename
);
1773 die(gettext("error compressing file %s"), filename
);
1775 /* Rename the compressed file to the actual file */
1776 if (rename(comp_filename
, filename
) == -1) {
1777 (void) unlink(comp_filename
);
1778 die(gettext("error compressing file %s"), filename
);
1781 if (compressed_seg
!= NULL
)
1782 free(compressed_seg
);
1783 if (uncompressed_seg
!= NULL
)
1784 free(uncompressed_seg
);
1788 (void) close(compfd
);
1790 (void) close(uncompfd
);
1796 lofi_compress_select(const char *algname
)
1800 for (i
= 0; i
< LOFI_COMPRESS_FUNCTIONS
; i
++) {
1801 if (strcmp(lofi_compress_table
[i
].l_name
, algname
) == 0)
1808 check_algorithm_validity(const char *algname
, int *compress_index
)
1810 *compress_index
= lofi_compress_select(algname
);
1811 if (*compress_index
< 0)
1812 die(gettext("invalid algorithm name: %s\n"), algname
);
1816 check_file_validity(const char *filename
)
1822 fd
= open64(filename
, O_RDONLY
);
1824 die(gettext("open: %s"), filename
);
1826 error
= fstat64(fd
, &buf
);
1828 die(gettext("fstat: %s"), filename
);
1829 } else if (!S_ISLOFIABLE(buf
.st_mode
)) {
1830 die(gettext("%s is not a regular file, "
1831 "block, or character device\n"),
1833 } else if ((buf
.st_size
% DEV_BSIZE
) != 0) {
1834 die(gettext("size of %s is not a multiple of %d\n"),
1835 filename
, DEV_BSIZE
);
1839 if (name_to_minor(filename
) != 0) {
1840 die(gettext("cannot use %s on itself\n"), LOFI_DRIVER_NAME
);
1845 check_file_is_encrypted(const char *filename
)
1848 char buf
[sizeof (lofi_crypto_magic
)];
1850 int rest
= sizeof (lofi_crypto_magic
);
1852 fd
= open64(filename
, O_RDONLY
);
1854 die(gettext("failed to open: %s"), filename
);
1856 if (lseek(fd
, CRYOFF
, SEEK_SET
) != CRYOFF
)
1857 die(gettext("failed to seek to offset 0x%lx in file %s"),
1861 got
= read(fd
, buf
+ sizeof (lofi_crypto_magic
) - rest
, rest
);
1862 if ((got
== 0) || ((got
== -1) && (errno
!= EINTR
)))
1863 die(gettext("failed to read crypto header"
1864 " at offset 0x%lx in file %s"), CRYOFF
, filename
);
1870 while (close(fd
) == -1) {
1872 die(gettext("failed to close file %s"), filename
);
1875 return (strncmp(buf
, lofi_crypto_magic
,
1876 sizeof (lofi_crypto_magic
)) == 0);
1880 convert_to_num(const char *str
)
1883 uint32_t segsize
, mult
= 1;
1886 if (len
&& isalpha(str
[len
- 1])) {
1887 switch (str
[len
- 1]) {
1905 die(gettext("invalid segment size %s\n"), str
);
1909 segsize
= atol(str
);
1916 main(int argc
, char *argv
[])
1920 const char *devicename
= NULL
;
1921 const char *filename
= NULL
;
1922 const char *algname
= COMPRESS_ALGORITHM
;
1926 uint32_t segsize
= SEGSIZE
;
1927 static char *lofictl
= "/dev/" LOFI_CTL_NAME
;
1928 boolean_t force
= B_FALSE
;
1930 boolean_t errflag
= B_FALSE
;
1931 boolean_t addflag
= B_FALSE
;
1932 boolean_t labelflag
= B_FALSE
;
1933 boolean_t rdflag
= B_FALSE
;
1934 boolean_t deleteflag
= B_FALSE
;
1935 boolean_t ephflag
= B_FALSE
;
1936 boolean_t compressflag
= B_FALSE
;
1937 boolean_t uncompressflag
= B_FALSE
;
1938 /* the next two work together for -c, -k, -T, -e options only */
1939 boolean_t need_crypto
= B_FALSE
; /* if any -c, -k, -T, -e */
1940 boolean_t cipher_only
= B_TRUE
; /* if -c only */
1941 const char *keyfile
= NULL
;
1942 mech_alias_t
*cipher
= NULL
;
1943 token_spec_t
*token
= NULL
;
1946 char realfilename
[MAXPATHLEN
];
1948 pname
= getpname(argv
[0]);
1950 (void) setlocale(LC_ALL
, "");
1951 (void) textdomain(TEXT_DOMAIN
);
1953 while ((c
= getopt(argc
, argv
, "a:c:Cd:efk:lrs:T:U")) != EOF
) {
1957 if ((filename
= realpath(optarg
, realfilename
)) == NULL
)
1959 if (((argc
- optind
) > 0) && (*argv
[optind
] != '-')) {
1960 /* optional device */
1961 devicename
= argv
[optind
];
1966 compressflag
= B_TRUE
;
1967 if (((argc
- optind
) > 1) && (*argv
[optind
] != '-')) {
1968 /* optional algorithm */
1969 algname
= argv
[optind
];
1972 check_algorithm_validity(algname
, &compress_index
);
1975 /* is the chosen cipher allowed? */
1976 if ((cipher
= ciph2mech(optarg
)) == NULL
) {
1978 warn(gettext("cipher %s not allowed\n"),
1981 need_crypto
= B_TRUE
;
1982 /* cipher_only is already set */
1985 deleteflag
= B_TRUE
;
1986 minor
= name_to_minor(optarg
);
1988 devicename
= optarg
;
1990 if ((filename
= realpath(optarg
,
1991 realfilename
)) == NULL
)
1997 need_crypto
= B_TRUE
;
1998 cipher_only
= B_FALSE
; /* need to unset cipher_only */
2005 need_crypto
= B_TRUE
;
2006 cipher_only
= B_FALSE
; /* need to unset cipher_only */
2015 segsize
= convert_to_num(optarg
);
2016 if (segsize
< DEV_BSIZE
|| !ISP2(segsize
))
2017 die(gettext("segment size %s is invalid "
2018 "or not a multiple of minimum block "
2019 "size %ld\n"), optarg
, DEV_BSIZE
);
2022 if ((token
= parsetoken(optarg
)) == NULL
) {
2025 gettext("invalid token key specifier %s\n"),
2028 need_crypto
= B_TRUE
;
2029 cipher_only
= B_FALSE
; /* need to unset cipher_only */
2032 uncompressflag
= B_TRUE
;
2041 /* Check for mutually exclusive combinations of options */
2043 (addflag
&& deleteflag
) ||
2044 (labelflag
&& !addflag
) ||
2045 (rdflag
&& !addflag
) ||
2046 (!addflag
&& need_crypto
) ||
2047 (need_crypto
&& labelflag
) ||
2048 ((compressflag
|| uncompressflag
) &&
2049 (labelflag
|| addflag
|| deleteflag
)))
2052 /* ephemeral key, and key from either file or token are incompatible */
2053 if (ephflag
&& (keyfile
!= NULL
|| token
!= NULL
)) {
2054 die(gettext("ephemeral key cannot be used with keyfile"
2055 " or token key\n"));
2059 * "-c" but no "-k", "-T", "-e", or "-T -k" means derive key from
2060 * command line passphrase
2063 switch (argc
- optind
) {
2064 case 0: /* no more args */
2065 if (compressflag
|| uncompressflag
) /* needs filename */
2069 if (addflag
|| deleteflag
)
2071 /* one arg means compress/uncompress the file ... */
2072 if (compressflag
|| uncompressflag
) {
2073 if ((filename
= realpath(argv
[optind
],
2074 realfilename
)) == NULL
)
2075 die("%s", argv
[optind
]);
2076 /* ... or without options means print the association */
2078 minor
= name_to_minor(argv
[optind
]);
2080 devicename
= argv
[optind
];
2082 if ((filename
= realpath(argv
[optind
],
2083 realfilename
)) == NULL
)
2084 die("%s", argv
[optind
]);
2093 if (addflag
|| compressflag
|| uncompressflag
)
2094 check_file_validity(filename
);
2096 if (filename
&& !valid_abspath(filename
))
2100 * Here, we know the arguments are correct, the filename is an
2101 * absolute path, it exists and is a regular file. We don't yet
2102 * know that the device name is ok or not.
2106 if (addflag
|| deleteflag
|| compressflag
|| uncompressflag
)
2109 openflag
|= O_RDONLY
;
2110 lfd
= open(lofictl
, openflag
);
2112 if ((errno
== EPERM
) || (errno
== EACCES
)) {
2113 die(gettext("you do not have permission to perform "
2114 "that operation.\n"));
2116 die(gettext("open: %s"), lofictl
);
2122 * No passphrase is needed for ephemeral key, or when key is
2123 * in a file and not wrapped by another key from a token.
2124 * However, a passphrase is needed in these cases:
2125 * 1. cipher with no ephemeral key, key file, or token,
2126 * in which case the passphrase is used to build the key
2127 * 2. token with an optional cipher or optional key file,
2128 * in which case the passphrase unlocks the token
2129 * If only the cipher is specified, reconfirm the passphrase
2130 * to ensure the user hasn't mis-entered it. Otherwise, the
2131 * token will enforce the token passphrase.
2134 CK_SESSION_HANDLE sess
;
2136 /* pick a cipher if none specified */
2138 cipher
= DEFAULT_CIPHER
;
2140 if (!kernel_cipher_check(cipher
))
2142 "use \"cryptoadm list -m\" to find available "
2145 init_crypto(token
, cipher
, &sess
);
2148 getkeyfromuser(cipher
, &rkey
, &rksz
,
2149 !check_file_is_encrypted(filename
));
2150 } else if (token
!= NULL
) {
2151 getkeyfromtoken(sess
, token
, keyfile
, cipher
,
2154 /* this also handles ephemeral keys */
2155 getkeyfromfile(keyfile
, cipher
, &rkey
, &rksz
);
2162 * Now to the real work.
2165 add_mapping(lfd
, devicename
, filename
, cipher
, rkey
, rksz
,
2167 else if (compressflag
)
2168 lofi_compress(&lfd
, filename
, compress_index
, segsize
);
2169 else if (uncompressflag
)
2170 lofi_uncompress(lfd
, filename
);
2171 else if (deleteflag
)
2172 delete_mapping(lfd
, devicename
, filename
, force
);
2173 else if (filename
|| devicename
)
2174 print_one_mapping(lfd
, devicename
, filename
);
2176 print_mappings(lfd
);