summaryrefslogtreecommitdiffstats
path: root/crypto/pkcs12/p12_p8e.c
blob: a6255155ba83b82930a797990cb4220052a2d970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
 * 2001.
 */
/* ====================================================================
 * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
#include "internal/cryptlib.h"
#include <openssl/pkcs12.h>

X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
                        const char *pass, int passlen,
                        unsigned char *salt, int saltlen, int iter,
                        PKCS8_PRIV_KEY_INFO *p8inf)
{
    X509_SIG *p8 = NULL;
    X509_ALGOR *pbe;

    if (pbe_nid == -1)
        pbe = PKCS5_pbe2_set(cipher, iter, salt, saltlen);
    else if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0))
        pbe = PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, pbe_nid);
    else {
        ERR_clear_error();
        pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen);
    }
    if (!pbe) {
        PKCS12err(PKCS12_F_PKCS8_ENCRYPT, ERR_R_ASN1_LIB);
        return NULL;
    }
    p8 = PKCS8_set0_pbe(pass, passlen, p8inf, pbe);
    if (p8 == NULL) {
        X509_ALGOR_free(pbe);
        return NULL;
    }

    return p8;
}

X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
                         PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
{
    X509_SIG *p8;
    ASN1_OCTET_STRING *enckey;

    enckey =
        PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
                                pass, passlen, p8inf, 1);
    if (!enckey) {
        PKCS12err(PKCS12_F_PKCS8_SET0_PBE, PKCS12_R_ENCRYPT_ERROR);
        return NULL;
    }

    if ((p8 = X509_SIG_new()) == NULL) {
        PKCS12err(PKCS12_F_PKCS8_SET0_PBE, ERR_R_MALLOC_FAILURE);
        ASN1_OCTET_STRING_free(enckey);
        return NULL;
    }
    X509_ALGOR_free(p8->algor);
    ASN1_OCTET_STRING_free(p8->digest);
    p8->
.TH "BUKU" "1" "Aug 2017" "Version 3.2" "User Commands"
.SH NAME
buku \- Powerful command-line bookmark manager. Your mini web!
.SH SYNOPSIS
.B buku [OPTIONS] [KEYWORD [KEYWORD ...]]
.SH DESCRIPTION
.B buku
is a command-line utility to store, tag, search and organize bookmarks.
.PP
.B Features
.PP
  * Lightweight, clean interface, custom colors
  * Text editor integration
  * Fetch, edit page title; add tags and notes
  * Powerful search modes (regex, substring...)
  * Continuous search with on the fly mode switch
  * Open bookmarks and search results in browser
  * Manual encryption support
  * Auto-import Firefox and Google Chrome bookmarks
  * Import/export bookmarks from/to HTML or Markdown
  * Shorten and expand URLs
  * Smart tag management using redirection (>>, >, <<)
  * Portable, merge-able database to sync between systems
  * Multithreaded full DB refresh
  * Shell completion scripts, man page with handy examples
.SH OPERATIONAL NOTES
.PP
.IP 1. 4
The database file is stored in:
  - \fI$XDG_DATA_HOME/buku/bookmarks.db\fR, if XDG_DATA_HOME is defined (first preference) or
  - \fI$HOME/.local/share/buku/bookmarks.db\fR, if HOME is defined (second preference) or
  - \fI%APPDATA%\buku\bookmarks.db\fR, if you are on Windows or
  - \fIthe current directory\fR.
.PP
.IP 2. 4
If the URL contains characters like ';', '&' or brackets they may be interpreted specially by the shell. To avoid it, add the URL within single or double quotes ('/").
.PP
.IP 3. 4
URLs are unique in DB. The same URL cannot be added twice.
.PP
.IP 4. 4
Bookmarks with immutable titles are listed with '(L)' after the title.
.PP
.IP 5. 4
\fBTags\fR:
  - Comma (',') is the tag delimiter in DB. A tag cannot have comma(s) in it. Tags are filtered (for unique tags) and sorted. Tags are stored in lower case and can be replaced, appended or deleted.
  - Folder names are converted to all-lowercase tags during bookmarks html import.
  - Releases prior to v2.7 support both capital and lower cases in tags. From v2.7 all tags are stored in lowercase. An undocumented option --\fIfixtags\fR is introduced to modify the older tags. It also fixes another issue where the same tag appears multiple times in the tagset of a record. Run \fBbuku --fixtags\fR once.
  - Tags can be edited from the prompt very easily using '>>' (append), '>' (overwrite) and '<<' (remove) symbols. The LHS of the operands denotes the indices and ranges of tags to apply (as listed by --tag or key 't' at prompt) and the RHS denotes the actual DB indices and ranges of the bookmarks to apply the change to.
.PP
.IP 6. 4
\fBUpdate\fR operation:
  - If --title, --tag or --comment is passed without argument, clear the corresponding field from DB.
  - If --url is passed (and --title is omitted), update the title from web using the URL.
  - If indices are passed without any other options (--url, --title, --tag, --comment and --immutable), read the URLs from DB and update titles from web. Bookmarks marked immutable are skipped.
  - Can update bookmarks matching a search, when combined with any of the search options and no arguments to update are passed.
.PP
.IP 7. 4
\fBDelete\fR operation:
  - When a record is deleted, the last record is moved to the index.
  - Delete doesn't work with range and indices provided together as arguments. It's an intentional decision to avoid extra sorting, in-range checks and to keep the auto-DB compaction functionality intact. On the same lines, indices are deleted in descending order.
  - Can delete bookmarks matching a search, when combined with any of the search options and no arguments to delete are passed.
.PP
.IP 8. 4
\fBSearch\fR works in mysterious ways:
  - Case-insensitive.
  - Matches words in URL, title and tags.
  - --sany : match any of the keywords in URL, title or tags. Default search option.
  - --sall : match all the keywords in URL, title or tags.
  - --deep : match \fBsubstrings\fR (`match` matches `rematched`) in URL, title and tags.
  - --sreg : match a regular expression (ignores --deep).
  - --stag : search bookmarks by tags, or list all tags alphabetically with usage count (if no arguments). Delimit the list of tags in the query with `,` to search for bookmarks that match ANY of the listed tags. Delimit tags with `+` to search for bookmarks that match ALL of the listed tags. Note that `,` and `+` cannot be used together in the same search. Exclude bookmarks matching certain tags from the results by using ` - ` followed by the tags. Note that the ` - ` operator and the ` + ` delimiter must be space separated: ` - ` instead of `-` and ` + ` instead of `+`. This is to distinguish them from hyphenated tags (e.g., `some-tag-name`) and tags with '+'s (e.g., `some+tag+name`).
  - Search results are indexed serially. This index is different from actual database index of a bookmark record which is shown within '[]' after the title.
.PP
.IP 9. 4
\fBImport\fR:
  - Auto-import looks in the default installation path and default user profile.
  - URLs starting with `place:`, `file://` and `apt:` are ignored during import.
  - Folder names are automatically imported as tags if --tacit is used.
  - An auto-generated tag in the format 'YYYYMonDD' is added if --tacit is not used.
.PP
.IP 10. 4
\fBEncryption\fR is optional and manual. AES256 algorithm is used. To use encryption, the database file should be unlocked (-k) before using \fBbuku\fR and locked (-l) afterwards. Between these 2 operations, the database file lies unencrypted on the disk, and NOT in memory. Also, note that the database file is \fBunencrypted on creation\fR.
.PP
.IP 11. 4
\fBEditor\fR support:
  - A single bookmark can be edited before adding. The editor can be set using the environment variable *EDITOR* or by explicitly specifying the editor. The latter takes precedence. If -a is used along with -w, the details are populated in the editor template.
  - In case of edit and update (a single bookmark), the existing record details are fetched from DB and populated in the editor template. The environment variable EDITOR must be set. Note that -u works independently of -w.
  - All lines beginning with "#" will be stripped. Then line 1 will be treated as the URL, line 2 will be the title, line 3 will be comma separated tags, and the rest of the lines will be parsed as descriptions.
.PP
.IP 12. 4
\fBProxy\fR support: please refer to the \fBENVIRONMENT\fR section.
.SH GENERAL OPTIONS
.TP
.BI \-a " " \--add " URL [tag, ...]"
Bookmark
.I URL
along with comma-separated tags. A tag can have multiple words.
.TP
.BI \-u " " \--update " [...]"
Update fields of the bookmarks at specified indices in DB. If no arguments are specified, all titles are refreshed from the web. Works with update modifiers for the fields url, title, tag and comment. If only indices are passed without any edit options, titles are fetched and updated (if not empty). Accepts hyphenated ranges and space-separated indices. Updates search results when used with search options, if no arguments.
.TP
.BI \-w " " \--write " [editor|index]"
Edit a bookmark in
.I editor
before adding it. To edit and update an existing bookmark, the
.I index
should be passed. However, in this case the environment variable EDITOR must be set.
.TP
.BI \-d " " \--delete " [...]"
Delete bookmarks. Accepts space-separated list of indices (e.g. 5 6 23 4 110 45) or a single hyphenated range (e.g. 100-200). Note that range and list don't work together. Deletes search results when combined with search options, if no arguments.
.TP
.BI \-v " " \--version
Show program version and exit.
.TP
.BI \-h " " \--help
Show program help and exit.
.SH EDIT OPTIONS
.TP
.BI \--url " [...]"
Specify the URL, works with --update only. Fetches and updates title if --title is not used.
.TP
.BI \--tag " [+|-] [...]"
Specify comma separated tags, works with --add, --update. Clears the tags, if no arguments passed. Appends or deletes tags, if list of tags is preceded by '+' or '-' respectively.
.TP
.BI \--title " [...]"
Manually specify the title, works with --add, --update. Omits or clears the title, if no arguments passed.
.TP
.BI \-c " " \--comment " [...]"
Add notes or description of the bookmark, works with --add, --update. Clears the comment, if no arguments passed.
.TP
.BI \--immutable " N"
Set the title of a bookmark immutable during updates. Works with --add, --update. N=1 sets the immutable flag, N=0 removes it. If omitted, bookmarks are added with N=0.
.SH SEARCH OPTIONS
.TP
.BI \-s " " \--sany " keyword [...]"
Search bookmarks with ANY of the keyword(s) in URL, title or tags and show the results. Prompts to enter result number to open in browser. Note that the sequential result index is not the DB index. The DB index is shown within '[]' after the title.
.br
This is the default search option for positional arguments if no other search option is specified.
.TP
.BI \-S " " \--sall " keyword [...]"
Search bookmarks with ALL keywords in URL, title or tags and show the results. Behaviour same as --sany.
.br
Special keywords:
.br
"blank": list entries with empty title/tag
.br
"immutable": list entries with locked title
.br
NOTE: To search the keywords, use --sany
.TP
.BI \--deep
Search modifier to match substrings. Works with --sany, --sall.
.TP
.BI \-r " " \--sreg " expression"
Scan for a regular expression match.
.TP
.BI \-t " " \--stag " [tag [,|+] ...] [\- tag, ...]"
Search bookmarks by tags.
.br
Use ',' delimiter to find entries matching ANY of the tags
.br
Use ' + ' delimiter to find entries matching ALL of the tags. (Note that the ' + ' delimiter must be space separated)
.br
NOTE: Cannot combine ',' and '+' in the same search
.br
Use ' - ' to exclude bookmarks that match the tags that follow. (Note that the '-' operator must be space separated).
.br
List all tags alphabetically, if no arguments. The usage count (number of bookmarks having the tag) is shown within first brackets.
.SH ENCRYPTION OPTIONS
.TP
.BI \-l " " \--lock " [N]"
Encrypt (lock) the DB file with
.I N
(> 0, default 8) hash passes to generate key.
.TP
.BI \-k " " \--unlock " [N]"
Decrypt (unlock) the DB file with
.I N
(> 0, default 8) hash passes to generate key.
.SH POWER OPTIONS
.TP
.BI \--ai
Auto-import bookmarks from Firefox and Google Chrome.
.TP
.BI \-e " " \--export " file"
Export bookmarks to Firefox bookmarks formatted HTML. Works with --tag to export only specific tags. Markdown is used if
.I file
has extension '.md'.
.br
Markdown format: [title](url), 1 entry per line.
.TP
.BI \-i " " \--import " file"
Import bookmarks exported from Firefox or Google Chrome as HTML.
.I file
is considered Markdown (compliant with --export format) if it has '.md' extension.
.TP
.BI \-m " " \--merge " file"
Add bookmarks from another buku database file.
.TP
.BI \-p " " \--print " [...]"
Show details (DB index, URL, title, tags and comment) of bookmark record by DB index. If no arguments, all records with actual index from DB are shown. Accepts hyphenated ranges and space-separated indices. A negative value (introduced for convenience) behaves like the tail utility, e.g., -n shows the details of the last n bookmarks.
.TP
.BI \-f " " \--format " N"
Show selective monochrome output with specific fields. Works with --print. Search results honour the option when used along with --json. Useful for creating batch scripts.
.br
.I N
= 1, show only URL.
.br
.I N
= 2, show URL and tags in a single line.
.br
.I N
= 3, show only title.
.br
.I N
= 4, show URL, title and tags in a single line
.TP
.BI \-j " " \--json
Output data formatted as json, works with --print output and search results.
.TP
.BI \--colors " COLORS"
Set output colors. Refer to the \fBCOLORS\fR section below for details.
.TP
.BI \--nc
Disable color output in all messages. Useful on terminals which can't handle ANSI color codes or scripted environments.
.TP
.BI \--np
Do not show the prompt, run and exit.
.TP
.BI \-o " " \--open " [...]"
Open bookmarks by DB indices or ranges in browser. Open a random index if argument is omitted.
.TP
.BI \--oa
Open all search results immediately in the browser. Works best with --np. When used along with --update or --delete, URLs are opened in the browser first and then modified or deleted.
.TP
.BI \--replace " old new"
Replace
.I old
tag with
.I new
tag if both are passed; delete
.I old
tag if
.I new
tag is not specified.
.TP
.BI \--shorten " index|URL"
Shorten the URL at DB
.I index
or an independent
.I URL
using the tny.im URL shortener service.
.TP
.BI \--expand " index|URL"
Expand the URL at DB
.I index
or an independent
.I URL
shortened using tny.im.
.TP
.BI \--suggest
Show a list of similar tags to choose from when adding a new bookmark.
.TP
.BI \--tacit
Show lesser output. Reduces the verbosity of certain operations like add, update etc.
.TP
.BI \--threads
Maximum number of parallel network connection threads to use during full DB refresh. By default 4 connections are spawned.
.I N
can range from 1 to 10.
.TP
.BI \-V
Check the latest upstream version available. This is FYI. It is possible the latest upstream released version is still not available in your package manager as the process takes a while.
.TP
.BI \-z " " \--debug
Show debug information and additional logs.
.SH PROMPT KEYS
.TP
.BI "1-N"
Browse search results by indices and ranges.
.TP
.BI "a"
Open all search results in browser.
.TP
.BI "s" " keyword [...]"
Search for records with ANY keyword.
.TP
.BI "S" " keyword [...]"
Search for records with ALL keywords.
.TP
.BI "d"
Toggle deep search to match substrings ('pen' matches 'opened').
.TP
.BI "r" " expression"
Run a regular expression search.
.TP
.BI "t" " [...]"
Search bookmarks by a tag. List all tags alphabetically, if no arguments. The index of a tag from the tag list can be used to search all bookmarks having the tag. Note that multiple indices and/or ranges do not work with this key.
.TP
.BI "o" " id|range [...]"
Browse bookmarks by indices and/or ranges.
.TP
.BI "p" " id|range [...]"
Print bookmarks by indices and/or ranges.
.TP
.BI "g" " [taglist id|range ...] [>>|>|<<] record id|range [...]"
Append, set, remove specific or all tags by indices and/or ranges to bookmark indices and/or ranges (see \fBEXAMPLES\fR section below).
.TP
.BI "w" " [editor|id]"
Edit and add or update a bookmark.
.TP
.BI "?"
Show help on prompt keys.
.TP
.BI "q, ^D, double Enter"
Exit buku.
.SH ENVIRONMENT
.TP
.BI "Completion scripts"
Shell completion scripts for Bash, Fish and Zsh can be found in:
.br
.I https://github.com/jarun/Buku/blob/master/auto-completion
.TP
.BI BROWSER
Overrides the default browser. Refer to:
.br
.I http://docs.python.org/library/webbrowser.html
.TP
.BI EDITOR
If defined, will be used as the editor to edit bookmarks with option --write.
.TP
.BI https_proxy
If defined, will be used to access http and https resources through the configured proxy. Supported format:

http[s]://[username:password@]proxyhost:proxyport/
.TP
.BI "GUI integration"
.B buku
can be integrated in a GUI environment with simple tweaks. Refer to:
.br
.I https://github.com/jarun/Buku#gui-integration
.SH COLORS
\fBbuku\fR allows you to customize the color scheme via a five-letter string, reminiscent of BSD \fBLSCOLORS\fR. The five letters represent the colors of
.IP - 2
index
.PD 0 \" Change paragraph spacing to 0 in the list
.IP - 2
title
.IP - 2
URL
.IP - 2
description/comment/note
.IP - 2
tag
.PD 1 \" Restore paragraph spacing
.TP
respectively. The five-letter string is passed is as the argument to the \fB--colors\fR option, or as the value of the environment variable \fBBUKU_COLORS\fR.
.TP
We offer the following colors/styles:
.TS
tab(;) box;
l|l
-|-
l|l.
Letter;Color/Style
a;black
b;red
c;green
d;yellow
e;blue
f;magenta
g;cyan
h;white
i;bright black
j;bright red
k;bright green
l;bright yellow
m;bright blue
n;bright magenta
o;bright cyan
p;bright white
A-H;bold version of the lowercase-letter color
I-P;bold version of the lowercase-letter bright color
x;normal
X;bold
y;reverse video
Y;bold reverse video
.TE
.TP
.TP
The default colors string is \fIoKlxm\fR, which stands for
.IP - 2
bright cyan index
.PD 0 \" Change paragraph spacing to 0 in the list
.IP - 2
bold bright green title
.IP - 2
bright yellow URL
.IP - 2
normal description
.<