1 /*- 2 * Copyright (c) 2009 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Alistair Crooks (agc@NetBSD.org) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk) 31 * All rights reserved. 32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted 33 * their moral rights under the UK Copyright Design and Patents Act 1988 to 34 * be recorded as the authors of this copyright work. 35 * 36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 37 * use this file except in compliance with the License. 38 * 39 * You may obtain a copy of the License at 40 * http://www.apache.org/licenses/LICENSE-2.0 41 * 42 * Unless required by applicable law or agreed to in writing, software 43 * distributed under the License is distributed on an "AS IS" BASIS, 44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45 * 46 * See the License for the specific language governing permissions and 47 * limitations under the License. 48 */ 49 50 /** \file 51 * packet related headers. 52 */ 53 54 #ifndef PACKET_H_ 55 #define PACKET_H_ 56 57 #include <time.h> 58 59 #ifdef HAVE_OPENSSL_BN_H 60 #include <openssl/bn.h> 61 #endif 62 63 #include "types.h" 64 #include "errors.h" 65 66 /* structure to keep track of printing state variables */ 67 typedef struct __ops_printstate_t { 68 unsigned unarmoured; 69 unsigned skipping; 70 int indent; 71 } __ops_printstate_t; 72 73 /** General-use structure for variable-length data 74 */ 75 76 typedef struct { 77 size_t len; 78 uint8_t *contents; 79 uint8_t mmapped; /* contents need an munmap(2) */ 80 } __ops_data_t; 81 82 /************************************/ 83 /* Packet Tags - RFC4880, 4.2 */ 84 /************************************/ 85 86 /** Packet Tag - Bit 7 Mask (this bit is always set). 87 * The first byte of a packet is the "Packet Tag". It always 88 * has bit 7 set. This is the mask for it. 89 * 90 * \see RFC4880 4.2 91 */ 92 #define OPS_PTAG_ALWAYS_SET 0x80 93 94 /** Packet Tag - New Format Flag. 95 * Bit 6 of the Packet Tag is the packet format indicator. 96 * If it is set, the new format is used, if cleared the 97 * old format is used. 98 * 99 * \see RFC4880 4.2 100 */ 101 #define OPS_PTAG_NEW_FORMAT 0x40 102 103 104 /** Old Packet Format: Mask for content tag. 105 * In the old packet format bits 5 to 2 (including) 106 * are the content tag. This is the mask to apply 107 * to the packet tag. Note that you need to 108 * shift by #OPS_PTAG_OF_CONTENT_TAG_SHIFT bits. 109 * 110 * \see RFC4880 4.2 111 */ 112 #define OPS_PTAG_OF_CONTENT_TAG_MASK 0x3c 113 /** Old Packet Format: Offset for the content tag. 114 * As described at #OPS_PTAG_OF_CONTENT_TAG_MASK the 115 * content tag needs to be shifted after being masked 116 * out from the Packet Tag. 117 * 118 * \see RFC4880 4.2 119 */ 120 #define OPS_PTAG_OF_CONTENT_TAG_SHIFT 2 121 /** Old Packet Format: Mask for length type. 122 * Bits 1 and 0 of the packet tag are the length type 123 * in the old packet format. 124 * 125 * See #__ops_ptag_of_lt_t for the meaning of the values. 126 * 127 * \see RFC4880 4.2 128 */ 129 #define OPS_PTAG_OF_LENGTH_TYPE_MASK 0x03 130 131 132 /** Old Packet Format Lengths. 133 * Defines the meanings of the 2 bits for length type in the 134 * old packet format. 135 * 136 * \see RFC4880 4.2.1 137 */ 138 typedef enum { 139 OPS_PTAG_OLD_LEN_1 = 0x00, /* Packet has a 1 byte length - 140 * header is 2 bytes long. */ 141 OPS_PTAG_OLD_LEN_2 = 0x01, /* Packet has a 2 byte length - 142 * header is 3 bytes long. */ 143 OPS_PTAG_OLD_LEN_4 = 0x02, /* Packet has a 4 byte 144 * length - header is 5 bytes 145 * long. */ 146 OPS_PTAG_OLD_LEN_INDETERMINATE = 0x03 /* Packet has a 147 * indeterminate length. */ 148 } __ops_ptag_of_lt_t; 149 150 151 /** New Packet Format: Mask for content tag. 152 * In the new packet format the 6 rightmost bits 153 * are the content tag. This is the mask to apply 154 * to the packet tag. Note that you need to 155 * shift by #OPS_PTAG_NF_CONTENT_TAG_SHIFT bits. 156 * 157 * \see RFC4880 4.2 158 */ 159 #define OPS_PTAG_NF_CONTENT_TAG_MASK 0x3f 160 /** New Packet Format: Offset for the content tag. 161 * As described at #OPS_PTAG_NF_CONTENT_TAG_MASK the 162 * content tag needs to be shifted after being masked 163 * out from the Packet Tag. 164 * 165 * \see RFC4880 4.2 166 */ 167 #define OPS_PTAG_NF_CONTENT_TAG_SHIFT 0 168 169 /* PTag Content Tags */ 170 /***************************/ 171 172 /** Package Tags (aka Content Tags) and signature subpacket types. 173 * This enumerates all rfc-defined packet tag values and the 174 * signature subpacket type values that we understand. 175 * 176 * \see RFC4880 4.3 177 * \see RFC4880 5.2.3.1 178 */ 179 typedef enum { 180 OPS_PTAG_CT_RESERVED = 0, /* Reserved - a packet tag must 181 * not have this value */ 182 OPS_PTAG_CT_PK_SESSION_KEY = 1, /* Public-Key Encrypted Session 183 * Key Packet */ 184 OPS_PTAG_CT_SIGNATURE = 2, /* Signature Packet */ 185 OPS_PTAG_CT_SK_SESSION_KEY = 3, /* Symmetric-Key Encrypted Session 186 * Key Packet */ 187 OPS_PTAG_CT_1_PASS_SIG = 4, /* One-Pass Signature 188 * Packet */ 189 OPS_PTAG_CT_SECRET_KEY = 5, /* Secret Key Packet */ 190 OPS_PTAG_CT_PUBLIC_KEY = 6, /* Public Key Packet */ 191 OPS_PTAG_CT_SECRET_SUBKEY = 7, /* Secret Subkey Packet */ 192 OPS_PTAG_CT_COMPRESSED = 8, /* Compressed Data Packet */ 193 OPS_PTAG_CT_SE_DATA = 9,/* Symmetrically Encrypted Data Packet */ 194 OPS_PTAG_CT_MARKER = 10,/* Marker Packet */ 195 OPS_PTAG_CT_LITDATA = 11, /* Literal Data Packet */ 196 OPS_PTAG_CT_TRUST = 12, /* Trust Packet */ 197 OPS_PTAG_CT_USER_ID = 13, /* User ID Packet */ 198 OPS_PTAG_CT_PUBLIC_SUBKEY = 14, /* Public Subkey Packet */ 199 OPS_PTAG_CT_RESERVED2 = 15, /* reserved */ 200 OPS_PTAG_CT_RESERVED3 = 16, /* reserved */ 201 OPS_PTAG_CT_USER_ATTR = 17, /* User Attribute Packet */ 202 OPS_PTAG_CT_SE_IP_DATA = 18, /* Sym. Encrypted and Integrity 203 * Protected Data Packet */ 204 OPS_PTAG_CT_MDC = 19, /* Modification Detection Code Packet */ 205 206 OPS_PARSER_PTAG = 0x100,/* Internal Use: The packet is the "Packet 207 * Tag" itself - used when callback sends 208 * back the PTag. */ 209 OPS_PTAG_RAW_SS = 0x101,/* Internal Use: content is raw sig subtag */ 210 OPS_PTAG_SS_ALL = 0x102,/* Internal Use: select all subtags */ 211 OPS_PARSER_PACKET_END = 0x103, 212 213 /* signature subpackets (0x200-2ff) (type+0x200) */ 214 /* only those we can parse are listed here */ 215 OPS_PTAG_SIG_SUBPKT_BASE = 0x200, /* Base for signature 216 * subpacket types - All 217 * signature type values 218 * are relative to this 219 * value. */ 220 OPS_PTAG_SS_CREATION_TIME = 0x200 + 2, /* signature creation time */ 221 OPS_PTAG_SS_EXPIRATION_TIME = 0x200 + 3, /* signature 222 * expiration time */ 223 224 OPS_PTAG_SS_EXPORT_CERT = 0x200 + 4, /* exportable certification */ 225 OPS_PTAG_SS_TRUST = 0x200 + 5, /* trust signature */ 226 OPS_PTAG_SS_REGEXP = 0x200 + 6, /* regular expression */ 227 OPS_PTAG_SS_REVOCABLE = 0x200 + 7, /* revocable */ 228 OPS_PTAG_SS_KEY_EXPIRY = 0x200 + 9, /* key expiration 229 * time */ 230 OPS_PTAG_SS_RESERVED = 0x200 + 10, /* reserved */ 231 OPS_PTAG_SS_PREFERRED_SKA = 0x200 + 11, /* preferred symmetric 232 * algs */ 233 OPS_PTAG_SS_REVOCATION_KEY = 0x200 + 12, /* revocation key */ 234 OPS_PTAG_SS_ISSUER_KEY_ID = 0x200 + 16, /* issuer key ID */ 235 OPS_PTAG_SS_NOTATION_DATA = 0x200 + 20, /* notation data */ 236 OPS_PTAG_SS_PREFERRED_HASH = 0x200 + 21, /* preferred hash 237 * algs */ 238 OPS_PTAG_SS_PREF_COMPRESS = 0x200 + 22, /* preferred 239 * compression 240 * algorithms */ 241 OPS_PTAG_SS_KEYSERV_PREFS = 0x200 + 23, /* key server 242 * preferences */ 243 OPS_PTAG_SS_PREF_KEYSERV = 0x200 + 24, /* Preferred Key 244 * Server */ 245 OPS_PTAG_SS_PRIMARY_USER_ID = 0x200 + 25, /* primary User ID */ 246 OPS_PTAG_SS_POLICY_URI = 0x200 + 26, /* Policy URI */ 247 OPS_PTAG_SS_KEY_FLAGS = 0x200 + 27, /* key flags */ 248 OPS_PTAG_SS_SIGNERS_USER_ID = 0x200 + 28, /* Signer's User ID */ 249 OPS_PTAG_SS_REVOCATION_REASON = 0x200 + 29, /* reason for 250 * revocation */ 251 OPS_PTAG_SS_FEATURES = 0x200 + 30, /* features */ 252 OPS_PTAG_SS_SIGNATURE_TARGET = 0x200 + 31, /* signature target */ 253 OPS_PTAG_SS_EMBEDDED_SIGNATURE = 0x200 + 32, /* embedded signature */ 254 255 OPS_PTAG_SS_USERDEFINED00 = 0x200 + 100, /* internal or 256 * user-defined */ 257 OPS_PTAG_SS_USERDEFINED01 = 0x200 + 101, 258 OPS_PTAG_SS_USERDEFINED02 = 0x200 + 102, 259 OPS_PTAG_SS_USERDEFINED03 = 0x200 + 103, 260 OPS_PTAG_SS_USERDEFINED04 = 0x200 + 104, 261 OPS_PTAG_SS_USERDEFINED05 = 0x200 + 105, 262 OPS_PTAG_SS_USERDEFINED06 = 0x200 + 106, 263 OPS_PTAG_SS_USERDEFINED07 = 0x200 + 107, 264 OPS_PTAG_SS_USERDEFINED08 = 0x200 + 108, 265 OPS_PTAG_SS_USERDEFINED09 = 0x200 + 109, 266 OPS_PTAG_SS_USERDEFINED10 = 0x200 + 110, 267 268 /* pseudo content types */ 269 OPS_PTAG_CT_LITDATA_HEADER = 0x300, 270 OPS_PTAG_CT_LITDATA_BODY = 0x300 + 1, 271 OPS_PTAG_CT_SIGNATURE_HEADER = 0x300 + 2, 272 OPS_PTAG_CT_SIGNATURE_FOOTER = 0x300 + 3, 273 OPS_PTAG_CT_ARMOUR_HEADER = 0x300 + 4, 274 OPS_PTAG_CT_ARMOUR_TRAILER = 0x300 + 5, 275 OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER = 0x300 + 6, 276 OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY = 0x300 + 7, 277 OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER = 0x300 + 8, 278 OPS_PTAG_CT_UNARMOURED_TEXT = 0x300 + 9, 279 OPS_PTAG_CT_ENCRYPTED_SECRET_KEY = 0x300 + 10, /* In this case the 280 * algorithm specific 281 * fields will not be 282 * initialised */ 283 OPS_PTAG_CT_SE_DATA_HEADER = 0x300 + 11, 284 OPS_PTAG_CT_SE_DATA_BODY = 0x300 + 12, 285 OPS_PTAG_CT_SE_IP_DATA_HEADER = 0x300 + 13, 286 OPS_PTAG_CT_SE_IP_DATA_BODY = 0x300 + 14, 287 OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY = 0x300 + 15, 288 289 /* commands to the callback */ 290 OPS_GET_PASSPHRASE = 0x400, 291 OPS_GET_SECKEY = 0x400 + 1, 292 293 /* Errors */ 294 OPS_PARSER_ERROR = 0x500, /* Internal Use: Parser Error */ 295 OPS_PARSER_ERRCODE = 0x500 + 1 /* Internal Use: Parser Error 296 * with errcode returned */ 297 } __ops_content_enum; 298 299 enum { 300 OPS_REVOCATION_NO_REASON = 0, 301 OPS_REVOCATION_SUPERSEDED = 1, 302 OPS_REVOCATION_COMPROMISED = 2, 303 OPS_REVOCATION_RETIRED = 3, 304 OPS_REVOCATION_NO_LONGER_VALID = 0x20 305 }; 306 307 /** Structure to hold one error code */ 308 typedef struct { 309 __ops_errcode_t errcode; 310 } __ops_parser_errcode_t; 311 312 /** Structure to hold one packet tag. 313 * \see RFC4880 4.2 314 */ 315 typedef struct { 316 unsigned new_format; /* Whether this packet tag is new 317 * (1) or old format (0) */ 318 unsigned type; /* content_tag value - See 319 * #__ops_content_enum for meanings */ 320 __ops_ptag_of_lt_t length_type; /* Length type (#__ops_ptag_of_lt_t) 321 * - only if this packet tag is old 322 * format. Set to 0 if new format. */ 323 unsigned length; /* The length of the packet. This value 324 * is set when we read and compute the length 325 * information, not at the same moment we 326 * create the packet tag structure. Only 327 * defined if #readc is set. *//* XXX: Ben, is this correct? */ 328 unsigned position; /* The position (within the 329 * current reader) of the packet */ 330 unsigned size; /* number of bits */ 331 } __ops_ptag_t; 332 333 /** Public Key Algorithm Numbers. 334 * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP. 335 * 336 * This lists algorithm numbers for public key algorithms. 337 * 338 * \see RFC4880 9.1 339 */ 340 typedef enum { 341 OPS_PKA_NOTHING = 0, /* No PKA */ 342 OPS_PKA_RSA = 1, /* RSA (Encrypt or Sign) */ 343 OPS_PKA_RSA_ENCRYPT_ONLY = 2, /* RSA Encrypt-Only (deprecated - 344 * \see RFC4880 13.5) */ 345 OPS_PKA_RSA_SIGN_ONLY = 3, /* RSA Sign-Only (deprecated - 346 * \see RFC4880 13.5) */ 347 OPS_PKA_ELGAMAL = 16, /* Elgamal (Encrypt-Only) */ 348 OPS_PKA_DSA = 17, /* DSA (Digital Signature Algorithm) */ 349 OPS_PKA_RESERVED_ELLIPTIC_CURVE = 18, /* Reserved for Elliptic 350 * Curve */ 351 OPS_PKA_RESERVED_ECDSA = 19, /* Reserved for ECDSA */ 352 OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN = 20, /* Deprecated. */ 353 OPS_PKA_RESERVED_DH = 21, /* Reserved for Diffie-Hellman 354 * (X9.42, as defined for 355 * IETF-S/MIME) */ 356 OPS_PKA_PRIVATE00 = 100,/* Private/Experimental Algorithm */ 357 OPS_PKA_PRIVATE01 = 101,/* Private/Experimental Algorithm */ 358 OPS_PKA_PRIVATE02 = 102,/* Private/Experimental Algorithm */ 359 OPS_PKA_PRIVATE03 = 103,/* Private/Experimental Algorithm */ 360 OPS_PKA_PRIVATE04 = 104,/* Private/Experimental Algorithm */ 361 OPS_PKA_PRIVATE05 = 105,/* Private/Experimental Algorithm */ 362 OPS_PKA_PRIVATE06 = 106,/* Private/Experimental Algorithm */ 363 OPS_PKA_PRIVATE07 = 107,/* Private/Experimental Algorithm */ 364 OPS_PKA_PRIVATE08 = 108,/* Private/Experimental Algorithm */ 365 OPS_PKA_PRIVATE09 = 109,/* Private/Experimental Algorithm */ 366 OPS_PKA_PRIVATE10 = 110 /* Private/Experimental Algorithm */ 367 } __ops_pubkey_alg_t; 368 369 /** Structure to hold one DSA public key params. 370 * 371 * \see RFC4880 5.5.2 372 */ 373 typedef struct { 374 BIGNUM *p; /* DSA prime p */ 375 BIGNUM *q; /* DSA group order q */ 376 BIGNUM *g; /* DSA group generator g */ 377 BIGNUM *y; /* DSA public key value y (= g^x mod p 378 * with x being the secret) */ 379 } __ops_dsa_pubkey_t; 380 381 /** Structure to hold an RSA public key. 382 * 383 * \see RFC4880 5.5.2 384 */ 385 typedef struct { 386 BIGNUM *n; /* RSA public modulus n */ 387 BIGNUM *e; /* RSA public encryption exponent e */ 388 } __ops_rsa_pubkey_t; 389 390 /** Structure to hold an ElGamal public key params. 391 * 392 * \see RFC4880 5.5.2 393 */ 394 typedef struct { 395 BIGNUM *p; /* ElGamal prime p */ 396 BIGNUM *g; /* ElGamal group generator g */ 397 BIGNUM *y; /* ElGamal public key value y (= g^x mod p 398 * with x being the secret) */ 399 } __ops_elgamal_pubkey_t; 400 401 /** Version. 402 * OpenPGP has two different protocol versions: version 3 and version 4. 403 * 404 * \see RFC4880 5.2 405 */ 406 typedef enum { 407 OPS_V2 = 2, /* Version 2 (essentially the same as v3) */ 408 OPS_V3 = 3, /* Version 3 */ 409 OPS_V4 = 4 /* Version 4 */ 410 } __ops_version_t; 411 412 /** Structure to hold a pgp public key */ 413 typedef struct { 414 __ops_version_t version;/* version of the key (v3, v4...) */ 415 time_t birthtime; 416 time_t duration; 417 /* validity period of the key in days since 418 * creation. A value of 0 has a special meaning 419 * indicating this key does not expire. Only used with 420 * v3 keys. */ 421 unsigned days_valid; /* v4 duration */ 422 __ops_pubkey_alg_t alg; /* Public Key Algorithm type */ 423 union { 424 __ops_dsa_pubkey_t dsa; /* A DSA public key */ 425 __ops_rsa_pubkey_t rsa; /* An RSA public key */ 426 __ops_elgamal_pubkey_t elgamal; /* An ElGamal public key */ 427 } key; /* Public Key Parameters */ 428 } __ops_pubkey_t; 429 430 /** Structure to hold data for one RSA secret key 431 */ 432 typedef struct { 433 BIGNUM *d; 434 BIGNUM *p; 435 BIGNUM *q; 436 BIGNUM *u; 437 } __ops_rsa_seckey_t; 438 439 /** __ops_dsa_seckey_t */ 440 typedef struct { 441 BIGNUM *x; 442 } __ops_dsa_seckey_t; 443 444 /** __ops_elgamal_seckey_t */ 445 typedef struct { 446 BIGNUM *x; 447 } __ops_elgamal_seckey_t; 448 449 /** s2k_usage_t 450 */ 451 typedef enum { 452 OPS_S2KU_NONE = 0, 453 OPS_S2KU_ENCRYPTED_AND_HASHED = 254, 454 OPS_S2KU_ENCRYPTED = 255 455 } __ops_s2k_usage_t; 456 457 /** s2k_specifier_t 458 */ 459 typedef enum { 460 OPS_S2KS_SIMPLE = 0, 461 OPS_S2KS_SALTED = 1, 462 OPS_S2KS_ITERATED_AND_SALTED = 3 463 } __ops_s2k_specifier_t; 464 465 /** Symmetric Key Algorithm Numbers. 466 * OpenPGP assigns a unique Algorithm Number to each algorithm that is 467 * part of OpenPGP. 468 * 469 * This lists algorithm numbers for symmetric key algorithms. 470 * 471 * \see RFC4880 9.2 472 */ 473 typedef enum { 474 OPS_SA_PLAINTEXT = 0, /* Plaintext or unencrypted data */ 475 OPS_SA_IDEA = 1, /* IDEA */ 476 OPS_SA_TRIPLEDES = 2, /* TripleDES */ 477 OPS_SA_CAST5 = 3, /* CAST5 */ 478 OPS_SA_BLOWFISH = 4, /* Blowfish */ 479 OPS_SA_AES_128 = 7, /* AES with 128-bit key (AES) */ 480 OPS_SA_AES_192 = 8, /* AES with 192-bit key */ 481 OPS_SA_AES_256 = 9, /* AES with 256-bit key */ 482 OPS_SA_TWOFISH = 10 /* Twofish with 256-bit key (TWOFISH) */ 483 } __ops_symm_alg_t; 484 485 /** Hashing Algorithm Numbers. 486 * OpenPGP assigns a unique Algorithm Number to each algorithm that is 487 * part of OpenPGP. 488 * 489 * This lists algorithm numbers for hash algorithms. 490 * 491 * \see RFC4880 9.4 492 */ 493 typedef enum { 494 OPS_HASH_UNKNOWN = -1, /* used to indicate errors */ 495 OPS_HASH_MD5 = 1, /* MD5 */ 496 OPS_HASH_SHA1 = 2, /* SHA-1 */ 497 OPS_HASH_RIPEMD = 3, /* RIPEMD160 */ 498 499 OPS_HASH_SHA256 = 8, /* SHA256 */ 500 OPS_HASH_SHA384 = 9, /* SHA384 */ 501 OPS_HASH_SHA512 = 10, /* SHA512 */ 502 OPS_HASH_SHA224 = 11 /* SHA224 */ 503 } __ops_hash_alg_t; 504 505 #define OPS_DEFAULT_HASH_ALGORITHM OPS_HASH_SHA256 506 507 void __ops_calc_mdc_hash(const uint8_t *, 508 const size_t, 509 const uint8_t *, 510 const unsigned, 511 uint8_t *); 512 unsigned __ops_is_hash_alg_supported(const __ops_hash_alg_t *); 513 514 /* Maximum block size for symmetric crypto */ 515 #define OPS_MAX_BLOCK_SIZE 16 516 517 /* Maximum key size for symmetric crypto */ 518 #define OPS_MAX_KEY_SIZE 32 519 520 /* Salt size for hashing */ 521 #define OPS_SALT_SIZE 8 522 523 /* Max hash size */ 524 #define OPS_MAX_HASH_SIZE 64 525 526 /** __ops_seckey_t 527 */ 528 typedef struct __ops_seckey_t { 529 __ops_pubkey_t pubkey; /* public key */ 530 __ops_s2k_usage_t s2k_usage; 531 __ops_s2k_specifier_t s2k_specifier; 532 __ops_symm_alg_t alg; /* symmetric alg */ 533 __ops_hash_alg_t hash_alg; /* hash algorithm */ 534 uint8_t salt[OPS_SALT_SIZE]; 535 unsigned octetc; 536 uint8_t iv[OPS_MAX_BLOCK_SIZE]; 537 union { 538 __ops_rsa_seckey_t rsa; 539 __ops_dsa_seckey_t dsa; 540 __ops_elgamal_seckey_t elgamal; 541 } key; 542 unsigned checksum; 543 uint8_t *checkhash; 544 } __ops_seckey_t; 545 546 /** Signature Type. 547 * OpenPGP defines different signature types that allow giving 548 * different meanings to signatures. Signature types include 0x10 for 549 * generitc User ID certifications (used when Ben signs Weasel's key), 550 * Subkey binding signatures, document signatures, key revocations, 551 * etc. 552 * 553 * Different types are used in different places, and most make only 554 * sense in their intended location (for instance a subkey binding has 555 * no place on a UserID). 556 * 557 * \see RFC4880 5.2.1 558 */ 559 typedef enum { 560 OPS_SIG_BINARY = 0x00, /* Signature of a binary document */ 561 OPS_SIG_TEXT = 0x01, /* Signature of a canonical text document */ 562 OPS_SIG_STANDALONE = 0x02, /* Standalone signature */ 563 564 OPS_CERT_GENERIC = 0x10,/* Generic certification of a User ID and 565 * Public Key packet */ 566 OPS_CERT_PERSONA = 0x11,/* Persona certification of a User ID and 567 * Public Key packet */ 568 OPS_CERT_CASUAL = 0x12, /* Casual certification of a User ID and 569 * Public Key packet */ 570 OPS_CERT_POSITIVE = 0x13, /* Positive certification of a 571 * User ID and Public Key packet */ 572 573 OPS_SIG_SUBKEY = 0x18, /* Subkey Binding Signature */ 574 OPS_SIG_PRIMARY = 0x19, /* Primary Key Binding Signature */ 575 OPS_SIG_DIRECT = 0x1f, /* Signature directly on a key */ 576 577 OPS_SIG_REV_KEY = 0x20, /* Key revocation signature */ 578 OPS_SIG_REV_SUBKEY = 0x28, /* Subkey revocation signature */ 579 OPS_SIG_REV_CERT = 0x30,/* Certification revocation signature */ 580 581 OPS_SIG_TIMESTAMP = 0x40, /* Timestamp signature */ 582 583 OPS_SIG_3RD_PARTY = 0x50/* Third-Party Confirmation signature */ 584 } __ops_sig_type_t; 585 586 /** Struct to hold params of an RSA signature */ 587 typedef struct __ops_rsa_sig_t { 588 BIGNUM *sig; /* the signature value (m^d % n) */ 589 } __ops_rsa_sig_t; 590 591 /** Struct to hold params of a DSA signature */ 592 typedef struct __ops_dsa_sig_t { 593 BIGNUM *r; /* DSA value r */ 594 BIGNUM *s; /* DSA value s */ 595 } __ops_dsa_sig_t; 596 597 /** __ops_elgamal_signature_t */ 598 typedef struct __ops_elgamal_sig_t { 599 BIGNUM *r; 600 BIGNUM *s; 601 } __ops_elgamal_sig_t; 602 603 #define OPS_KEY_ID_SIZE 8 604 #define OPS_FINGERPRINT_SIZE 20 605 606 /** Struct to hold a signature packet. 607 * 608 * \see RFC4880 5.2.2 609 * \see RFC4880 5.2.3 610 */ 611 typedef struct __ops_sig_info_t { 612 __ops_version_t version;/* signature version number */ 613 __ops_sig_type_t type; /* signature type value */ 614 time_t birthtime; /* creation time of the signature */ 615 time_t duration; /* number of seconds it's valid for */ 616 uint8_t signer_id[OPS_KEY_ID_SIZE]; /* Eight-octet key ID 617 * of signer */ 618 __ops_pubkey_alg_t key_alg; /* public key algorithm number */ 619 __ops_hash_alg_t hash_alg; /* hashing algorithm number */ 620 union { 621 __ops_rsa_sig_t rsa; /* An RSA Signature */ 622 __ops_dsa_sig_t dsa; /* A DSA Signature */ 623 __ops_elgamal_sig_t elgamal; /* deprecated */ 624 __ops_data_t unknown; /* private or experimental */ 625 } sig; /* signature params */ 626 size_t v4_hashlen; 627 uint8_t *v4_hashed; 628 unsigned birthtime_set:1; 629 unsigned signer_id_set:1; 630 unsigned duration_set:1; 631 } __ops_sig_info_t; 632 633 /** Struct used when parsing a signature */ 634 typedef struct __ops_sig_t { 635 __ops_sig_info_t info; /* The signature information */ 636 /* The following fields are only used while parsing the signature */ 637 uint8_t hash2[2]; /* high 2 bytes of hashed value */ 638 size_t v4_hashstart; /* only valid if accumulate is set */ 639 __ops_hash_t *hash; /* the hash filled in for the data so far */ 640 } __ops_sig_t; 641 642 /** The raw bytes of a signature subpacket */ 643 644 typedef struct __ops_ss_raw_t { 645 __ops_content_enum tag; 646 size_t length; 647 uint8_t *raw; 648 } __ops_ss_raw_t; 649 650 /** Signature Subpacket : Trust Level */ 651 652 typedef struct __ops_ss_trust_t { 653 uint8_t level; /* Trust Level */ 654 uint8_t amount; /* Amount */ 655 } __ops_ss_trust_t; 656 657 /** Signature Subpacket : Notation Data */ 658 typedef struct __ops_ss_notation_t { 659 __ops_data_t flags; 660 __ops_data_t name; 661 __ops_data_t value; 662 } __ops_ss_notation_t; 663 664 /** Signature Subpacket : Signature Target */ 665 typedef struct __ops_ss_sig_target_t { 666 __ops_pubkey_alg_t pka_alg; 667 __ops_hash_alg_t hash_alg; 668 __ops_data_t hash; 669 } __ops_ss_sig_target_t; 670 671 /** __ops_subpacket_t */ 672 typedef struct __ops_subpacket_t { 673 size_t length; 674 uint8_t *raw; 675 } __ops_subpacket_t; 676 677 /** Types of Compression */ 678 typedef enum { 679 OPS_C_NONE = 0, 680 OPS_C_ZIP = 1, 681 OPS_C_ZLIB = 2, 682 OPS_C_BZIP2 = 3 683 } __ops_compression_type_t; 684 685 /** __ops_one_pass_sig_t */ 686 typedef struct { 687 uint8_t version; 688 __ops_sig_type_t sig_type; 689 __ops_hash_alg_t hash_alg; 690 __ops_pubkey_alg_t key_alg; 691 uint8_t keyid[OPS_KEY_ID_SIZE]; 692 unsigned nested; 693 } __ops_one_pass_sig_t; 694 695 /** Signature Subpacket : Revocation Key */ 696 typedef struct { 697 uint8_t class; 698 uint8_t algid; 699 uint8_t fingerprint[OPS_FINGERPRINT_SIZE]; 700 } __ops_ss_revocation_key_t; 701 702 /** Signature Subpacket : Revocation Reason */ 703 typedef struct { 704 uint8_t code; 705 char *reason; 706 } __ops_ss_revocation_t; 707 708 /** litdata_type_t */ 709 typedef enum { 710 OPS_LDT_BINARY = 'b', 711 OPS_LDT_TEXT = 't', 712 OPS_LDT_UTF8 = 'u', 713 OPS_LDT_LOCAL = 'l', 714 OPS_LDT_LOCAL2 = '1' 715 } __ops_litdata_enum; 716 717 /** __ops_litdata_header_t */ 718 typedef struct { 719 __ops_litdata_enum format; 720 char filename[256]; 721 time_t mtime; 722 } __ops_litdata_header_t; 723 724 /** __ops_litdata_body_t */ 725 typedef struct { 726 unsigned length; 727 uint8_t *data; 728 void *mem; /* __ops_memory_t pointer */ 729 } __ops_litdata_body_t; 730 731 /** __ops_header_var_t */ 732 typedef struct { 733 char *key; 734 char *value; 735 } __ops_header_var_t; 736 737 /** __ops_headers_t */ 738 typedef struct { 739 __ops_header_var_t *headers; 740 unsigned headerc; 741 } __ops_headers_t; 742 743 /** __ops_armour_header_t */ 744 typedef struct { 745 const char *type; 746 __ops_headers_t headers; 747 } __ops_armour_header_t; 748 749 /** __ops_fixed_body_t */ 750 typedef struct __ops_fixed_body_t { 751 unsigned length; 752 uint8_t data[8192]; /* \todo fix hard-coded value? */ 753 } __ops_fixed_body_t; 754 755 /** __ops_dyn_body_t */ 756 typedef struct __ops_dyn_body_t { 757 unsigned length; 758 uint8_t *data; 759 } __ops_dyn_body_t; 760 761 enum { 762 OPS_SE_IP_DATA_VERSION = 1, 763 OPS_PKSK_V3 = 3 764 }; 765 766 /** __ops_pk_sesskey_params_rsa_t */ 767 typedef struct { 768 BIGNUM *encrypted_m; 769 BIGNUM *m; 770 } __ops_pk_sesskey_params_rsa_t; 771 772 /** __ops_pk_sesskey_params_elgamal_t */ 773 typedef struct { 774 BIGNUM *g_to_k; 775 BIGNUM *encrypted_m; 776 } __ops_pk_sesskey_params_elgamal_t; 777 778 /** __ops_pk_sesskey_params_t */ 779 typedef union { 780 __ops_pk_sesskey_params_rsa_t rsa; 781 __ops_pk_sesskey_params_elgamal_t elgamal; 782 } __ops_pk_sesskey_params_t; 783 784 /** __ops_pk_sesskey_t */ 785 typedef struct { 786 unsigned version; 787 uint8_t key_id[OPS_KEY_ID_SIZE]; 788 __ops_pubkey_alg_t alg; 789 __ops_pk_sesskey_params_t params; 790 __ops_symm_alg_t symm_alg; 791 uint8_t key[OPS_MAX_KEY_SIZE]; 792 uint16_t checksum; 793 } __ops_pk_sesskey_t; 794 795 /** __ops_seckey_passphrase_t */ 796 typedef struct { 797 const __ops_seckey_t *seckey; 798 char **passphrase; /* point somewhere that gets filled 799 * in to work around constness of 800 * content */ 801 } __ops_seckey_passphrase_t; 802 803 /** __ops_get_seckey_t */ 804 typedef struct { 805 const __ops_seckey_t **seckey; 806 const __ops_pk_sesskey_t *pk_sesskey; 807 } __ops_get_seckey_t; 808 809 /** __ops_parser_union_content_t */ 810 typedef union { 811 const char *error; 812 __ops_parser_errcode_t errcode; 813 __ops_ptag_t ptag; 814 __ops_pubkey_t pubkey; 815 __ops_data_t trust; 816 uint8_t *userid; 817 __ops_data_t userattr; 818 __ops_sig_t sig; 819 __ops_ss_raw_t ss_raw; 820 __ops_ss_trust_t ss_trust; 821 unsigned ss_revocable; 822 time_t ss_time; 823 uint8_t ss_issuer[OPS_KEY_ID_SIZE]; 824 __ops_ss_notation_t ss_notation; 825 __ops_subpacket_t packet; 826 __ops_compression_type_t compressed; 827 __ops_one_pass_sig_t one_pass_sig; 828 __ops_data_t ss_skapref; 829 __ops_data_t ss_hashpref; 830 __ops_data_t ss_zpref; 831 __ops_data_t ss_key_flags; 832 __ops_data_t ss_key_server_prefs; 833 unsigned ss_primary_userid; 834 char *ss_regexp; 835 char *ss_policy; 836 char *ss_keyserv; 837 __ops_ss_revocation_key_t ss_revocation_key; 838 __ops_data_t ss_userdef; 839 __ops_data_t ss_unknown; 840 __ops_litdata_header_t litdata_header; 841 __ops_litdata_body_t litdata_body; 842 __ops_dyn_body_t mdc; 843 __ops_data_t ss_features; 844 __ops_ss_sig_target_t ss_sig_target; 845 __ops_data_t ss_embedded_sig; 846 __ops_ss_revocation_t ss_revocation; 847 __ops_seckey_t seckey; 848 uint8_t *ss_signer; 849 __ops_armour_header_t armour_header; 850 const char *armour_trailer; 851 __ops_headers_t cleartext_head; 852 __ops_fixed_body_t cleartext_body; 853 struct _ops_hash_t *cleartext_trailer; 854 __ops_dyn_body_t unarmoured_text; 855 __ops_pk_sesskey_t pk_sesskey; 856 __ops_seckey_passphrase_t skey_passphrase; 857 unsigned se_ip_data_header; 858 __ops_dyn_body_t se_ip_data_body; 859 __ops_fixed_body_t se_data_body; 860 __ops_get_seckey_t get_seckey; 861 } __ops_contents_t; 862 863 /** __ops_packet_t */ 864 struct __ops_packet_t { 865 __ops_content_enum tag; /* type of contents */ 866 uint8_t critical; /* for sig subpackets */ 867 __ops_contents_t u; /* union for contents */ 868 }; 869 870 /** __ops_fingerprint_t */ 871 typedef struct { 872 uint8_t fingerprint[OPS_FINGERPRINT_SIZE]; 873 unsigned length; 874 __ops_hash_alg_t hashtype; 875 } __ops_fingerprint_t; 876 877 int __ops_keyid(uint8_t *, const size_t, const __ops_pubkey_t *, __ops_hash_alg_t); 878 int __ops_fingerprint(__ops_fingerprint_t *, const __ops_pubkey_t *, __ops_hash_alg_t); 879 880 void __ops_finish(void); 881 void __ops_pubkey_free(__ops_pubkey_t *); 882 void __ops_userid_free(uint8_t **); 883 void __ops_data_free(__ops_data_t *); 884 void __ops_sig_free(__ops_sig_t *); 885 void __ops_ss_notation_free(__ops_ss_notation_t *); 886 void __ops_ss_revocation_free(__ops_ss_revocation_t *); 887 void __ops_ss_sig_target_free(__ops_ss_sig_target_t *); 888 889 void __ops_subpacket_free(__ops_subpacket_t *); 890 void __ops_parser_content_free(__ops_packet_t *); 891 void __ops_seckey_free(__ops_seckey_t *); 892 void __ops_pk_sesskey_free(__ops_pk_sesskey_t *); 893 894 int __ops_print_packet(__ops_printstate_t *, const __ops_packet_t *); 895 896 #define DYNARRAY(type, arr) \ 897 unsigned arr##c; unsigned arr##vsize; type *arr##s 898 899 #define EXPAND_ARRAY(str, arr) do { \ 900 if (str->arr##c == str->arr##vsize) { \ 901 void *__newarr; \ 902 char *__newarrc; \ 903 unsigned __newsize; \ 904 __newsize = (str->arr##vsize * 2) + 10; \ 905 if ((__newarrc = __newarr = realloc(str->arr##s, \ 906 __newsize * sizeof(*str->arr##s))) == NULL) { \ 907 (void) fprintf(stderr, "EXPAND_ARRAY - bad realloc\n"); \ 908 } else { \ 909 (void) memset(&__newarrc[str->arr##vsize * sizeof(*str->arr##s)], \ 910 0x0, (__newsize - str->arr##vsize) * sizeof(*str->arr##s)); \ 911 str->arr##s = __newarr; \ 912 str->arr##vsize = __newsize; \ 913 } \ 914 } \ 915 } while(/*CONSTCOND*/0) 916 917 /** __ops_keydata_key_t 918 */ 919 typedef union { 920 __ops_pubkey_t pubkey; 921 __ops_seckey_t seckey; 922 } __ops_keydata_key_t; 923 924 925 /* sigpacket_t */ 926 typedef struct { 927 uint8_t **userid; 928 __ops_subpacket_t *packet; 929 } sigpacket_t; 930 931 /* user revocation info */ 932 typedef struct __ops_revoke_t { 933 uint32_t uid; /* index in uid array */ 934 uint8_t code; /* revocation code */ 935 char *reason; /* c'mon, spill the beans */ 936 } __ops_revoke_t; 937 938 /** signature subpackets */ 939 typedef struct __ops_subsig_t { 940 uint32_t uid; /* index in userid array in key */ 941 __ops_sig_t sig; /* trust signature */ 942 uint8_t trustlevel; /* level of trust */ 943 uint8_t trustamount; /* amount of trust */ 944 } __ops_subsig_t; 945 946 /* describes a user's key */ 947 struct __ops_key_t { 948 DYNARRAY(uint8_t *, uid); /* array of user ids */ 949 DYNARRAY(__ops_subpacket_t, packet); /* array of raw subpackets */ 950 DYNARRAY(__ops_subsig_t, subsig); /* array of signature subkeys */ 951 DYNARRAY(__ops_revoke_t, revoke); /* array of signature revocations */ 952 __ops_content_enum type; /* type of key */ 953 __ops_keydata_key_t key; /* pubkey/seckey data */ 954 __ops_pubkey_t sigkey; /* signature key */ 955 uint8_t sigid[OPS_KEY_ID_SIZE]; 956 __ops_fingerprint_t sigfingerprint; /* pgp signature fingerprint */ 957 __ops_pubkey_t enckey; /* encryption key */ 958 uint8_t encid[OPS_KEY_ID_SIZE]; 959 __ops_fingerprint_t encfingerprint; /* pgp encryption id fingerprint */ 960 uint32_t uid0; /* primary uid index in uids array */ 961 uint8_t revoked; /* key has been revoked */ 962 __ops_revoke_t revocation; /* revocation reason */ 963 }; 964 965 #define MDC_PKT_TAG 0xd3 966 967 #endif /* PACKET_H_ */ 968