add_key()函數 Unix/Linux
名稱
add_key - 添加到內核的密鑰管理機製一個鍵
內容簡介
#include <keyutils.h> key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t keyring); |
描述
add_key() 要求內核給定類型和描述來創建或更新一個鍵,它的有效載荷plen 長度實例,將它安裝到提名 keyringand,返回其序列號。
密鑰類型可能會拒絕該數據,如果它是在錯誤的格式或以其他方式無效。
如果目標的鑰匙圈已經包含匹配指定類型和描述,然後,如果密鑰類型支持一個鍵,該鍵將被更新,而不是創建一個新的密鑰,如果冇有,將創建一個新的密鑰,它將取代鏈接到現存的核心,從鑰匙圈。
目的地鑰匙圈序號可能是一個有效的鑰匙圈,主調用寫入權限,或者它可以是一個特殊的密鑰環ID:
標簽 | 描述 |
---|---|
KEY_SPEC_THREAD_KEYRING | This specifies the caller’s thread-specific keyring. |
KEY_SPEC_PROCESS_KEYRING | This specifies the caller’s process-specific keyring. |
KEY_SPEC_SESSION_KEYRING | This specifies the caller’s session-specific keyring. |
KEY_SPEC_USER_KEYRING | This specifies the caller’s UID-specific keyring. |
KEY_SPEC_USER_SESSION_KEYRING | This specifies the caller’s UID-session keyring. |
密鑰類型
有很多可供選擇的核心密鑰管理代碼的密鑰類型,而這些可以被指定為這個函數:
標簽 | 描述 |
---|---|
“user” | Keys of the user-defined key type may contain a blob of arbitrary data, and thedescription may be any valid string, though it is preferred that the description be prefixed with a string representing the service to which the key is of interest and a colon (for instance “afs:mykey”). The payload may be empty or NULL for keys of this type. |
“keyring” | Keyrings are special key types that may contain links to sequences of other keys of any type. If this interface is used to create a keyring, then a NULL payload should be specified, andplen should be zero. |
返回值
成功 add_key() 返回序列號密鑰,它創建或更新。錯誤將返回值-1並且errno將被設置為一個適當的錯誤。
錯誤
標簽 | 描述 |
---|---|
ENOKEY | The keyring doesn’t exist. |
EKEYEXPIRED | The keyring has expired. |
EKEYREVOKED | The keyring has been revoked. |
EINVAL | The payload data was invalid. |
ENOMEM | Insufficient memory to create a key. |
EDQUOT | The key quota for this user would be exceeded by creating this key or linking it to the keyring. |
EACCES | The keyring wasn’t available for modification by the user. |
鏈接
雖然這是一個Linux係統調用,它是在libc中不存在,但可以發現合適的 libkey 工具。鏈接時,lkey 工具應指定給鏈接器。