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 /** General-use structure for variable-length data 67 */ 68 69 typedef struct { 70 size_t len; 71 unsigned char *contents; 72 unsigned char mmapped; /* contents need an munmap(2) */ 73 } __ops_data_t; 74 75 /************************************/ 76 /* Packet Tags - RFC4880, 4.2 */ 77 /************************************/ 78 79 /** Packet Tag - Bit 7 Mask (this bit is always set). 80 * The first byte of a packet is the "Packet Tag". It always 81 * has bit 7 set. This is the mask for it. 82 * 83 * \see RFC4880 4.2 84 */ 85 #define OPS_PTAG_ALWAYS_SET 0x80 86 87 /** Packet Tag - New Format Flag. 88 * Bit 6 of the Packet Tag is the packet format indicator. 89 * If it is set, the new format is used, if cleared the 90 * old format is used. 91 * 92 * \see RFC4880 4.2 93 */ 94 #define OPS_PTAG_NEW_FORMAT 0x40 95 96 97 /** Old Packet Format: Mask for content tag. 98 * In the old packet format bits 5 to 2 (including) 99 * are the content tag. This is the mask to apply 100 * to the packet tag. Note that you need to 101 * shift by #OPS_PTAG_OF_CONTENT_TAG_SHIFT bits. 102 * 103 * \see RFC4880 4.2 104 */ 105 #define OPS_PTAG_OF_CONTENT_TAG_MASK 0x3c 106 /** Old Packet Format: Offset for the content tag. 107 * As described at #OPS_PTAG_OF_CONTENT_TAG_MASK the 108 * content tag needs to be shifted after being masked 109 * out from the Packet Tag. 110 * 111 * \see RFC4880 4.2 112 */ 113 #define OPS_PTAG_OF_CONTENT_TAG_SHIFT 2 114 /** Old Packet Format: Mask for length type. 115 * Bits 1 and 0 of the packet tag are the length type 116 * in the old packet format. 117 * 118 * See #__ops_ptag_of_lt_t for the meaning of the values. 119 * 120 * \see RFC4880 4.2 121 */ 122 #define OPS_PTAG_OF_LENGTH_TYPE_MASK 0x03 123 124 125 /** Old Packet Format Lengths. 126 * Defines the meanings of the 2 bits for length type in the 127 * old packet format. 128 * 129 * \see RFC4880 4.2.1 130 */ 131 typedef enum { 132 OPS_PTAG_OLD_LEN_1 = 0x00, /* Packet has a 1 byte length - 133 * header is 2 bytes long. */ 134 OPS_PTAG_OLD_LEN_2 = 0x01, /* Packet has a 2 byte length - 135 * header is 3 bytes long. */ 136 OPS_PTAG_OLD_LEN_4 = 0x02, /* Packet has a 4 byte 137 * length - header is 5 bytes 138 * long. */ 139 OPS_PTAG_OLD_LEN_INDETERMINATE = 0x03 /* Packet has a 140 * indeterminate length. */ 141 } __ops_ptag_of_lt_t; 142 143 144 /** New Packet Format: Mask for content tag. 145 * In the new packet format the 6 rightmost bits 146 * are the content tag. This is the mask to apply 147 * to the packet tag. Note that you need to 148 * shift by #OPS_PTAG_NF_CONTENT_TAG_SHIFT bits. 149 * 150 * \see RFC4880 4.2 151 */ 152 #define OPS_PTAG_NF_CONTENT_TAG_MASK 0x3f 153 /** New Packet Format: Offset for the content tag. 154 * As described at #OPS_PTAG_NF_CONTENT_TAG_MASK the 155 * content tag needs to be shifted after being masked 156 * out from the Packet Tag. 157 * 158 * \see RFC4880 4.2 159 */ 160 #define OPS_PTAG_NF_CONTENT_TAG_SHIFT 0 161 162 /* PTag Content Tags */ 163 /***************************/ 164 165 /** Package Tags (aka Content Tags) and signature subpacket types. 166 * This enumerates all rfc-defined packet tag values and the 167 * signature subpacket type values that we understand. 168 * 169 * \see RFC4880 4.3 170 * \see RFC4880 5.2.3.1 171 */ 172 typedef enum { 173 OPS_PTAG_CT_RESERVED = 0, /* Reserved - a packet tag must 174 * not have this value */ 175 OPS_PTAG_CT_PK_SESSION_KEY = 1, /* Public-Key Encrypted Session 176 * Key Packet */ 177 OPS_PTAG_CT_SIGNATURE = 2, /* Signature Packet */ 178 OPS_PTAG_CT_SK_SESSION_KEY = 3, /* Symmetric-Key Encrypted Session 179 * Key Packet */ 180 OPS_PTAG_CT_1_PASS_SIG = 4, /* One-Pass Signature 181 * Packet */ 182 OPS_PTAG_CT_SECRET_KEY = 5, /* Secret Key Packet */ 183 OPS_PTAG_CT_PUBLIC_KEY = 6, /* Public Key Packet */ 184 OPS_PTAG_CT_SECRET_SUBKEY = 7, /* Secret Subkey Packet */ 185 OPS_PTAG_CT_COMPRESSED = 8, /* Compressed Data Packet */ 186 OPS_PTAG_CT_SE_DATA = 9,/* Symmetrically Encrypted Data Packet */ 187 OPS_PTAG_CT_MARKER = 10,/* Marker Packet */ 188 OPS_PTAG_CT_LITDATA = 11, /* Literal Data Packet */ 189 OPS_PTAG_CT_TRUST = 12, /* Trust Packet */ 190 OPS_PTAG_CT_USER_ID = 13, /* User ID Packet */ 191 OPS_PTAG_CT_PUBLIC_SUBKEY = 14, /* Public Subkey Packet */ 192 OPS_PTAG_CT_RESERVED2 = 15, /* reserved */ 193 OPS_PTAG_CT_RESERVED3 = 16, /* reserved */ 194 OPS_PTAG_CT_USER_ATTR = 17, /* User Attribute Packet */ 195 OPS_PTAG_CT_SE_IP_DATA = 18, /* Sym. Encrypted and Integrity 196 * Protected Data Packet */ 197 OPS_PTAG_CT_MDC = 19, /* Modification Detection Code Packet */ 198 199 OPS_PARSER_PTAG = 0x100,/* Internal Use: The packet is the "Packet 200 * Tag" itself - used when callback sends 201 * back the PTag. */ 202 OPS_PTAG_RAW_SS = 0x101,/* Internal Use: content is raw sig subtag */ 203 OPS_PTAG_SS_ALL = 0x102,/* Internal Use: select all subtags */ 204 OPS_PARSER_PACKET_END = 0x103, 205 206 /* signature subpackets (0x200-2ff) (type+0x200) */ 207 /* only those we can parse are listed here */ 208 OPS_PTAG_SIG_SUBPKT_BASE = 0x200, /* Base for signature 209 * subpacket types - All 210 * signature type values 211 * are relative to this 212 * value. */ 213 OPS_PTAG_SS_CREATION_TIME = 0x200 + 2, /* signature creation time */ 214 OPS_PTAG_SS_EXPIRATION_TIME = 0x200 + 3, /* signature 215 * expiration time */ 216 217 OPS_PTAG_SS_EXPORT_CERT = 0x200 + 4, /* exportable certification */ 218 OPS_PTAG_SS_TRUST = 0x200 + 5, /* trust signature */ 219 OPS_PTAG_SS_REGEXP = 0x200 + 6, /* regular expression */ 220 OPS_PTAG_SS_REVOCABLE = 0x200 + 7, /* revocable */ 221 OPS_PTAG_SS_KEY_EXPIRY = 0x200 + 9, /* key expiration 222 * time */ 223 OPS_PTAG_SS_RESERVED = 0x200 + 10, /* reserved */ 224 OPS_PTAG_SS_PREFERRED_SKA = 0x200 + 11, /* preferred symmetric 225 * algs */ 226 OPS_PTAG_SS_REVOCATION_KEY = 0x200 + 12, /* revocation key */ 227 OPS_PTAG_SS_ISSUER_KEY_ID = 0x200 + 16, /* issuer key ID */ 228 OPS_PTAG_SS_NOTATION_DATA = 0x200 + 20, /* notation data */ 229 OPS_PTAG_SS_PREFERRED_HASH = 0x200 + 21, /* preferred hash 230 * algs */ 231 OPS_PTAG_SS_PREF_COMPRESS = 0x200 + 22, /* preferred 232 * compression 233 * algorithms */ 234 OPS_PTAG_SS_KEYSERV_PREFS = 0x200 + 23, /* key server 235 * preferences */ 236 OPS_PTAG_SS_PREF_KEYSERV = 0x200 + 24, /* Preferred Key 237 * Server */ 238 OPS_PTAG_SS_PRIMARY_USER_ID = 0x200 + 25, /* primary User ID */ 239 OPS_PTAG_SS_POLICY_URI = 0x200 + 26, /* Policy URI */ 240 OPS_PTAG_SS_KEY_FLAGS = 0x200 + 27, /* key flags */ 241 OPS_PTAG_SS_SIGNERS_USER_ID = 0x200 + 28, /* Signer's User ID */ 242 OPS_PTAG_SS_REVOCATION_REASON = 0x200 + 29, /* reason for 243 * revocation */ 244 OPS_PTAG_SS_FEATURES = 0x200 + 30, /* features */ 245 OPS_PTAG_SS_SIGNATURE_TARGET = 0x200 + 31, /* signature target */ 246 OPS_PTAG_SS_EMBEDDED_SIGNATURE = 0x200 + 32, /* embedded signature */ 247 248 OPS_PTAG_SS_USERDEFINED00 = 0x200 + 100, /* internal or 249 * user-defined */ 250 OPS_PTAG_SS_USERDEFINED01 = 0x200 + 101, 251 OPS_PTAG_SS_USERDEFINED02 = 0x200 + 102, 252 OPS_PTAG_SS_USERDEFINED03 = 0x200 + 103, 253 OPS_PTAG_SS_USERDEFINED04 = 0x200 + 104, 254 OPS_PTAG_SS_USERDEFINED05 = 0x200 + 105, 255 OPS_PTAG_SS_USERDEFINED06 = 0x200 + 106, 256 OPS_PTAG_SS_USERDEFINED07 = 0x200 + 107, 257 OPS_PTAG_SS_USERDEFINED08 = 0x200 + 108, 258 OPS_PTAG_SS_USERDEFINED09 = 0x200 + 109, 259 OPS_PTAG_SS_USERDEFINED10 = 0x200 + 110, 260 261 /* pseudo content types */ 262 OPS_PTAG_CT_LITDATA_HEADER = 0x300, 263 OPS_PTAG_CT_LITDATA_BODY = 0x300 + 1, 264 OPS_PTAG_CT_SIGNATURE_HEADER = 0x300 + 2, 265 OPS_PTAG_CT_SIGNATURE_FOOTER = 0x300 + 3, 266 OPS_PTAG_CT_ARMOUR_HEADER = 0x300 + 4, 267 OPS_PTAG_CT_ARMOUR_TRAILER = 0x300 + 5, 268 OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER = 0x300 + 6, 269 OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY = 0x300 + 7, 270 OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER = 0x300 + 8, 271 OPS_PTAG_CT_UNARMOURED_TEXT = 0x300 + 9, 272 OPS_PTAG_CT_ENCRYPTED_SECRET_KEY = 0x300 + 10, /* In this case the 273 * algorithm specific 274 * fields will not be 275 * initialised */ 276 OPS_PTAG_CT_SE_DATA_HEADER = 0x300 + 11, 277 OPS_PTAG_CT_SE_DATA_BODY = 0x300 + 12, 278 OPS_PTAG_CT_SE_IP_DATA_HEADER = 0x300 + 13, 279 OPS_PTAG_CT_SE_IP_DATA_BODY = 0x300 + 14, 280 OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY = 0x300 + 15, 281 282 /* commands to the callback */ 283 OPS_GET_PASSPHRASE = 0x400, 284 OPS_GET_SECKEY = 0x400 + 1, 285 286 /* Errors */ 287 OPS_PARSER_ERROR = 0x500, /* Internal Use: Parser Error */ 288 OPS_PARSER_ERRCODE = 0x500 + 1 /* Internal Use: Parser Error 289 * with errcode returned */ 290 } __ops_content_tag_t; 291 292 typedef __ops_content_tag_t __ops_packet_tag_t; 293 typedef __ops_content_tag_t __ops_ss_type_t; 294 295 /** Structure to hold one parse error string. */ 296 typedef struct { 297 const char *error; /* error message. */ 298 } __ops_parser_error_t; 299 300 /** Structure to hold one error code */ 301 typedef struct { 302 __ops_errcode_t errcode; 303 } __ops_parser_errcode_t; 304 305 /** Structure to hold one packet tag. 306 * \see RFC4880 4.2 307 */ 308 typedef struct { 309 unsigned new_format; /* Whether this packet tag is new 310 * (1) or old format (0) */ 311 unsigned type; /* content_tag value - See 312 * #__ops_content_tag_t for meanings */ 313 __ops_ptag_of_lt_t length_type; /* Length type (#__ops_ptag_of_lt_t) 314 * - only if this packet tag is old 315 * format. Set to 0 if new format. */ 316 unsigned length; /* The length of the packet. This value 317 * is set when we read and compute the length 318 * information, not at the same moment we 319 * create the packet tag structure. Only 320 * defined if #readc is set. *//* XXX: Ben, is this correct? */ 321 unsigned position; /* The position (within the 322 * current reader) of the packet */ 323 unsigned size; /* number of bits */ 324 } __ops_ptag_t; 325 326 /** Public Key Algorithm Numbers. 327 * OpenPGP assigns a unique Algorithm Number to each algorithm that is part of OpenPGP. 328 * 329 * This lists algorithm numbers for public key algorithms. 330 * 331 * \see RFC4880 9.1 332 */ 333 typedef enum { 334 OPS_PKA_RSA = 1, /* RSA (Encrypt or Sign) */ 335 OPS_PKA_RSA_ENCRYPT_ONLY = 2, /* RSA Encrypt-Only (deprecated - 336 * \see RFC4880 13.5) */ 337 OPS_PKA_RSA_SIGN_ONLY = 3, /* RSA Sign-Only (deprecated - 338 * \see RFC4880 13.5) */ 339 OPS_PKA_ELGAMAL = 16, /* Elgamal (Encrypt-Only) */ 340 OPS_PKA_DSA = 17, /* DSA (Digital Signature Algorithm) */ 341 OPS_PKA_RESERVED_ELLIPTIC_CURVE = 18, /* Reserved for Elliptic 342 * Curve */ 343 OPS_PKA_RESERVED_ECDSA = 19, /* Reserved for ECDSA */ 344 OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN = 20, /* Deprecated. */ 345 OPS_PKA_RESERVED_DH = 21, /* Reserved for Diffie-Hellman 346 * (X9.42, as defined for 347 * IETF-S/MIME) */ 348 OPS_PKA_PRIVATE00 = 100,/* Private/Experimental Algorithm */ 349 OPS_PKA_PRIVATE01 = 101,/* Private/Experimental Algorithm */ 350 OPS_PKA_PRIVATE02 = 102,/* Private/Experimental Algorithm */ 351 OPS_PKA_PRIVATE03 = 103,/* Private/Experimental Algorithm */ 352 OPS_PKA_PRIVATE04 = 104,/* Private/Experimental Algorithm */ 353 OPS_PKA_PRIVATE05 = 105,/* Private/Experimental Algorithm */ 354 OPS_PKA_PRIVATE06 = 106,/* Private/Experimental Algorithm */ 355 OPS_PKA_PRIVATE07 = 107,/* Private/Experimental Algorithm */ 356 OPS_PKA_PRIVATE08 = 108,/* Private/Experimental Algorithm */ 357 OPS_PKA_PRIVATE09 = 109,/* Private/Experimental Algorithm */ 358 OPS_PKA_PRIVATE10 = 110 /* Private/Experimental Algorithm */ 359 } __ops_pubkey_alg_t; 360 361 /** Structure to hold one DSA public key params. 362 * 363 * \see RFC4880 5.5.2 364 */ 365 typedef struct { 366 BIGNUM *p; /* DSA prime p */ 367 BIGNUM *q; /* DSA group order q */ 368 BIGNUM *g; /* DSA group generator g */ 369 BIGNUM *y; /* DSA public key value y (= g^x mod p 370 * with x being the secret) */ 371 } __ops_dsa_pubkey_t; 372 373 /** Structure to hold an RSA public key. 374 * 375 * \see RFC4880 5.5.2 376 */ 377 typedef struct { 378 BIGNUM *n; /* RSA public modulus n */ 379 BIGNUM *e; /* RSA public encryption exponent e */ 380 } __ops_rsa_pubkey_t; 381 382 /** Structure to hold an ElGamal public key params. 383 * 384 * \see RFC4880 5.5.2 385 */ 386 typedef struct { 387 BIGNUM *p; /* ElGamal prime p */ 388 BIGNUM *g; /* ElGamal group generator g */ 389 BIGNUM *y; /* ElGamal public key value y (= g^x mod p 390 * with x being the secret) */ 391 } __ops_elgamal_pubkey_t; 392 393 /** Union to hold public key params of any algorithm */ 394 typedef union { 395 __ops_dsa_pubkey_t dsa; /* A DSA public key */ 396 __ops_rsa_pubkey_t rsa; /* An RSA public key */ 397 __ops_elgamal_pubkey_t elgamal; /* An ElGamal public key */ 398 } __ops_pubkey_union_t; 399 400 /** Version. 401 * OpenPGP has two different protocol versions: version 3 and version 4. 402 * 403 * \see RFC4880 5.2 404 */ 405 typedef enum { 406 OPS_V2 = 2, /* Version 2 (essentially the same as v3) */ 407 OPS_V3 = 3, /* Version 3 */ 408 OPS_V4 = 4 /* Version 4 */ 409 } __ops_version_t; 410 411 /** Structure to hold a pgp public key */ 412 typedef struct { 413 __ops_version_t version;/* version of the key (v3, v4...) */ 414 time_t birthtime; 415 /* when the key was created. Note that 416 * interpretation varies with key version. */ 417 unsigned days_valid; 418 /* validity period of the key in days since 419 * creation. A value of 0 has a special meaning 420 * indicating this key does not expire. Only used with 421 * v3 keys. */ 422 __ops_pubkey_alg_t alg; /* Public Key Algorithm type */ 423 __ops_pubkey_union_t key; /* Public Key Parameters */ 424 } __ops_pubkey_t; 425 426 /** Structure to hold data for one RSA secret key 427 */ 428 typedef struct { 429 BIGNUM *d; 430 BIGNUM *p; 431 BIGNUM *q; 432 BIGNUM *u; 433 } __ops_rsa_seckey_t; 434 435 /** __ops_dsa_seckey_t */ 436 typedef struct { 437 BIGNUM *x; 438 } __ops_dsa_seckey_t; 439 440 /** __ops_seckey_union_t */ 441 typedef union { 442 __ops_rsa_seckey_t rsa; 443 __ops_dsa_seckey_t dsa; 444 } __ops_seckey_union_t; 445 446 /** s2k_usage_t 447 */ 448 typedef enum { 449 OPS_S2KU_NONE = 0, 450 OPS_S2KU_ENCRYPTED_AND_HASHED = 254, 451 OPS_S2KU_ENCRYPTED = 255 452 } __ops_s2k_usage_t; 453 454 /** s2k_specifier_t 455 */ 456 typedef enum { 457 OPS_S2KS_SIMPLE = 0, 458 OPS_S2KS_SALTED = 1, 459 OPS_S2KS_ITERATED_AND_SALTED = 3 460 } __ops_s2k_specifier_t; 461 462 /** Symmetric Key Algorithm Numbers. 463 * OpenPGP assigns a unique Algorithm Number to each algorithm that is 464 * part of OpenPGP. 465 * 466 * This lists algorithm numbers for symmetric key algorithms. 467 * 468 * \see RFC4880 9.2 469 */ 470 typedef enum { 471 OPS_SA_PLAINTEXT = 0, /* Plaintext or unencrypted data */ 472 OPS_SA_IDEA = 1, /* IDEA */ 473 OPS_SA_TRIPLEDES = 2, /* TripleDES */ 474 OPS_SA_CAST5 = 3, /* CAST5 */ 475 OPS_SA_BLOWFISH = 4, /* Blowfish */ 476 OPS_SA_AES_128 = 7, /* AES with 128-bit key (AES) */ 477 OPS_SA_AES_192 = 8, /* AES with 192-bit key */ 478 OPS_SA_AES_256 = 9, /* AES with 256-bit key */ 479 OPS_SA_TWOFISH = 10 /* Twofish with 256-bit key (TWOFISH) */ 480 } __ops_symm_alg_t; 481 482 /** Hashing Algorithm Numbers. 483 * OpenPGP assigns a unique Algorithm Number to each algorithm that is 484 * part of OpenPGP. 485 * 486 * This lists algorithm numbers for hash algorithms. 487 * 488 * \see RFC4880 9.4 489 */ 490 typedef enum { 491 OPS_HASH_UNKNOWN = -1, /* used to indicate errors */ 492 OPS_HASH_MD5 = 1, /* MD5 */ 493 OPS_HASH_SHA1 = 2, /* SHA-1 */ 494 OPS_HASH_RIPEMD = 3, /* RIPEMD160 */ 495 496 OPS_HASH_SHA256 = 8, /* SHA256 */ 497 OPS_HASH_SHA384 = 9, /* SHA384 */ 498 OPS_HASH_SHA512 = 10, /* SHA512 */ 499 OPS_HASH_SHA224 = 11 /* SHA224 */ 500 } __ops_hash_alg_t; 501 502 void __ops_calc_mdc_hash(const unsigned char *, 503 const size_t, 504 const unsigned char *, 505 const unsigned int, 506 unsigned char *); 507 unsigned __ops_is_hash_alg_supported(const __ops_hash_alg_t *); 508 509 /* Maximum block size for symmetric crypto */ 510 #define OPS_MAX_BLOCK_SIZE 16 511 512 /* Maximum key size for symmetric crypto */ 513 #define OPS_MAX_KEY_SIZE 32 514 515 /* Salt size for hashing */ 516 #define OPS_SALT_SIZE 8 517 518 /* Max hash size */ 519 #define OPS_MAX_HASH_SIZE 64 520 521 /** __ops_seckey_t 522 */ 523 typedef struct { 524 __ops_pubkey_t pubkey; 525 __ops_s2k_usage_t s2k_usage; 526 __ops_s2k_specifier_t s2k_specifier; 527 __ops_symm_alg_t alg; 528 __ops_hash_alg_t hash_alg; 529 unsigned char salt[OPS_SALT_SIZE]; 530 unsigned octetc; 531 unsigned char iv[OPS_MAX_BLOCK_SIZE]; 532 __ops_seckey_union_t key; 533 unsigned checksum; 534 unsigned char *checkhash; 535 } __ops_seckey_t; 536 537 /** Structure to hold one trust packet's data */ 538 539 typedef struct { 540 __ops_data_t data; /* Trust Packet */ 541 } __ops_trust_t; 542 543 /** Structure to hold one user id */ 544 typedef struct { 545 unsigned char *userid;/* User ID - UTF-8 string */ 546 } __ops_userid_t; 547 548 /** Structure to hold one user attribute */ 549 typedef struct { 550 __ops_data_t data; /* User Attribute */ 551 } __ops_userattr_t; 552 553 /** Signature Type. 554 * OpenPGP defines different signature types that allow giving 555 * different meanings to signatures. Signature types include 0x10 for 556 * generitc User ID certifications (used when Ben signs Weasel's key), 557 * Subkey binding signatures, document signatures, key revocations, 558 * etc. 559 * 560 * Different types are used in different places, and most make only 561 * sense in their intended location (for instance a subkey binding has 562 * no place on a UserID). 563 * 564 * \see RFC4880 5.2.1 565 */ 566 typedef enum { 567 OPS_SIG_BINARY = 0x00, /* Signature of a binary document */ 568 OPS_SIG_TEXT = 0x01, /* Signature of a canonical text document */ 569 OPS_SIG_STANDALONE = 0x02, /* Standalone signature */ 570 571 OPS_CERT_GENERIC = 0x10,/* Generic certification of a User ID and 572 * Public Key packet */ 573 OPS_CERT_PERSONA = 0x11,/* Persona certification of a User ID and 574 * Public Key packet */ 575 OPS_CERT_CASUAL = 0x12, /* Casual certification of a User ID and 576 * Public Key packet */ 577 OPS_CERT_POSITIVE = 0x13, /* Positive certification of a 578 * User ID and Public Key packet */ 579 580 OPS_SIG_SUBKEY = 0x18, /* Subkey Binding Signature */ 581 OPS_SIG_PRIMARY = 0x19, /* Primary Key Binding Signature */ 582 OPS_SIG_DIRECT = 0x1f, /* Signature directly on a key */ 583 584 OPS_SIG_REV_KEY = 0x20, /* Key revocation signature */ 585 OPS_SIG_REV_SUBKEY = 0x28, /* Subkey revocation signature */ 586 OPS_SIG_REV_CERT = 0x30,/* Certification revocation signature */ 587 588 OPS_SIG_TIMESTAMP = 0x40, /* Timestamp signature */ 589 590 OPS_SIG_3RD_PARTY = 0x50/* Third-Party Confirmation signature */ 591 } __ops_sig_type_t; 592 593 /** Struct to hold params of an RSA signature */ 594 typedef struct { 595 BIGNUM *sig; /* the signature value (m^d % n) */ 596 } __ops_rsa_sig_t; 597 598 /** Struct to hold params of a DSA signature */ 599 typedef struct { 600 BIGNUM *r; /* DSA value r */ 601 BIGNUM *s; /* DSA value s */ 602 } __ops_dsa_sig_t; 603 604 /** __ops_elgamal_signature_t */ 605 typedef struct { 606 BIGNUM *r; 607 BIGNUM *s; 608 } __ops_elgamal_sig_t; 609 610 /** Struct to hold data for a private/experimental signature */ 611 typedef struct { 612 __ops_data_t data; 613 } __ops_unknown_sig_t; 614 615 /** Union to hold signature params of any algorithm */ 616 typedef union { 617 __ops_rsa_sig_t rsa;/* An RSA Signature */ 618 __ops_dsa_sig_t dsa;/* A DSA Signature */ 619 __ops_elgamal_sig_t elgamal; /* deprecated */ 620 __ops_unknown_sig_t unknown; /* private or experimental */ 621 } __ops_sig_union_t; 622 623 #define OPS_KEY_ID_SIZE 8 624 #define OPS_FINGERPRINT_SIZE 20 625 626 /** Struct to hold a signature packet. 627 * 628 * \see RFC4880 5.2.2 629 * \see RFC4880 5.2.3 630 */ 631 typedef struct { 632 __ops_version_t version;/* signature version number */ 633 __ops_sig_type_t type; /* signature type value */ 634 time_t birthtime; /* creation time of the signature */ 635 unsigned char signer_id[OPS_KEY_ID_SIZE]; /* Eight-octet key ID 636 * of signer */ 637 __ops_pubkey_alg_t key_alg; /* public key 638 * algorithm number */ 639 __ops_hash_alg_t hash_alg; /* hashing algorithm 640 * number */ 641 __ops_sig_union_t sig; /* signature params */ 642 size_t v4_hashlen; 643 unsigned char *v4_hashed; 644 unsigned birthtime_set:1; 645 unsigned signer_id_set:1; 646 } __ops_sig_info_t; 647 648 /** Struct used when parsing a signature */ 649 typedef struct __ops_sig_t { 650 __ops_sig_info_t info; /* The signature information */ 651 /* The following fields are only used while parsing the signature */ 652 unsigned char hash2[2]; /* high 2 bytes of hashed value - 653 * for quick test */ 654 size_t v4_hashstart; /* only valid if accumulate 655 * is set */ 656 __ops_hash_t *hash; /* if set, the hash filled in for the data 657 * so far */ 658 } __ops_sig_t; 659 660 /** The raw bytes of a signature subpacket */ 661 662 typedef struct { 663 __ops_content_tag_t tag; 664 size_t length; 665 unsigned char *raw; 666 } __ops_ss_raw_t; 667 668 /** Signature Subpacket : Trust Level */ 669 670 typedef struct { 671 unsigned char level; /* Trust Level */ 672 unsigned char amount; /* Amount */ 673 } __ops_ss_trust_t; 674 675 /** Signature Subpacket : Revocable */ 676 typedef struct { 677 unsigned revocable; 678 } __ops_ss_revocable_t; 679 680 /** Signature Subpacket : Time */ 681 typedef struct { 682 time_t time; 683 } __ops_ss_time_t; 684 685 /** Signature Subpacket : Key ID */ 686 typedef struct { 687 unsigned char key_id[OPS_KEY_ID_SIZE]; 688 } __ops_ss_key_id_t; 689 690 /** Signature Subpacket : Notation Data */ 691 typedef struct { 692 __ops_data_t flags; 693 __ops_data_t name; 694 __ops_data_t value; 695 } __ops_ss_notation_t; 696 697 /** Signature Subpacket : User Defined */ 698 typedef struct { 699 __ops_data_t data; 700 } __ops_ss_userdef_t; 701 702 /** Signature Subpacket : Unknown */ 703 typedef struct { 704 __ops_data_t data; 705 } __ops_ss_unknown_t; 706 707 /** Signature Subpacket : Preferred Symmetric Key Algorithm */ 708 typedef struct { 709 __ops_data_t data; 710 /* 711 * Note that value 0 may represent the plaintext algorithm so we 712 * cannot expect data->contents to be a null-terminated list 713 */ 714 } __ops_ss_skapref_t; 715 716 /** Signature Subpacket : Preferrred Hash Algorithm */ 717 typedef struct { 718 __ops_data_t data; 719 } __ops_ss_hashpref_t; 720 721 /** Signature Subpacket : Preferred Compression */ 722 typedef struct { 723 __ops_data_t data; 724 } __ops_ss_zpref_t; 725 726 /** Signature Subpacket : Key Flags */ 727 typedef struct { 728 __ops_data_t data; 729 } __ops_ss_key_flags_t; 730 731 /** Signature Subpacket : Key Server Preferences */ 732 typedef struct { 733 __ops_data_t data; 734 } __ops_ss_key_server_prefs_t; 735 736 /** Signature Subpacket : Features */ 737 typedef struct { 738 __ops_data_t data; 739 } __ops_ss_features_t; 740 741 /** Signature Subpacket : Signature Target */ 742 typedef struct { 743 __ops_pubkey_alg_t pka_alg; 744 __ops_hash_alg_t hash_alg; 745 __ops_data_t hash; 746 } __ops_ss_sig_target_t; 747 748 /** Signature Subpacket : Embedded Signature */ 749 typedef struct { 750 __ops_data_t sig; 751 } __ops_ss_embedded_sig_t; 752 753 /** __ops_subpacket_t */ 754 755 typedef struct __ops_subpacket_t { 756 size_t length; 757 unsigned char *raw; 758 } __ops_subpacket_t; 759 760 /** Types of Compression */ 761 typedef enum { 762 OPS_C_NONE = 0, 763 OPS_C_ZIP = 1, 764 OPS_C_ZLIB = 2, 765 OPS_C_BZIP2 = 3 766 } __ops_compression_type_t; 767 768 /* 769 * unlike most structures, this will feed its data as a stream to the 770 * application instead of directly including it 771 */ 772 /** __ops_compressed_t */ 773 typedef struct { 774 __ops_compression_type_t type; 775 } __ops_compressed_t; 776 777 /** __ops_one_pass_sig_t */ 778 typedef struct { 779 unsigned char version; 780 __ops_sig_type_t sig_type; 781 __ops_hash_alg_t hash_alg; 782 __ops_pubkey_alg_t key_alg; 783 unsigned char keyid[OPS_KEY_ID_SIZE]; 784 unsigned nested; 785 } __ops_one_pass_sig_t; 786 787 /** Signature Subpacket : Primary User ID */ 788 typedef struct { 789 unsigned primary_userid; 790 } __ops_ss_primary_userid_t; 791 792 /** Signature Subpacket : Regexp */ 793 typedef struct { 794 char *regexp; 795 } __ops_ss_regexp_t; 796 797 /** Signature Subpacket : Policy URL */ 798 typedef struct { 799 char *url; 800 } __ops_ss_policy_t; 801 802 /** Signature Subpacket : Preferred Key Server */ 803 typedef struct { 804 char *name; 805 } __ops_ss_keyserv_t; 806 807 /** Signature Subpacket : Revocation Key */ 808 typedef struct { 809 unsigned char class; 810 unsigned char algid; 811 unsigned char fingerprint[OPS_FINGERPRINT_SIZE]; 812 } __ops_ss_revocation_key_t; 813 814 /** Signature Subpacket : Revocation Reason */ 815 typedef struct { 816 unsigned char code; 817 char *reason; 818 } __ops_ss_revocation_t; 819 820 /** litdata_type_t */ 821 typedef enum { 822 OPS_LDT_BINARY = 'b', 823 OPS_LDT_TEXT = 't', 824 OPS_LDT_UTF8 = 'u', 825 OPS_LDT_LOCAL = 'l', 826 OPS_LDT_LOCAL2 = '1' 827 } __ops_litdata_type_t; 828 829 /** __ops_litdata_header_t */ 830 typedef struct { 831 __ops_litdata_type_t format; 832 char filename[256]; 833 time_t mtime; 834 } __ops_litdata_header_t; 835 836 /** __ops_litdata_body_t */ 837 typedef struct { 838 unsigned length; 839 unsigned char *data; 840 void *mem; /* __ops_memory_t pointer */ 841 } __ops_litdata_body_t; 842 843 /** __ops_mdc_t */ 844 typedef struct { 845 unsigned length; 846 unsigned char *data; 847 } __ops_mdc_t; 848 849 /** __ops_header_var_t */ 850 typedef struct { 851 char *key; 852 char *value; 853 } __ops_header_var_t; 854 855 /** __ops_headers_t */ 856 typedef struct { 857 __ops_header_var_t *headers; 858 unsigned headerc; 859 } __ops_headers_t; 860 861 /** __ops_armour_header_t */ 862 typedef struct { 863 const char *type; 864 __ops_headers_t headers; 865 } __ops_armour_header_t; 866 867 /** __ops_armour_trailer_t */ 868 typedef struct { 869 const char *type; 870 } __ops_armour_trailer_t; 871 872 /** __ops_cleartext_head_t */ 873 typedef struct { 874 __ops_headers_t headers; 875 } __ops_cleartext_head_t; 876 877 /** __ops_cleartext_body_t */ 878 typedef struct { 879 unsigned length; 880 unsigned char data[8192]; /* \todo fix hard-coded value? */ 881 } __ops_cleartext_body_t; 882 883 /** __ops_cleartext_trailer_t */ 884 typedef struct { 885 struct _ops_hash_t *hash; /* This will not have been 886 * finalised, but will have seen all 887 * the cleartext data in canonical 888 * form */ 889 } __ops_cleartext_trailer_t; 890 891 /** __ops_unarmoured_text_t */ 892 typedef struct { 893 unsigned length; 894 unsigned char *data; 895 } __ops_unarmoured_text_t; 896 897 typedef enum { 898 SE_IP_DATA_VERSION = 1 899 } __ops_se_ip_data_version_t; 900 901 typedef enum { 902 OPS_PKSK_V3 = 3 903 } __ops_pk_sesskey_version_t; 904 905 /** __ops_pk_sesskey_params_rsa_t */ 906 typedef struct { 907 BIGNUM *encrypted_m; 908 BIGNUM *m; 909 } __ops_pk_sesskey_params_rsa_t; 910 911 /** __ops_pk_sesskey_params_elgamal_t */ 912 typedef struct { 913 BIGNUM *g_to_k; 914 BIGNUM *encrypted_m; 915 } __ops_pk_sesskey_params_elgamal_t; 916 917 /** __ops_pk_sesskey_params_t */ 918 typedef union { 919 __ops_pk_sesskey_params_rsa_t rsa; 920 __ops_pk_sesskey_params_elgamal_t elgamal; 921 } __ops_pk_sesskey_params_t; 922 923 /** __ops_pk_sesskey_t */ 924 typedef struct { 925 __ops_pk_sesskey_version_t version; 926 unsigned char key_id[OPS_KEY_ID_SIZE]; 927 __ops_pubkey_alg_t alg; 928 __ops_pk_sesskey_params_t params; 929 __ops_symm_alg_t symm_alg; 930 unsigned char key[OPS_MAX_KEY_SIZE]; 931 unsigned short checksum; 932 } __ops_pk_sesskey_t; 933 934 /** __ops_seckey_passphrase_t */ 935 typedef struct { 936 const __ops_seckey_t *seckey; 937 char **passphrase; /* point somewhere that gets filled 938 * in to work around constness of 939 * content */ 940 } __ops_seckey_passphrase_t; 941 942 typedef enum { 943 OPS_SE_IP_V1 = 1 944 } __ops_se_ip_version_t; 945 946 /** __ops_se_ip_data_header_t */ 947 typedef struct { 948 __ops_se_ip_version_t version; 949 } __ops_se_ip_data_header_t; 950 951 /** __ops_se_ip_data_body_t */ 952 typedef struct { 953 unsigned length; 954 unsigned char *data; /* \todo remember to free this */ 955 } __ops_se_ip_data_body_t; 956 957 /** __ops_se_data_body_t */ 958 typedef struct { 959 unsigned length; 960 unsigned char data[8192]; /* \todo parameterise this! */ 961 } __ops_se_data_body_t; 962 963 /** __ops_get_seckey_t */ 964 typedef struct { 965 const __ops_seckey_t **seckey; 966 const __ops_pk_sesskey_t *pk_sesskey; 967 } __ops_get_seckey_t; 968 969 /** __ops_parser_union_content_t */ 970 typedef union { 971 __ops_parser_error_t error; 972 __ops_parser_errcode_t errcode; 973 __ops_ptag_t ptag; 974 __ops_pubkey_t pubkey; 975 __ops_trust_t trust; 976 __ops_userid_t userid; 977 __ops_userattr_t userattr; 978 __ops_sig_t sig; 979 __ops_ss_raw_t ss_raw; 980 __ops_ss_trust_t ss_trust; 981 __ops_ss_revocable_t ss_revocable; 982 __ops_ss_time_t ss_time; 983 __ops_ss_key_id_t ss_issuer; 984 __ops_ss_notation_t ss_notation; 985 __ops_subpacket_t packet; 986 __ops_compressed_t compressed; 987 __ops_one_pass_sig_t one_pass_sig; 988 __ops_ss_skapref_t ss_skapref; 989 __ops_ss_hashpref_t ss_hashpref; 990 __ops_ss_zpref_t ss_zpref; 991 __ops_ss_key_flags_t ss_key_flags; 992 __ops_ss_key_server_prefs_t ss_key_server_prefs; 993 __ops_ss_primary_userid_t ss_primary_userid; 994 __ops_ss_regexp_t ss_regexp; 995 __ops_ss_policy_t ss_policy; 996 __ops_ss_keyserv_t ss_keyserv; 997 __ops_ss_revocation_key_t ss_revocation_key; 998 __ops_ss_userdef_t ss_userdef; 999 __ops_ss_unknown_t ss_unknown; 1000 __ops_litdata_header_t litdata_header; 1001 __ops_litdata_body_t litdata_body; 1002 __ops_mdc_t mdc; 1003 __ops_ss_features_t ss_features; 1004 __ops_ss_sig_target_t ss_sig_target; 1005 __ops_ss_embedded_sig_t ss_embedded_sig; 1006 __ops_ss_revocation_t ss_revocation; 1007 __ops_seckey_t seckey; 1008 __ops_userid_t ss_signer; 1009 __ops_armour_header_t armour_header; 1010 __ops_armour_trailer_t armour_trailer; 1011 __ops_cleartext_head_t cleartext_head; 1012 __ops_cleartext_body_t cleartext_body; 1013 __ops_cleartext_trailer_t cleartext_trailer; 1014 __ops_unarmoured_text_t unarmoured_text; 1015 __ops_pk_sesskey_t pk_sesskey; 1016 __ops_seckey_passphrase_t skey_passphrase; 1017 __ops_se_ip_data_header_t se_ip_data_header; 1018 __ops_se_ip_data_body_t se_ip_data_body; 1019 __ops_se_data_body_t se_data_body; 1020 __ops_get_seckey_t get_seckey; 1021 } __ops_contents_t; 1022 1023 /** __ops_packet_t */ 1024 struct __ops_packet_t { 1025 __ops_content_tag_t tag; /* type of contents */ 1026 unsigned char critical; /* for sig subpackets */ 1027 __ops_contents_t u; /* union for contents */ 1028 }; 1029 1030 /** __ops_fingerprint_t */ 1031 typedef struct { 1032 unsigned char fingerprint[OPS_FINGERPRINT_SIZE]; 1033 unsigned length; 1034 } __ops_fingerprint_t; 1035 1036 void __ops_init(void); 1037 void __ops_finish(void); 1038 void __ops_keyid(unsigned char *, const size_t, const __ops_pubkey_t *); 1039 void __ops_fingerprint(__ops_fingerprint_t *, const __ops_pubkey_t *); 1040 void __ops_pubkey_free(__ops_pubkey_t *); 1041 void __ops_userid_free(__ops_userid_t *); 1042 void __ops_userattr_free(__ops_userattr_t *); 1043 void __ops_sig_free(__ops_sig_t *); 1044 void __ops_trust_free(__ops_trust_t *); 1045 void __ops_ss_skapref_free(__ops_ss_skapref_t *); 1046 void __ops_ss_hashpref_free(__ops_ss_hashpref_t *); 1047 void __ops_ss_zpref_free(__ops_ss_zpref_t *); 1048 void __ops_ss_key_flags_free(__ops_ss_key_flags_t *); 1049 void __ops_ss_key_server_prefs_free(__ops_ss_key_server_prefs_t *); 1050 void __ops_ss_features_free(__ops_ss_features_t *); 1051 void __ops_ss_notation_free(__ops_ss_notation_t *); 1052 void __ops_ss_policy_free(__ops_ss_policy_t *); 1053 void __ops_ss_keyserv_free(__ops_ss_keyserv_t *); 1054 void __ops_ss_regexp_free(__ops_ss_regexp_t *); 1055 void __ops_ss_userdef_free(__ops_ss_userdef_t *); 1056 void __ops_ss_reserved_free(__ops_ss_unknown_t *); 1057 void __ops_ss_revocation_free(__ops_ss_revocation_t *); 1058 void __ops_ss_sig_target_free(__ops_ss_sig_target_t *); 1059 void __ops_ss_embedded_sig_free(__ops_ss_embedded_sig_t *); 1060 1061 void __ops_subpacket_free(__ops_subpacket_t *); 1062 void __ops_parser_content_free(__ops_packet_t *); 1063 void __ops_seckey_free(__ops_seckey_t *); 1064 void __ops_pk_sesskey_free(__ops_pk_sesskey_t *); 1065 1066 int __ops_print_packet(const __ops_packet_t *); 1067 1068 #define DYNARRAY(type, arr) \ 1069 unsigned arr##c; unsigned arr##vsize; type *arr##s 1070 1071 #define EXPAND_ARRAY(str, arr) do { \ 1072 if (str->arr##c == str->arr##vsize) { \ 1073 str->arr##vsize = (str->arr##vsize * 2) + 10; \ 1074 str->arr##s = realloc(str->arr##s, \ 1075 str->arr##vsize * sizeof(*str->arr##s)); \ 1076 } \ 1077 } while(/*CONSTCOND*/0) 1078 1079 /** __ops_keydata_key_t 1080 */ 1081 typedef union { 1082 __ops_pubkey_t pubkey; 1083 __ops_seckey_t seckey; 1084 } __ops_keydata_key_t; 1085 1086 1087 /** sigpacket_t */ 1088 typedef struct { 1089 __ops_userid_t *userid; 1090 __ops_subpacket_t *packet; 1091 } sigpacket_t; 1092 1093 /* XXX: gonna have to expand this to hold onto subkeys, too... */ 1094 /** \struct __ops_keydata 1095 * \todo expand to hold onto subkeys 1096 */ 1097 struct __ops_key_t { 1098 DYNARRAY(__ops_userid_t, uid); 1099 DYNARRAY(__ops_subpacket_t, packet); 1100 DYNARRAY(sigpacket_t, sig); 1101 unsigned char key_id[OPS_KEY_ID_SIZE]; 1102 __ops_fingerprint_t fingerprint; 1103 __ops_content_tag_t type; 1104 __ops_keydata_key_t key; 1105 }; 1106 1107 #define MDC_PKT_TAG 0xd3 1108 1109 #endif /* PACKET_H_ */ 1110