/** Copyright 2020 Haiku, Inc. All rights reserved.* Distributed under the terms of the MIT License.** Authors:* Niels Sascha Reedijk, niels.reedijk@gmail.com*//*!\page app_keystore Password and Key Storage APIHaiku R1 introduces the first version of a system-wide key store service,allows you as developer to outsource some of the credential and certificatemanagement, as well as providing an infrastructure that enables sharingthese artifacts between applications.\warning The implementation in Haiku R1 is limited and is a promise of moreto come. Functionality beyond storing and sharing passwords is for thefuture. Also please read the security section of this document,especially when you are working with artifacts that are more sensitive.In many cases you will find that this system service will work just aswell as making your own implementation, but there are instances inwhich you may chose not to use it.\section app_keystore_overview 1. Highlevel Overview (components)The implementation is based around the following concepts:- The \b keystore is the centralized repository for your keys. It ismanaged by the \b keystore_server and it contains one or more\b keyrings.- A \b keyring is a collection of keys. There is always a\b master \b keyring, which cannot be removed. Access is organizedaround keyrings. From a user's perspective, when an application wantsto access keys in a keyring, the user will have to grant permission tothat application to access the keyring. A keyring is identified by aname, which needs to be unique on the user's system.- A keyring contains \b keys. These are the smallest unit in the system.A key can be anything that you want to safeguard in the keystore. Keysare identified by the combination of an identifier and a secondaryidentifier. These should be unique within a keyring.- The final piece is the concept of \b permissions. In the currentimplementation, an application needs to ask permission to access akeyring. The \c keystore_server will validate the permissions, and ifnecessary prompt the user to grant one-time or a permant access. If theuser only allows it once, access is granted until the applicationterminates.As a user of the API, you will mostly be working with \ref BKeyStore, asthe access point that queries and modifies keyrings and keys. An individualkey is represented by the \ref BKey object.\section app_keystore_security 2. SecurityThe current implementation of this API should be considered low-security.The most important thing to know is that there is \b no \b encryptionapplied when storing the keys and keyrings to the drive. This means thatthe data can be read by any malicious actor that can access the drive ofyour machine.This should also puts the current locking mechanism in perspective. Whilethe \c keystore_server will prompt the user to grant (or deny) access toyour application, when it wants to access a keyring, this again does notprevent any malicious actor to bypass the \c keystore_server and directlyread from (and write to!) the file.When considering on whether to use the current API, there are a few thingsto think about:- First, consider whether you should store the keys at all. Passwords toservices with extremely sensitive personal or financial information,such as email passwords or credentials to financial institutions, shouldnot be stored at all. Prompt your user for the credentials when needed,and don't keep them for later use.- Secondly, if you are storing credentials for use with web services,check if the service you are using supports using access tokens. ManyAPIs have them, and often use it in combination with some form ofpermission system or scoping, making it possible for you to keep accessas limited as possible. Furthermore, the user often has the ability torevoke access to a token, in case they think it is compromised.- When you assess that you really do need to store the credentials, makea determination first about whether or not the credentials should havesome form of encryption. For now you should consider looking for anothersolution to storing sensitive data, but contributions to improve thisAPI are very welcome. It is beyond the scope of this document to discussstrategies around encryption.- When you assess the risk is low enough not to employ encryptionstrategies, you may consider using this API. It is particularlyrecommended if you will be sharing the credentials with more than oneapplication.\warning In the end, it is up to you as a developer to be conscious of anychoices you make when it comes to user data, and credentials are nodifferent. When you decide that the Password and Key API does not fityour needs, choose a framework or library that does fit your purpose.\section app_keystore_usage 3. Practical use of the APIBelow are two distinct examples on how you may use the API.\subsection app_keystore_usage_web The Langlaufer Web BrowserWe are working on the infamous Langlaufer web browser, and we are addinga feature where we autocomplete user names and passwords. It is decided touse the Password and Key Storage API to do our key management. Whenever weland on a web page with a login screen, we will try to see if we havecredentials for that web page. Part of the requirements is that we supportmore than one set of credentials for a web page.It is decided that the application will store the user credentials in it'sown keyring, as we do not want to interfere with any other keys in themaster key. Additionally, we will use both the primary and secondaryidentifier fields. The primary will contain the hostname of the website,and the secondary will contain the user name.One final design note is that all the calls to the \c keystore_server aresynchronous, meaning they will block until there is a response back fromthe keystore. In the case that a user needs to be prompted for a password,the call will be blocked until they make a decision. That is why any callson \ref BKeyStore should be done on a separate worker thread, instead ofwithin the logic of a Window.For clarity, the example below displays the interaction with the\ref BKeyStore and \ref BKey classes through some utility functions. It isup to the reader to put that in a separate working thread.\code{.cpp}#include <Key.h>#include <KeyStore.h>const char *kLanglauferKeyringName = "Langlaufer";BObjectList<BPasswordKey>GetKeysForWebsite(const char *baseUrl) {// There may be more than one match, so we use the iteration methods.BKeyStore keyStore;uint32 cookie;BPasswordKey currentKey;BObjectList<BPasswordKey> list;bool next = true;while(next) {status_t status = keyStore.GetNextKey(kLanglauferKeyringName,B_KEY_TYPE_PASSWORD, B_KEY_PURPOSE_WEB, cookie, currentKey);switch(status) {case B_OK:// Try to see if the key matches the websiteif (currentKey.Identifier() == baseUrl) {// Add the item to the list.list.AddItem(new BPasswordKey(currentKey));}break;case B_BAD_VALUE:// The keyring does not exist, create it, and end the// searchCreateKeyring();next = false;break;default:// Something else went wrong, like the user did not give// authorization, or we are at the end of the list.// Bail out the search at this point.next = false;break;}}}return list;}voidCreateKeyring() {BKeyStore keyStore;// Ignore the return value in the next line, it may fail but that won't// interrupt the flow of our program.keyStore.AddKeyring(kLanglauferKeyringName);}voidAddOrReplaceKey(const char *baseUrl, const char *user, const char *password) {BKeyStore keyStore;BPasswordKey key;// Fill out the key with existing data, or create new dataif (keyStore.GetKey(kLanglauferKeyringName, B_KEY_TYPE_PASSWORD, baseUrl, user, &key) == B_OK) {// Remove the existing keykeyStore.RemoveKey(kLanglauferKeyringName, key);// Update the passwordkey.SetPassword(password);} else {key.SetTo(password, B_KEY_PURPOSE_WEB, user, password);}// Store the updated/new key in the keyringkeyStore.AddKey(kLanglauferKeyringName, key);}\endcode\subsection app_keystore_usage_coolwebservice The CoolWebService Tool SuiteWe are working on a set of tools that interface with a cool web service.Instead of building one monolithic application, we make several small toolswith specific jobs for this cool web service. One of the tools does theauthentication, and stores the key in the master keyring on the system. Theother tools use this key to access the API.Each tool requires the authentication token to be set up properly. That'swhy in the \ref BApplication::ReadyToRun() hook we check for theavailability of the key. If it is not available, or it does not work, theuser will be redirected to the authentication tool. The key will be storedas a password. It will be identified by the identifier "CoolWebService".\code{.cpp}voidCoolPushTool::ReadyToRun() {BKeyStore keyStore;BPasswordKey key;if (keyStore.GetKey(B_KEY_TYPE_PASSWORD, "CoolWebService", key) != B_OK) {// Terminate the application and re-authenticate...}// Extract the keyBString accessToken = key.Password();// Validate the key, and if succesful, continue...}\endcode*/