ADD_KEY(2) | Linux Key Management Calls | ADD_KEY(2) |
add_key - add a key to the kernel's key management facility
#include <sys/types.h> #include <keyutils.h>
key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t keyring);
No glibc wrapper is provided for this system call; see NOTES.
add_key() creates or updates a key of the given type and description, instantiates it with the payload of length plen, attaches it to the nominated keyring, and returns the key's serial number.
The key may be rejected if the provided data is in the wrong format or it is invalid in some other way.
If the destination keyring already contains a key that matches the specified type and description, then, if the key type supports it, that key will be updated rather than a new key being created; if not, a new key (with a different ID) will be created and it will displace the link to the extant key from the keyring.
The destination keyring serial number may be that of a valid keyring for which the caller has write permission. Alternatively, it may be one of the following special keyring IDs:
The key type is a string that specifies the key's type. Internally, the kernel defines a number of key types that are available in the core key management code. Among the types that are available for user-space use and can be specified as the type argument to add_key() are the following:
This key type vets the description to ensure that it is qualified by a "service" prefix, by checking to ensure that the description contains a ':' that is preceded by other characters.
For further details on these key types, see keyrings(7).
On success, add_key() returns the serial number of the key it created or updated. On error, -1 is returned and errno is set to indicate the cause of the error.
This system call first appeared in Linux 2.6.10.
This system call is a nonstandard Linux extension.
No wrapper for this system call is provided in glibc. A wrapper is provided in the libkeyutils package. When employing the wrapper in that library, link with -lkeyutils.
The program below creates a key with the type, description, and payload specified in its command-line arguments, and links that key into the session keyring. The following shell session demonstrates the use of the program:
$ ./a.out user mykey "Some payload" Key ID is 64a4dca $ grep '64a4dca' /proc/keys 064a4dca I--Q--- 1 perm 3f010000 1000 1000 user mykey: 12
#include <sys/types.h> #include <keyutils.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) {
key_serial_t key;
if (argc != 4) {
fprintf(stderr, "Usage: %s type description payload\n",
argv[0]);
exit(EXIT_FAILURE);
}
key = add_key(argv[1], argv[2], argv[3], strlen(argv[3]),
KEY_SPEC_SESSION_KEYRING);
if (key == -1) {
perror("add_key");
exit(EXIT_FAILURE);
}
printf("Key ID is %jx\n", (uintmax_t) key);
exit(EXIT_SUCCESS); }
keyctl(1), keyctl(2), request_key(2), keyctl(3), keyrings(7), keyutils(7), persistent-keyring(7), process-keyring(7), session-keyring(7), thread-keyring(7), user-keyring(7), user-session-keyring(7)
The kernel source files Documentation/security/keys/core.rst and Documentation/keys/request-key.rst (or, before Linux 4.13, in the files Documentation/security/keys.txt and Documentation/security/keys-request-key.txt).
This page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
2020-11-01 | Linux |