/* GSS-PROXY Copyright (C) 2013 Simo Sorce Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "config.h" #include #include #include #include #include #include #include #include "gp_common.h" #include "gp_crypto.h" #include "gp_secrets.h" #define PRIV_PATH GPSTATE_PATH"/private" #define USER_KEY "user" #define CREDS_KEY_NAME "GSS-Proxy-Creds-Key" int gp_priv_init(void) { struct gp_crypto_key *encryption_key; key_serial_t keyring, keyid; size_t len; void *buf; int ret; /* create private dir if necessary */ ret = mkdir(PRIV_PATH, S_IRWXU); if (ret == -1 && errno != EEXIST) { ret = errno; GPERROR("Failed to create %s directory.\n", PRIV_PATH); return ret; } keyring = keyctl_get_persistent(getuid(), KEY_SPEC_PROCESS_KEYRING); if (keyring == -1) { ret = errno; GPERROR("Failed to get persistent keyring: (%d) %s\n", ret, gp_strerror(ret)); return ret; } keyid = keyctl_search(keyring, USER_KEY, CREDS_KEY_NAME, 0); if (keyid == -1) { /* not found, generate a new key */ encryption_key = gp_crypto_new_key(); if (!encryption_key) { GPERROR("Failed to generate key\n"); return EINVAL; } ret = gp_crypto_export_key(encryption_key, &buf, &len); if (ret) { GPERROR("Failed to serialize key\n"); return ret; } keyid = add_key(USER_KEY, CREDS_KEY_NAME, buf, len, keyring); if (keyid == -1) { ret = errno; GPERROR("Failed to store random key: (%d) %s\n", ret, gp_strerror(ret)); return ret; } gp_safe_zero(buf, len); } return 0; } static int get_crypto_key(struct gp_crypto_key **key) { key_serial_t keyring, keyid; void *kbuf = NULL; size_t klen = 0; int ret; keyring = keyctl_get_persistent(getuid(), KEY_SPEC_PROCESS_KEYRING); if (keyring == -1) { ret = errno; GPERROR("Failed to get persistent keyring: (%d) %s\n", ret, gp_strerror(ret)); return ret; } keyid = keyctl_search(keyring, USER_KEY, CREDS_KEY_NAME, 0); if (keyid == -1) { ret = errno; GPERROR("Failed to get encryption key: (%d) %s\n", ret, gp_strerror(ret)); return ret; } klen = keyctl_read_alloc(keyid, &kbuf); if (klen == -1) { ret = errno; GPERROR("Failed to read encryption key: (%d) %s\n", ret, gp_strerror(ret)); return ret; } ret = gp_crypto_import_key(kbuf, klen, key); if (ret) { GPERROR("Failed to serialize key: (%d) %s\n", ret, gp_strerror(ret)); } gp_safe_zero(kbuf, klen); free(kbuf); return ret; } static int decrypt_user_creds(char *buf, size_t len) { struct gp_crypto_key *encryption_key; void *tmpbuf; size_t tmplen; int ret; ret = get_crypto_key(&encryption_key); if (ret) { return ret; } ret = gp_crypto_decrypt(encryption_key, buf, len, &tmpbuf, &tmplen); if (ret) { GPERROR("Failed to decrypt user creds: (%d) %s\n", ret, gp_strerror(ret)); goto done; } /* copy plaintext over */ memcpy(buf, tmpbuf, tmplen); buf[tmplen] = '\0'; /* then zero and free temporary buffer */ gp_safe_zero(tmpbuf, tmplen); free(tmpbuf); done: gp_crypto_free_key(&encryption_key); return ret; } static int encrypt_user_creds(void *in, size_t inlen, void **out, size_t *outlen) { struct gp_crypto_key *encryption_key; int ret; ret = get_crypto_key(&encryption_key); if (ret) { return ret; } ret = gp_crypto_encrypt(encryption_key, in, inlen, out, outlen); if (ret) { GPERROR("Failed to encrypt user creds: (%d) %s\n", ret, gp_strerror(ret)); } gp_crypto_free_key(&encryption_key); return ret; } #define MAX_CREDS_SIZE 1024 int gp_get_uid_creds(uid_t uid, char **domain, char **username, char **password) { char *filename; char *s, *e; char buf[MAX_CREDS_SIZE + 1]; int len; int ret; int fd; ret = asprintf(&filename, "%s/creds_%d", PRIV_PATH, uid); if (ret == -1) { return ENOMEM; } *domain = NULL; *username = NULL; *password = NULL; fd = open(filename, O_RDONLY | O_CLOEXEC); if (fd == -1) { ret = errno; GPERROR("Failed to open creds file [%s]: (%d) %s\n", filename, ret, gp_strerror(ret)); goto done; } len = gp_safe_read(fd, buf, MAX_CREDS_SIZE + 1); if (len == -1) { ret = errno; GPERROR("Failed to read creds file [%s]: (%d) %s\n", filename, ret, gp_strerror(ret)); goto done; } if (len > MAX_CREDS_SIZE) { GPERROR("Creds file %s is too big.\n", filename); ret = E2BIG; goto done; } ret = decrypt_user_creds(buf, len); if (ret) goto done; /* get domain */ s = buf; e = strchr(s, ':'); if (!e) { GPERROR("Invalid creds (malformed)\n"); ret = EINVAL; goto done; } ret = asprintf(domain, "%.*s", (int)(e - s), s); if (ret == -1) { ret = ENOMEM; goto done; } /* get username */ s = e + 1; e = strchr(s, ':'); if (!e) { GPERROR("Invalid creds (malformed)\n"); ret = EINVAL; goto done; } ret = asprintf(username, "%.*s", (int)(e - s), s); if (ret == -1) { ret = ENOMEM; goto done; } /* get password */ s = e + 1; *password = strdup(s); if (*password == NULL) { ret = ENOMEM; goto done; } ret = 0; done: if (ret) { strzerofree(*domain); strzerofree(*username); strzerofree(*password); } gp_safe_zero(buf, MAX_CREDS_SIZE); free(filename); close(fd); return ret; } int gp_save_uid_creds(uid_t uid, char *domain, char *username, char *password) { char *filename; char buf[MAX_CREDS_SIZE + 1]; void *ebuf = NULL; size_t elen = 0; int len; int ret; int fd; ret = asprintf(&filename, "%s/creds_%d", PRIV_PATH, uid); if (ret == -1) { return ENOMEM; } fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0600); if (fd == -1) { ret = errno; GPERROR("Failed to open creds file [%s]: (%d) %s\n", filename, ret, gp_strerror(ret)); goto done; } len = snprintf(buf, MAX_CREDS_SIZE, "%s:%s:%s", domain ? domain : "", username, password); if (len < 0 || len > MAX_CREDS_SIZE) { GPERROR("Failed to properly format creds buffer.\n"); ret = EINVAL; goto done; } ret = encrypt_user_creds(buf, len, &ebuf, &elen); if (ret) goto done; if (elen > MAX_CREDS_SIZE) { GPERROR("Final creds buffer too long. Max is %d, got %lu.\n", MAX_CREDS_SIZE, elen); ret = EINVAL; goto done; } len = gp_safe_write(fd, ebuf, elen); if (len != elen) { ret = errno; GPERROR("Failed to write creds file [%s]: (%d) %s\n", filename, ret, gp_strerror(ret)); goto done; } ret = 0; done: gp_safe_zero(buf, MAX_CREDS_SIZE); gp_safe_zero(ebuf, elen); free(filename); free(ebuf); close(fd); return ret; }