| 1 |
/* keyinfo.c - Parse and build a keyInfo structure |
| 2 |
* Copyright (C) 2001, 2002, 2007, 2008, 2012, 2020 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* Instead of using the ASN parser - which is easily possible - we use |
| 32 |
a simple handcoded one to speed up the operation and to make it |
| 33 |
more robust. */ |
| 34 |
|
| 35 |
#include <config.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <assert.h> |
| 40 |
|
| 41 |
#include "util.h" |
| 42 |
#include "asn1-func.h" |
| 43 |
#include "keyinfo.h" |
| 44 |
#include "shared.h" |
| 45 |
#include "convert.h" |
| 46 |
#include "ber-help.h" |
| 47 |
#include "sexp-parse.h" |
| 48 |
#include "stringbuf.h" |
| 49 |
#include "der-builder.h" |
| 50 |
|
| 51 |
/* Constants used for the public key algorithms. */ |
| 52 |
typedef enum |
| 53 |
{ |
| 54 |
PKALGO_NONE, |
| 55 |
PKALGO_RSA, |
| 56 |
PKALGO_DSA, |
| 57 |
PKALGO_ECC, |
| 58 |
PKALGO_X25519, |
| 59 |
PKALGO_X448, |
| 60 |
PKALGO_ED25519, |
| 61 |
PKALGO_ED448 |
| 62 |
} |
| 63 |
pkalgo_t; |
| 64 |
|
| 65 |
|
| 66 |
struct algo_table_s { |
| 67 |
const char *oidstring; |
| 68 |
const unsigned char *oid; /* NULL indicattes end of table */ |
| 69 |
int oidlen; |
| 70 |
int supported; /* Values > 1 are also used to indicate hacks. */ |
| 71 |
pkalgo_t pkalgo; |
| 72 |
const char *algo_string; |
| 73 |
const char *elem_string; /* parameter names or '-', 'P' for plain ECDSA */ |
| 74 |
const char *ctrl_string; /* expected tag values (value > 127 are raw data)*/ |
| 75 |
const char *parmelem_string; /* parameter name or '-'. */ |
| 76 |
const char *parmctrl_string; /* expected tag values. */ |
| 77 |
const char *digest_string; /* The digest algo if included in the OID. */ |
| 78 |
}; |
| 79 |
|
| 80 |
/* Special values for the supported field. */ |
| 81 |
#define SUPPORTED_RSAPSS 2 |
| 82 |
|
| 83 |
|
| 84 |
static const struct algo_table_s pk_algo_table[] = { |
| 85 |
|
| 86 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.1 */ |
| 87 |
"1.2.840.113549.1.1.1", /* rsaEncryption (RSAES-PKCA1-v1.5) */ |
| 88 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01", 9, |
| 89 |
1, PKALGO_RSA, "rsa", "-ne", "\x30\x02\x02" }, |
| 90 |
|
| 91 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.7 */ |
| 92 |
"1.2.840.113549.1.1.7", /* RSAES-OAEP */ |
| 93 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x07", 9, |
| 94 |
0, PKALGO_RSA, "rsa", "-ne", "\x30\x02\x02"}, |
| 95 |
|
| 96 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.10 */ |
| 97 |
"1.2.840.113549.1.1.10", /* rsaPSS */ |
| 98 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0a", 9, |
| 99 |
SUPPORTED_RSAPSS, PKALGO_RSA, "rsa", "-ne", "\x30\x02\x02"}, |
| 100 |
|
| 101 |
{ /* */ |
| 102 |
"2.5.8.1.1", /* rsa (ambiguous due to missing padding rules)*/ |
| 103 |
"\x55\x08\x01\x01", 4, |
| 104 |
1, PKALGO_RSA, "ambiguous-rsa", "-ne", "\x30\x02\x02" }, |
| 105 |
|
| 106 |
{ /* iso.member-body.us.x9-57.x9cm.1 */ |
| 107 |
"1.2.840.10040.4.1", /* dsa */ |
| 108 |
"\x2a\x86\x48\xce\x38\x04\x01", 7, |
| 109 |
1, PKALGO_DSA, "dsa", "y", "\x02", "-pqg", "\x30\x02\x02\x02" }, |
| 110 |
|
| 111 |
{ /* iso.member-body.us.ansi-x9-62.2.1 */ |
| 112 |
"1.2.840.10045.2.1", /* ecPublicKey */ |
| 113 |
"\x2a\x86\x48\xce\x3d\x02\x01", 7, |
| 114 |
1, PKALGO_ECC, "ecc", "q", "\x80" }, |
| 115 |
|
| 116 |
{ /* iso.identified-organization.thawte.110 */ |
| 117 |
"1.3.101.110", /* X25519 */ |
| 118 |
"\x2b\x65\x6e", 3, |
| 119 |
1, PKALGO_X25519, "ecc", "q", "\x80" }, |
| 120 |
|
| 121 |
{ /* iso.identified-organization.thawte.111 */ |
| 122 |
"1.3.101.111", /* X448 */ |
| 123 |
"\x2b\x65\x6f", 3, |
| 124 |
1, PKALGO_X448, "ecc", "q", "\x80" }, |
| 125 |
|
| 126 |
{ /* iso.identified-organization.thawte.112 */ |
| 127 |
"1.3.101.112", /* Ed25519 */ |
| 128 |
"\x2b\x65\x70", 3, |
| 129 |
1, PKALGO_ED25519, "ecc", "q", "\x80" }, |
| 130 |
|
| 131 |
{ /* iso.identified-organization.thawte.113 */ |
| 132 |
"1.3.101.113", /* Ed448 */ |
| 133 |
"\x2b\x65\x71", 3, |
| 134 |
1, PKALGO_ED448, "ecc", "q", "\x80" }, |
| 135 |
|
| 136 |
{NULL} |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
static const struct algo_table_s sig_algo_table[] = { |
| 141 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 */ |
| 142 |
"1.2.840.113549.1.1.5", /* sha1WithRSAEncryption */ |
| 143 |
"\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05", 9, |
| 144 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "sha1" }, |
| 145 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.4 */ |
| 146 |
"1.2.840.113549.1.1.4", /* md5WithRSAEncryption */ |
| 147 |
"\x2A\x86\x48\x86\xF7\x0D\x01\x01\x04", 9, |
| 148 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "md5" }, |
| 149 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.2 */ |
| 150 |
"1.2.840.113549.1.1.2", /* md2WithRSAEncryption */ |
| 151 |
"\x2A\x86\x48\x86\xF7\x0D\x01\x01\x02", 9, |
| 152 |
0, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "md2" }, |
| 153 |
{ /* iso.member-body.us.x9-57.x9cm.1 */ |
| 154 |
"1.2.840.10040.4.3", /* dsa */ |
| 155 |
"\x2a\x86\x48\xce\x38\x04\x01", 7, |
| 156 |
1, PKALGO_DSA, "dsa", "-rs", "\x30\x02\x02" }, |
| 157 |
{ /* iso.member-body.us.x9-57.x9cm.3 */ |
| 158 |
"1.2.840.10040.4.3", /* dsaWithSha1 */ |
| 159 |
"\x2a\x86\x48\xce\x38\x04\x03", 7, |
| 160 |
1, PKALGO_DSA, "dsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha1" }, |
| 161 |
{ /* Teletrust signature algorithm. */ |
| 162 |
"1.3.36.8.5.1.2.2", /* dsaWithRIPEMD160 */ |
| 163 |
"\x2b\x24\x08\x05\x01\x02\x02", 7, |
| 164 |
1, PKALGO_DSA, "dsa", "-rs", "\x30\x02\x02", NULL, NULL, "rmd160" }, |
| 165 |
{ /* NIST Algorithm */ |
| 166 |
"2.16.840.1.101.3.4.3.1", /* dsaWithSha224 */ |
| 167 |
"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x03\x01", 11, |
| 168 |
1, PKALGO_DSA, "dsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha224" }, |
| 169 |
{ /* NIST Algorithm (the draft also used .1 but we better use .2) */ |
| 170 |
"2.16.840.1.101.3.4.3.2", /* dsaWithSha256 */ |
| 171 |
"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x03\x01", 11, |
| 172 |
1, PKALGO_DSA, "dsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha256" }, |
| 173 |
|
| 174 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */ |
| 175 |
"1.2.840.10045.4.1", /* ecdsa */ |
| 176 |
"\x2a\x86\x48\xce\x3d\x04\x01", 7, |
| 177 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha1" }, |
| 178 |
|
| 179 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-specified */ |
| 180 |
"1.2.840.10045.4.3", |
| 181 |
"\x2a\x86\x48\xce\x3d\x04\x03", 7, |
| 182 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, NULL }, |
| 183 |
/* The digest algorithm is given by the parameter. */ |
| 184 |
|
| 185 |
|
| 186 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha224 */ |
| 187 |
"1.2.840.10045.4.3.1", |
| 188 |
"\x2a\x86\x48\xce\x3d\x04\x03\x01", 8, |
| 189 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha224" }, |
| 190 |
|
| 191 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha256 */ |
| 192 |
"1.2.840.10045.4.3.2", |
| 193 |
"\x2a\x86\x48\xce\x3d\x04\x03\x02", 8, |
| 194 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha256" }, |
| 195 |
|
| 196 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha384 */ |
| 197 |
"1.2.840.10045.4.3.3", |
| 198 |
"\x2a\x86\x48\xce\x3d\x04\x03\x03", 8, |
| 199 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha384" }, |
| 200 |
|
| 201 |
{ /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha512 */ |
| 202 |
"1.2.840.10045.4.3.4", |
| 203 |
"\x2a\x86\x48\xce\x3d\x04\x03\x04", 8, |
| 204 |
1, PKALGO_ECC, "ecdsa", "-rs", "\x30\x02\x02", NULL, NULL, "sha512" }, |
| 205 |
|
| 206 |
{ /* BSI TR-03111 bsiEcdsaWithSHA1 */ |
| 207 |
"0.4.0.127.0.7.1.1.4.1.1", |
| 208 |
"\x04\x00\x7f\x00\x07\x01\x01\x04\x01\x01", 10, |
| 209 |
1, PKALGO_ECC, "ecdsa", "P", "", NULL, NULL, "sha1" }, |
| 210 |
|
| 211 |
{ /* BSI TR-03111 bsiEcdsaWithSHA224 */ |
| 212 |
"0.4.0.127.0.7.1.1.4.1.2", |
| 213 |
"\x04\x00\x7f\x00\x07\x01\x01\x04\x01\x02", 10, |
| 214 |
1, PKALGO_ECC, "ecdsa", "P", "", NULL, NULL, "sha224" }, |
| 215 |
|
| 216 |
{ /* BSI TR-03111 bsiEcdsaWithSHA256 */ |
| 217 |
"0.4.0.127.0.7.1.1.4.1.3", |
| 218 |
"\x04\x00\x7f\x00\x07\x01\x01\x04\x01\x03", 10, |
| 219 |
1, PKALGO_ECC, "ecdsa", "P", "", NULL, NULL, "sha256" }, |
| 220 |
|
| 221 |
{ /* BSI TR-03111 bsiEcdsaWithSHA384 */ |
| 222 |
"0.4.0.127.0.7.1.1.4.1.4", |
| 223 |
"\x04\x00\x7f\x00\x07\x01\x01\x04\x01\x04", 10, |
| 224 |
1, PKALGO_ECC, "ecdsa", "P", "", NULL, NULL, "sha384" }, |
| 225 |
|
| 226 |
{ /* BSI TR-03111 bsiEcdsaWithSHA512 */ |
| 227 |
"0.4.0.127.0.7.1.1.4.1.5", |
| 228 |
"\x04\x00\x7f\x00\x07\x01\x01\x04\x01\x05", 10, |
| 229 |
1, PKALGO_ECC, "ecdsa", "P", "", NULL, NULL, "sha512" }, |
| 230 |
|
| 231 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.1 */ |
| 232 |
"1.2.840.113549.1.1.1", /* rsaEncryption used without hash algo*/ |
| 233 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01", 9, |
| 234 |
1, PKALGO_RSA, "rsa", "s", "\x82" }, |
| 235 |
{ /* from NIST's OIW - actually belongs in a pure hash table */ |
| 236 |
"1.3.14.3.2.26", /* sha1 */ |
| 237 |
"\x2B\x0E\x03\x02\x1A", 5, |
| 238 |
0, PKALGO_RSA, "sha-1", "", "", NULL, NULL, "sha1" }, |
| 239 |
|
| 240 |
{ /* As used by telesec cards */ |
| 241 |
"1.3.36.3.3.1.2", /* rsaSignatureWithripemd160 */ |
| 242 |
"\x2b\x24\x03\x03\x01\x02", 6, |
| 243 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "rmd160" }, |
| 244 |
|
| 245 |
{ /* from NIST's OIW - used by TU Darmstadt */ |
| 246 |
"1.3.14.3.2.29", /* sha-1WithRSAEncryption */ |
| 247 |
"\x2B\x0E\x03\x02\x1D", 5, |
| 248 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "sha1" }, |
| 249 |
|
| 250 |
{ /* from PKCS#1 */ |
| 251 |
"1.2.840.113549.1.1.11", /* sha256WithRSAEncryption */ |
| 252 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0b", 9, |
| 253 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "sha256" }, |
| 254 |
|
| 255 |
{ /* from PKCS#1 */ |
| 256 |
"1.2.840.113549.1.1.12", /* sha384WithRSAEncryption */ |
| 257 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0c", 9, |
| 258 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "sha384" }, |
| 259 |
|
| 260 |
{ /* from PKCS#1 */ |
| 261 |
"1.2.840.113549.1.1.13", /* sha512WithRSAEncryption */ |
| 262 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0d", 9, |
| 263 |
1, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "sha512" }, |
| 264 |
|
| 265 |
{ /* iso.member-body.us.rsadsi.pkcs.pkcs-1.10 */ |
| 266 |
"1.2.840.113549.1.1.10", /* rsaPSS */ |
| 267 |
"\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0a", 9, |
| 268 |
SUPPORTED_RSAPSS, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, NULL}, |
| 269 |
|
| 270 |
{ /* TeleTrust signature scheme with RSA signature and DSI according |
| 271 |
to ISO/IEC 9796-2 with random number and RIPEMD-160. I am not |
| 272 |
sure for what this is good; thus disabled. */ |
| 273 |
"1.3.36.3.4.3.2.2", /* sigS_ISO9796-2rndWithrsa_ripemd160 */ |
| 274 |
"\x2B\x24\x03\x04\x03\x02\x02", 7, |
| 275 |
0, PKALGO_RSA, "rsa", "s", "\x82", NULL, NULL, "rmd160" }, |
| 276 |
|
| 277 |
|
| 278 |
{ /* iso.identified-organization.thawte.112 */ |
| 279 |
"1.3.101.112", /* Ed25519 */ |
| 280 |
"\x2b\x65\x70", 3, |
| 281 |
1, PKALGO_ED25519, "eddsa", "", "", NULL, NULL, NULL }, |
| 282 |
{ /* iso.identified-organization.thawte.113 */ |
| 283 |
"1.3.101.113", /* Ed448 */ |
| 284 |
"\x2b\x65\x71", 3, |
| 285 |
1, PKALGO_ED448, "eddsa", "", "", NULL, NULL, NULL }, |
| 286 |
|
| 287 |
{NULL} |
| 288 |
}; |
| 289 |
|
| 290 |
static const struct algo_table_s enc_algo_table[] = { |
| 291 |
{/* iso.member-body.us.rsadsi.pkcs.pkcs-1.1 */ |
| 292 |
"1.2.840.113549.1.1.1", /* rsaEncryption (RSAES-PKCA1-v1.5) */ |
| 293 |
"\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01", 9, |
| 294 |
1, PKALGO_RSA, "rsa", "a", "\x82" }, |
| 295 |
{/* iso.member-body.us.ansi-x9-62.2.1 */ |
| 296 |
"1.2.840.10045.2.1", /* ecPublicKey */ |
| 297 |
"\x2a\x86\x48\xce\x3d\x02\x01", 7, |
| 298 |
1, PKALGO_ECC, "ecdh", "e", "\x80" }, |
| 299 |
{NULL} |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
/* This tables maps names of ECC curves names to OIDs. A similar |
| 304 |
table is used by Libgcrypt. */ |
| 305 |
static const struct |
| 306 |
{ |
| 307 |
const char *oid; |
| 308 |
const char *name; |
| 309 |
unsigned char pkalgo; /* If not 0 force the use of ALGO. */ |
| 310 |
} curve_names[] = |
| 311 |
{ |
| 312 |
{ "1.3.101.112", "Ed25519", PKALGO_ED25519}, |
| 313 |
{ "1.3.101.110", "Curve25519", PKALGO_X25519}, |
| 314 |
{ "1.3.101.110", "X25519", PKALGO_X25519}, |
| 315 |
|
| 316 |
{ "1.3.101.113", "Ed448", PKALGO_ED448 }, |
| 317 |
{ "1.3.101.111", "X448", PKALGO_X448 }, |
| 318 |
|
| 319 |
{ "1.2.840.10045.3.1.1", "NIST P-192" }, |
| 320 |
{ "1.2.840.10045.3.1.1", "nistp192" }, |
| 321 |
{ "1.2.840.10045.3.1.1", "prime192v1" }, |
| 322 |
{ "1.2.840.10045.3.1.1", "secp192r1" }, |
| 323 |
|
| 324 |
{ "1.3.132.0.33", "NIST P-224" }, |
| 325 |
{ "1.3.132.0.33", "nistp224" }, |
| 326 |
{ "1.3.132.0.33", "secp224r1" }, |
| 327 |
|
| 328 |
{ "1.2.840.10045.3.1.7", "NIST P-256" }, |
| 329 |
{ "1.2.840.10045.3.1.7", "nistp256" }, |
| 330 |
{ "1.2.840.10045.3.1.7", "prime256v1" }, |
| 331 |
{ "1.2.840.10045.3.1.7", "secp256r1" }, |
| 332 |
|
| 333 |
{ "1.3.132.0.34", "NIST P-384" }, |
| 334 |
{ "1.3.132.0.34", "nistp384" }, |
| 335 |
{ "1.3.132.0.34", "secp384r1" }, |
| 336 |
|
| 337 |
{ "1.3.132.0.35", "NIST P-521" }, |
| 338 |
{ "1.3.132.0.35", "nistp521" }, |
| 339 |
{ "1.3.132.0.35", "secp521r1" }, |
| 340 |
|
| 341 |
{ "1.3.36.3.3.2.8.1.1.1" , "brainpoolP160r1" }, |
| 342 |
{ "1.3.36.3.3.2.8.1.1.3" , "brainpoolP192r1" }, |
| 343 |
{ "1.3.36.3.3.2.8.1.1.5" , "brainpoolP224r1" }, |
| 344 |
{ "1.3.36.3.3.2.8.1.1.7" , "brainpoolP256r1" }, |
| 345 |
{ "1.3.36.3.3.2.8.1.1.9" , "brainpoolP320r1" }, |
| 346 |
{ "1.3.36.3.3.2.8.1.1.11", "brainpoolP384r1" }, |
| 347 |
{ "1.3.36.3.3.2.8.1.1.13", "brainpoolP512r1" }, |
| 348 |
|
| 349 |
|
| 350 |
{ "1.2.643.2.2.35.1", "GOST2001-CryptoPro-A" }, |
| 351 |
{ "1.2.643.2.2.35.2", "GOST2001-CryptoPro-B" }, |
| 352 |
{ "1.2.643.2.2.35.3", "GOST2001-CryptoPro-C" }, |
| 353 |
{ "1.2.643.7.1.2.1.2.1", "GOST2012-tc26-A" }, |
| 354 |
{ "1.2.643.7.1.2.1.2.2", "GOST2012-tc26-B" }, |
| 355 |
|
| 356 |
{ "1.3.132.0.10", "secp256k1" }, |
| 357 |
|
| 358 |
{ NULL, NULL} |
| 359 |
}; |
| 360 |
|
| 361 |
|
| 362 |
/* Table to map well known curve parameters to their name. */ |
| 363 |
static const struct |
| 364 |
{ |
| 365 |
const char *name; |
| 366 |
unsigned int derlen; |
| 367 |
const unsigned char *der; |
| 368 |
} ecdomainparm_to_name[] = |
| 369 |
{ |
| 370 |
{ "brainpoolP256r1", 227, |
| 371 |
"\x30\x81\xe0\x02\x01\x01\x30\x2c\x06\x07\x2a\x86\x48\xce\x3d\x01" |
| 372 |
"\x01\x02\x21\x00\xa9\xfb\x57\xdb\xa1\xee\xa9\xbc\x3e\x66\x0a\x90" |
| 373 |
"\x9d\x83\x8d\x72\x6e\x3b\xf6\x23\xd5\x26\x20\x28\x20\x13\x48\x1d" |
| 374 |
"\x1f\x6e\x53\x77\x30\x44\x04\x20\x7d\x5a\x09\x75\xfc\x2c\x30\x57" |
| 375 |
"\xee\xf6\x75\x30\x41\x7a\xff\xe7\xfb\x80\x55\xc1\x26\xdc\x5c\x6c" |
| 376 |
"\xe9\x4a\x4b\x44\xf3\x30\xb5\xd9\x04\x20\x26\xdc\x5c\x6c\xe9\x4a" |
| 377 |
"\x4b\x44\xf3\x30\xb5\xd9\xbb\xd7\x7c\xbf\x95\x84\x16\x29\x5c\xf7" |
| 378 |
"\xe1\xce\x6b\xcc\xdc\x18\xff\x8c\x07\xb6\x04\x41\x04\x8b\xd2\xae" |
| 379 |
"\xb9\xcb\x7e\x57\xcb\x2c\x4b\x48\x2f\xfc\x81\xb7\xaf\xb9\xde\x27" |
| 380 |
"\xe1\xe3\xbd\x23\xc2\x3a\x44\x53\xbd\x9a\xce\x32\x62\x54\x7e\xf8" |
| 381 |
"\x35\xc3\xda\xc4\xfd\x97\xf8\x46\x1a\x14\x61\x1d\xc9\xc2\x77\x45" |
| 382 |
"\x13\x2d\xed\x8e\x54\x5c\x1d\x54\xc7\x2f\x04\x69\x97\x02\x21\x00" |
| 383 |
"\xa9\xfb\x57\xdb\xa1\xee\xa9\xbc\x3e\x66\x0a\x90\x9d\x83\x8d\x71" |
| 384 |
"\x8c\x39\x7a\xa3\xb5\x61\xa6\xf7\x90\x1e\x0e\x82\x97\x48\x56\xa7" |
| 385 |
"\x02\x01\x01" |
| 386 |
}, |
| 387 |
|
| 388 |
{ "brainpoolP384r1", 324, |
| 389 |
"\x30\x82\x01\x40\x02\x01\x01\x30\x3c\x06\x07\x2a\x86\x48\xce\x3d" |
| 390 |
"\x01\x01\x02\x31\x00\x8c\xb9\x1e\x82\xa3\x38\x6d\x28\x0f\x5d\x6f" |
| 391 |
"\x7e\x50\xe6\x41\xdf\x15\x2f\x71\x09\xed\x54\x56\xb4\x12\xb1\xda" |
| 392 |
"\x19\x7f\xb7\x11\x23\xac\xd3\xa7\x29\x90\x1d\x1a\x71\x87\x47\x00" |
| 393 |
"\x13\x31\x07\xec\x53\x30\x64\x04\x30\x7b\xc3\x82\xc6\x3d\x8c\x15" |
| 394 |
"\x0c\x3c\x72\x08\x0a\xce\x05\xaf\xa0\xc2\xbe\xa2\x8e\x4f\xb2\x27" |
| 395 |
"\x87\x13\x91\x65\xef\xba\x91\xf9\x0f\x8a\xa5\x81\x4a\x50\x3a\xd4" |
| 396 |
"\xeb\x04\xa8\xc7\xdd\x22\xce\x28\x26\x04\x30\x04\xa8\xc7\xdd\x22" |
| 397 |
"\xce\x28\x26\x8b\x39\xb5\x54\x16\xf0\x44\x7c\x2f\xb7\x7d\xe1\x07" |
| 398 |
"\xdc\xd2\xa6\x2e\x88\x0e\xa5\x3e\xeb\x62\xd5\x7c\xb4\x39\x02\x95" |
| 399 |
"\xdb\xc9\x94\x3a\xb7\x86\x96\xfa\x50\x4c\x11\x04\x61\x04\x1d\x1c" |
| 400 |
"\x64\xf0\x68\xcf\x45\xff\xa2\xa6\x3a\x81\xb7\xc1\x3f\x6b\x88\x47" |
| 401 |
"\xa3\xe7\x7e\xf1\x4f\xe3\xdb\x7f\xca\xfe\x0c\xbd\x10\xe8\xe8\x26" |
| 402 |
"\xe0\x34\x36\xd6\x46\xaa\xef\x87\xb2\xe2\x47\xd4\xaf\x1e\x8a\xbe" |
| 403 |
"\x1d\x75\x20\xf9\xc2\xa4\x5c\xb1\xeb\x8e\x95\xcf\xd5\x52\x62\xb7" |
| 404 |
"\x0b\x29\xfe\xec\x58\x64\xe1\x9c\x05\x4f\xf9\x91\x29\x28\x0e\x46" |
| 405 |
"\x46\x21\x77\x91\x81\x11\x42\x82\x03\x41\x26\x3c\x53\x15\x02\x31" |
| 406 |
"\x00\x8c\xb9\x1e\x82\xa3\x38\x6d\x28\x0f\x5d\x6f\x7e\x50\xe6\x41" |
| 407 |
"\xdf\x15\x2f\x71\x09\xed\x54\x56\xb3\x1f\x16\x6e\x6c\xac\x04\x25" |
| 408 |
"\xa7\xcf\x3a\xb6\xaf\x6b\x7f\xc3\x10\x3b\x88\x32\x02\xe9\x04\x65" |
| 409 |
"\x65\x02\x01\x01" |
| 410 |
}, |
| 411 |
|
| 412 |
{ "brainpoolP512r1", 422, |
| 413 |
"\x30\x82\x01\xa2\x02\x01\x01\x30\x4c\x06\x07\x2a\x86\x48\xce\x3d" |
| 414 |
"\x01\x01\x02\x41\x00\xaa\xdd\x9d\xb8\xdb\xe9\xc4\x8b\x3f\xd4\xe6" |
| 415 |
"\xae\x33\xc9\xfc\x07\xcb\x30\x8d\xb3\xb3\xc9\xd2\x0e\xd6\x63\x9c" |
| 416 |
"\xca\x70\x33\x08\x71\x7d\x4d\x9b\x00\x9b\xc6\x68\x42\xae\xcd\xa1" |
| 417 |
"\x2a\xe6\xa3\x80\xe6\x28\x81\xff\x2f\x2d\x82\xc6\x85\x28\xaa\x60" |
| 418 |
"\x56\x58\x3a\x48\xf3\x30\x81\x84\x04\x40\x78\x30\xa3\x31\x8b\x60" |
| 419 |
"\x3b\x89\xe2\x32\x71\x45\xac\x23\x4c\xc5\x94\xcb\xdd\x8d\x3d\xf9" |
| 420 |
"\x16\x10\xa8\x34\x41\xca\xea\x98\x63\xbc\x2d\xed\x5d\x5a\xa8\x25" |
| 421 |
"\x3a\xa1\x0a\x2e\xf1\xc9\x8b\x9a\xc8\xb5\x7f\x11\x17\xa7\x2b\xf2" |
| 422 |
"\xc7\xb9\xe7\xc1\xac\x4d\x77\xfc\x94\xca\x04\x40\x3d\xf9\x16\x10" |
| 423 |
"\xa8\x34\x41\xca\xea\x98\x63\xbc\x2d\xed\x5d\x5a\xa8\x25\x3a\xa1" |
| 424 |
"\x0a\x2e\xf1\xc9\x8b\x9a\xc8\xb5\x7f\x11\x17\xa7\x2b\xf2\xc7\xb9" |
| 425 |
"\xe7\xc1\xac\x4d\x77\xfc\x94\xca\xdc\x08\x3e\x67\x98\x40\x50\xb7" |
| 426 |
"\x5e\xba\xe5\xdd\x28\x09\xbd\x63\x80\x16\xf7\x23\x04\x81\x81\x04" |
| 427 |
"\x81\xae\xe4\xbd\xd8\x2e\xd9\x64\x5a\x21\x32\x2e\x9c\x4c\x6a\x93" |
| 428 |
"\x85\xed\x9f\x70\xb5\xd9\x16\xc1\xb4\x3b\x62\xee\xf4\xd0\x09\x8e" |
| 429 |
"\xff\x3b\x1f\x78\xe2\xd0\xd4\x8d\x50\xd1\x68\x7b\x93\xb9\x7d\x5f" |
| 430 |
"\x7c\x6d\x50\x47\x40\x6a\x5e\x68\x8b\x35\x22\x09\xbc\xb9\xf8\x22" |
| 431 |
"\x7d\xde\x38\x5d\x56\x63\x32\xec\xc0\xea\xbf\xa9\xcf\x78\x22\xfd" |
| 432 |
"\xf2\x09\xf7\x00\x24\xa5\x7b\x1a\xa0\x00\xc5\x5b\x88\x1f\x81\x11" |
| 433 |
"\xb2\xdc\xde\x49\x4a\x5f\x48\x5e\x5b\xca\x4b\xd8\x8a\x27\x63\xae" |
| 434 |
"\xd1\xca\x2b\x2f\xa8\xf0\x54\x06\x78\xcd\x1e\x0f\x3a\xd8\x08\x92" |
| 435 |
"\x02\x41\x00\xaa\xdd\x9d\xb8\xdb\xe9\xc4\x8b\x3f\xd4\xe6\xae\x33" |
| 436 |
"\xc9\xfc\x07\xcb\x30\x8d\xb3\xb3\xc9\xd2\x0e\xd6\x63\x9c\xca\x70" |
| 437 |
"\x33\x08\x70\x55\x3e\x5c\x41\x4c\xa9\x26\x19\x41\x86\x61\x19\x7f" |
| 438 |
"\xac\x10\x47\x1d\xb1\xd3\x81\x08\x5d\xda\xdd\xb5\x87\x96\x82\x9c" |
| 439 |
"\xa9\x00\x69\x02\x01\x01" |
| 440 |
}, |
| 441 |
|
| 442 |
{ "brainpoolP256r1", 195, /* with compressed base point */ |
| 443 |
"\x30\x81\xc0\x02\x01\x01\x30\x2c\x06\x07\x2a\x86\x48\xce\x3d\x01" |
| 444 |
"\x01\x02\x21\x00\xa9\xfb\x57\xdb\xa1\xee\xa9\xbc\x3e\x66\x0a\x90" |
| 445 |
"\x9d\x83\x8d\x72\x6e\x3b\xf6\x23\xd5\x26\x20\x28\x20\x13\x48\x1d" |
| 446 |
"\x1f\x6e\x53\x77\x30\x44\x04\x20\x7d\x5a\x09\x75\xfc\x2c\x30\x57" |
| 447 |
"\xee\xf6\x75\x30\x41\x7a\xff\xe7\xfb\x80\x55\xc1\x26\xdc\x5c\x6c" |
| 448 |
"\xe9\x4a\x4b\x44\xf3\x30\xb5\xd9\x04\x20\x26\xdc\x5c\x6c\xe9\x4a" |
| 449 |
"\x4b\x44\xf3\x30\xb5\xd9\xbb\xd7\x7c\xbf\x95\x84\x16\x29\x5c\xf7" |
| 450 |
"\xe1\xce\x6b\xcc\xdc\x18\xff\x8c\x07\xb6\x04\x21\x03\x8b\xd2\xae" |
| 451 |
"\xb9\xcb\x7e\x57\xcb\x2c\x4b\x48\x2f\xfc\x81\xb7\xaf\xb9\xde\x27" |
| 452 |
"\xe1\xe3\xbd\x23\xc2\x3a\x44\x53\xbd\x9a\xce\x32\x62\x02\x21\x00" |
| 453 |
"\xa9\xfb\x57\xdb\xa1\xee\xa9\xbc\x3e\x66\x0a\x90\x9d\x83\x8d\x71" |
| 454 |
"\x8c\x39\x7a\xa3\xb5\x61\xa6\xf7\x90\x1e\x0e\x82\x97\x48\x56\xa7" |
| 455 |
"\x02\x01\x01" |
| 456 |
}, |
| 457 |
|
| 458 |
{ "brainpoolP384r1", 276, /* with compressed base point */ |
| 459 |
"\x30\x82\x01\x10\x02\x01\x01\x30\x3c\x06\x07\x2a\x86\x48\xce\x3d" |
| 460 |
"\x01\x01\x02\x31\x00\x8c\xb9\x1e\x82\xa3\x38\x6d\x28\x0f\x5d\x6f" |
| 461 |
"\x7e\x50\xe6\x41\xdf\x15\x2f\x71\x09\xed\x54\x56\xb4\x12\xb1\xda" |
| 462 |
"\x19\x7f\xb7\x11\x23\xac\xd3\xa7\x29\x90\x1d\x1a\x71\x87\x47\x00" |
| 463 |
"\x13\x31\x07\xec\x53\x30\x64\x04\x30\x7b\xc3\x82\xc6\x3d\x8c\x15" |
| 464 |
"\x0c\x3c\x72\x08\x0a\xce\x05\xaf\xa0\xc2\xbe\xa2\x8e\x4f\xb2\x27" |
| 465 |
"\x87\x13\x91\x65\xef\xba\x91\xf9\x0f\x8a\xa5\x81\x4a\x50\x3a\xd4" |
| 466 |
"\xeb\x04\xa8\xc7\xdd\x22\xce\x28\x26\x04\x30\x04\xa8\xc7\xdd\x22" |
| 467 |
"\xce\x28\x26\x8b\x39\xb5\x54\x16\xf0\x44\x7c\x2f\xb7\x7d\xe1\x07" |
| 468 |
"\xdc\xd2\xa6\x2e\x88\x0e\xa5\x3e\xeb\x62\xd5\x7c\xb4\x39\x02\x95" |
| 469 |
"\xdb\xc9\x94\x3a\xb7\x86\x96\xfa\x50\x4c\x11\x04\x31\x03\x1d\x1c" |
| 470 |
"\x64\xf0\x68\xcf\x45\xff\xa2\xa6\x3a\x81\xb7\xc1\x3f\x6b\x88\x47" |
| 471 |
"\xa3\xe7\x7e\xf1\x4f\xe3\xdb\x7f\xca\xfe\x0c\xbd\x10\xe8\xe8\x26" |
| 472 |
"\xe0\x34\x36\xd6\x46\xaa\xef\x87\xb2\xe2\x47\xd4\xaf\x1e\x02\x31" |
| 473 |
"\x00\x8c\xb9\x1e\x82\xa3\x38\x6d\x28\x0f\x5d\x6f\x7e\x50\xe6\x41" |
| 474 |
"\xdf\x15\x2f\x71\x09\xed\x54\x56\xb3\x1f\x16\x6e\x6c\xac\x04\x25" |
| 475 |
"\xa7\xcf\x3a\xb6\xaf\x6b\x7f\xc3\x10\x3b\x88\x32\x02\xe9\x04\x65" |
| 476 |
"\x65\x02\x01\x01" |
| 477 |
}, |
| 478 |
|
| 479 |
{ "brainpoolP512r1", 357, /* with compressed base point */ |
| 480 |
"\x30\x82\x01\x61\x02\x01\x01\x30\x4c\x06\x07\x2a\x86\x48\xce\x3d" |
| 481 |
"\x01\x01\x02\x41\x00\xaa\xdd\x9d\xb8\xdb\xe9\xc4\x8b\x3f\xd4\xe6" |
| 482 |
"\xae\x33\xc9\xfc\x07\xcb\x30\x8d\xb3\xb3\xc9\xd2\x0e\xd6\x63\x9c" |
| 483 |
"\xca\x70\x33\x08\x71\x7d\x4d\x9b\x00\x9b\xc6\x68\x42\xae\xcd\xa1" |
| 484 |
"\x2a\xe6\xa3\x80\xe6\x28\x81\xff\x2f\x2d\x82\xc6\x85\x28\xaa\x60" |
| 485 |
"\x56\x58\x3a\x48\xf3\x30\x81\x84\x04\x40\x78\x30\xa3\x31\x8b\x60" |
| 486 |
"\x3b\x89\xe2\x32\x71\x45\xac\x23\x4c\xc5\x94\xcb\xdd\x8d\x3d\xf9" |
| 487 |
"\x16\x10\xa8\x34\x41\xca\xea\x98\x63\xbc\x2d\xed\x5d\x5a\xa8\x25" |
| 488 |
"\x3a\xa1\x0a\x2e\xf1\xc9\x8b\x9a\xc8\xb5\x7f\x11\x17\xa7\x2b\xf2" |
| 489 |
"\xc7\xb9\xe7\xc1\xac\x4d\x77\xfc\x94\xca\x04\x40\x3d\xf9\x16\x10" |
| 490 |
"\xa8\x34\x41\xca\xea\x98\x63\xbc\x2d\xed\x5d\x5a\xa8\x25\x3a\xa1" |
| 491 |
"\x0a\x2e\xf1\xc9\x8b\x9a\xc8\xb5\x7f\x11\x17\xa7\x2b\xf2\xc7\xb9" |
| 492 |
"\xe7\xc1\xac\x4d\x77\xfc\x94\xca\xdc\x08\x3e\x67\x98\x40\x50\xb7" |
| 493 |
"\x5e\xba\xe5\xdd\x28\x09\xbd\x63\x80\x16\xf7\x23\x04\x41\x02\x81" |
| 494 |
"\xae\xe4\xbd\xd8\x2e\xd9\x64\x5a\x21\x32\x2e\x9c\x4c\x6a\x93\x85" |
| 495 |
"\xed\x9f\x70\xb5\xd9\x16\xc1\xb4\x3b\x62\xee\xf4\xd0\x09\x8e\xff" |
| 496 |
"\x3b\x1f\x78\xe2\xd0\xd4\x8d\x50\xd1\x68\x7b\x93\xb9\x7d\x5f\x7c" |
| 497 |
"\x6d\x50\x47\x40\x6a\x5e\x68\x8b\x35\x22\x09\xbc\xb9\xf8\x22\x02" |
| 498 |
"\x41\x00\xaa\xdd\x9d\xb8\xdb\xe9\xc4\x8b\x3f\xd4\xe6\xae\x33\xc9" |
| 499 |
"\xfc\x07\xcb\x30\x8d\xb3\xb3\xc9\xd2\x0e\xd6\x63\x9c\xca\x70\x33" |
| 500 |
"\x08\x70\x55\x3e\x5c\x41\x4c\xa9\x26\x19\x41\x86\x61\x19\x7f\xac" |
| 501 |
"\x10\x47\x1d\xb1\xd3\x81\x08\x5d\xda\xdd\xb5\x87\x96\x82\x9c\xa9" |
| 502 |
"\x00\x69\x02\x01\x01" |
| 503 |
}, |
| 504 |
|
| 505 |
{ NULL } |
| 506 |
}; |
| 507 |
|
| 508 |
|
| 509 |
#define TLV_LENGTH(prefix) do { \ |
| 510 |
if (!prefix ## len) \ |
| 511 |
return gpg_error (GPG_ERR_INV_KEYINFO); \ |
| 512 |
c = *(prefix)++; prefix ## len--; \ |
| 513 |
if (c == 0x80) \ |
| 514 |
return gpg_error (GPG_ERR_NOT_DER_ENCODED); \ |
| 515 |
if (c == 0xff) \ |
| 516 |
return gpg_error (GPG_ERR_BAD_BER); \ |
| 517 |
\ |
| 518 |
if ( !(c & 0x80) ) \ |
| 519 |
len = c; \ |
| 520 |
else \ |
| 521 |
{ \ |
| 522 |
int count = c & 0x7f; \ |
| 523 |
\ |
| 524 |
for (len=0; count; count--) \ |
| 525 |
{ \ |
| 526 |
len <<= 8; \ |
| 527 |
if (!prefix ## len) \ |
| 528 |
return gpg_error (GPG_ERR_BAD_BER);\ |
| 529 |
c = *(prefix)++; prefix ## len--; \ |
| 530 |
len |= c & 0xff; \ |
| 531 |
} \ |
| 532 |
} \ |
| 533 |
if (len > prefix ## len) \ |
| 534 |
return gpg_error (GPG_ERR_INV_KEYINFO); \ |
| 535 |
} while (0) |
| 536 |
|
| 537 |
|
| 538 |
/* Given a string BUF of length BUFLEN with either a curve name or its |
| 539 |
* OID in dotted form return a string in dotted form of the name. The |
| 540 |
* caller must free the result. On error NULL is returned. If a |
| 541 |
* curve requires the use of a certain algorithm, that algorithm is |
| 542 |
* stored at R_PKALGO. */ |
| 543 |
static char * |
| 544 |
get_ecc_curve_oid (const unsigned char *buf, size_t buflen, pkalgo_t *r_pkalgo) |
| 545 |
{ |
| 546 |
unsigned char *result; |
| 547 |
int i, find_pkalgo; |
| 548 |
|
| 549 |
/* Skip an optional "oid." prefix. */ |
| 550 |
if (buflen > 4 && buf[3] == '.' && digitp (buf+4) |
| 551 |
&& ((buf[0] == 'o' && buf[1] == 'i' && buf[2] == 'd') |
| 552 |
||(buf[0] == 'O' && buf[1] == 'I' && buf[2] == 'D'))) |
| 553 |
{ |
| 554 |
buf += 4; |
| 555 |
buflen -= 4; |
| 556 |
} |
| 557 |
|
| 558 |
/* If it does not look like an OID - map it through the table. */ |
| 559 |
if (buflen && !digitp (buf)) |
| 560 |
{ |
| 561 |
for (i=0; curve_names[i].oid; i++) |
| 562 |
if (buflen == strlen (curve_names[i].name) |
| 563 |
&& !memcmp (buf, curve_names[i].name, buflen)) |
| 564 |
break; |
| 565 |
if (!curve_names[i].oid) |
| 566 |
return NULL; /* Not found. */ |
| 567 |
buf = curve_names[i].oid; |
| 568 |
buflen = strlen (curve_names[i].oid); |
| 569 |
*r_pkalgo = curve_names[i].pkalgo; |
| 570 |
find_pkalgo = 0; |
| 571 |
} |
| 572 |
else |
| 573 |
find_pkalgo = 1; |
| 574 |
|
| 575 |
result = xtrymalloc (buflen + 1); |
| 576 |
if (!result) |
| 577 |
return NULL; /* Ooops */ |
| 578 |
memcpy (result, buf, buflen); |
| 579 |
result[buflen] = 0; |
| 580 |
|
| 581 |
if (find_pkalgo) |
| 582 |
{ |
| 583 |
/* We still need to check whether the OID requires a certain ALGO. */ |
| 584 |
for (i=0; curve_names[i].oid; i++) |
| 585 |
if (!strcmp (curve_names[i].oid, result)) |
| 586 |
{ |
| 587 |
*r_pkalgo = curve_names[i].pkalgo; |
| 588 |
break; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
return result; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
/* Return the OFF and the LEN of algorithm within DER. Do some checks |
| 598 |
and return the number of bytes read in r_nread, adding this to der |
| 599 |
does point into the BIT STRING. |
| 600 |
|
| 601 |
mode 0: just get the algorithm identifier. FIXME: should be able to |
| 602 |
handle BER Encoding. |
| 603 |
mode 1: as described. |
| 604 |
*/ |
| 605 |
static gpg_error_t |
| 606 |
get_algorithm (int mode, const unsigned char *der, size_t derlen, int firsttag, |
| 607 |
size_t *r_nread, size_t *r_pos, size_t *r_len, int *r_bitstr, |
| 608 |
size_t *r_parm_pos, size_t *r_parm_len, int *r_parm_type) |
| 609 |
{ |
| 610 |
int c; |
| 611 |
const unsigned char *start = der; |
| 612 |
const unsigned char *startseq; |
| 613 |
unsigned long seqlen, len; |
| 614 |
|
| 615 |
*r_bitstr = 0; |
| 616 |
if (r_parm_pos) |
| 617 |
*r_parm_pos = 0; |
| 618 |
if (r_parm_len) |
| 619 |
*r_parm_len = 0; |
| 620 |
if (r_parm_type) |
| 621 |
*r_parm_type = 0; |
| 622 |
/* get the inner sequence */ |
| 623 |
if (!derlen) |
| 624 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 625 |
c = *der++; derlen--; |
| 626 |
if ( c != firsttag ) |
| 627 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); /* not a SEQUENCE or whatever */ |
| 628 |
TLV_LENGTH(der); |
| 629 |
seqlen = len; |
| 630 |
startseq = der; |
| 631 |
|
| 632 |
/* get the object identifier */ |
| 633 |
if (!derlen) |
| 634 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 635 |
c = *der++; derlen--; |
| 636 |
if ( c != 0x06 ) |
| 637 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); /* not an OBJECT IDENTIFIER */ |
| 638 |
TLV_LENGTH(der); |
| 639 |
|
| 640 |
/* DER does now point to an oid of length LEN */ |
| 641 |
/* { */ |
| 642 |
/* char *p = ksba_oid_to_str (der, len); */ |
| 643 |
/* gpgrt_log_debug ("%s: algorithm: %s\n", __func__, p); */ |
| 644 |
/* xfree (p); */ |
| 645 |
/* } */ |
| 646 |
*r_pos = der - start; |
| 647 |
*r_len = len; |
| 648 |
der += len; |
| 649 |
derlen -= len; |
| 650 |
seqlen -= der - startseq;; |
| 651 |
|
| 652 |
/* Parse the parameter. */ |
| 653 |
if (seqlen) |
| 654 |
{ |
| 655 |
const unsigned char *startparm = der; |
| 656 |
|
| 657 |
if (!derlen) |
| 658 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 659 |
c = *der++; derlen--; |
| 660 |
if ( c == 0x05 ) |
| 661 |
{ |
| 662 |
/* gpgrt_log_debug ("%s: parameter: NULL \n", __func__); */ |
| 663 |
if (!derlen) |
| 664 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 665 |
c = *der++; derlen--; |
| 666 |
if (c) |
| 667 |
return gpg_error (GPG_ERR_BAD_BER); /* NULL must have a |
| 668 |
length of 0 */ |
| 669 |
seqlen -= 2; |
| 670 |
} |
| 671 |
else if (r_parm_pos && r_parm_len && c == 0x04) |
| 672 |
{ |
| 673 |
/* This is an octet string parameter and we need it. */ |
| 674 |
/* gpgrt_log_debug ("%s: parameter: an octet string\n", __func__); */ |
| 675 |
if (r_parm_type) |
| 676 |
*r_parm_type = TYPE_OCTET_STRING; |
| 677 |
TLV_LENGTH(der); |
| 678 |
*r_parm_pos = der - start; |
| 679 |
*r_parm_len = len; |
| 680 |
seqlen -= der - startparm; |
| 681 |
der += len; |
| 682 |
derlen -= len; |
| 683 |
seqlen -= len; |
| 684 |
} |
| 685 |
else if (r_parm_pos && r_parm_len && c == 0x06) |
| 686 |
{ |
| 687 |
/* This is an object identifier. */ |
| 688 |
/* gpgrt_log_debug ("%s: parameter: an OID\n", __func__); */ |
| 689 |
if (r_parm_type) |
| 690 |
*r_parm_type = TYPE_OBJECT_ID; |
| 691 |
TLV_LENGTH(der); |
| 692 |
*r_parm_pos = der - start; |
| 693 |
*r_parm_len = len; |
| 694 |
seqlen -= der - startparm; |
| 695 |
der += len; |
| 696 |
derlen -= len; |
| 697 |
seqlen -= len; |
| 698 |
} |
| 699 |
else if (r_parm_pos && r_parm_len && c == 0x30) |
| 700 |
{ |
| 701 |
/* This is a sequence. */ |
| 702 |
/* gpgrt_log_debug ("%s: parameter: a sequence\n", __func__); */ |
| 703 |
if (r_parm_type) |
| 704 |
*r_parm_type = TYPE_SEQUENCE; |
| 705 |
TLV_LENGTH(der); |
| 706 |
*r_parm_pos = startparm - start; |
| 707 |
*r_parm_len = len + (der - startparm); |
| 708 |
seqlen -= der - startparm; |
| 709 |
der += len; |
| 710 |
derlen -= len; |
| 711 |
seqlen -= len; |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
/* gpgrt_log_debug ("%s: parameter: with tag %02x - ignored\n", */ |
| 716 |
/* __func__, c); */ |
| 717 |
TLV_LENGTH(der); |
| 718 |
seqlen -= der - startparm; |
| 719 |
/* skip the value */ |
| 720 |
der += len; |
| 721 |
derlen -= len; |
| 722 |
seqlen -= len; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
if (seqlen) |
| 727 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 728 |
|
| 729 |
if (mode) |
| 730 |
{ |
| 731 |
/* move forward to the BIT_STR */ |
| 732 |
if (!derlen) |
| 733 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 734 |
c = *der++; derlen--; |
| 735 |
|
| 736 |
if (c == 0x03) |
| 737 |
*r_bitstr = 1; /* BIT STRING */ |
| 738 |
else if (c == 0x04) |
| 739 |
; /* OCTECT STRING */ |
| 740 |
else |
| 741 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); /* not a BIT STRING */ |
| 742 |
TLV_LENGTH(der); |
| 743 |
} |
| 744 |
|
| 745 |
*r_nread = der - start; |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
gpg_error_t |
| 751 |
_ksba_parse_algorithm_identifier (const unsigned char *der, size_t derlen, |
| 752 |
size_t *r_nread, char **r_oid) |
| 753 |
{ |
| 754 |
return _ksba_parse_algorithm_identifier3 (der, derlen, 0x30, |
| 755 |
r_nread, r_oid, NULL, NULL, NULL); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
gpg_error_t |
| 760 |
_ksba_parse_algorithm_identifier2 (const unsigned char *der, size_t derlen, |
| 761 |
size_t *r_nread, char **r_oid, |
| 762 |
char **r_parm, size_t *r_parmlen) |
| 763 |
{ |
| 764 |
return _ksba_parse_algorithm_identifier3 (der, derlen, 0x30, |
| 765 |
r_nread, r_oid, |
| 766 |
r_parm, r_parmlen, NULL); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
/* Note that R_NREAD, R_PARM, and R_PARMLEN are optional. */ |
| 771 |
gpg_error_t |
| 772 |
_ksba_parse_algorithm_identifier3 (const unsigned char *der, size_t derlen, |
| 773 |
int firsttag, |
| 774 |
size_t *r_nread, char **r_oid, |
| 775 |
char **r_parm, size_t *r_parmlen, |
| 776 |
int *r_parmtype) |
| 777 |
{ |
| 778 |
gpg_error_t err; |
| 779 |
int is_bitstr; |
| 780 |
size_t nread, off, len, off2, len2; |
| 781 |
int parm_type; |
| 782 |
|
| 783 |
/* fixme: get_algorithm might return the error invalid keyinfo - |
| 784 |
this should be invalid algorithm identifier */ |
| 785 |
*r_oid = NULL; |
| 786 |
if (r_nread) |
| 787 |
*r_nread = 0; |
| 788 |
off2 = len2 = 0; |
| 789 |
err = get_algorithm (0, der, derlen, firsttag, |
| 790 |
&nread, &off, &len, &is_bitstr, |
| 791 |
&off2, &len2, &parm_type); |
| 792 |
if (err) |
| 793 |
return err; |
| 794 |
if (r_nread) |
| 795 |
*r_nread = nread; |
| 796 |
*r_oid = ksba_oid_to_str (der+off, len); |
| 797 |
if (!*r_oid) |
| 798 |
return gpg_error (GPG_ERR_ENOMEM); |
| 799 |
|
| 800 |
/* Special hack for ecdsaWithSpecified. We replace the returned OID |
| 801 |
by the one in the parameter. */ |
| 802 |
if (off2 && len2 && parm_type == TYPE_SEQUENCE && firsttag == 0x30 |
| 803 |
&& !strcmp (*r_oid, "1.2.840.10045.4.3")) |
| 804 |
{ |
| 805 |
xfree (*r_oid); |
| 806 |
*r_oid = NULL; |
| 807 |
err = get_algorithm (0, der+off2, len2, 0x30, |
| 808 |
&nread, &off, &len, &is_bitstr, |
| 809 |
NULL, NULL, NULL); |
| 810 |
if (err) |
| 811 |
{ |
| 812 |
if (r_nread) |
| 813 |
*r_nread = 0; |
| 814 |
return err; |
| 815 |
} |
| 816 |
*r_oid = ksba_oid_to_str (der+off2+off, len); |
| 817 |
if (!*r_oid) |
| 818 |
{ |
| 819 |
if (r_nread) |
| 820 |
*r_nread = 0; |
| 821 |
return gpg_error (GPG_ERR_ENOMEM); |
| 822 |
} |
| 823 |
|
| 824 |
off2 = len2 = 0; /* So that R_PARM is set to NULL. */ |
| 825 |
} |
| 826 |
|
| 827 |
if (r_parm && r_parmlen) |
| 828 |
{ |
| 829 |
if (off2 && len2) |
| 830 |
{ |
| 831 |
*r_parm = xtrymalloc (len2); |
| 832 |
if (!*r_parm) |
| 833 |
{ |
| 834 |
xfree (*r_oid); |
| 835 |
*r_oid = NULL; |
| 836 |
return gpg_error (GPG_ERR_ENOMEM); |
| 837 |
} |
| 838 |
memcpy (*r_parm, der+off2, len2); |
| 839 |
*r_parmlen = len2; |
| 840 |
} |
| 841 |
else |
| 842 |
{ |
| 843 |
*r_parm = NULL; |
| 844 |
*r_parmlen = 0; |
| 845 |
} |
| 846 |
} |
| 847 |
if (r_parmtype) |
| 848 |
*r_parmtype = parm_type; |
| 849 |
|
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
/* Assume that DER is a buffer of length DERLEN with a DER encoded |
| 855 |
ASN.1 structure like this: |
| 856 |
|
| 857 |
keyInfo ::= SEQUENCE { |
| 858 |
SEQUENCE { |
| 859 |
algorithm OBJECT IDENTIFIER, |
| 860 |
parameters ANY DEFINED BY algorithm OPTIONAL } |
| 861 |
publicKey BIT STRING } |
| 862 |
|
| 863 |
The function parses this structure and create a SEXP suitable to be |
| 864 |
used as a public key in Libgcrypt. The S-Exp will be returned in a |
| 865 |
string which the caller must free. |
| 866 |
|
| 867 |
We don't pass an ASN.1 node here but a plain memory block. */ |
| 868 |
|
| 869 |
gpg_error_t |
| 870 |
_ksba_keyinfo_to_sexp (const unsigned char *der, size_t derlen, |
| 871 |
ksba_sexp_t *r_string) |
| 872 |
{ |
| 873 |
gpg_error_t err; |
| 874 |
int c, i; |
| 875 |
size_t nread, off, len, parm_off, parm_len; |
| 876 |
int parm_type; |
| 877 |
char *parm_oid = NULL; |
| 878 |
int algoidx; |
| 879 |
int is_bitstr; |
| 880 |
int got_curve = 0; |
| 881 |
const unsigned char *parmder = NULL; |
| 882 |
size_t parmderlen = 0; |
| 883 |
const unsigned char *ctrl; |
| 884 |
const char *elem; |
| 885 |
struct stringbuf sb; |
| 886 |
|
| 887 |
*r_string = NULL; |
| 888 |
|
| 889 |
/* check the outer sequence */ |
| 890 |
if (!derlen) |
| 891 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 892 |
c = *der++; derlen--; |
| 893 |
if ( c != 0x30 ) |
| 894 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); /* not a SEQUENCE */ |
| 895 |
TLV_LENGTH(der); |
| 896 |
/* and now the inner part */ |
| 897 |
err = get_algorithm (1, der, derlen, 0x30, |
| 898 |
&nread, &off, &len, &is_bitstr, |
| 899 |
&parm_off, &parm_len, &parm_type); |
| 900 |
if (err) |
| 901 |
return err; |
| 902 |
|
| 903 |
/* look into our table of supported algorithms */ |
| 904 |
for (algoidx=0; pk_algo_table[algoidx].oid; algoidx++) |
| 905 |
{ |
| 906 |
if ( len == pk_algo_table[algoidx].oidlen |
| 907 |
&& !memcmp (der+off, pk_algo_table[algoidx].oid, len)) |
| 908 |
break; |
| 909 |
} |
| 910 |
if (!pk_algo_table[algoidx].oid) |
| 911 |
return gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); |
| 912 |
if (!pk_algo_table[algoidx].supported) |
| 913 |
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM); |
| 914 |
|
| 915 |
if (parm_off && parm_len && parm_type == TYPE_OBJECT_ID) |
| 916 |
parm_oid = ksba_oid_to_str (der+parm_off, parm_len); |
| 917 |
else if (parm_off && parm_len) |
| 918 |
{ |
| 919 |
parmder = der + parm_off; |
| 920 |
parmderlen = parm_len; |
| 921 |
} |
| 922 |
|
| 923 |
der += nread; |
| 924 |
derlen -= nread; |
| 925 |
|
| 926 |
if (is_bitstr) |
| 927 |
{ /* Funny: X.509 defines the signature value as a bit string but |
| 928 |
CMS as an octet string - for ease of implementation we always |
| 929 |
allow both */ |
| 930 |
if (!derlen) |
| 931 |
{ |
| 932 |
xfree (parm_oid); |
| 933 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 934 |
} |
| 935 |
c = *der++; derlen--; |
| 936 |
if (c) |
| 937 |
gpgrt_log_info ("%s: warning: number of unused bits is not zero\n", |
| 938 |
__func__); |
| 939 |
} |
| 940 |
|
| 941 |
/* fixme: we should calculate the initial length form the size of the |
| 942 |
sequence, so that we don't need a realloc later */ |
| 943 |
init_stringbuf (&sb, 100); |
| 944 |
put_stringbuf (&sb, "(10:public-key("); |
| 945 |
|
| 946 |
/* fixme: we can also use the oidstring here and prefix it with |
| 947 |
"oid." - this way we can pass more information into Libgcrypt or |
| 948 |
whatever library is used */ |
| 949 |
put_stringbuf_sexp (&sb, pk_algo_table[algoidx].algo_string); |
| 950 |
|
| 951 |
/* Insert the curve name for ECC. */ |
| 952 |
if (pk_algo_table[algoidx].pkalgo == PKALGO_ECC && parm_oid) |
| 953 |
{ |
| 954 |
put_stringbuf (&sb, "("); |
| 955 |
put_stringbuf_sexp (&sb, "curve"); |
| 956 |
put_stringbuf_sexp (&sb, parm_oid); |
| 957 |
put_stringbuf (&sb, ")"); |
| 958 |
got_curve = 1; |
| 959 |
} |
| 960 |
else if (pk_algo_table[algoidx].pkalgo == PKALGO_ED25519 |
| 961 |
|| pk_algo_table[algoidx].pkalgo == PKALGO_ED448 |
| 962 |
|| pk_algo_table[algoidx].pkalgo == PKALGO_X25519 |
| 963 |
|| pk_algo_table[algoidx].pkalgo == PKALGO_X448) |
| 964 |
{ |
| 965 |
put_stringbuf (&sb, "("); |
| 966 |
put_stringbuf_sexp (&sb, "curve"); |
| 967 |
put_stringbuf_sexp (&sb, pk_algo_table[algoidx].oidstring); |
| 968 |
put_stringbuf (&sb, ")"); |
| 969 |
} |
| 970 |
|
| 971 |
/* If parameters are given and we have a description for them, parse |
| 972 |
them. */ |
| 973 |
if (parmder && parmderlen |
| 974 |
&& pk_algo_table[algoidx].parmelem_string |
| 975 |
&& pk_algo_table[algoidx].parmctrl_string) |
| 976 |
{ |
| 977 |
elem = pk_algo_table[algoidx].parmelem_string; |
| 978 |
ctrl = pk_algo_table[algoidx].parmctrl_string; |
| 979 |
for (; *elem; ctrl++, elem++) |
| 980 |
{ |
| 981 |
int is_int; |
| 982 |
|
| 983 |
if ( (*ctrl & 0x80) && !elem[1] ) |
| 984 |
{ |
| 985 |
/* Hack to allow reading a raw value. */ |
| 986 |
is_int = 1; |
| 987 |
len = parmderlen; |
| 988 |
} |
| 989 |
else |
| 990 |
{ |
| 991 |
if (!parmderlen) |
| 992 |
{ |
| 993 |
xfree (parm_oid); |
| 994 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 995 |
} |
| 996 |
c = *parmder++; parmderlen--; |
| 997 |
if ( c != *ctrl ) |
| 998 |
{ |
| 999 |
xfree (parm_oid); |
| 1000 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); |
| 1001 |
} |
| 1002 |
is_int = c == 0x02; |
| 1003 |
TLV_LENGTH (parmder); |
| 1004 |
} |
| 1005 |
if (is_int && *elem != '-') /* Take this integer. */ |
| 1006 |
{ |
| 1007 |
char tmp[2]; |
| 1008 |
|
| 1009 |
put_stringbuf (&sb, "("); |
| 1010 |
tmp[0] = *elem; tmp[1] = 0; |
| 1011 |
put_stringbuf_sexp (&sb, tmp); |
| 1012 |
put_stringbuf_mem_sexp (&sb, parmder, len); |
| 1013 |
parmder += len; |
| 1014 |
parmderlen -= len; |
| 1015 |
put_stringbuf (&sb, ")"); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
else if (!got_curve && parmder && parmderlen |
| 1020 |
&& pk_algo_table[algoidx].pkalgo == PKALGO_ECC) |
| 1021 |
{ |
| 1022 |
/* This is ecPublicKey but has no named curve. This is not |
| 1023 |
* allowed for PKIX but we try to figure the curve name out for |
| 1024 |
* some well known curves by a simple parameter match. */ |
| 1025 |
for (i=0; ecdomainparm_to_name[i].name; i++) |
| 1026 |
if (ecdomainparm_to_name[i].derlen == parmderlen |
| 1027 |
&& !memcmp (ecdomainparm_to_name[i].der, parmder, parmderlen)) |
| 1028 |
{ |
| 1029 |
put_stringbuf (&sb, "("); |
| 1030 |
put_stringbuf_sexp (&sb, "curve"); |
| 1031 |
put_stringbuf_sexp (&sb, ecdomainparm_to_name[i].name); |
| 1032 |
put_stringbuf (&sb, ")"); |
| 1033 |
got_curve = 1; |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
/* if (!got_curve) */ |
| 1037 |
/* gpgrt_log_printhex (parmder, parmderlen, "ECDomainParm:"); */ |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
/* FIXME: We don't release the stringbuf in case of error |
| 1042 |
better let the macro jump to a label */ |
| 1043 |
elem = pk_algo_table[algoidx].elem_string; |
| 1044 |
ctrl = pk_algo_table[algoidx].ctrl_string; |
| 1045 |
for (; *elem; ctrl++, elem++) |
| 1046 |
{ |
| 1047 |
int is_int; |
| 1048 |
|
| 1049 |
if ( (*ctrl & 0x80) && !elem[1] ) |
| 1050 |
{ |
| 1051 |
/* Hack to allow reading a raw value. */ |
| 1052 |
is_int = 1; |
| 1053 |
len = derlen; |
| 1054 |
} |
| 1055 |
else |
| 1056 |
{ |
| 1057 |
if (!derlen) |
| 1058 |
{ |
| 1059 |
xfree (parm_oid); |
| 1060 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 1061 |
} |
| 1062 |
c = *der++; derlen--; |
| 1063 |
if ( c != *ctrl ) |
| 1064 |
{ |
| 1065 |
xfree (parm_oid); |
| 1066 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); |
| 1067 |
} |
| 1068 |
is_int = c == 0x02; |
| 1069 |
TLV_LENGTH (der); |
| 1070 |
} |
| 1071 |
if (is_int && *elem != '-') /* Take this integer. */ |
| 1072 |
{ |
| 1073 |
char tmp[2]; |
| 1074 |
|
| 1075 |
put_stringbuf (&sb, "("); |
| 1076 |
tmp[0] = *elem; tmp[1] = 0; |
| 1077 |
put_stringbuf_sexp (&sb, tmp); |
| 1078 |
put_stringbuf_mem_sexp (&sb, der, len); |
| 1079 |
der += len; |
| 1080 |
derlen -= len; |
| 1081 |
put_stringbuf (&sb, ")"); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
put_stringbuf (&sb, "))"); |
| 1085 |
xfree (parm_oid); |
| 1086 |
|
| 1087 |
*r_string = get_stringbuf (&sb); |
| 1088 |
if (!*r_string) |
| 1089 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1090 |
|
| 1091 |
return 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
/* Match the algorithm string given in BUF which is of length BUFLEN |
| 1096 |
* with the known algorithms from our table and return the table |
| 1097 |
* entriy with the OID string. If WITH_SIG is true, the table of |
| 1098 |
* signature algorithms is consulted first. */ |
| 1099 |
static const char * |
| 1100 |
oid_from_buffer (const unsigned char *buf, unsigned int buflen, |
| 1101 |
pkalgo_t *r_pkalgo, int with_sig) |
| 1102 |
{ |
| 1103 |
int i; |
| 1104 |
|
| 1105 |
/* Ignore an optional "oid." prefix. */ |
| 1106 |
if (buflen > 4 && buf[3] == '.' && digitp (buf+4) |
| 1107 |
&& ((buf[0] == 'o' && buf[1] == 'i' && buf[2] == 'd') |
| 1108 |
||(buf[0] == 'O' && buf[1] == 'I' && buf[2] == 'D'))) |
| 1109 |
{ |
| 1110 |
buf += 4; |
| 1111 |
buflen -= 4; |
| 1112 |
} |
| 1113 |
|
| 1114 |
if (with_sig) |
| 1115 |
{ |
| 1116 |
/* Scan the signature table first. */ |
| 1117 |
for (i=0; sig_algo_table[i].oid; i++) |
| 1118 |
{ |
| 1119 |
if (!sig_algo_table[i].supported) |
| 1120 |
continue; |
| 1121 |
if (buflen == strlen (sig_algo_table[i].oidstring) |
| 1122 |
&& !memcmp (buf, sig_algo_table[i].oidstring, buflen)) |
| 1123 |
break; |
| 1124 |
if (buflen == strlen (sig_algo_table[i].algo_string) |
| 1125 |
&& !memcmp (buf, sig_algo_table[i].algo_string, buflen)) |
| 1126 |
break; |
| 1127 |
} |
| 1128 |
if (sig_algo_table[i].oid) |
| 1129 |
{ |
| 1130 |
*r_pkalgo = sig_algo_table[i].pkalgo; |
| 1131 |
return sig_algo_table[i].oidstring; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* Scan the standard table. */ |
| 1136 |
for (i=0; pk_algo_table[i].oid; i++) |
| 1137 |
{ |
| 1138 |
if (!pk_algo_table[i].supported) |
| 1139 |
continue; |
| 1140 |
if (buflen == strlen (pk_algo_table[i].oidstring) |
| 1141 |
&& !memcmp (buf, pk_algo_table[i].oidstring, buflen)) |
| 1142 |
break; |
| 1143 |
if (buflen == strlen (pk_algo_table[i].algo_string) |
| 1144 |
&& !memcmp (buf, pk_algo_table[i].algo_string, buflen)) |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
if (!pk_algo_table[i].oid) |
| 1148 |
return NULL; |
| 1149 |
|
| 1150 |
*r_pkalgo = pk_algo_table[i].pkalgo; |
| 1151 |
return pk_algo_table[i].oidstring; |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
/* If ALGOINFOMODE is false: Take the "public-key" s-expression SEXP |
| 1156 |
* and convert it into a DER encoded publicKeyInfo. |
| 1157 |
* |
| 1158 |
* If ALGOINFOMODE is true: Take the "sig-val" s-expression SEXP and |
| 1159 |
* convert it into a DER encoded algorithmInfo. */ |
| 1160 |
gpg_error_t |
| 1161 |
_ksba_keyinfo_from_sexp (ksba_const_sexp_t sexp, int algoinfomode, |
| 1162 |
unsigned char **r_der, size_t *r_derlen) |
| 1163 |
{ |
| 1164 |
gpg_error_t err; |
| 1165 |
const unsigned char *s; |
| 1166 |
char *endp; |
| 1167 |
unsigned long n; |
| 1168 |
const char *algo_oid; |
| 1169 |
char *curve_oid = NULL; |
| 1170 |
pkalgo_t pkalgo, force_pkalgo; |
| 1171 |
int i; |
| 1172 |
struct { |
| 1173 |
const char *name; |
| 1174 |
int namelen; |
| 1175 |
const unsigned char *value; |
| 1176 |
int valuelen; |
| 1177 |
} parm[10]; |
| 1178 |
int parmidx; |
| 1179 |
const char *parmdesc, *algoparmdesc; |
| 1180 |
ksba_der_t dbld = NULL; |
| 1181 |
ksba_der_t dbld2 = NULL; |
| 1182 |
unsigned char *tmpder; |
| 1183 |
size_t tmpderlen; |
| 1184 |
|
| 1185 |
if (!sexp) |
| 1186 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1187 |
|
| 1188 |
s = sexp; |
| 1189 |
if (*s != '(') |
| 1190 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 1191 |
s++; |
| 1192 |
|
| 1193 |
n = strtoul (s, &endp, 10); |
| 1194 |
s = endp; |
| 1195 |
if (!n || *s != ':') |
| 1196 |
return gpg_error (GPG_ERR_INV_SEXP); /* We don't allow empty lengths. */ |
| 1197 |
s++; |
| 1198 |
|
| 1199 |
if (algoinfomode && n == 7 && !memcmp (s, "sig-val", 7)) |
| 1200 |
s += 7; |
| 1201 |
else if (n == 10 || !memcmp (s, "public-key", 10)) |
| 1202 |
s += 10; |
| 1203 |
else |
| 1204 |
return gpg_error (GPG_ERR_UNKNOWN_SEXP); |
| 1205 |
|
| 1206 |
if (*s != '(') |
| 1207 |
return gpg_error (digitp (s)? GPG_ERR_UNKNOWN_SEXP : GPG_ERR_INV_SEXP); |
| 1208 |
s++; |
| 1209 |
|
| 1210 |
/* Break out the algorithm ID */ |
| 1211 |
n = strtoul (s, &endp, 10); |
| 1212 |
s = endp; |
| 1213 |
if (!n || *s != ':') |
| 1214 |
return gpg_error (GPG_ERR_INV_SEXP); /* We don't allow empty lengths. */ |
| 1215 |
s++; |
| 1216 |
|
| 1217 |
algo_oid = oid_from_buffer (s, n, &pkalgo, algoinfomode); |
| 1218 |
if (!algo_oid) |
| 1219 |
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM); |
| 1220 |
s += n; |
| 1221 |
|
| 1222 |
/* Collect all the values. */ |
| 1223 |
force_pkalgo = 0; |
| 1224 |
for (parmidx = 0; *s != ')' ; parmidx++) |
| 1225 |
{ |
| 1226 |
if (parmidx >= DIM(parm)) |
| 1227 |
{ |
| 1228 |
err = gpg_error (GPG_ERR_GENERAL); |
| 1229 |
goto leave; |
| 1230 |
} |
| 1231 |
if (*s != '(') |
| 1232 |
{ |
| 1233 |
err = gpg_error (digitp(s)? GPG_ERR_UNKNOWN_SEXP:GPG_ERR_INV_SEXP); |
| 1234 |
goto leave; |
| 1235 |
} |
| 1236 |
s++; |
| 1237 |
n = strtoul (s, &endp, 10); |
| 1238 |
s = endp; |
| 1239 |
if (!n || *s != ':') |
| 1240 |
{ |
| 1241 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 1242 |
goto leave; |
| 1243 |
} |
| 1244 |
s++; |
| 1245 |
parm[parmidx].name = s; |
| 1246 |
parm[parmidx].namelen = n; |
| 1247 |
s += n; |
| 1248 |
if (!digitp(s)) |
| 1249 |
{ |
| 1250 |
err = gpg_error (GPG_ERR_UNKNOWN_SEXP); /* ... or invalid S-Exp. */ |
| 1251 |
goto leave; |
| 1252 |
} |
| 1253 |
|
| 1254 |
n = strtoul (s, &endp, 10); |
| 1255 |
s = endp; |
| 1256 |
if (!n || *s != ':') |
| 1257 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 1258 |
s++; |
| 1259 |
parm[parmidx].value = s; |
| 1260 |
parm[parmidx].valuelen = n; |
| 1261 |
s += n; |
| 1262 |
if ( *s != ')') |
| 1263 |
{ |
| 1264 |
err = gpg_error (GPG_ERR_UNKNOWN_SEXP); /* ... or invalid S-Exp. */ |
| 1265 |
goto leave; |
| 1266 |
} |
| 1267 |
s++; |
| 1268 |
|
| 1269 |
if (parm[parmidx].namelen == 5 |
| 1270 |
&& !memcmp (parm[parmidx].name, "curve", 5) |
| 1271 |
&& !curve_oid) |
| 1272 |
{ |
| 1273 |
curve_oid = get_ecc_curve_oid (parm[parmidx].value, |
| 1274 |
parm[parmidx].valuelen, &force_pkalgo); |
| 1275 |
parmidx--; /* No need to store this parameter. */ |
| 1276 |
} |
| 1277 |
} |
| 1278 |
s++; |
| 1279 |
/* Allow for optional elements. */ |
| 1280 |
if (*s == '(') |
| 1281 |
{ |
| 1282 |
int depth = 1; |
| 1283 |
err = sskip (&s, &depth); |
| 1284 |
if (err) |
| 1285 |
goto leave; |
| 1286 |
} |
| 1287 |
/* We need another closing parenthesis. */ |
| 1288 |
if ( *s != ')' ) |
| 1289 |
{ |
| 1290 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 1291 |
goto leave; |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (force_pkalgo) |
| 1295 |
pkalgo = force_pkalgo; |
| 1296 |
|
| 1297 |
/* Describe the parameters in the order we want them. For DSA wie |
| 1298 |
* also set algoparmdesc so that we can later build the parameters |
| 1299 |
* for the algorithmIdentifier. */ |
| 1300 |
algoparmdesc = NULL; |
| 1301 |
switch (pkalgo) |
| 1302 |
{ |
| 1303 |
case PKALGO_RSA: |
| 1304 |
parmdesc = algoinfomode? "" : "ne"; |
| 1305 |
break; |
| 1306 |
case PKALGO_DSA: |
| 1307 |
parmdesc = algoinfomode? "" : "y"; |
| 1308 |
algoparmdesc = "pqg"; |
| 1309 |
break; |
| 1310 |
case PKALGO_ECC: |
| 1311 |
parmdesc = algoinfomode? "" : "q"; |
| 1312 |
break; |
| 1313 |
case PKALGO_ED25519: |
| 1314 |
case PKALGO_X25519: |
| 1315 |
case PKALGO_ED448: |
| 1316 |
case PKALGO_X448: |
| 1317 |
parmdesc = algoinfomode? "" : "q"; |
| 1318 |
if (curve_oid) |
| 1319 |
algo_oid = curve_oid; |
| 1320 |
break; |
| 1321 |
default: |
| 1322 |
err = gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); |
| 1323 |
goto leave; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* Create a builder. */ |
| 1327 |
dbld = _ksba_der_builder_new (0); |
| 1328 |
if (!dbld) |
| 1329 |
{ |
| 1330 |
err = gpg_error_from_syserror (); |
| 1331 |
goto leave; |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* The outer sequence. */ |
| 1335 |
if (!algoinfomode) |
| 1336 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 1337 |
/* The sequence. */ |
| 1338 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 1339 |
/* The object id. */ |
| 1340 |
_ksba_der_add_oid (dbld, algo_oid); |
| 1341 |
|
| 1342 |
/* The parameter. */ |
| 1343 |
if (algoparmdesc) |
| 1344 |
{ |
| 1345 |
/* Write the sequence tag followed by the integers. */ |
| 1346 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 1347 |
for (s = algoparmdesc; *s; s++) |
| 1348 |
for (i=0; i < parmidx; i++) |
| 1349 |
if (parm[i].namelen == 1 && parm[i].name[0] == *s) |
| 1350 |
{ |
| 1351 |
_ksba_der_add_int (dbld, parm[i].value, parm[i].valuelen, 1); |
| 1352 |
break; /* inner loop */ |
| 1353 |
} |
| 1354 |
_ksba_der_add_end (dbld); |
| 1355 |
} |
| 1356 |
else if (pkalgo == PKALGO_ECC && !algoinfomode) |
| 1357 |
{ |
| 1358 |
/* We only support the namedCurve choice for ECC parameters. */ |
| 1359 |
if (!curve_oid) |
| 1360 |
{ |
| 1361 |
err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
| 1362 |
goto leave; |
| 1363 |
} |
| 1364 |
_ksba_der_add_oid (dbld, curve_oid); |
| 1365 |
} |
| 1366 |
else if (pkalgo == PKALGO_RSA) |
| 1367 |
{ |
| 1368 |
_ksba_der_add_ptr (dbld, 0, TYPE_NULL, NULL, 0); |
| 1369 |
} |
| 1370 |
|
| 1371 |
_ksba_der_add_end (dbld); /* sequence. */ |
| 1372 |
|
| 1373 |
/* Add the bit string if we are not in algoinfomode. */ |
| 1374 |
if (!algoinfomode) |
| 1375 |
{ |
| 1376 |
if (*parmdesc == 'q' && !parmdesc[1]) |
| 1377 |
{ |
| 1378 |
/* This is ECC - Q is directly written as a bit string. */ |
| 1379 |
for (i=0; i < parmidx; i++) |
| 1380 |
if (parm[i].namelen == 1 && parm[i].name[0] == 'q') |
| 1381 |
{ |
| 1382 |
if ((parm[i].valuelen & 1) && parm[i].valuelen > 32 |
| 1383 |
&& (parm[i].value[0] == 0x40 |
| 1384 |
|| parm[i].value[0] == 0x41 |
| 1385 |
|| parm[i].value[0] == 0x42)) |
| 1386 |
{ |
| 1387 |
/* Odd length and prefixed with 0x40 - this is the |
| 1388 |
* rfc4880bis indicator octet for extended point |
| 1389 |
* formats - we may not emit that octet here. */ |
| 1390 |
_ksba_der_add_bts (dbld, parm[i].value+1, |
| 1391 |
parm[i].valuelen-1, 0); |
| 1392 |
} |
| 1393 |
else |
| 1394 |
_ksba_der_add_bts (dbld, parm[i].value, parm[i].valuelen, 0); |
| 1395 |
break; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
else /* Non-ECC - embed the values. */ |
| 1399 |
{ |
| 1400 |
dbld2 = _ksba_der_builder_new (10); |
| 1401 |
if (!dbld2) |
| 1402 |
{ |
| 1403 |
err = gpg_error_from_syserror (); |
| 1404 |
goto leave; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/* Note that no sequence is used if only one integer is written. */ |
| 1408 |
if (parmdesc[0] && parmdesc[1]) |
| 1409 |
_ksba_der_add_tag (dbld2, 0, TYPE_SEQUENCE); |
| 1410 |
|
| 1411 |
for (s = parmdesc; *s; s++) |
| 1412 |
for (i=0; i < parmidx; i++) |
| 1413 |
if (parm[i].namelen == 1 && parm[i].name[0] == *s) |
| 1414 |
{ |
| 1415 |
_ksba_der_add_int (dbld2, parm[i].value, parm[i].valuelen, 1); |
| 1416 |
break; /* inner loop */ |
| 1417 |
} |
| 1418 |
|
| 1419 |
if (parmdesc[0] && parmdesc[1]) |
| 1420 |
_ksba_der_add_end (dbld2); |
| 1421 |
|
| 1422 |
err = _ksba_der_builder_get (dbld2, &tmpder, &tmpderlen); |
| 1423 |
if (err) |
| 1424 |
goto leave; |
| 1425 |
_ksba_der_add_bts (dbld, tmpder, tmpderlen, 0); |
| 1426 |
xfree (tmpder); |
| 1427 |
} |
| 1428 |
|
| 1429 |
_ksba_der_add_end (dbld); /* Outer sequence. */ |
| 1430 |
} |
| 1431 |
|
| 1432 |
/* Get the result. */ |
| 1433 |
err = _ksba_der_builder_get (dbld, r_der, r_derlen); |
| 1434 |
|
| 1435 |
leave: |
| 1436 |
_ksba_der_release (dbld2); |
| 1437 |
_ksba_der_release (dbld); |
| 1438 |
xfree (curve_oid); |
| 1439 |
return err; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
/* Helper function to parse the parameters used for rsaPSS. |
| 1444 |
* Given this sample DER object in (DER,DERLEN): |
| 1445 |
* |
| 1446 |
* SEQUENCE { |
| 1447 |
* [0] { |
| 1448 |
* SEQUENCE { |
| 1449 |
* OBJECT IDENTIFIER sha-512 (2 16 840 1 101 3 4 2 3) |
| 1450 |
* } |
| 1451 |
* } |
| 1452 |
* [1] { |
| 1453 |
* SEQUENCE { |
| 1454 |
* OBJECT IDENTIFIER pkcs1-MGF (1 2 840 113549 1 1 8) |
| 1455 |
* SEQUENCE { |
| 1456 |
* OBJECT IDENTIFIER sha-512 (2 16 840 1 101 3 4 2 3) |
| 1457 |
* } |
| 1458 |
* } |
| 1459 |
* } |
| 1460 |
* [2] { |
| 1461 |
* INTEGER 64 |
| 1462 |
* } |
| 1463 |
* } |
| 1464 |
* |
| 1465 |
* The function returns the first OID at R_PSSHASH and the salt length |
| 1466 |
* at R_SALTLEN. If the salt length is missing its default value is |
| 1467 |
* returned. In case object does not resemble a the expected rsaPSS |
| 1468 |
* parameters GPG_ERR_INV_OBJ is returned; other errors are returned |
| 1469 |
* for an syntatically invalid object. On error NULL is stored at |
| 1470 |
* R_PSSHASH. |
| 1471 |
*/ |
| 1472 |
gpg_error_t |
| 1473 |
_ksba_keyinfo_get_pss_info (const unsigned char *der, size_t derlen, |
| 1474 |
char **r_psshash, unsigned int *r_saltlen) |
| 1475 |
{ |
| 1476 |
gpg_error_t err; |
| 1477 |
struct tag_info ti; |
| 1478 |
char *psshash = NULL; |
| 1479 |
char *tmpoid = NULL; |
| 1480 |
unsigned int saltlen; |
| 1481 |
|
| 1482 |
*r_psshash = NULL; |
| 1483 |
*r_saltlen = 0; |
| 1484 |
|
| 1485 |
err = parse_sequence (&der, &derlen, &ti); |
| 1486 |
if (err) |
| 1487 |
goto leave; |
| 1488 |
|
| 1489 |
/* Get the hash algo. */ |
| 1490 |
err = parse_context_tag (&der, &derlen, &ti, 0); |
| 1491 |
if (err) |
| 1492 |
goto unknown_parms; |
| 1493 |
err = parse_sequence (&der, &derlen, &ti); |
| 1494 |
if (err) |
| 1495 |
goto unknown_parms; |
| 1496 |
err = parse_object_id_into_str (&der, &derlen, &psshash); |
| 1497 |
if (err) |
| 1498 |
goto unknown_parms; |
| 1499 |
err = parse_optional_null (&der, &derlen, NULL); |
| 1500 |
if (err) |
| 1501 |
goto unknown_parms; |
| 1502 |
|
| 1503 |
/* Check the MGF OID and that its hash algo matches. */ |
| 1504 |
err = parse_context_tag (&der, &derlen, &ti, 1); |
| 1505 |
if (err) |
| 1506 |
goto unknown_parms; |
| 1507 |
err = parse_sequence (&der, &derlen, &ti); |
| 1508 |
if (err) |
| 1509 |
goto leave; |
| 1510 |
err = parse_object_id_into_str (&der, &derlen, &tmpoid); |
| 1511 |
if (err) |
| 1512 |
goto unknown_parms; |
| 1513 |
if (strcmp (tmpoid, "1.2.840.113549.1.1.8")) /* MGF1 */ |
| 1514 |
goto unknown_parms; |
| 1515 |
err = parse_sequence (&der, &derlen, &ti); |
| 1516 |
if (err) |
| 1517 |
goto leave; |
| 1518 |
xfree (tmpoid); |
| 1519 |
err = parse_object_id_into_str (&der, &derlen, &tmpoid); |
| 1520 |
if (err) |
| 1521 |
goto unknown_parms; |
| 1522 |
if (strcmp (tmpoid, psshash)) |
| 1523 |
goto unknown_parms; |
| 1524 |
err = parse_optional_null (&der, &derlen, NULL); |
| 1525 |
if (err) |
| 1526 |
goto unknown_parms; |
| 1527 |
|
| 1528 |
/* Get the optional saltLength. */ |
| 1529 |
err = parse_context_tag (&der, &derlen, &ti, 2); |
| 1530 |
if (gpg_err_code (err) == GPG_ERR_INV_OBJ |
| 1531 |
|| gpg_err_code (err) == GPG_ERR_FALSE) |
| 1532 |
saltlen = 20; /* Optional element - use default value */ |
| 1533 |
else if (err) |
| 1534 |
goto unknown_parms; |
| 1535 |
else |
| 1536 |
{ |
| 1537 |
err = parse_integer (&der, &derlen, &ti); |
| 1538 |
if (err) |
| 1539 |
goto leave; |
| 1540 |
for (saltlen=0; ti.length; ti.length--) |
| 1541 |
{ |
| 1542 |
saltlen <<= 8; |
| 1543 |
saltlen |= (*der++) & 0xff; |
| 1544 |
derlen--; |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
/* All fine. */ |
| 1549 |
*r_psshash = psshash; |
| 1550 |
psshash = NULL; |
| 1551 |
*r_saltlen = saltlen; |
| 1552 |
err = 0; |
| 1553 |
goto leave; |
| 1554 |
|
| 1555 |
unknown_parms: |
| 1556 |
err = gpg_error (GPG_ERR_INV_OBJ); |
| 1557 |
|
| 1558 |
leave: |
| 1559 |
xfree (psshash); |
| 1560 |
xfree (tmpoid); |
| 1561 |
return err; |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
/* Mode 0: work as described under _ksba_sigval_to_sexp |
| 1566 |
* mode 1: work as described under _ksba_encval_to_sexp |
| 1567 |
* mode 2: same as mode 1 but for ECDH; in this mode |
| 1568 |
* KEYENCRYALO, KEYWRAPALGO, ENCRKEY, ENCRYKLEYLEN |
| 1569 |
* are also required. |
| 1570 |
*/ |
| 1571 |
static gpg_error_t |
| 1572 |
cryptval_to_sexp (int mode, const unsigned char *der, size_t derlen, |
| 1573 |
const char *keyencralgo, const char *keywrapalgo, |
| 1574 |
const void *encrkey, size_t encrkeylen, |
| 1575 |
ksba_sexp_t *r_string) |
| 1576 |
{ |
| 1577 |
gpg_error_t err; |
| 1578 |
const struct algo_table_s *algo_table; |
| 1579 |
int c; |
| 1580 |
size_t nread, off, len; |
| 1581 |
int algoidx; |
| 1582 |
int is_bitstr; |
| 1583 |
const unsigned char *ctrl; |
| 1584 |
const char *elem; |
| 1585 |
struct stringbuf sb; |
| 1586 |
size_t parm_off, parm_len; |
| 1587 |
int parm_type; |
| 1588 |
char *pss_hash = NULL; |
| 1589 |
unsigned int salt_length = 0; |
| 1590 |
|
| 1591 |
/* FIXME: The entire function is very similar to keyinfo_to_sexp */ |
| 1592 |
*r_string = NULL; |
| 1593 |
|
| 1594 |
if (!mode) |
| 1595 |
algo_table = sig_algo_table; |
| 1596 |
else |
| 1597 |
algo_table = enc_algo_table; |
| 1598 |
|
| 1599 |
err = get_algorithm (1, der, derlen, 0x30, |
| 1600 |
&nread, &off, &len, &is_bitstr, |
| 1601 |
&parm_off, &parm_len, &parm_type); |
| 1602 |
if (err) |
| 1603 |
return err; |
| 1604 |
|
| 1605 |
/* look into our table of supported algorithms */ |
| 1606 |
for (algoidx=0; algo_table[algoidx].oid; algoidx++) |
| 1607 |
{ |
| 1608 |
if ( len == algo_table[algoidx].oidlen |
| 1609 |
&& !memcmp (der+off, algo_table[algoidx].oid, len)) |
| 1610 |
break; |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (!algo_table[algoidx].oid) |
| 1614 |
return gpg_error (GPG_ERR_UNKNOWN_ALGORITHM); |
| 1615 |
if (!algo_table[algoidx].supported) |
| 1616 |
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM); |
| 1617 |
|
| 1618 |
if (parm_type == TYPE_SEQUENCE |
| 1619 |
&& algo_table[algoidx].supported == SUPPORTED_RSAPSS) |
| 1620 |
{ |
| 1621 |
/* This is rsaPSS and we collect the parameters. We simplify |
| 1622 |
* this by assuming that pkcs1-MGF is used with an identical |
| 1623 |
* hash algorithm. All other kinds of parameters are ignored. */ |
| 1624 |
err = _ksba_keyinfo_get_pss_info (der + parm_off, parm_len, |
| 1625 |
&pss_hash, &salt_length); |
| 1626 |
if (gpg_err_code (err) == GPG_ERR_INV_OBJ) |
| 1627 |
err = 0; |
| 1628 |
if (err) |
| 1629 |
return err; |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
der += nread; |
| 1634 |
derlen -= nread; |
| 1635 |
|
| 1636 |
if (is_bitstr) |
| 1637 |
{ /* Funny: X.509 defines the signature value as a bit string but |
| 1638 |
CMS as an octet string - for ease of implementation we always |
| 1639 |
allow both */ |
| 1640 |
if (!derlen) |
| 1641 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 1642 |
c = *der++; derlen--; |
| 1643 |
if (c) |
| 1644 |
gpgrt_log_debug ("%s: warning: number of unused bits is not zero\n", |
| 1645 |
__func__); |
| 1646 |
} |
| 1647 |
|
| 1648 |
/* fixme: we should calculate the initial length form the size of the |
| 1649 |
sequence, so that we don't neen a realloc later */ |
| 1650 |
init_stringbuf (&sb, 100); |
| 1651 |
put_stringbuf (&sb, mode? "(7:enc-val(":"(7:sig-val("); |
| 1652 |
put_stringbuf_sexp (&sb, algo_table[algoidx].algo_string); |
| 1653 |
|
| 1654 |
/* FIXME: We don't release the stringbuf in case of error |
| 1655 |
better let the macro jump to a label */ |
| 1656 |
if (!mode && (algo_table[algoidx].pkalgo == PKALGO_ED25519 |
| 1657 |
||algo_table[algoidx].pkalgo == PKALGO_ED448 |
| 1658 |
|| (algo_table[algoidx].pkalgo == PKALGO_ECC |
| 1659 |
&& *algo_table[algoidx].elem_string == 'P'))) |
| 1660 |
{ |
| 1661 |
/* EdDSA is special: R and S are simply concatenated; see |
| 1662 |
* rfc8410. The same code is used for Plain ECDSA format as |
| 1663 |
* specified in BSI TR-03111; we indicate this with a 'P' in the |
| 1664 |
* elem string. */ |
| 1665 |
put_stringbuf (&sb, "(1:r"); |
| 1666 |
put_stringbuf_mem_sexp (&sb, der, derlen/2); |
| 1667 |
put_stringbuf (&sb, ")"); |
| 1668 |
der += derlen/2; |
| 1669 |
derlen /= 2; |
| 1670 |
put_stringbuf (&sb, "(1:s"); |
| 1671 |
put_stringbuf_mem_sexp (&sb, der, derlen); |
| 1672 |
put_stringbuf (&sb, ")"); |
| 1673 |
} |
| 1674 |
else |
| 1675 |
{ |
| 1676 |
elem = algo_table[algoidx].elem_string; |
| 1677 |
ctrl = algo_table[algoidx].ctrl_string; |
| 1678 |
for (; *elem; ctrl++, elem++) |
| 1679 |
{ |
| 1680 |
int is_int; |
| 1681 |
|
| 1682 |
if ( (*ctrl & 0x80) && !elem[1] ) |
| 1683 |
{ /* Hack to allow a raw value */ |
| 1684 |
is_int = 1; |
| 1685 |
len = derlen; |
| 1686 |
} |
| 1687 |
else |
| 1688 |
{ |
| 1689 |
if (!derlen) |
| 1690 |
return gpg_error (GPG_ERR_INV_KEYINFO); |
| 1691 |
c = *der++; derlen--; |
| 1692 |
if ( c != *ctrl ) |
| 1693 |
return gpg_error (GPG_ERR_UNEXPECTED_TAG); |
| 1694 |
is_int = c == 0x02; |
| 1695 |
TLV_LENGTH (der); |
| 1696 |
} |
| 1697 |
if (is_int && *elem != '-') |
| 1698 |
{ /* take this integer */ |
| 1699 |
char tmp[2]; |
| 1700 |
|
| 1701 |
put_stringbuf (&sb, "("); |
| 1702 |
tmp[0] = *elem; tmp[1] = 0; |
| 1703 |
put_stringbuf_sexp (&sb, tmp); |
| 1704 |
put_stringbuf_mem_sexp (&sb, der, len); |
| 1705 |
der += len; |
| 1706 |
derlen -= len; |
| 1707 |
put_stringbuf (&sb, ")"); |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
if (mode == 2) /* ECDH */ |
| 1712 |
{ |
| 1713 |
put_stringbuf (&sb, "(1:s"); |
| 1714 |
put_stringbuf_mem_sexp (&sb, encrkey, encrkeylen); |
| 1715 |
put_stringbuf (&sb, ")"); |
| 1716 |
} |
| 1717 |
put_stringbuf (&sb, ")"); |
| 1718 |
if (!mode && algo_table[algoidx].digest_string) |
| 1719 |
{ |
| 1720 |
/* Insert the hash algorithm if included in the OID. */ |
| 1721 |
put_stringbuf (&sb, "(4:hash"); |
| 1722 |
put_stringbuf_sexp (&sb, algo_table[algoidx].digest_string); |
| 1723 |
put_stringbuf (&sb, ")"); |
| 1724 |
} |
| 1725 |
if (!mode && pss_hash) |
| 1726 |
{ |
| 1727 |
put_stringbuf (&sb, "(5:flags3:pss)"); |
| 1728 |
put_stringbuf (&sb, "(9:hash-algo"); |
| 1729 |
put_stringbuf_sexp (&sb, pss_hash); |
| 1730 |
put_stringbuf (&sb, ")"); |
| 1731 |
put_stringbuf (&sb, "(11:salt-length"); |
| 1732 |
put_stringbuf_uint (&sb, salt_length); |
| 1733 |
put_stringbuf (&sb, ")"); |
| 1734 |
} |
| 1735 |
if (mode == 2) /* ECDH */ |
| 1736 |
{ |
| 1737 |
put_stringbuf (&sb, "(9:encr-algo"); |
| 1738 |
put_stringbuf_sexp (&sb, keyencralgo); |
| 1739 |
put_stringbuf (&sb, ")(9:wrap-algo"); |
| 1740 |
put_stringbuf_sexp (&sb, keywrapalgo); |
| 1741 |
put_stringbuf (&sb, ")"); |
| 1742 |
} |
| 1743 |
put_stringbuf (&sb, ")"); |
| 1744 |
|
| 1745 |
*r_string = get_stringbuf (&sb); |
| 1746 |
if (!*r_string) |
| 1747 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1748 |
|
| 1749 |
xfree (pss_hash); |
| 1750 |
return 0; |
| 1751 |
} |
| 1752 |
|
| 1753 |
/* Assume that DER is a buffer of length DERLEN with a DER encoded |
| 1754 |
Asn.1 structure like this: |
| 1755 |
|
| 1756 |
SEQUENCE { |
| 1757 |
algorithm OBJECT IDENTIFIER, |
| 1758 |
parameters ANY DEFINED BY algorithm OPTIONAL } |
| 1759 |
signature BIT STRING |
| 1760 |
|
| 1761 |
We only allow parameters == NULL. |
| 1762 |
|
| 1763 |
The function parses this structure and creates a S-Exp suitable to be |
| 1764 |
used as signature value in Libgcrypt: |
| 1765 |
|
| 1766 |
(sig-val |
| 1767 |
(<algo> |
| 1768 |
(<param_name1> <mpi>) |
| 1769 |
... |
| 1770 |
(<param_namen> <mpi>)) |
| 1771 |
(hash algo)) |
| 1772 |
|
| 1773 |
The S-Exp will be returned in a string which the caller must free. |
| 1774 |
We don't pass an ASN.1 node here but a plain memory block. */ |
| 1775 |
gpg_error_t |
| 1776 |
_ksba_sigval_to_sexp (const unsigned char *der, size_t derlen, |
| 1777 |
ksba_sexp_t *r_string) |
| 1778 |
{ |
| 1779 |
return cryptval_to_sexp (0, der, derlen, NULL, NULL, NULL, 0, r_string); |
| 1780 |
} |
| 1781 |
|
| 1782 |
|
| 1783 |
/* Assume that der is a buffer of length DERLEN with a DER encoded |
| 1784 |
* ASN.1 structure like this: |
| 1785 |
* |
| 1786 |
* SEQUENCE { |
| 1787 |
* algorithm OBJECT IDENTIFIER, |
| 1788 |
* parameters ANY DEFINED BY algorithm OPTIONAL |
| 1789 |
* } |
| 1790 |
* encryptedKey OCTET STRING |
| 1791 |
* |
| 1792 |
* The function parses this structure and creates a S-expression |
| 1793 |
* suitable to be used as encrypted value in Libgcrypt's public key |
| 1794 |
* functions: |
| 1795 |
* |
| 1796 |
* (enc-val |
| 1797 |
* (<algo> |
| 1798 |
* (<param_name1> <mpi>) |
| 1799 |
* ... |
| 1800 |
* (<param_namen> <mpi>) |
| 1801 |
* )) |
| 1802 |
* |
| 1803 |
* The S-expression will be returned in a string which the caller must |
| 1804 |
* free. Note that the input buffer may not a proper ASN.1 object but |
| 1805 |
* a plain memory block; this is becuase the SEQUENCE is followed by |
| 1806 |
* an OCTET STRING or BIT STRING. |
| 1807 |
*/ |
| 1808 |
gpg_error_t |
| 1809 |
_ksba_encval_to_sexp (const unsigned char *der, size_t derlen, |
| 1810 |
ksba_sexp_t *r_string) |
| 1811 |
{ |
| 1812 |
return cryptval_to_sexp (1, der, derlen, NULL, NULL, NULL, 0, r_string); |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
/* Assume that der is a buffer of length DERLEN with a DER encoded |
| 1817 |
* ASN.1 structure like this: |
| 1818 |
* |
| 1819 |
* [1] { |
| 1820 |
* SEQUENCE { |
| 1821 |
* algorithm OBJECT IDENTIFIER, |
| 1822 |
* parameters ANY DEFINED BY algorithm OPTIONAL |
| 1823 |
* } |
| 1824 |
* encryptedKey BIT STRING |
| 1825 |
* } |
| 1826 |
* |
| 1827 |
* The function parses this structure and creates an S-expression |
| 1828 |
* conveying all parameters required for ECDH: |
| 1829 |
* |
| 1830 |
* (enc-val |
| 1831 |
* (ecdh |
| 1832 |
* (e <octetstring>) |
| 1833 |
* (s <octetstring>) |
| 1834 |
* (ukm <octetstring>) |
| 1835 |
* (encr-algo <oid>) |
| 1836 |
* (wrap-algo <oid>))) |
| 1837 |
* |
| 1838 |
* E is the ephemeral public key and S is the encrypted key. The user |
| 1839 |
* keying material (ukm) is optional. The S-expression will be |
| 1840 |
* returned in a string which the caller must free. |
| 1841 |
*/ |
| 1842 |
gpg_error_t |
| 1843 |
_ksba_encval_kari_to_sexp (const unsigned char *der, size_t derlen, |
| 1844 |
const char *keyencralgo, const char *keywrapalgo, |
| 1845 |
const void *enckey, size_t enckeylen, |
| 1846 |
ksba_sexp_t *r_string) |
| 1847 |
{ |
| 1848 |
gpg_error_t err; |
| 1849 |
struct tag_info ti; |
| 1850 |
size_t save_derlen = derlen; |
| 1851 |
|
| 1852 |
err = parse_context_tag (&der, &derlen, &ti, 1); |
| 1853 |
if (err) |
| 1854 |
return err; |
| 1855 |
if (save_derlen < ti.nhdr) |
| 1856 |
return gpg_error (GPG_ERR_INV_BER); |
| 1857 |
derlen = save_derlen - ti.nhdr; |
| 1858 |
return cryptval_to_sexp (2, der, derlen, |
| 1859 |
keyencralgo, keywrapalgo, enckey, enckeylen, |
| 1860 |
r_string); |
| 1861 |
} |