1 /* misc.c - Miscellaneous fucntions
2 * Copyright (C) 2004, 2009, 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
34 #include "../common/tlv.h"
35 #include "../common/sexp-parse.h"
38 /* Setup the environment so that the pinentry is able to get all
39 required information. This is used prior to an exec of the
42 setup_pinentry_env (void)
44 #ifndef HAVE_W32_SYSTEM
46 const char *name, *value;
49 /* Try to make sure that GPG_TTY has been set. This is needed if we
50 call for example the protect-tools with redirected stdin and thus
51 it won't be able to ge a default by itself. Try to do it here
52 but print a warning. */
53 value = session_env_getenv (opt.session_env, "GPG_TTY");
55 gnupg_setenv ("GPG_TTY", value, 1);
56 else if (!(lc=getenv ("GPG_TTY")) || !*lc)
58 log_error (_("GPG_TTY has not been set - "
59 "using maybe bogus default\n"));
60 lc = gnupg_ttyname (0);
63 gnupg_setenv ("GPG_TTY", lc, 1);
67 gnupg_setenv ("LC_CTYPE", opt.lc_ctype, 1);
68 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
69 else if ( (lc = setlocale (LC_CTYPE, "")) )
70 gnupg_setenv ("LC_CTYPE", lc, 1);
74 gnupg_setenv ("LC_MESSAGES", opt.lc_messages, 1);
75 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
76 else if ( (lc = setlocale (LC_MESSAGES, "")) )
77 gnupg_setenv ("LC_MESSAGES", lc, 1);
81 while ((name = session_env_list_stdenvnames (&iterator, NULL)))
83 if (!strcmp (name, "GPG_TTY"))
84 continue; /* Already set. */
85 value = session_env_getenv (opt.session_env, name);
87 gnupg_setenv (name, value, 1);
90 #endif /*!HAVE_W32_SYSTEM*/
95 /* Transform a sig-val style s-expression as returned by Libgcrypt to
96 one which includes an algorithm identifier encoding the public key
97 and the hash algorithm. The public key algorithm is taken directly
98 from SIGVAL and the hash algorithm is given by MDALGO. This is
99 required because X.509 merges the public key algorithm and the hash
100 algorithm into one OID but Libgcrypt is not aware of that. The
101 function ignores missing parameters so that it can also be used to
102 create an siginfo value as expected by ksba_certreq_set_siginfo.
103 To create a siginfo s-expression a public-key s-expression may be
104 used instead of a sig-val. We only support RSA for now. */
106 transform_sigval (const unsigned char *sigval, size_t sigvallen, int mdalgo,
107 unsigned char **r_newsigval, size_t *r_newsigvallen)
110 const unsigned char *buf, *tok;
111 size_t buflen, toklen;
112 int depth, last_depth1, last_depth2;
114 const unsigned char *rsa_s = NULL;
115 size_t rsa_s_len = 0;
126 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
128 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
130 if (tok && toklen == 7 && !memcmp ("sig-val", tok, toklen))
132 else if (tok && toklen == 10 && !memcmp ("public-key", tok, toklen))
135 return gpg_error (GPG_ERR_UNKNOWN_SEXP);
136 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
138 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
140 if (!tok || toklen != 3 || memcmp ("rsa", tok, toklen))
141 return gpg_error (GPG_ERR_WRONG_PUBKEY_ALGO);
144 while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
145 && depth && depth >= last_depth1)
148 return gpg_error (GPG_ERR_UNKNOWN_SEXP);
149 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
151 if (tok && toklen == 1)
153 const unsigned char **mpi;
158 case 's': mpi = &rsa_s; mpi_len = &rsa_s_len; break;
159 default: mpi = NULL; mpi_len = NULL; break;
162 return gpg_error (GPG_ERR_DUP_VALUE);
164 if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
173 /* Skip to the end of the list. */
175 while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
176 && depth && depth >= last_depth2)
184 /* Map the hash algorithm to an OID. */
188 oid = "1.2.840.113549.1.1.5"; /* sha1WithRSAEncryption */
192 oid = "1.2.840.113549.1.1.11"; /* sha256WithRSAEncryption */
196 oid = "1.2.840.113549.1.1.12"; /* sha384WithRSAEncryption */
200 oid = "1.2.840.113549.1.1.13"; /* sha512WithRSAEncryption */
204 return gpg_error (GPG_ERR_DIGEST_ALGO);
207 if (rsa_s && !is_pubkey)
208 err = gcry_sexp_build (&sexp, NULL, "(sig-val(%s(s%b)))",
209 oid, (int)rsa_s_len, rsa_s);
211 err = gcry_sexp_build (&sexp, NULL, "(sig-val(%s))", oid);
214 err = make_canon_sexp (sexp, r_newsigval, r_newsigvallen);
215 gcry_sexp_release (sexp);