wmi-1.3.16 from opsview.com

This commit is contained in:
Are Casilla
2019-02-16 00:16:52 +01:00
parent 163fdd3d1b
commit 17b3af2911
2146 changed files with 678824 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
-- From RFC 3369 --
-- $Id: CMS.asn1,v 1.5 2006/09/07 12:20:42 lha Exp $ --
CMS DEFINITIONS ::= BEGIN
IMPORTS CertificateSerialNumber, AlgorithmIdentifier, Name,
Attribute, Certificate, Name, SubjectKeyIdentifier FROM rfc2459
heim_any, heim_any_set FROM heim;
id-pkcs7 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
us(840) rsadsi(113549) pkcs(1) pkcs7(7) }
id-pkcs7-data OBJECT IDENTIFIER ::= { id-pkcs7 1 }
id-pkcs7-signedData OBJECT IDENTIFIER ::= { id-pkcs7 2 }
id-pkcs7-envelopedData OBJECT IDENTIFIER ::= { id-pkcs7 3 }
id-pkcs7-signedAndEnvelopedData OBJECT IDENTIFIER ::= { id-pkcs7 4 }
id-pkcs7-digestedData OBJECT IDENTIFIER ::= { id-pkcs7 5 }
id-pkcs7-encryptedData OBJECT IDENTIFIER ::= { id-pkcs7 6 }
CMSVersion ::= INTEGER {
CMSVersion_v0(0),
CMSVersion_v1(1),
CMSVersion_v2(2),
CMSVersion_v3(3),
CMSVersion_v4(4)
}
DigestAlgorithmIdentifier ::= AlgorithmIdentifier
DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
ContentType ::= OBJECT IDENTIFIER
MessageDigest ::= OCTET STRING
ContentInfo ::= SEQUENCE {
contentType ContentType,
content [0] EXPLICIT heim_any OPTIONAL -- DEFINED BY contentType
}
EncapsulatedContentInfo ::= SEQUENCE {
eContentType ContentType,
eContent [0] EXPLICIT OCTET STRING OPTIONAL
}
CertificateSet ::= SET OF heim_any
CertificateList ::= Certificate
CertificateRevocationLists ::= SET OF CertificateList
IssuerAndSerialNumber ::= SEQUENCE {
issuer Name,
serialNumber CertificateSerialNumber
}
-- RecipientIdentifier is same as SignerIdentifier,
-- lets glue them togheter and save some bytes and share code for them
CMSIdentifier ::= CHOICE {
issuerAndSerialNumber IssuerAndSerialNumber,
subjectKeyIdentifier [0] SubjectKeyIdentifier
}
SignerIdentifier ::= CMSIdentifier
RecipientIdentifier ::= CMSIdentifier
--- CMSAttributes are the combined UnsignedAttributes and SignedAttributes
--- to store space and share code
CMSAttributes ::= SET OF Attribute -- SIZE (1..MAX)
SignatureValue ::= OCTET STRING
SignerInfo ::= SEQUENCE {
version CMSVersion,
sid SignerIdentifier,
digestAlgorithm DigestAlgorithmIdentifier,
signedAttrs [0] IMPLICIT -- CMSAttributes --
SET OF Attribute OPTIONAL,
signatureAlgorithm SignatureAlgorithmIdentifier,
signature SignatureValue,
unsignedAttrs [1] IMPLICIT -- CMSAttributes --
SET OF Attribute OPTIONAL
}
SignerInfos ::= SET OF SignerInfo
SignedData ::= SEQUENCE {
version CMSVersion,
digestAlgorithms DigestAlgorithmIdentifiers,
encapContentInfo EncapsulatedContentInfo,
certificates [0] IMPLICIT -- CertificateSet --
SET OF heim_any OPTIONAL,
crls [1] IMPLICIT -- CertificateRevocationLists --
heim_any OPTIONAL,
signerInfos SignerInfos
}
OriginatorInfo ::= SEQUENCE {
certs [0] IMPLICIT -- CertificateSet --
SET OF heim_any OPTIONAL,
crls [1] IMPLICIT --CertificateRevocationLists --
heim_any OPTIONAL
}
KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
EncryptedKey ::= OCTET STRING
KeyTransRecipientInfo ::= SEQUENCE {
version CMSVersion, -- always set to 0 or 2
rid RecipientIdentifier,
keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
encryptedKey EncryptedKey
}
RecipientInfo ::= KeyTransRecipientInfo
RecipientInfos ::= SET OF RecipientInfo
EncryptedContent ::= OCTET STRING
EncryptedContentInfo ::= SEQUENCE {
contentType ContentType,
contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
encryptedContent [0] IMPLICIT OCTET STRING OPTIONAL
}
UnprotectedAttributes ::= SET OF Attribute -- SIZE (1..MAX)
CMSEncryptedData ::= SEQUENCE {
version CMSVersion,
encryptedContentInfo EncryptedContentInfo,
unprotectedAttrs [1] IMPLICIT -- UnprotectedAttributes --
heim_any OPTIONAL
}
EnvelopedData ::= SEQUENCE {
version CMSVersion,
originatorInfo [0] IMPLICIT -- OriginatorInfo -- heim_any OPTIONAL,
recipientInfos RecipientInfos,
encryptedContentInfo EncryptedContentInfo,
unprotectedAttrs [1] IMPLICIT -- UnprotectedAttributes --
heim_any OPTIONAL
}
-- Data ::= OCTET STRING
CMSRC2CBCParameter ::= SEQUENCE {
rc2ParameterVersion INTEGER (0..4294967295),
iv OCTET STRING -- exactly 8 octets
}
CMSCBCParameter ::= OCTET STRING
END
@@ -0,0 +1,65 @@
/* $Id: asn1-common.h,v 1.6 2006/10/14 05:09:47 lha Exp $ */
#include <stddef.h>
#include <time.h>
#ifndef __asn1_common_definitions__
#define __asn1_common_definitions__
typedef struct heim_integer {
size_t length;
void *data;
int negative;
} heim_integer;
typedef struct heim_octet_string {
size_t length;
void *data;
} heim_octet_string;
typedef char *heim_general_string;
typedef char *heim_utf8_string;
typedef char *heim_printable_string;
typedef char *heim_ia5_string;
typedef struct heim_bmp_string {
size_t length;
uint16_t *data;
} heim_bmp_string;
typedef struct heim_universal_string {
size_t length;
uint32_t *data;
} heim_universal_string;
typedef struct heim_oid {
size_t length;
unsigned *components;
} heim_oid;
typedef struct heim_bit_string {
size_t length;
void *data;
} heim_bit_string;
typedef struct heim_octet_string heim_any;
typedef struct heim_octet_string heim_any_set;
#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \
do { \
(BL) = length_##T((S)); \
(B) = malloc((BL)); \
if((B) == NULL) { \
(R) = ENOMEM; \
} else { \
(R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \
(S), (L)); \
if((R) != 0) { \
free((B)); \
(B) = NULL; \
} \
} \
} while (0)
#endif
+21
View File
@@ -0,0 +1,21 @@
#
# Error messages for the asn.1 library
#
# This might look like a com_err file, but is not
#
id "$Id: asn1_err.et,v 1.6 2006/10/24 14:11:20 lha Exp $"
error_table asn1
prefix ASN1
error_code BAD_TIMEFORMAT, "ASN.1 failed call to system time library"
error_code MISSING_FIELD, "ASN.1 structure is missing a required field"
error_code MISPLACED_FIELD, "ASN.1 unexpected field number"
error_code TYPE_MISMATCH, "ASN.1 type numbers are inconsistent"
error_code OVERFLOW, "ASN.1 value too large"
error_code OVERRUN, "ASN.1 encoding ended unexpectedly"
error_code BAD_ID, "ASN.1 identifier doesn't match expected value"
error_code BAD_LENGTH, "ASN.1 length doesn't match expected value"
error_code BAD_FORMAT, "ASN.1 badly-formatted encoding"
error_code PARSE_ERROR, "ASN.1 parse error"
error_code EXTRA_DATA, "ASN.1 extra data past end of end structure"
end
+187
View File
@@ -0,0 +1,187 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
#include <com_err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <getarg.h>
#include <hex.h>
#include <err.h>
RCSID("$Id: asn1_gen.c,v 1.4 2006/01/30 15:06:03 lha Exp $");
static int
doit(const char *fn)
{
char buf[2048];
char *fnout;
const char *bname;
unsigned long line = 0;
FILE *f, *fout;
size_t offset = 0;
f = fopen(fn, "r");
if (f == NULL)
err(1, "fopen");
bname = strrchr(fn, '/');
if (bname)
bname++;
else
bname = fn;
asprintf(&fnout, "%s.out", bname);
if (fnout == NULL)
errx(1, "malloc");
fout = fopen(fnout, "w");
if (fout == NULL)
err(1, "fopen: output file");
while (fgets(buf, sizeof(buf), f) != NULL) {
char *ptr, *class, *type, *tag, *length, *data, *foo;
int ret, l, c, ty, ta;
unsigned char p[6], *pdata;
size_t sz;
line++;
buf[strcspn(buf, "\r\n")] = '\0';
if (buf[0] == '#' || buf[0] == '\0')
continue;
ptr = buf;
while (isspace((unsigned char)*ptr))
ptr++;
class = strtok_r(ptr, " \t\n", &foo);
if (class == NULL) errx(1, "class missing on line %lu", line);
type = strtok_r(NULL, " \t\n", &foo);
if (type == NULL) errx(1, "type missing on line %lu", line);
tag = strtok_r(NULL, " \t\n", &foo);
if (tag == NULL) errx(1, "tag missing on line %lu", line);
length = strtok_r(NULL, " \t\n", &foo);
if (length == NULL) errx(1, "length missing on line %lu", line);
data = strtok_r(NULL, " \t\n", &foo);
c = der_get_class_num(class);
if (c == -1) errx(1, "no valid class on line %lu", line);
ty = der_get_type_num(type);
if (ty == -1) errx(1, "no valid type on line %lu", line);
ta = der_get_tag_num(tag);
if (ta == -1)
ta = atoi(tag);
l = atoi(length);
printf("line: %3lu offset: %3lu class: %d type: %d "
"tag: %3d length: %3d %s\n",
line, (unsigned long)offset, c, ty, ta, l,
data ? "<have data>" : "<no data>");
ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
l,
c,
ty,
ta,
&sz);
if (ret)
errx(1, "der_put_length_and_tag: %d", ret);
if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
err(1, "fwrite length/tag failed");
offset += sz;
if (data) {
size_t datalen;
datalen = strlen(data) / 2;
pdata = emalloc(sz);
if (hex_decode(data, pdata, datalen) != datalen)
errx(1, "failed to decode data");
if (fwrite(pdata, datalen, 1, fout) != 1)
err(1, "fwrite data failed");
offset += datalen;
free(pdata);
}
}
printf("line: eof offset: %lu\n", (unsigned long)offset);
fclose(fout);
fclose(f);
return 0;
}
static int version_flag;
static int help_flag;
struct getargs args[] = {
{ "version", 0, arg_flag, &version_flag },
{ "help", 0, arg_flag, &help_flag }
};
int num_args = sizeof(args) / sizeof(args[0]);
static void
usage(int code)
{
arg_printusage(args, num_args, NULL, "parse-file");
exit(code);
}
int
main(int argc, char **argv)
{
int optidx = 0;
setprogname (argv[0]);
if(getarg(args, num_args, argc, argv, &optidx))
usage(1);
if(help_flag)
usage(0);
if(version_flag) {
print_version(NULL);
exit(0);
}
argv += optidx;
argc -= optidx;
if (argc != 1)
usage (1);
return doit (argv[0]);
}
+167
View File
@@ -0,0 +1,167 @@
/* $NetBSD: queue.h,v 1.38 2004/04/18 14:12:05 lukem Exp $ */
/* $Id: asn1_queue.h,v 1.2 2005/07/12 06:27:15 lha Exp $ */
/*
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)queue.h 8.5 (Berkeley) 8/20/94
*/
#ifndef _ASN1_QUEUE_H_
#define _ASN1_QUEUE_H_
/*
* Tail queue definitions.
*/
#define ASN1_TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
#define ASN1_TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
#define ASN1_TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
struct type **tqe_prev; /* address of previous next element */ \
}
/*
* Tail queue functions.
*/
#if defined(_KERNEL) && defined(QUEUEDEBUG)
#define QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD(head, elm, field) \
if ((head)->tqh_first && \
(head)->tqh_first->field.tqe_prev != &(head)->tqh_first) \
panic("ASN1_TAILQ_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__);
#define QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL(head, elm, field) \
if (*(head)->tqh_last != NULL) \
panic("ASN1_TAILQ_INSERT_TAIL %p %s:%d", (head), __FILE__, __LINE__);
#define QUEUEDEBUG_ASN1_TAILQ_OP(elm, field) \
if ((elm)->field.tqe_next && \
(elm)->field.tqe_next->field.tqe_prev != \
&(elm)->field.tqe_next) \
panic("ASN1_TAILQ_* forw %p %s:%d", (elm), __FILE__, __LINE__);\
if (*(elm)->field.tqe_prev != (elm)) \
panic("ASN1_TAILQ_* back %p %s:%d", (elm), __FILE__, __LINE__);
#define QUEUEDEBUG_ASN1_TAILQ_PREREMOVE(head, elm, field) \
if ((elm)->field.tqe_next == NULL && \
(head)->tqh_last != &(elm)->field.tqe_next) \
panic("ASN1_TAILQ_PREREMOVE head %p elm %p %s:%d", \
(head), (elm), __FILE__, __LINE__);
#define QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE(elm, field) \
(elm)->field.tqe_next = (void *)1L; \
(elm)->field.tqe_prev = (void *)1L;
#else
#define QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD(head, elm, field)
#define QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL(head, elm, field)
#define QUEUEDEBUG_ASN1_TAILQ_OP(elm, field)
#define QUEUEDEBUG_ASN1_TAILQ_PREREMOVE(head, elm, field)
#define QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE(elm, field)
#endif
#define ASN1_TAILQ_INIT(head) do { \
(head)->tqh_first = NULL; \
(head)->tqh_last = &(head)->tqh_first; \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_INSERT_HEAD(head, elm, field) do { \
QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD((head), (elm), field) \
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
(head)->tqh_first->field.tqe_prev = \
&(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(head)->tqh_first = (elm); \
(elm)->field.tqe_prev = &(head)->tqh_first; \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_INSERT_TAIL(head, elm, field) do { \
QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL((head), (elm), field) \
(elm)->field.tqe_next = NULL; \
(elm)->field.tqe_prev = (head)->tqh_last; \
*(head)->tqh_last = (elm); \
(head)->tqh_last = &(elm)->field.tqe_next; \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
QUEUEDEBUG_ASN1_TAILQ_OP((listelm), field) \
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
(elm)->field.tqe_next->field.tqe_prev = \
&(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(listelm)->field.tqe_next = (elm); \
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
QUEUEDEBUG_ASN1_TAILQ_OP((listelm), field) \
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
(elm)->field.tqe_next = (listelm); \
*(listelm)->field.tqe_prev = (elm); \
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_REMOVE(head, elm, field) do { \
QUEUEDEBUG_ASN1_TAILQ_PREREMOVE((head), (elm), field) \
QUEUEDEBUG_ASN1_TAILQ_OP((elm), field) \
if (((elm)->field.tqe_next) != NULL) \
(elm)->field.tqe_next->field.tqe_prev = \
(elm)->field.tqe_prev; \
else \
(head)->tqh_last = (elm)->field.tqe_prev; \
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE((elm), field); \
} while (/*CONSTCOND*/0)
#define ASN1_TAILQ_FOREACH(var, head, field) \
for ((var) = ((head)->tqh_first); \
(var); \
(var) = ((var)->field.tqe_next))
#define ASN1_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
(var); \
(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
/*
* Tail queue access methods.
*/
#define ASN1_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
#define ASN1_TAILQ_FIRST(head) ((head)->tqh_first)
#define ASN1_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#define ASN1_TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
#define ASN1_TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#endif /* !_ASN1_QUEUE_H_ */
@@ -0,0 +1,34 @@
-- $Id: canthandle.asn1,v 1.6 2006/01/18 19:12:33 lha Exp $ --
CANTHANDLE DEFINITIONS ::= BEGIN
-- Code the tag [1] but not the [ CONTEXT CONS UT_Sequence ] for Kaka2
-- Workaround: use inline the structure directly
-- Code the tag [2] but it should be primitive since KAKA3 is
-- Workaround: use the INTEGER type directly
Kaka2 ::= SEQUENCE {
kaka2-1 [0] INTEGER
}
Kaka3 ::= INTEGER
Foo ::= SEQUENCE {
kaka1 [0] IMPLICIT INTEGER OPTIONAL,
kaka2 [1] IMPLICIT Kaka2 OPTIONAL,
kaka3 [2] IMPLICIT Kaka3 OPTIONAL
}
-- Don't code kaka if its 1
-- Workaround is to use OPTIONAL and check for in the encoder stubs
Bar ::= SEQUENCE {
kaka [0] INTEGER DEFAULT 1
}
-- Can't handle primitives in SET OF
-- Workaround is to define a type that is only an integer and use that
Baz ::= SET OF INTEGER
END
+542
View File
@@ -0,0 +1,542 @@
/* This is a generated file */
#ifndef __der_protos_h__
#define __der_protos_h__
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
int
copy_heim_any (
const heim_any */*from*/,
heim_any */*to*/);
int
copy_heim_any_set (
const heim_any_set */*from*/,
heim_any_set */*to*/);
int
decode_heim_any (
const unsigned char */*p*/,
size_t /*len*/,
heim_any */*data*/,
size_t */*size*/);
int
decode_heim_any_set (
const unsigned char */*p*/,
size_t /*len*/,
heim_any_set */*data*/,
size_t */*size*/);
int
der_copy_bit_string (
const heim_bit_string */*from*/,
heim_bit_string */*to*/);
int
der_copy_bmp_string (
const heim_bmp_string */*from*/,
heim_bmp_string */*to*/);
int
der_copy_general_string (
const heim_general_string */*from*/,
heim_general_string */*to*/);
int
der_copy_heim_integer (
const heim_integer */*from*/,
heim_integer */*to*/);
int
der_copy_ia5_string (
const heim_printable_string */*from*/,
heim_printable_string */*to*/);
int
der_copy_octet_string (
const heim_octet_string */*from*/,
heim_octet_string */*to*/);
int
der_copy_oid (
const heim_oid */*from*/,
heim_oid */*to*/);
int
der_copy_printable_string (
const heim_printable_string */*from*/,
heim_printable_string */*to*/);
int
der_copy_universal_string (
const heim_universal_string */*from*/,
heim_universal_string */*to*/);
int
der_copy_utf8string (
const heim_utf8_string */*from*/,
heim_utf8_string */*to*/);
void
der_free_bit_string (heim_bit_string */*k*/);
void
der_free_bmp_string (heim_bmp_string */*k*/);
void
der_free_general_string (heim_general_string */*str*/);
void
der_free_heim_integer (heim_integer */*k*/);
void
der_free_ia5_string (heim_ia5_string */*str*/);
void
der_free_octet_string (heim_octet_string */*k*/);
void
der_free_oid (heim_oid */*k*/);
void
der_free_printable_string (heim_printable_string */*str*/);
void
der_free_universal_string (heim_universal_string */*k*/);
void
der_free_utf8string (heim_utf8_string */*str*/);
int
der_get_bit_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_bit_string */*data*/,
size_t */*size*/);
int
der_get_bmp_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_bmp_string */*data*/,
size_t */*size*/);
int
der_get_boolean (
const unsigned char */*p*/,
size_t /*len*/,
int */*data*/,
size_t */*size*/);
const char *
der_get_class_name (unsigned /*num*/);
int
der_get_class_num (const char */*name*/);
int
der_get_general_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_general_string */*str*/,
size_t */*size*/);
int
der_get_generalized_time (
const unsigned char */*p*/,
size_t /*len*/,
time_t */*data*/,
size_t */*size*/);
int
der_get_heim_integer (
const unsigned char */*p*/,
size_t /*len*/,
heim_integer */*data*/,
size_t */*size*/);
int
der_get_ia5_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_ia5_string */*str*/,
size_t */*size*/);
int
der_get_integer (
const unsigned char */*p*/,
size_t /*len*/,
int */*ret*/,
size_t */*size*/);
int
der_get_length (
const unsigned char */*p*/,
size_t /*len*/,
size_t */*val*/,
size_t */*size*/);
int
der_get_octet_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_octet_string */*data*/,
size_t */*size*/);
int
der_get_oid (
const unsigned char */*p*/,
size_t /*len*/,
heim_oid */*data*/,
size_t */*size*/);
int
der_get_printable_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_printable_string */*str*/,
size_t */*size*/);
int
der_get_tag (
const unsigned char */*p*/,
size_t /*len*/,
Der_class */*class*/,
Der_type */*type*/,
unsigned int */*tag*/,
size_t */*size*/);
const char *
der_get_tag_name (unsigned /*num*/);
int
der_get_tag_num (const char */*name*/);
const char *
der_get_type_name (unsigned /*num*/);
int
der_get_type_num (const char */*name*/);
int
der_get_universal_string (
const unsigned char */*p*/,
size_t /*len*/,
heim_universal_string */*data*/,
size_t */*size*/);
int
der_get_unsigned (
const unsigned char */*p*/,
size_t /*len*/,
unsigned */*ret*/,
size_t */*size*/);
int
der_get_utctime (
const unsigned char */*p*/,
size_t /*len*/,
time_t */*data*/,
size_t */*size*/);
int
der_get_utf8string (
const unsigned char */*p*/,
size_t /*len*/,
heim_utf8_string */*str*/,
size_t */*size*/);
int
der_heim_bit_string_cmp (
const heim_bit_string */*p*/,
const heim_bit_string */*q*/);
int
der_heim_bmp_string_cmp (
const heim_bmp_string */*p*/,
const heim_bmp_string */*q*/);
int
der_heim_integer_cmp (
const heim_integer */*p*/,
const heim_integer */*q*/);
int
der_heim_octet_string_cmp (
const heim_octet_string */*p*/,
const heim_octet_string */*q*/);
int
der_heim_oid_cmp (
const heim_oid */*p*/,
const heim_oid */*q*/);
int
der_heim_universal_string_cmp (
const heim_universal_string */*p*/,
const heim_universal_string */*q*/);
size_t
der_length_bit_string (const heim_bit_string */*k*/);
size_t
der_length_bmp_string (const heim_bmp_string */*data*/);
size_t
der_length_boolean (const int */*k*/);
size_t
der_length_enumerated (const unsigned */*data*/);
size_t
der_length_general_string (const heim_general_string */*data*/);
size_t
der_length_generalized_time (const time_t */*t*/);
size_t
der_length_heim_integer (const heim_integer */*k*/);
size_t
der_length_ia5_string (const heim_ia5_string */*data*/);
size_t
der_length_integer (const int */*data*/);
size_t
der_length_len (size_t /*len*/);
size_t
der_length_octet_string (const heim_octet_string */*k*/);
size_t
der_length_oid (const heim_oid */*k*/);
size_t
der_length_printable_string (const heim_printable_string */*data*/);
size_t
der_length_universal_string (const heim_universal_string */*data*/);
size_t
der_length_unsigned (const unsigned */*data*/);
size_t
der_length_utctime (const time_t */*t*/);
size_t
der_length_utf8string (const heim_utf8_string */*data*/);
int
der_match_tag (
const unsigned char */*p*/,
size_t /*len*/,
Der_class /*class*/,
Der_type /*type*/,
unsigned int /*tag*/,
size_t */*size*/);
int
der_match_tag_and_length (
const unsigned char */*p*/,
size_t /*len*/,
Der_class /*class*/,
Der_type /*type*/,
unsigned int /*tag*/,
size_t */*length_ret*/,
size_t */*size*/);
int
der_parse_heim_oid (
const char */*str*/,
const char */*sep*/,
heim_oid */*data*/);
int
der_parse_hex_heim_integer (
const char */*p*/,
heim_integer */*data*/);
int
der_print_heim_oid (
const heim_oid */*oid*/,
char /*delim*/,
char **/*str*/);
int
der_print_hex_heim_integer (
const heim_integer */*data*/,
char **/*p*/);
int
der_put_bit_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_bit_string */*data*/,
size_t */*size*/);
int
der_put_bmp_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_bmp_string */*data*/,
size_t */*size*/);
int
der_put_boolean (
unsigned char */*p*/,
size_t /*len*/,
const int */*data*/,
size_t */*size*/);
int
der_put_general_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_general_string */*str*/,
size_t */*size*/);
int
der_put_generalized_time (
unsigned char */*p*/,
size_t /*len*/,
const time_t */*data*/,
size_t */*size*/);
int
der_put_heim_integer (
unsigned char */*p*/,
size_t /*len*/,
const heim_integer */*data*/,
size_t */*size*/);
int
der_put_ia5_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_ia5_string */*str*/,
size_t */*size*/);
int
der_put_integer (
unsigned char */*p*/,
size_t /*len*/,
const int */*v*/,
size_t */*size*/);
int
der_put_length (
unsigned char */*p*/,
size_t /*len*/,
size_t /*val*/,
size_t */*size*/);
int
der_put_length_and_tag (
unsigned char */*p*/,
size_t /*len*/,
size_t /*len_val*/,
Der_class /*class*/,
Der_type /*type*/,
unsigned int /*tag*/,
size_t */*size*/);
int
der_put_octet_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_octet_string */*data*/,
size_t */*size*/);
int
der_put_oid (
unsigned char */*p*/,
size_t /*len*/,
const heim_oid */*data*/,
size_t */*size*/);
int
der_put_printable_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_printable_string */*str*/,
size_t */*size*/);
int
der_put_tag (
unsigned char */*p*/,
size_t /*len*/,
Der_class /*class*/,
Der_type /*type*/,
unsigned int /*tag*/,
size_t */*size*/);
int
der_put_universal_string (
unsigned char */*p*/,
size_t /*len*/,
const heim_universal_string */*data*/,
size_t */*size*/);
int
der_put_unsigned (
unsigned char */*p*/,
size_t /*len*/,
const unsigned */*v*/,
size_t */*size*/);
int
der_put_utctime (
unsigned char */*p*/,
size_t /*len*/,
const time_t */*data*/,
size_t */*size*/);
int
der_put_utf8string (
unsigned char */*p*/,
size_t /*len*/,
const heim_utf8_string */*str*/,
size_t */*size*/);
int
encode_heim_any (
unsigned char */*p*/,
size_t /*len*/,
const heim_any */*data*/,
size_t */*size*/);
int
encode_heim_any_set (
unsigned char */*p*/,
size_t /*len*/,
const heim_any_set */*data*/,
size_t */*size*/);
void
free_heim_any (heim_any */*data*/);
void
free_heim_any_set (heim_any_set */*data*/);
int
heim_any_cmp (
const heim_any_set */*p*/,
const heim_any_set */*q*/);
size_t
length_heim_any (const heim_any */*data*/);
size_t
length_heim_any_set (const heim_any */*data*/);
#ifdef __cplusplus
}
#endif
#endif /* __der_protos_h__ */
+142
View File
@@ -0,0 +1,142 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
#include <com_err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getarg.h>
#include <err.h>
RCSID("$Id: der.c,v 1.2 2005/07/12 06:27:19 lha Exp $");
static const char *class_names[] = {
"UNIV", /* 0 */
"APPL", /* 1 */
"CONTEXT", /* 2 */
"PRIVATE" /* 3 */
};
static const char *type_names[] = {
"PRIM", /* 0 */
"CONS" /* 1 */
};
static const char *tag_names[] = {
"EndOfContent", /* 0 */
"Boolean", /* 1 */
"Integer", /* 2 */
"BitString", /* 3 */
"OctetString", /* 4 */
"Null", /* 5 */
"ObjectID", /* 6 */
NULL, /* 7 */
NULL, /* 8 */
NULL, /* 9 */
"Enumerated", /* 10 */
NULL, /* 11 */
NULL, /* 12 */
NULL, /* 13 */
NULL, /* 14 */
NULL, /* 15 */
"Sequence", /* 16 */
"Set", /* 17 */
NULL, /* 18 */
"PrintableString", /* 19 */
NULL, /* 20 */
NULL, /* 21 */
"IA5String", /* 22 */
"UTCTime", /* 23 */
"GeneralizedTime", /* 24 */
NULL, /* 25 */
"VisibleString", /* 26 */
"GeneralString", /* 27 */
NULL, /* 28 */
NULL, /* 29 */
"BMPString" /* 30 */
};
static int
get_type(const char *name, const char *list[], unsigned len)
{
unsigned i;
for (i = 0; i < len; i++)
if (list[i] && strcasecmp(list[i], name) == 0)
return i;
return -1;
}
#define SIZEOF_ARRAY(a) (sizeof((a))/sizeof((a)[0]))
const char *
der_get_class_name(unsigned num)
{
if (num >= SIZEOF_ARRAY(class_names))
return NULL;
return class_names[num];
}
int
der_get_class_num(const char *name)
{
return get_type(name, class_names, SIZEOF_ARRAY(class_names));
}
const char *
der_get_type_name(unsigned num)
{
if (num >= SIZEOF_ARRAY(type_names))
return NULL;
return type_names[num];
}
int
der_get_type_num(const char *name)
{
return get_type(name, type_names, SIZEOF_ARRAY(type_names));
}
const char *
der_get_tag_name(unsigned num)
{
if (num >= SIZEOF_ARRAY(tag_names))
return NULL;
return tag_names[num];
}
int
der_get_tag_num(const char *name)
{
return get_type(name, tag_names, SIZEOF_ARRAY(tag_names));
}
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: der.h,v 1.36 2006/10/14 05:16:08 lha Exp $ */
#ifndef __DER_H__
#define __DER_H__
typedef enum {
ASN1_C_UNIV = 0,
ASN1_C_APPL = 1,
ASN1_C_CONTEXT = 2,
ASN1_C_PRIVATE = 3
} Der_class;
typedef enum {PRIM = 0, CONS = 1} Der_type;
#define MAKE_TAG(CLASS, TYPE, TAG) (((CLASS) << 6) | ((TYPE) << 5) | (TAG))
/* Universal tags */
enum {
UT_EndOfContent = 0,
UT_Boolean = 1,
UT_Integer = 2,
UT_BitString = 3,
UT_OctetString = 4,
UT_Null = 5,
UT_OID = 6,
UT_Enumerated = 10,
UT_UTF8String = 12,
UT_Sequence = 16,
UT_Set = 17,
UT_PrintableString = 19,
UT_IA5String = 22,
UT_UTCTime = 23,
UT_GeneralizedTime = 24,
UT_UniversalString = 25,
UT_VisibleString = 26,
UT_GeneralString = 27,
UT_BMPString = 30,
/* unsupported types */
UT_ObjectDescriptor = 7,
UT_External = 8,
UT_Real = 9,
UT_EmbeddedPDV = 11,
UT_RelativeOID = 13,
UT_NumericString = 18,
UT_TeletexString = 20,
UT_VideotexString = 21,
UT_GraphicString = 25
};
#define ASN1_INDEFINITE 0xdce0deed
typedef struct heim_der_time_t {
time_t dt_sec;
unsigned long dt_nsec;
} heim_der_time_t;
typedef struct heim_ber_time_t {
time_t bt_sec;
unsigned bt_nsec;
int bt_zone;
} heim_ber_time_t;
#include <der-protos.h>
int _heim_fix_dce(size_t reallen, size_t *len);
int _heim_der_set_sort(const void *, const void *);
int _heim_time2generalizedtime (time_t, heim_octet_string *, int);
#endif /* __DER_H__ */
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2003-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
int
der_heim_oid_cmp(const heim_oid *p, const heim_oid *q)
{
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->components,
q->components,
p->length * sizeof(*p->components));
}
int
der_heim_octet_string_cmp(const heim_octet_string *p,
const heim_octet_string *q)
{
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->data, q->data, p->length);
}
int
der_heim_bit_string_cmp(const heim_bit_string *p,
const heim_bit_string *q)
{
int i, r1, r2;
if (p->length != q->length)
return p->length - q->length;
i = memcmp(p->data, q->data, p->length / 8);
if (i)
return i;
if ((p->length % 8) == 0)
return 0;
i = (p->length / 8);
r1 = ((unsigned char *)p->data)[i];
r2 = ((unsigned char *)q->data)[i];
i = 8 - (p->length % 8);
r1 = r1 >> i;
r2 = r2 >> i;
return r1 - r2;
}
int
der_heim_integer_cmp(const heim_integer *p,
const heim_integer *q)
{
if (p->negative != q->negative)
return q->negative - p->negative;
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->data, q->data, p->length);
}
int
der_heim_bmp_string_cmp(const heim_bmp_string *p, const heim_bmp_string *q)
{
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
}
int
der_heim_universal_string_cmp(const heim_universal_string *p,
const heim_universal_string *q)
{
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->data, q->data, q->length * sizeof(q->data[0]));
}
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: der_copy.c,v 1.16 2006/10/14 05:30:02 lha Exp $");
int
der_copy_general_string (const heim_general_string *from,
heim_general_string *to)
{
*to = strdup(*from);
if(*to == NULL)
return ENOMEM;
return 0;
}
int
der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
{
return der_copy_general_string(from, to);
}
int
der_copy_printable_string (const heim_printable_string *from,
heim_printable_string *to)
{
return der_copy_general_string(from, to);
}
int
der_copy_ia5_string (const heim_printable_string *from,
heim_printable_string *to)
{
return der_copy_general_string(from, to);
}
int
der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
{
to->length = from->length;
to->data = malloc(to->length * sizeof(to->data[0]));
if(to->length != 0 && to->data == NULL)
return ENOMEM;
memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
return 0;
}
int
der_copy_universal_string (const heim_universal_string *from,
heim_universal_string *to)
{
to->length = from->length;
to->data = malloc(to->length * sizeof(to->data[0]));
if(to->length != 0 && to->data == NULL)
return ENOMEM;
memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
return 0;
}
int
der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
{
to->length = from->length;
to->data = malloc(to->length);
if(to->length != 0 && to->data == NULL)
return ENOMEM;
memcpy(to->data, from->data, to->length);
return 0;
}
int
der_copy_heim_integer (const heim_integer *from, heim_integer *to)
{
to->length = from->length;
to->data = malloc(to->length);
if(to->length != 0 && to->data == NULL)
return ENOMEM;
memcpy(to->data, from->data, to->length);
to->negative = from->negative;
return 0;
}
int
der_copy_oid (const heim_oid *from, heim_oid *to)
{
to->length = from->length;
to->components = malloc(to->length * sizeof(*to->components));
if (to->length != 0 && to->components == NULL)
return ENOMEM;
memcpy(to->components, from->components,
to->length * sizeof(*to->components));
return 0;
}
int
der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
{
size_t len;
len = (from->length + 7) / 8;
to->length = from->length;
to->data = malloc(len);
if(len != 0 && to->data == NULL)
return ENOMEM;
memcpy(to->data, from->data, len);
return 0;
}
+167
View File
@@ -0,0 +1,167 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
#include <hex.h>
RCSID("$Id: der_format.c,v 1.6 2006/10/21 18:24:15 lha Exp $");
int
der_parse_hex_heim_integer (const char *p, heim_integer *data)
{
ssize_t len;
data->length = 0;
data->negative = 0;
data->data = NULL;
if (*p == '-') {
p++;
data->negative = 1;
}
len = strlen(p);
if (len < 0) {
data->data = NULL;
data->length = 0;
return EINVAL;
}
data->length = (len / 2) + 1;
data->data = malloc(data->length);
if (data->data == NULL) {
data->length = 0;
return ENOMEM;
}
len = hex_decode(p, data->data, data->length);
if (len < 0) {
free(data->data);
data->data = NULL;
data->length = 0;
return EINVAL;
}
{
unsigned char *q = data->data;
while(*q == 0 && len > 0) {
q++;
len--;
}
data->length = len;
memmove(data->data, q, len);
}
return 0;
}
int
der_print_hex_heim_integer (const heim_integer *data, char **p)
{
ssize_t len;
char *q;
len = hex_encode(data->data, data->length, p);
if (len < 0)
return ENOMEM;
if (data->negative) {
len = asprintf(&q, "-%s", *p);
free(*p);
if (len < 0)
return ENOMEM;
*p = q;
}
return 0;
}
int
der_print_heim_oid (const heim_oid *oid, char delim, char **str)
{
struct rk_strpool *p = NULL;
int i;
for (i = 0; i < oid->length ; i++) {
p = rk_strpoolprintf(p, "%d%s",
oid->components[i],
i < oid->length - 1 ? " " : "");
if (p == NULL) {
*str = NULL;
return ENOMEM;
}
}
*str = rk_strpoolcollect(p);
if (*str == NULL)
return ENOMEM;
return 0;
}
int
der_parse_heim_oid (const char *str, const char *sep, heim_oid *data)
{
char *s, *w, *brkt, *endptr;
unsigned int *c;
long l;
data->length = 0;
data->components = NULL;
if (sep == NULL)
sep = ".";
s = strdup(str);
for (w = strtok_r(s, sep, &brkt);
w != NULL;
w = strtok_r(NULL, sep, &brkt)) {
c = realloc(data->components,
(data->length + 1) * sizeof(data->components[0]));
if (c == NULL) {
der_free_oid(data);
free(s);
return ENOMEM;
}
data->components = c;
l = strtol(w, &endptr, 10);
if (*endptr != '\0' || l < 0 || l > INT_MAX) {
der_free_oid(data);
free(s);
return EINVAL;
}
data->components[data->length++] = l;
}
free(s);
return 0;
}
+112
View File
@@ -0,0 +1,112 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: der_free.c,v 1.13 2006/10/14 05:30:47 lha Exp $");
void
der_free_general_string (heim_general_string *str)
{
free(*str);
*str = NULL;
}
void
der_free_utf8string (heim_utf8_string *str)
{
free(*str);
*str = NULL;
}
void
der_free_printable_string (heim_printable_string *str)
{
free(*str);
*str = NULL;
}
void
der_free_ia5_string (heim_ia5_string *str)
{
free(*str);
*str = NULL;
}
void
der_free_bmp_string (heim_bmp_string *k)
{
free(k->data);
k->data = NULL;
k->length = 0;
}
void
der_free_universal_string (heim_universal_string *k)
{
free(k->data);
k->data = NULL;
k->length = 0;
}
void
der_free_octet_string (heim_octet_string *k)
{
free(k->data);
k->data = NULL;
k->length = 0;
}
void
der_free_heim_integer (heim_integer *k)
{
free(k->data);
k->data = NULL;
k->length = 0;
}
void
der_free_oid (heim_oid *k)
{
free(k->components);
k->components = NULL;
k->length = 0;
}
void
der_free_bit_string (heim_bit_string *k)
{
free(k->data);
k->data = NULL;
k->length = 0;
}
+526
View File
@@ -0,0 +1,526 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: der_get.c,v 1.50 2006/10/19 16:27:44 lha Exp $");
#include <version.h>
/*
* All decoding functions take a pointer `p' to first position in
* which to read, from the left, `len' which means the maximum number
* of characters we are able to read, `ret' were the value will be
* returned and `size' where the number of used bytes is stored.
* Either 0 or an error code is returned.
*/
int
der_get_unsigned (const unsigned char *p, size_t len,
unsigned *ret, size_t *size)
{
unsigned val = 0;
size_t oldlen = len;
if (len == sizeof(unsigned) + 1 && p[0] == 0)
;
else if (len > sizeof(unsigned))
return ASN1_OVERRUN;
while (len--)
val = val * 256 + *p++;
*ret = val;
if(size) *size = oldlen;
return 0;
}
int
der_get_integer (const unsigned char *p, size_t len,
int *ret, size_t *size)
{
int val = 0;
size_t oldlen = len;
if (len > sizeof(int))
return ASN1_OVERRUN;
if (len > 0) {
val = (signed char)*p++;
while (--len)
val = val * 256 + *p++;
}
*ret = val;
if(size) *size = oldlen;
return 0;
}
int
der_get_length (const unsigned char *p, size_t len,
size_t *val, size_t *size)
{
size_t v;
if (len <= 0)
return ASN1_OVERRUN;
--len;
v = *p++;
if (v < 128) {
*val = v;
if(size) *size = 1;
} else {
int e;
size_t l;
unsigned tmp;
if(v == 0x80){
*val = ASN1_INDEFINITE;
if(size) *size = 1;
return 0;
}
v &= 0x7F;
if (len < v)
return ASN1_OVERRUN;
e = der_get_unsigned (p, v, &tmp, &l);
if(e) return e;
*val = tmp;
if(size) *size = l + 1;
}
return 0;
}
int
der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
{
if(len < 1)
return ASN1_OVERRUN;
if(*p != 0)
*data = 1;
else
*data = 0;
*size = 1;
return 0;
}
int
der_get_general_string (const unsigned char *p, size_t len,
heim_general_string *str, size_t *size)
{
char *s;
if (len > len + 1)
return ASN1_BAD_LENGTH;
s = malloc (len + 1);
if (s == NULL)
return ENOMEM;
memcpy (s, p, len);
s[len] = '\0';
*str = s;
if(size) *size = len;
return 0;
}
int
der_get_utf8string (const unsigned char *p, size_t len,
heim_utf8_string *str, size_t *size)
{
return der_get_general_string(p, len, str, size);
}
int
der_get_printable_string (const unsigned char *p, size_t len,
heim_printable_string *str, size_t *size)
{
return der_get_general_string(p, len, str, size);
}
int
der_get_ia5_string (const unsigned char *p, size_t len,
heim_ia5_string *str, size_t *size)
{
return der_get_general_string(p, len, str, size);
}
int
der_get_bmp_string (const unsigned char *p, size_t len,
heim_bmp_string *data, size_t *size)
{
size_t i;
if (len & 1)
return ASN1_BAD_FORMAT;
data->length = len / 2;
data->data = malloc(data->length * sizeof(data->data[0]));
if (data->data == NULL && data->length != 0)
return ENOMEM;
for (i = 0; i < data->length; i++) {
data->data[i] = (p[0] << 8) | p[1];
p += 2;
}
if (size) *size = len;
return 0;
}
int
der_get_universal_string (const unsigned char *p, size_t len,
heim_universal_string *data, size_t *size)
{
size_t i;
if (len & 3)
return ASN1_BAD_FORMAT;
data->length = len / 4;
data->data = malloc(data->length * sizeof(data->data[0]));
if (data->data == NULL && data->length != 0)
return ENOMEM;
for (i = 0; i < data->length; i++) {
data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
p += 4;
}
if (size) *size = len;
return 0;
}
int
der_get_octet_string (const unsigned char *p, size_t len,
heim_octet_string *data, size_t *size)
{
data->length = len;
data->data = malloc(len);
if (data->data == NULL && data->length != 0)
return ENOMEM;
memcpy (data->data, p, len);
if(size) *size = len;
return 0;
}
int
der_get_heim_integer (const unsigned char *p, size_t len,
heim_integer *data, size_t *size)
{
data->length = 0;
data->negative = 0;
data->data = NULL;
if (len == 0) {
if (size)
*size = 0;
return 0;
}
if (p[0] & 0x80) {
unsigned char *q;
int carry = 1;
data->negative = 1;
data->length = len;
if (p[0] == 0xff) {
p++;
data->length--;
}
data->data = malloc(data->length);
if (data->data == NULL) {
data->length = 0;
if (size)
*size = 0;
return ENOMEM;
}
q = &((unsigned char*)data->data)[data->length - 1];
p += data->length - 1;
while (q >= (unsigned char*)data->data) {
*q = *p ^ 0xff;
if (carry)
carry = !++*q;
p--;
q--;
}
} else {
data->negative = 0;
data->length = len;
if (p[0] == 0) {
p++;
data->length--;
}
data->data = malloc(data->length);
if (data->data == NULL && data->length != 0) {
data->length = 0;
if (size)
*size = 0;
return ENOMEM;
}
memcpy(data->data, p, data->length);
}
if (size)
*size = len;
return 0;
}
static int
generalizedtime2time (const char *s, time_t *t)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
&tm.tm_min, &tm.tm_sec) != 6) {
if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
&tm.tm_min, &tm.tm_sec) != 6)
return ASN1_BAD_TIMEFORMAT;
if (tm.tm_year < 50)
tm.tm_year += 2000;
else
tm.tm_year += 1900;
}
tm.tm_year -= 1900;
tm.tm_mon -= 1;
*t = _der_timegm (&tm);
return 0;
}
#undef timegm
static int
der_get_time (const unsigned char *p, size_t len,
time_t *data, size_t *size)
{
heim_octet_string k;
char *times;
size_t ret = 0;
size_t l;
int e;
e = der_get_octet_string (p, len, &k, &l);
if (e) return e;
p += l;
len -= l;
ret += l;
times = realloc(k.data, k.length + 1);
if (times == NULL){
free(k.data);
return ENOMEM;
}
times[k.length] = 0;
e = generalizedtime2time(times, data);
free (times);
if(size) *size = ret;
return e;
}
int
der_get_generalized_time (const unsigned char *p, size_t len,
time_t *data, size_t *size)
{
return der_get_time(p, len, data, size);
}
int
der_get_utctime (const unsigned char *p, size_t len,
time_t *data, size_t *size)
{
return der_get_time(p, len, data, size);
}
int
der_get_oid (const unsigned char *p, size_t len,
heim_oid *data, size_t *size)
{
int n;
size_t oldlen = len;
if (len < 1)
return ASN1_OVERRUN;
if (len > len + 1)
return ASN1_BAD_LENGTH;
data->components = malloc((len + 1) * sizeof(*data->components));
if (data->components == NULL)
return ENOMEM;
data->components[0] = (*p) / 40;
data->components[1] = (*p) % 40;
--len;
++p;
for (n = 2; len > 0; ++n) {
unsigned u = 0, u1;
do {
--len;
u1 = u * 128 + (*p++ % 128);
/* check that we don't overflow the element */
if (u1 < u) {
der_free_oid(data);
return ASN1_OVERRUN;
}
u = u1;
} while (len > 0 && p[-1] & 0x80);
data->components[n] = u;
}
if (n > 2 && p[-1] & 0x80) {
der_free_oid (data);
return ASN1_OVERRUN;
}
data->length = n;
if (size)
*size = oldlen;
return 0;
}
int
der_get_tag (const unsigned char *p, size_t len,
Der_class *class, Der_type *type,
unsigned int *tag, size_t *size)
{
size_t ret = 0;
if (len < 1)
return ASN1_OVERRUN;
*class = (Der_class)(((*p) >> 6) & 0x03);
*type = (Der_type)(((*p) >> 5) & 0x01);
*tag = (*p) & 0x1f;
p++; len--; ret++;
if(*tag == 0x1f) {
unsigned int continuation;
unsigned int tag1;
*tag = 0;
do {
if(len < 1)
return ASN1_OVERRUN;
continuation = *p & 128;
tag1 = *tag * 128 + (*p % 128);
/* check that we don't overflow the tag */
if (tag1 < *tag)
return ASN1_OVERFLOW;
*tag = tag1;
p++; len--; ret++;
} while(continuation);
}
if(size) *size = ret;
return 0;
}
int
der_match_tag (const unsigned char *p, size_t len,
Der_class class, Der_type type,
unsigned int tag, size_t *size)
{
size_t l;
Der_class thisclass;
Der_type thistype;
unsigned int thistag;
int e;
e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
if (e) return e;
if (class != thisclass || type != thistype)
return ASN1_BAD_ID;
if(tag > thistag)
return ASN1_MISPLACED_FIELD;
if(tag < thistag)
return ASN1_MISSING_FIELD;
if(size) *size = l;
return 0;
}
int
der_match_tag_and_length (const unsigned char *p, size_t len,
Der_class class, Der_type type, unsigned int tag,
size_t *length_ret, size_t *size)
{
size_t l, ret = 0;
int e;
e = der_match_tag (p, len, class, type, tag, &l);
if (e) return e;
p += l;
len -= l;
ret += l;
e = der_get_length (p, len, length_ret, &l);
if (e) return e;
p += l;
len -= l;
ret += l;
if(size) *size = ret;
return 0;
}
/*
* Old versions of DCE was based on a very early beta of the MIT code,
* which used MAVROS for ASN.1 encoding. MAVROS had the interesting
* feature that it encoded data in the forward direction, which has
* it's problems, since you have no idea how long the data will be
* until after you're done. MAVROS solved this by reserving one byte
* for length, and later, if the actual length was longer, it reverted
* to indefinite, BER style, lengths. The version of MAVROS used by
* the DCE people could apparently generate correct X.509 DER encodings, and
* did this by making space for the length after encoding, but
* unfortunately this feature wasn't used with Kerberos.
*/
int
_heim_fix_dce(size_t reallen, size_t *len)
{
if(reallen == ASN1_INDEFINITE)
return 1;
if(*len < reallen)
return -1;
*len = reallen;
return 0;
}
int
der_get_bit_string (const unsigned char *p, size_t len,
heim_bit_string *data, size_t *size)
{
if (len < 1)
return ASN1_OVERRUN;
if (p[0] > 7)
return ASN1_BAD_FORMAT;
if (len - 1 == 0 && p[0] != 0)
return ASN1_BAD_FORMAT;
/* check if any of the three upper bits are set
* any of them will cause a interger overrun */
if ((len - 1) >> (sizeof(len) * 8 - 3))
return ASN1_OVERRUN;
data->length = (len - 1) * 8;
data->data = malloc(len - 1);
if (data->data == NULL && (len - 1) != 0)
return ENOMEM;
memcpy (data->data, p + 1, len - 1);
data->length -= p[0];
if(size) *size = len;
return 0;
}
+226
View File
@@ -0,0 +1,226 @@
/*
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: der_length.c,v 1.19 2006/10/14 05:26:06 lha Exp $");
size_t
_heim_len_unsigned (unsigned val)
{
size_t ret = 0;
int last_val_gt_128;
do {
++ret;
last_val_gt_128 = (val >= 128);
val /= 256;
} while (val);
if(last_val_gt_128)
ret++;
return ret;
}
size_t
_heim_len_int (int val)
{
unsigned char q;
size_t ret = 0;
if (val >= 0) {
do {
q = val % 256;
ret++;
val /= 256;
} while(val);
if(q >= 128)
ret++;
} else {
val = ~val;
do {
q = ~(val % 256);
ret++;
val /= 256;
} while(val);
if(q < 128)
ret++;
}
return ret;
}
static size_t
len_oid (const heim_oid *oid)
{
size_t ret = 1;
int n;
for (n = 2; n < oid->length; ++n) {
unsigned u = oid->components[n];
do {
++ret;
u /= 128;
} while(u > 0);
}
return ret;
}
size_t
der_length_len (size_t len)
{
if (len < 128)
return 1;
else {
int ret = 0;
do {
++ret;
len /= 256;
} while (len);
return ret + 1;
}
}
size_t
der_length_integer (const int *data)
{
return _heim_len_int (*data);
}
size_t
der_length_unsigned (const unsigned *data)
{
return _heim_len_unsigned(*data);
}
size_t
der_length_enumerated (const unsigned *data)
{
return _heim_len_int (*data);
}
size_t
der_length_general_string (const heim_general_string *data)
{
return strlen(*data);
}
size_t
der_length_utf8string (const heim_utf8_string *data)
{
return strlen(*data);
}
size_t
der_length_printable_string (const heim_printable_string *data)
{
return strlen(*data);
}
size_t
der_length_ia5_string (const heim_ia5_string *data)
{
return strlen(*data);
}
size_t
der_length_bmp_string (const heim_bmp_string *data)
{
return data->length * 2;
}
size_t
der_length_universal_string (const heim_universal_string *data)
{
return data->length * 4;
}
size_t
der_length_octet_string (const heim_octet_string *k)
{
return k->length;
}
size_t
der_length_heim_integer (const heim_integer *k)
{
if (k->length == 0)
return 1;
if (k->negative)
return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
else
return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
}
size_t
der_length_oid (const heim_oid *k)
{
return len_oid (k);
}
size_t
der_length_generalized_time (const time_t *t)
{
heim_octet_string k;
size_t ret;
_heim_time2generalizedtime (*t, &k, 1);
ret = k.length;
free(k.data);
return ret;
}
size_t
der_length_utctime (const time_t *t)
{
heim_octet_string k;
size_t ret;
_heim_time2generalizedtime (*t, &k, 0);
ret = k.length;
free(k.data);
return ret;
}
size_t
der_length_boolean (const int *k)
{
return 1;
}
size_t
der_length_bit_string (const heim_bit_string *k)
{
return (k->length + 7) / 8 + 1;
}
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (c) 1997 - 2002, 2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: der_locl.h,v 1.8 2006/10/19 16:24:02 lha Exp $ */
#ifndef __DER_LOCL_H__
#define __DER_LOCL_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <roken.h>
#include <asn1-common.h>
#include <asn1_err.h>
#include <der.h>
time_t _der_timegm (struct tm *);
size_t _heim_len_unsigned (unsigned);
size_t _heim_len_int (int);
#endif /* __DER_LOCL_H__ */
+476
View File
@@ -0,0 +1,476 @@
/*
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: der_put.c,v 1.33 2005/07/12 06:27:23 lha Exp $");
/*
* All encoding functions take a pointer `p' to first position in
* which to write, from the right, `len' which means the maximum
* number of characters we are able to write. The function returns
* the number of characters written in `size' (if non-NULL).
* The return value is 0 or an error.
*/
int
der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
{
unsigned char *base = p;
unsigned val = *v;
if (val) {
while (len > 0 && val) {
*p-- = val % 256;
val /= 256;
--len;
}
if (val != 0)
return ASN1_OVERFLOW;
else {
if(p[1] >= 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0;
}
*size = base - p;
return 0;
}
} else if (len < 1)
return ASN1_OVERFLOW;
else {
*p = 0;
*size = 1;
return 0;
}
}
int
der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
{
unsigned char *base = p;
int val = *v;
if(val >= 0) {
do {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = val % 256;
len--;
val /= 256;
} while(val);
if(p[1] >= 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0;
len--;
}
} else {
val = ~val;
do {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = ~(val % 256);
len--;
val /= 256;
} while(val);
if(p[1] < 128) {
if(len < 1)
return ASN1_OVERFLOW;
*p-- = 0xff;
len--;
}
}
*size = base - p;
return 0;
}
int
der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
{
if (len < 1)
return ASN1_OVERFLOW;
if (val < 128) {
*p = val;
*size = 1;
} else {
size_t l = 0;
while(val > 0) {
if(len < 2)
return ASN1_OVERFLOW;
*p-- = val % 256;
val /= 256;
len--;
l++;
}
*p = 0x80 | l;
if(size)
*size = l + 1;
}
return 0;
}
int
der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
{
if(len < 1)
return ASN1_OVERFLOW;
if(*data != 0)
*p = 0xff;
else
*p = 0;
*size = 1;
return 0;
}
int
der_put_general_string (unsigned char *p, size_t len,
const heim_general_string *str, size_t *size)
{
size_t slen = strlen(*str);
if (len < slen)
return ASN1_OVERFLOW;
p -= slen;
len -= slen;
memcpy (p+1, *str, slen);
*size = slen;
return 0;
}
int
der_put_utf8string (unsigned char *p, size_t len,
const heim_utf8_string *str, size_t *size)
{
return der_put_general_string(p, len, str, size);
}
int
der_put_printable_string (unsigned char *p, size_t len,
const heim_printable_string *str, size_t *size)
{
return der_put_general_string(p, len, str, size);
}
int
der_put_ia5_string (unsigned char *p, size_t len,
const heim_ia5_string *str, size_t *size)
{
return der_put_general_string(p, len, str, size);
}
int
der_put_bmp_string (unsigned char *p, size_t len,
const heim_bmp_string *data, size_t *size)
{
size_t i;
if (len / 2 < data->length)
return ASN1_OVERFLOW;
p -= data->length * 2;
len -= data->length * 2;
for (i = 0; i < data->length; i++) {
p[1] = (data->data[i] >> 8) & 0xff;
p[2] = data->data[i] & 0xff;
p += 2;
}
if (size) *size = data->length * 2;
return 0;
}
int
der_put_universal_string (unsigned char *p, size_t len,
const heim_universal_string *data, size_t *size)
{
size_t i;
if (len / 4 < data->length)
return ASN1_OVERFLOW;
p -= data->length * 4;
len -= data->length * 4;
for (i = 0; i < data->length; i++) {
p[1] = (data->data[i] >> 24) & 0xff;
p[2] = (data->data[i] >> 16) & 0xff;
p[3] = (data->data[i] >> 8) & 0xff;
p[4] = data->data[i] & 0xff;
p += 4;
}
if (size) *size = data->length * 4;
return 0;
}
int
der_put_octet_string (unsigned char *p, size_t len,
const heim_octet_string *data, size_t *size)
{
if (len < data->length)
return ASN1_OVERFLOW;
p -= data->length;
len -= data->length;
memcpy (p+1, data->data, data->length);
*size = data->length;
return 0;
}
int
der_put_heim_integer (unsigned char *p, size_t len,
const heim_integer *data, size_t *size)
{
unsigned char *buf = data->data;
int hibitset = 0;
if (data->length == 0) {
if (len < 1)
return ASN1_OVERFLOW;
*p-- = 0;
if (size)
*size = 1;
return 0;
}
if (len < data->length)
return ASN1_OVERFLOW;
len -= data->length;
if (data->negative) {
int i, carry;
for (i = data->length - 1, carry = 1; i >= 0; i--) {
*p = buf[i] ^ 0xff;
if (carry)
carry = !++*p;
p--;
}
if (p[1] < 128) {
if (len < 1)
return ASN1_OVERFLOW;
*p-- = 0xff;
len--;
hibitset = 1;
}
} else {
p -= data->length;
memcpy(p + 1, buf, data->length);
if (p[1] >= 128) {
if (len < 1)
return ASN1_OVERFLOW;
p[0] = 0;
len--;
hibitset = 1;
}
}
if (size)
*size = data->length + hibitset;
return 0;
}
int
der_put_generalized_time (unsigned char *p, size_t len,
const time_t *data, size_t *size)
{
heim_octet_string k;
size_t l;
int e;
e = _heim_time2generalizedtime (*data, &k, 1);
if (e)
return e;
e = der_put_octet_string(p, len, &k, &l);
free(k.data);
if(e)
return e;
if(size)
*size = l;
return 0;
}
int
der_put_utctime (unsigned char *p, size_t len,
const time_t *data, size_t *size)
{
heim_octet_string k;
size_t l;
int e;
e = _heim_time2generalizedtime (*data, &k, 0);
if (e)
return e;
e = der_put_octet_string(p, len, &k, &l);
free(k.data);
if(e)
return e;
if(size)
*size = l;
return 0;
}
int
der_put_oid (unsigned char *p, size_t len,
const heim_oid *data, size_t *size)
{
unsigned char *base = p;
int n;
for (n = data->length - 1; n >= 2; --n) {
unsigned u = data->components[n];
if (len < 1)
return ASN1_OVERFLOW;
*p-- = u % 128;
u /= 128;
--len;
while (u > 0) {
if (len < 1)
return ASN1_OVERFLOW;
*p-- = 128 + u % 128;
u /= 128;
--len;
}
}
if (len < 1)
return ASN1_OVERFLOW;
*p-- = 40 * data->components[0] + data->components[1];
*size = base - p;
return 0;
}
int
der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
unsigned int tag, size_t *size)
{
if (tag <= 30) {
if (len < 1)
return ASN1_OVERFLOW;
*p = MAKE_TAG(class, type, tag);
*size = 1;
} else {
size_t ret = 0;
unsigned int continuation = 0;
do {
if (len < 1)
return ASN1_OVERFLOW;
*p-- = tag % 128 | continuation;
len--;
ret++;
tag /= 128;
continuation = 0x80;
} while(tag > 0);
if (len < 1)
return ASN1_OVERFLOW;
*p-- = MAKE_TAG(class, type, 0x1f);
ret++;
*size = ret;
}
return 0;
}
int
der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
Der_class class, Der_type type,
unsigned int tag, size_t *size)
{
size_t ret = 0;
size_t l;
int e;
e = der_put_length (p, len, len_val, &l);
if(e)
return e;
p -= l;
len -= l;
ret += l;
e = der_put_tag (p, len, class, type, tag, &l);
if(e)
return e;
p -= l;
len -= l;
ret += l;
*size = ret;
return 0;
}
int
_heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
{
struct tm *tm;
const size_t len = gtimep ? 15 : 13;
s->data = malloc(len + 1);
if (s->data == NULL)
return ENOMEM;
s->length = len;
tm = gmtime (&t);
if (gtimep)
snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
else
snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
int
der_put_bit_string (unsigned char *p, size_t len,
const heim_bit_string *data, size_t *size)
{
size_t data_size = (data->length + 7) / 8;
if (len < data_size + 1)
return ASN1_OVERFLOW;
p -= data_size + 1;
len -= data_size + 1;
memcpy (p+2, data->data, data_size);
if (data->length && (data->length % 8) != 0)
p[1] = 8 - (data->length % 8);
else
p[1] = 0;
*size = data_size + 1;
return 0;
}
int
_heim_der_set_sort(const void *a1, const void *a2)
{
const struct heim_octet_string *s1 = a1, *s2 = a2;
int ret;
ret = memcmp(s1->data, s2->data,
s1->length < s2->length ? s1->length : s2->length);
if(ret)
return ret;
return s1->length - s2->length;
}
+115
View File
@@ -0,0 +1,115 @@
-- $Id: digest.asn1,v 1.9 2006/08/25 11:57:54 lha Exp $
DIGEST DEFINITIONS ::=
BEGIN
IMPORTS EncryptedData, Principal FROM krb5;
DigestInit ::= SEQUENCE {
type UTF8String, -- http, sasl, chap, cram-md5 --
channel [0] SEQUENCE {
cb-type UTF8String,
cb-binding UTF8String
} OPTIONAL,
hostname [1] UTF8String OPTIONAL -- for chap/cram-md5
}
DigestInitReply ::= SEQUENCE {
nonce UTF8String, -- service nonce/challange
opaque UTF8String, -- server state
identifier [0] UTF8String OPTIONAL
}
DigestRequest ::= SEQUENCE {
type UTF8String, -- http, sasl-md5, chap, cram-md5 --
digest UTF8String, -- http:md5/md5-sess sasl:clear/int/conf --
username UTF8String, -- username user used
authid [0] UTF8String OPTIONAL,
authentication-user [1] Principal OPTIONAL, -- principal to get key from
realm [2] UTF8String OPTIONAL,
method [3] UTF8String OPTIONAL,
uri [4] UTF8String OPTIONAL,
serverNonce UTF8String, -- same as "DigestInitReply.nonce"
clientNonce [5] UTF8String OPTIONAL,
nonceCount [6] UTF8String OPTIONAL,
qop [7] UTF8String OPTIONAL,
identifier [8] UTF8String OPTIONAL,
hostname [9] UTF8String OPTIONAL,
opaque UTF8String -- same as "DigestInitReply.opaque"
}
-- opaque = hex(cksum(type|serverNonce|identifier|hostname,digest-key))
-- serverNonce = hex(time[4bytes]random[12bytes])(-cbType:cbBinding)
DigestError ::= SEQUENCE {
reason UTF8String,
code INTEGER (-2147483648..2147483647)
}
DigestResponse ::= SEQUENCE {
responseData UTF8String,
rsp [0] UTF8String OPTIONAL,
tickets [1] SEQUENCE OF OCTET STRING OPTIONAL,
channel [2] SEQUENCE {
cb-type UTF8String,
cb-binding UTF8String
} OPTIONAL,
hash-a1 [3] OCTET STRING OPTIONAL
}
DigestReqInner ::= CHOICE {
init [0] DigestInit,
digestRequest [1] DigestRequest
}
DigestREQ ::= [APPLICATION 128] SEQUENCE {
apReq [0] OCTET STRING,
innerReq [1] EncryptedData
}
DigestRepInner ::= CHOICE {
error [0] DigestError,
initReply [1] DigestInitReply,
response [2] DigestResponse
}
DigestREP ::= [APPLICATION 129] SEQUENCE {
apRep [0] OCTET STRING,
innerRep [1] EncryptedData
}
-- HTTP
-- md5
-- A1 = unq(username-value) ":" unq(realm-value) ":" passwd
-- md5-sess
-- A1 = HEX(H(unq(username-value) ":" unq(realm-value) ":" passwd ) ":" unq(nonce-value) ":" unq(cnonce-value))
-- qop == auth
-- A2 = Method ":" digest-uri-value
-- qop == auth-int
-- A2 = Method ":" digest-uri-value ":" H(entity-body)
-- request-digest = HEX(KD(HEX(H(A1)),
-- unq(nonce-value) ":" nc-value ":" unq(cnonce-value) ":" unq(qop-value) ":" HEX(H(A2))))
-- no "qop"
-- request-digest = HEX(KD(HEX(H(A1)), unq(nonce-value) ":" HEX(H(A2))))
-- SASL:
-- SS = H( { unq(username-value), ":", unq(realm-value), ":", password } )
-- A1 = { SS, ":", unq(nonce-value), ":", unq(cnonce-value) }
-- A1 = { SS, ":", unq(nonce-value), ":", unq(cnonce-value), ":", unq(authzid-value) }
-- A2 = "AUTHENTICATE:", ":", digest-uri-value
-- qop == auth-int,auth-conf
-- A2 = "AUTHENTICATE:", ":", digest-uri-value, ":00000000000000000000000000000000"
-- response-value = HEX( KD ( HEX(H(A1)),
-- { unq(nonce-value), ":" nc-value, ":",
-- unq(cnonce-value), ":", qop-value, ":",
-- HEX(H(A2)) }))
END
+155
View File
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2003 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
#include "heim_asn1.h"
RCSID("$Id: extra.c,v 1.6 2006/01/31 09:44:54 lha Exp $");
int
encode_heim_any(unsigned char *p, size_t len,
const heim_any *data, size_t *size)
{
if (data->length > len)
return ASN1_OVERFLOW;
p -= data->length;
len -= data->length;
memcpy (p+1, data->data, data->length);
*size = data->length;
return 0;
}
int
decode_heim_any(const unsigned char *p, size_t len,
heim_any *data, size_t *size)
{
size_t len_len, length, l;
Der_class thisclass;
Der_type thistype;
unsigned int thistag;
int e;
memset(data, 0, sizeof(*data));
e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
if (e) return e;
if (l > len)
return ASN1_OVERFLOW;
e = der_get_length(p + l, len - l, &length, &len_len);
if (e) return e;
if (length + len_len + l > len)
return ASN1_OVERFLOW;
data->data = malloc(length + len_len + l);
if (data->data == NULL)
return ENOMEM;
data->length = length + len_len + l;
memcpy(data->data, p, length + len_len + l);
if (size)
*size = length + len_len + l;
return 0;
}
void
free_heim_any(heim_any *data)
{
free(data->data);
data->data = NULL;
}
size_t
length_heim_any(const heim_any *data)
{
return data->length;
}
int
copy_heim_any(const heim_any *from, heim_any *to)
{
to->data = malloc(from->length);
if (to->data == NULL && from->length != 0)
return ENOMEM;
memcpy(to->data, from->data, from->length);
to->length = from->length;
return 0;
}
int
encode_heim_any_set(unsigned char *p, size_t len,
const heim_any_set *data, size_t *size)
{
return encode_heim_any(p, len, data, size);
}
int
decode_heim_any_set(const unsigned char *p, size_t len,
heim_any_set *data, size_t *size)
{
memset(data, 0, sizeof(*data));
data->data = malloc(len);
if (data->data == NULL && len != 0)
return ENOMEM;
data->length = len;
memcpy(data->data, p, len);
if (size) *size = len;
return 0;
}
void
free_heim_any_set(heim_any_set *data)
{
free_heim_any(data);
}
size_t
length_heim_any_set(const heim_any *data)
{
return length_heim_any(data);
}
int
copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
{
return copy_heim_any(from, to);
}
int
heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
{
if (p->length != q->length)
return p->length - q->length;
return memcmp(p->data, q->data, p->length);
}
+784
View File
@@ -0,0 +1,784 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen.c,v 1.69 2006/10/14 05:11:52 lha Exp $");
FILE *headerfile, *codefile, *logfile;
#define STEM "asn1"
static const char *orig_filename;
static char *header;
static const char *headerbase = STEM;
/*
* list of all IMPORTs
*/
struct import {
const char *module;
struct import *next;
};
static struct import *imports = NULL;
void
add_import (const char *module)
{
struct import *tmp = emalloc (sizeof(*tmp));
tmp->module = module;
tmp->next = imports;
imports = tmp;
fprintf (headerfile, "#include <%s_asn1.h>\n", module);
}
const char *
get_filename (void)
{
return orig_filename;
}
void
init_generate (const char *filename, const char *base)
{
char *fn;
orig_filename = filename;
if (base != NULL) {
headerbase = strdup(base);
if (headerbase == NULL)
errx(1, "strdup");
}
asprintf(&header, "%s.h", headerbase);
if (header == NULL)
errx(1, "malloc");
headerfile = fopen (header, "w");
if (headerfile == NULL)
err (1, "open %s", header);
fprintf (headerfile,
"/* Generated from %s */\n"
"/* Do not edit */\n\n",
filename);
fprintf (headerfile,
"#ifndef __%s_h__\n"
"#define __%s_h__\n\n", headerbase, headerbase);
fprintf (headerfile,
"#include <stddef.h>\n"
"#include <time.h>\n\n");
fprintf (headerfile,
"#ifndef __asn1_common_definitions__\n"
"#define __asn1_common_definitions__\n\n");
fprintf (headerfile,
"typedef struct heim_integer {\n"
" size_t length;\n"
" void *data;\n"
" int negative;\n"
"} heim_integer;\n\n");
fprintf (headerfile,
"typedef struct heim_octet_string {\n"
" size_t length;\n"
" void *data;\n"
"} heim_octet_string;\n\n");
fprintf (headerfile,
"typedef char *heim_general_string;\n\n"
);
fprintf (headerfile,
"typedef char *heim_utf8_string;\n\n"
);
fprintf (headerfile,
"typedef char *heim_printable_string;\n\n"
);
fprintf (headerfile,
"typedef char *heim_ia5_string;\n\n"
);
fprintf (headerfile,
"typedef struct heim_bmp_string {\n"
" size_t length;\n"
" uint16_t *data;\n"
"} heim_bmp_string;\n\n");
fprintf (headerfile,
"typedef struct heim_universal_string {\n"
" size_t length;\n"
" uint32_t *data;\n"
"} heim_universal_string;\n\n");
fprintf (headerfile,
"typedef struct heim_oid {\n"
" size_t length;\n"
" unsigned *components;\n"
"} heim_oid;\n\n");
fprintf (headerfile,
"typedef struct heim_bit_string {\n"
" size_t length;\n"
" void *data;\n"
"} heim_bit_string;\n\n");
fprintf (headerfile,
"typedef struct heim_octet_string heim_any;\n"
"typedef struct heim_octet_string heim_any_set;\n\n");
fputs("#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \\\n"
" do { \\\n"
" (BL) = length_##T((S)); \\\n"
" (B) = malloc((BL)); \\\n"
" if((B) == NULL) { \\\n"
" (R) = ENOMEM; \\\n"
" } else { \\\n"
" (R) = encode_##T(((unsigned char*)(B)) + (BL) - 1, (BL), \\\n"
" (S), (L)); \\\n"
" if((R) != 0) { \\\n"
" free((B)); \\\n"
" (B) = NULL; \\\n"
" } \\\n"
" } \\\n"
" } while (0)\n\n",
headerfile);
fprintf (headerfile, "#endif\n\n");
asprintf(&fn, "%s_files", base);
if (fn == NULL)
errx(1, "malloc");
logfile = fopen(fn, "w");
if (logfile == NULL)
err (1, "open %s", fn);
}
void
close_generate (void)
{
fprintf (headerfile, "#endif /* __%s_h__ */\n", headerbase);
fclose (headerfile);
fprintf (logfile, "\n");
fclose (logfile);
}
void
gen_assign_defval(const char *var, struct value *val)
{
switch(val->type) {
case stringvalue:
fprintf(codefile, "if((%s = strdup(\"%s\")) == NULL)\nreturn ENOMEM;\n", var, val->u.stringvalue);
break;
case integervalue:
fprintf(codefile, "%s = %d;\n", var, val->u.integervalue);
break;
case booleanvalue:
if(val->u.booleanvalue)
fprintf(codefile, "%s = TRUE;\n", var);
else
fprintf(codefile, "%s = FALSE;\n", var);
break;
default:
abort();
}
}
void
gen_compare_defval(const char *var, struct value *val)
{
switch(val->type) {
case stringvalue:
fprintf(codefile, "if(strcmp(%s, \"%s\") != 0)\n", var, val->u.stringvalue);
break;
case integervalue:
fprintf(codefile, "if(%s != %d)\n", var, val->u.integervalue);
break;
case booleanvalue:
if(val->u.booleanvalue)
fprintf(codefile, "if(!%s)\n", var);
else
fprintf(codefile, "if(%s)\n", var);
break;
default:
abort();
}
}
static void
generate_header_of_codefile(const char *name)
{
char *filename;
if (codefile != NULL)
abort();
asprintf (&filename, "%s_%s.x", STEM, name);
if (filename == NULL)
errx(1, "malloc");
codefile = fopen (filename, "w");
if (codefile == NULL)
err (1, "fopen %s", filename);
fprintf(logfile, "%s ", filename);
free(filename);
fprintf (codefile,
"/* Generated from %s */\n"
"/* Do not edit */\n\n"
"#include <stdio.h>\n"
"#include <stdlib.h>\n"
"#include <time.h>\n"
"#include <string.h>\n"
"#include <errno.h>\n"
"#include <krb5-types.h>\n",
orig_filename);
fprintf (codefile,
"#include <%s.h>\n",
headerbase);
fprintf (codefile,
"#include <asn1_err.h>\n"
"#include <der.h>\n"
"#include <parse_units.h>\n\n");
}
static void
close_codefile(void)
{
if (codefile == NULL)
abort();
fclose(codefile);
codefile = NULL;
}
void
generate_constant (const Symbol *s)
{
switch(s->value->type) {
case booleanvalue:
break;
case integervalue:
fprintf (headerfile, "enum { %s = %d };\n\n",
s->gen_name, s->value->u.integervalue);
break;
case nullvalue:
break;
case stringvalue:
break;
case objectidentifiervalue: {
struct objid *o, **list;
int i, len;
generate_header_of_codefile(s->gen_name);
len = 0;
for (o = s->value->u.objectidentifiervalue; o != NULL; o = o->next)
len++;
list = emalloc(sizeof(*list) * len);
i = 0;
for (o = s->value->u.objectidentifiervalue; o != NULL; o = o->next)
list[i++] = o;
fprintf (headerfile, "/* OBJECT IDENTIFIER %s ::= { ", s->name);
for (i = len - 1 ; i >= 0; i--) {
o = list[i];
fprintf(headerfile, "%s(%d) ",
o->label ? o->label : "label-less", o->value);
}
fprintf (headerfile, "} */\n");
fprintf (headerfile, "const heim_oid *oid_%s(void);\n\n",
s->gen_name);
fprintf (codefile, "static unsigned oid_%s_variable_num[%d] = {",
s->gen_name, len);
for (i = len - 1 ; i >= 0; i--) {
fprintf(codefile, "%d%s ", list[i]->value, i > 0 ? "," : "");
}
fprintf(codefile, "};\n");
fprintf (codefile, "static const heim_oid oid_%s_variable = "
"{ %d, oid_%s_variable_num };\n\n",
s->gen_name, len, s->gen_name);
fprintf (codefile, "const heim_oid *oid_%s(void)\n"
"{\n"
"return &oid_%s_variable;\n"
"}\n\n",
s->gen_name, s->gen_name);
close_codefile();
break;
}
default:
abort();
}
}
static void
space(int level)
{
while(level-- > 0)
fprintf(headerfile, " ");
}
static const char *
last_member_p(struct member *m)
{
struct member *n = ASN1_TAILQ_NEXT(m, members);
if (n == NULL)
return "";
if (n->ellipsis && ASN1_TAILQ_NEXT(n, members) == NULL)
return "";
return ",";
}
static struct member *
have_ellipsis(Type *t)
{
struct member *m;
ASN1_TAILQ_FOREACH(m, t->members, members) {
if (m->ellipsis)
return m;
}
return NULL;
}
static void
define_asn1 (int level, Type *t)
{
switch (t->type) {
case TType:
fprintf (headerfile, "%s", t->symbol->name);
break;
case TInteger:
if(t->members == NULL) {
fprintf (headerfile, "INTEGER");
if (t->range)
fprintf (headerfile, " (%d..%d)",
t->range->min, t->range->max);
} else {
Member *m;
fprintf (headerfile, "INTEGER {\n");
ASN1_TAILQ_FOREACH(m, t->members, members) {
space (level + 1);
fprintf(headerfile, "%s(%d)%s\n", m->gen_name, m->val,
last_member_p(m));
}
space(level);
fprintf (headerfile, "}");
}
break;
case TBoolean:
fprintf (headerfile, "BOOLEAN");
break;
case TOctetString:
fprintf (headerfile, "OCTET STRING");
break;
case TEnumerated :
case TBitString: {
Member *m;
space(level);
if(t->type == TBitString)
fprintf (headerfile, "BIT STRING {\n");
else
fprintf (headerfile, "ENUMERATED {\n");
ASN1_TAILQ_FOREACH(m, t->members, members) {
space(level + 1);
fprintf (headerfile, "%s(%d)%s\n", m->name, m->val,
last_member_p(m));
}
space(level);
fprintf (headerfile, "}");
break;
}
case TChoice:
case TSet:
case TSequence: {
Member *m;
int max_width = 0;
if(t->type == TChoice)
fprintf(headerfile, "CHOICE {\n");
else if(t->type == TSet)
fprintf(headerfile, "SET {\n");
else
fprintf(headerfile, "SEQUENCE {\n");
ASN1_TAILQ_FOREACH(m, t->members, members) {
if(strlen(m->name) > max_width)
max_width = strlen(m->name);
}
max_width += 3;
if(max_width < 16) max_width = 16;
ASN1_TAILQ_FOREACH(m, t->members, members) {
int width = max_width;
space(level + 1);
if (m->ellipsis) {
fprintf (headerfile, "...");
} else {
width -= fprintf(headerfile, "%s", m->name);
fprintf(headerfile, "%*s", width, "");
define_asn1(level + 1, m->type);
if(m->optional)
fprintf(headerfile, " OPTIONAL");
}
if(last_member_p(m))
fprintf (headerfile, ",");
fprintf (headerfile, "\n");
}
space(level);
fprintf (headerfile, "}");
break;
}
case TSequenceOf:
fprintf (headerfile, "SEQUENCE OF ");
define_asn1 (0, t->subtype);
break;
case TSetOf:
fprintf (headerfile, "SET OF ");
define_asn1 (0, t->subtype);
break;
case TGeneralizedTime:
fprintf (headerfile, "GeneralizedTime");
break;
case TGeneralString:
fprintf (headerfile, "GeneralString");
break;
case TTag: {
const char *classnames[] = { "UNIVERSAL ", "APPLICATION ",
"" /* CONTEXT */, "PRIVATE " };
if(t->tag.tagclass != ASN1_C_UNIV)
fprintf (headerfile, "[%s%d] ",
classnames[t->tag.tagclass],
t->tag.tagvalue);
if(t->tag.tagenv == TE_IMPLICIT)
fprintf (headerfile, "IMPLICIT ");
define_asn1 (level, t->subtype);
break;
}
case TUTCTime:
fprintf (headerfile, "UTCTime");
break;
case TUTF8String:
space(level);
fprintf (headerfile, "UTF8String");
break;
case TPrintableString:
space(level);
fprintf (headerfile, "PrintableString");
break;
case TIA5String:
space(level);
fprintf (headerfile, "IA5String");
break;
case TBMPString:
space(level);
fprintf (headerfile, "BMPString");
break;
case TUniversalString:
space(level);
fprintf (headerfile, "UniversalString");
break;
case TOID :
space(level);
fprintf(headerfile, "OBJECT IDENTIFIER");
break;
case TNull:
space(level);
fprintf (headerfile, "NULL");
break;
default:
abort ();
}
}
static void
define_type (int level, const char *name, Type *t, int typedefp, int preservep)
{
switch (t->type) {
case TType:
space(level);
fprintf (headerfile, "%s %s;\n", t->symbol->gen_name, name);
break;
case TInteger:
space(level);
if(t->members) {
Member *m;
fprintf (headerfile, "enum %s {\n", typedefp ? name : "");
ASN1_TAILQ_FOREACH(m, t->members, members) {
space (level + 1);
fprintf(headerfile, "%s = %d%s\n", m->gen_name, m->val,
last_member_p(m));
}
fprintf (headerfile, "} %s;\n", name);
} else if (t->range == NULL) {
fprintf (headerfile, "heim_integer %s;\n", name);
} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
fprintf (headerfile, "int %s;\n", name);
} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
fprintf (headerfile, "unsigned int %s;\n", name);
} else if (t->range->min == 0 && t->range->max == INT_MAX) {
fprintf (headerfile, "unsigned int %s;\n", name);
} else
errx(1, "%s: unsupported range %d -> %d",
name, t->range->min, t->range->max);
break;
case TBoolean:
space(level);
fprintf (headerfile, "int %s;\n", name);
break;
case TOctetString:
space(level);
fprintf (headerfile, "heim_octet_string %s;\n", name);
break;
case TBitString: {
Member *m;
Type i;
struct range range = { 0, INT_MAX };
i.type = TInteger;
i.range = &range;
i.members = NULL;
i.constraint = NULL;
space(level);
if(ASN1_TAILQ_EMPTY(t->members))
fprintf (headerfile, "heim_bit_string %s;\n", name);
else {
fprintf (headerfile, "struct %s {\n", typedefp ? name : "");
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *n;
asprintf (&n, "%s:1", m->gen_name);
if (n == NULL)
errx(1, "malloc");
define_type (level + 1, n, &i, FALSE, FALSE);
free (n);
}
space(level);
fprintf (headerfile, "} %s;\n\n", name);
}
break;
}
case TEnumerated: {
Member *m;
space(level);
fprintf (headerfile, "enum %s {\n", typedefp ? name : "");
ASN1_TAILQ_FOREACH(m, t->members, members) {
space(level + 1);
if (m->ellipsis)
fprintf (headerfile, "/* ... */\n");
else
fprintf (headerfile, "%s = %d%s\n", m->gen_name, m->val,
last_member_p(m));
}
space(level);
fprintf (headerfile, "} %s;\n\n", name);
break;
}
case TSet:
case TSequence: {
Member *m;
space(level);
fprintf (headerfile, "struct %s {\n", typedefp ? name : "");
if (t->type == TSequence && preservep) {
space(level + 1);
fprintf(headerfile, "heim_octet_string _save;\n");
}
ASN1_TAILQ_FOREACH(m, t->members, members) {
if (m->ellipsis) {
;
} else if (m->optional) {
char *n;
asprintf (&n, "*%s", m->gen_name);
if (n == NULL)
errx(1, "malloc");
define_type (level + 1, n, m->type, FALSE, FALSE);
free (n);
} else
define_type (level + 1, m->gen_name, m->type, FALSE, FALSE);
}
space(level);
fprintf (headerfile, "} %s;\n", name);
break;
}
case TSetOf:
case TSequenceOf: {
Type i;
struct range range = { 0, INT_MAX };
i.type = TInteger;
i.range = &range;
i.members = NULL;
i.constraint = NULL;
space(level);
fprintf (headerfile, "struct %s {\n", typedefp ? name : "");
define_type (level + 1, "len", &i, FALSE, FALSE);
define_type (level + 1, "*val", t->subtype, FALSE, FALSE);
space(level);
fprintf (headerfile, "} %s;\n", name);
break;
}
case TGeneralizedTime:
space(level);
fprintf (headerfile, "time_t %s;\n", name);
break;
case TGeneralString:
space(level);
fprintf (headerfile, "heim_general_string %s;\n", name);
break;
case TTag:
define_type (level, name, t->subtype, typedefp, preservep);
break;
case TChoice: {
int first = 1;
Member *m;
space(level);
fprintf (headerfile, "struct %s {\n", typedefp ? name : "");
if (preservep) {
space(level + 1);
fprintf(headerfile, "heim_octet_string _save;\n");
}
space(level + 1);
fprintf (headerfile, "enum {\n");
m = have_ellipsis(t);
if (m) {
space(level + 2);
fprintf (headerfile, "%s = 0,\n", m->label);
first = 0;
}
ASN1_TAILQ_FOREACH(m, t->members, members) {
space(level + 2);
if (m->ellipsis)
fprintf (headerfile, "/* ... */\n");
else
fprintf (headerfile, "%s%s%s\n", m->label,
first ? " = 1" : "",
last_member_p(m));
first = 0;
}
space(level + 1);
fprintf (headerfile, "} element;\n");
space(level + 1);
fprintf (headerfile, "union {\n");
ASN1_TAILQ_FOREACH(m, t->members, members) {
if (m->ellipsis) {
space(level + 2);
fprintf(headerfile, "heim_octet_string asn1_ellipsis;\n");
} else if (m->optional) {
char *n;
asprintf (&n, "*%s", m->gen_name);
if (n == NULL)
errx(1, "malloc");
define_type (level + 2, n, m->type, FALSE, FALSE);
free (n);
} else
define_type (level + 2, m->gen_name, m->type, FALSE, FALSE);
}
space(level + 1);
fprintf (headerfile, "} u;\n");
space(level);
fprintf (headerfile, "} %s;\n", name);
break;
}
case TUTCTime:
space(level);
fprintf (headerfile, "time_t %s;\n", name);
break;
case TUTF8String:
space(level);
fprintf (headerfile, "heim_utf8_string %s;\n", name);
break;
case TPrintableString:
space(level);
fprintf (headerfile, "heim_printable_string %s;\n", name);
break;
case TIA5String:
space(level);
fprintf (headerfile, "heim_ia5_string %s;\n", name);
break;
case TBMPString:
space(level);
fprintf (headerfile, "heim_bmp_string %s;\n", name);
break;
case TUniversalString:
space(level);
fprintf (headerfile, "heim_universal_string %s;\n", name);
break;
case TOID :
space(level);
fprintf (headerfile, "heim_oid %s;\n", name);
break;
case TNull:
space(level);
fprintf (headerfile, "int %s;\n", name);
break;
default:
abort ();
}
}
static void
generate_type_header (const Symbol *s)
{
int preservep = preserve_type(s->name) ? TRUE : FALSE;
fprintf (headerfile, "/*\n");
fprintf (headerfile, "%s ::= ", s->name);
define_asn1 (0, s->type);
fprintf (headerfile, "\n*/\n\n");
fprintf (headerfile, "typedef ");
define_type (0, s->gen_name, s->type, TRUE, preservep);
fprintf (headerfile, "\n");
}
void
generate_type (const Symbol *s)
{
generate_header_of_codefile(s->gen_name);
generate_type_header (s);
generate_type_encode (s);
generate_type_decode (s);
generate_type_free (s);
generate_type_length (s);
generate_type_copy (s);
generate_type_seq (s);
generate_glue (s->type, s->gen_name);
fprintf(headerfile, "\n\n");
close_codefile();
}
+246
View File
@@ -0,0 +1,246 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_copy.c,v 1.18 2006/10/14 05:34:19 lha Exp $");
static int used_fail;
static void
copy_primitive (const char *typename, const char *from, const char *to)
{
fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
typename, from, to);
used_fail++;
}
static void
copy_type (const char *from, const char *to, const Type *t, int preserve)
{
switch (t->type) {
case TType:
#if 0
copy_type (from, to, t->symbol->type, preserve);
#endif
fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
t->symbol->gen_name, from, to);
used_fail++;
break;
case TInteger:
if (t->range == NULL && t->members == NULL) {
copy_primitive ("heim_integer", from, to);
break;
}
case TBoolean:
case TEnumerated :
fprintf(codefile, "*(%s) = *(%s);\n", to, from);
break;
case TOctetString:
copy_primitive ("octet_string", from, to);
break;
case TBitString:
if (ASN1_TAILQ_EMPTY(t->members))
copy_primitive ("bit_string", from, to);
else
fprintf(codefile, "*(%s) = *(%s);\n", to, from);
break;
case TSet:
case TSequence:
case TChoice: {
Member *m, *have_ellipsis = NULL;
if(t->members == NULL)
break;
if ((t->type == TSequence || t->type == TChoice) && preserve) {
fprintf(codefile,
"{ int ret;\n"
"ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
"if (ret) goto fail;\n"
"}\n",
from, to);
used_fail++;
}
if(t->type == TChoice) {
fprintf(codefile, "(%s)->element = (%s)->element;\n", to, from);
fprintf(codefile, "switch((%s)->element) {\n", from);
}
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *fs;
char *ts;
if (m->ellipsis) {
have_ellipsis = m;
continue;
}
if(t->type == TChoice)
fprintf(codefile, "case %s:\n", m->label);
asprintf (&fs, "%s(%s)->%s%s",
m->optional ? "" : "&", from,
t->type == TChoice ? "u." : "", m->gen_name);
if (fs == NULL)
errx(1, "malloc");
asprintf (&ts, "%s(%s)->%s%s",
m->optional ? "" : "&", to,
t->type == TChoice ? "u." : "", m->gen_name);
if (ts == NULL)
errx(1, "malloc");
if(m->optional){
fprintf(codefile, "if(%s) {\n", fs);
fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
used_fail++;
}
copy_type (fs, ts, m->type, FALSE);
if(m->optional){
fprintf(codefile, "}else\n");
fprintf(codefile, "%s = NULL;\n", ts);
}
free (fs);
free (ts);
if(t->type == TChoice)
fprintf(codefile, "break;\n");
}
if(t->type == TChoice) {
if (have_ellipsis) {
fprintf(codefile, "case %s: {\n"
"int ret;\n"
"ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
"if (ret) goto fail;\n"
"break;\n"
"}\n",
have_ellipsis->label,
from, have_ellipsis->gen_name,
to, have_ellipsis->gen_name);
used_fail++;
}
fprintf(codefile, "}\n");
}
break;
}
case TSetOf:
case TSequenceOf: {
char *f;
char *T;
fprintf (codefile, "if(((%s)->val = "
"malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
to, from, to, from);
fprintf (codefile, "goto fail;\n");
used_fail++;
fprintf(codefile,
"for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
to, to, from, to);
asprintf(&f, "&(%s)->val[(%s)->len]", from, to);
if (f == NULL)
errx(1, "malloc");
asprintf(&T, "&(%s)->val[(%s)->len]", to, to);
if (T == NULL)
errx(1, "malloc");
copy_type(f, T, t->subtype, FALSE);
fprintf(codefile, "}\n");
free(f);
free(T);
break;
}
case TGeneralizedTime:
fprintf(codefile, "*(%s) = *(%s);\n", to, from);
break;
case TGeneralString:
copy_primitive ("general_string", from, to);
break;
case TUTCTime:
fprintf(codefile, "*(%s) = *(%s);\n", to, from);
break;
case TUTF8String:
copy_primitive ("utf8string", from, to);
break;
case TPrintableString:
copy_primitive ("printable_string", from, to);
break;
case TIA5String:
copy_primitive ("ia5_string", from, to);
break;
case TBMPString:
copy_primitive ("bmp_string", from, to);
break;
case TUniversalString:
copy_primitive ("universal_string", from, to);
break;
case TTag:
copy_type (from, to, t->subtype, preserve);
break;
case TOID:
copy_primitive ("oid", from, to);
break;
case TNull:
break;
default :
abort ();
}
}
void
generate_type_copy (const Symbol *s)
{
int preserve = preserve_type(s->name) ? TRUE : FALSE;
used_fail = 0;
fprintf (headerfile,
"int copy_%s (const %s *, %s *);\n",
s->gen_name, s->gen_name, s->gen_name);
fprintf (codefile, "int\n"
"copy_%s(const %s *from, %s *to)\n"
"{\n"
"memset(to, 0, sizeof(*to));\n",
s->gen_name, s->gen_name, s->gen_name);
copy_type ("from", "to", s->type, preserve);
fprintf (codefile, "return 0;\n");
if (used_fail)
fprintf (codefile, "fail:\n"
"free_%s(to);\n"
"return ENOMEM;\n",
s->gen_name);
fprintf(codefile,
"}\n\n");
}
+666
View File
@@ -0,0 +1,666 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
#include "lex.h"
RCSID("$Id: gen_decode.c,v 1.30 2006/09/24 09:13:12 lha Exp $");
static void
decode_primitive (const char *typename, const char *name, const char *forwstr)
{
#if 0
fprintf (codefile,
"e = decode_%s(p, len, %s, &l);\n"
"%s;\n",
typename,
name,
forwstr);
#else
fprintf (codefile,
"e = der_get_%s(p, len, %s, &l);\n"
"if(e) %s;\np += l; len -= l; ret += l;\n",
typename,
name,
forwstr);
#endif
}
static int
is_primitive_type(int type)
{
switch(type) {
case TInteger:
case TBoolean:
case TOctetString:
case TBitString:
case TEnumerated:
case TGeneralizedTime:
case TGeneralString:
case TOID:
case TUTCTime:
case TUTF8String:
case TPrintableString:
case TIA5String:
case TBMPString:
case TUniversalString:
case TNull:
return 1;
default:
return 0;
}
}
static void
find_tag (const Type *t,
Der_class *cl, Der_type *ty, unsigned *tag)
{
switch (t->type) {
case TBitString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_BitString;
break;
case TBoolean:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_Boolean;
break;
case TChoice:
errx(1, "Cannot have recursive CHOICE");
case TEnumerated:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_Enumerated;
break;
case TGeneralString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_GeneralString;
break;
case TGeneralizedTime:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_GeneralizedTime;
break;
case TIA5String:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_IA5String;
break;
case TInteger:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_Integer;
break;
case TNull:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_Null;
break;
case TOID:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_OID;
break;
case TOctetString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_OctetString;
break;
case TPrintableString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_PrintableString;
break;
case TSequence:
case TSequenceOf:
*cl = ASN1_C_UNIV;
*ty = CONS;
*tag = UT_Sequence;
break;
case TSet:
case TSetOf:
*cl = ASN1_C_UNIV;
*ty = CONS;
*tag = UT_Set;
break;
case TTag:
*cl = t->tag.tagclass;
*ty = is_primitive_type(t->subtype->type) ? PRIM : CONS;
*tag = t->tag.tagvalue;
break;
case TType:
if ((t->symbol->stype == Stype && t->symbol->type == NULL)
|| t->symbol->stype == SUndefined) {
error_message("%s is imported or still undefined, "
" can't generate tag checking data in CHOICE "
"without this information",
t->symbol->name);
exit(1);
}
find_tag(t->symbol->type, cl, ty, tag);
return;
case TUTCTime:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_UTCTime;
break;
case TUTF8String:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_UTF8String;
break;
case TBMPString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_BMPString;
break;
case TUniversalString:
*cl = ASN1_C_UNIV;
*ty = PRIM;
*tag = UT_UniversalString;
break;
default:
abort();
}
}
static int
decode_type (const char *name, const Type *t, int optional,
const char *forwstr, const char *tmpstr)
{
switch (t->type) {
case TType: {
if (optional)
fprintf(codefile,
"%s = calloc(1, sizeof(*%s));\n"
"if (%s == NULL) %s;\n",
name, name, name, forwstr);
fprintf (codefile,
"e = decode_%s(p, len, %s, &l);\n",
t->symbol->gen_name, name);
if (optional) {
fprintf (codefile,
"if(e) {\n"
"free(%s);\n"
"%s = NULL;\n"
"} else {\n"
"p += l; len -= l; ret += l;\n"
"}\n",
name, name);
} else {
fprintf (codefile,
"if(e) %s;\n",
forwstr);
fprintf (codefile,
"p += l; len -= l; ret += l;\n");
}
break;
}
case TInteger:
if(t->members) {
char *s;
asprintf(&s, "(int*)%s", name);
if (s == NULL)
errx (1, "out of memory");
decode_primitive ("integer", s, forwstr);
free(s);
} else if (t->range == NULL) {
decode_primitive ("heim_integer", name, forwstr);
} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
decode_primitive ("integer", name, forwstr);
} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
decode_primitive ("unsigned", name, forwstr);
} else if (t->range->min == 0 && t->range->max == INT_MAX) {
decode_primitive ("unsigned", name, forwstr);
} else
errx(1, "%s: unsupported range %d -> %d",
name, t->range->min, t->range->max);
break;
case TBoolean:
decode_primitive ("boolean", name, forwstr);
break;
case TEnumerated:
decode_primitive ("enumerated", name, forwstr);
break;
case TOctetString:
decode_primitive ("octet_string", name, forwstr);
break;
case TBitString: {
Member *m;
int pos = 0;
if (ASN1_TAILQ_EMPTY(t->members)) {
decode_primitive ("bit_string", name, forwstr);
break;
}
fprintf(codefile,
"if (len < 1) return ASN1_OVERRUN;\n"
"p++; len--; ret++;\n");
fprintf(codefile,
"do {\n"
"if (len < 1) break;\n");
ASN1_TAILQ_FOREACH(m, t->members, members) {
while (m->val / 8 > pos / 8) {
fprintf (codefile,
"p++; len--; ret++;\n"
"if (len < 1) break;\n");
pos += 8;
}
fprintf (codefile,
"(%s)->%s = (*p >> %d) & 1;\n",
name, m->gen_name, 7 - m->val % 8);
}
fprintf(codefile,
"} while(0);\n");
fprintf (codefile,
"p += len; ret += len;\n");
break;
}
case TSequence: {
Member *m;
if (t->members == NULL)
break;
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *s;
if (m->ellipsis)
continue;
asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&",
name, m->gen_name);
if (s == NULL)
errx(1, "malloc");
decode_type (s, m->type, m->optional, forwstr, m->gen_name);
free (s);
}
break;
}
case TSet: {
Member *m;
unsigned int memno;
if(t->members == NULL)
break;
fprintf(codefile, "{\n");
fprintf(codefile, "unsigned int members = 0;\n");
fprintf(codefile, "while(len > 0) {\n");
fprintf(codefile,
"Der_class class;\n"
"Der_type type;\n"
"int tag;\n"
"e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
"if(e) %s;\n", forwstr);
fprintf(codefile, "switch (MAKE_TAG(class, type, tag)) {\n");
memno = 0;
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *s;
assert(m->type->type == TTag);
fprintf(codefile, "case MAKE_TAG(%s, %s, %s):\n",
classname(m->type->tag.tagclass),
is_primitive_type(m->type->subtype->type) ? "PRIM" : "CONS",
valuename(m->type->tag.tagclass, m->type->tag.tagvalue));
asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
if (s == NULL)
errx(1, "malloc");
if(m->optional)
fprintf(codefile,
"%s = calloc(1, sizeof(*%s));\n"
"if (%s == NULL) { e = ENOMEM; %s; }\n",
s, s, s, forwstr);
decode_type (s, m->type, 0, forwstr, m->gen_name);
free (s);
fprintf(codefile, "members |= (1 << %d);\n", memno);
memno++;
fprintf(codefile, "break;\n");
}
fprintf(codefile,
"default:\n"
"return ASN1_MISPLACED_FIELD;\n"
"break;\n");
fprintf(codefile, "}\n");
fprintf(codefile, "}\n");
memno = 0;
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *s;
asprintf (&s, "%s->%s", name, m->gen_name);
if (s == NULL)
errx(1, "malloc");
fprintf(codefile, "if((members & (1 << %d)) == 0)\n", memno);
if(m->optional)
fprintf(codefile, "%s = NULL;\n", s);
else if(m->defval)
gen_assign_defval(s, m->defval);
else
fprintf(codefile, "return ASN1_MISSING_FIELD;\n");
free(s);
memno++;
}
fprintf(codefile, "}\n");
break;
}
case TSetOf:
case TSequenceOf: {
char *n;
char *sname;
fprintf (codefile,
"{\n"
"size_t %s_origlen = len;\n"
"size_t %s_oldret = ret;\n"
"void *%s_tmp;\n"
"ret = 0;\n"
"(%s)->len = 0;\n"
"(%s)->val = NULL;\n"
"while(ret < %s_origlen) {\n"
"%s_tmp = realloc((%s)->val, "
" sizeof(*((%s)->val)) * ((%s)->len + 1));\n"
"if (%s_tmp == NULL) { %s; }\n"
"(%s)->val = %s_tmp;\n",
tmpstr, tmpstr, tmpstr,
name, name,
tmpstr, tmpstr,
name, name, name,
tmpstr, forwstr,
name, tmpstr);
asprintf (&n, "&(%s)->val[(%s)->len]", name, name);
if (n == NULL)
errx(1, "malloc");
asprintf (&sname, "%s_s_of", tmpstr);
if (sname == NULL)
errx(1, "malloc");
decode_type (n, t->subtype, 0, forwstr, sname);
fprintf (codefile,
"(%s)->len++;\n"
"len = %s_origlen - ret;\n"
"}\n"
"ret += %s_oldret;\n"
"}\n",
name,
tmpstr, tmpstr);
free (n);
free (sname);
break;
}
case TGeneralizedTime:
decode_primitive ("generalized_time", name, forwstr);
break;
case TGeneralString:
decode_primitive ("general_string", name, forwstr);
break;
case TTag:{
char *tname;
fprintf(codefile,
"{\n"
"size_t %s_datalen, %s_oldlen;\n",
tmpstr, tmpstr);
if(dce_fix)
fprintf(codefile,
"int dce_fix;\n");
fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, %s, %s, "
"&%s_datalen, &l);\n",
classname(t->tag.tagclass),
is_primitive_type(t->subtype->type) ? "PRIM" : "CONS",
valuename(t->tag.tagclass, t->tag.tagvalue),
tmpstr);
if(optional) {
fprintf(codefile,
"if(e) {\n"
"%s = NULL;\n"
"} else {\n"
"%s = calloc(1, sizeof(*%s));\n"
"if (%s == NULL) { e = ENOMEM; %s; }\n",
name, name, name, name, forwstr);
} else {
fprintf(codefile, "if(e) %s;\n", forwstr);
}
fprintf (codefile,
"p += l; len -= l; ret += l;\n"
"%s_oldlen = len;\n",
tmpstr);
if(dce_fix)
fprintf (codefile,
"if((dce_fix = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
"{ e = ASN1_BAD_FORMAT; %s; }\n",
tmpstr, forwstr);
else
fprintf(codefile,
"if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
"len = %s_datalen;\n", tmpstr, forwstr, tmpstr);
asprintf (&tname, "%s_Tag", tmpstr);
if (tname == NULL)
errx(1, "malloc");
decode_type (name, t->subtype, 0, forwstr, tname);
if(dce_fix)
fprintf(codefile,
"if(dce_fix){\n"
"e = der_match_tag_and_length (p, len, "
"(Der_class)0,(Der_type)0, UT_EndOfContent, "
"&%s_datalen, &l);\n"
"if(e) %s;\np += l; len -= l; ret += l;\n"
"} else \n", tmpstr, forwstr);
fprintf(codefile,
"len = %s_oldlen - %s_datalen;\n",
tmpstr, tmpstr);
if(optional)
fprintf(codefile,
"}\n");
fprintf(codefile,
"}\n");
free(tname);
break;
}
case TChoice: {
Member *m, *have_ellipsis = NULL;
const char *els = "";
if (t->members == NULL)
break;
ASN1_TAILQ_FOREACH(m, t->members, members) {
const Type *tt = m->type;
char *s;
Der_class cl;
Der_type ty;
unsigned tag;
if (m->ellipsis) {
have_ellipsis = m;
continue;
}
find_tag(tt, &cl, &ty, &tag);
fprintf(codefile,
"%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
els,
classname(cl),
ty ? "CONS" : "PRIM",
valuename(cl, tag));
asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&",
name, m->gen_name);
if (s == NULL)
errx(1, "malloc");
decode_type (s, m->type, m->optional, forwstr, m->gen_name);
fprintf(codefile,
"(%s)->element = %s;\n",
name, m->label);
free(s);
fprintf(codefile,
"}\n");
els = "else ";
}
if (have_ellipsis) {
fprintf(codefile,
"else {\n"
"(%s)->u.%s.data = calloc(1, len);\n"
"if ((%s)->u.%s.data == NULL) {\n"
"e = ENOMEM; %s;\n"
"}\n"
"(%s)->u.%s.length = len;\n"
"memcpy((%s)->u.%s.data, p, len);\n"
"(%s)->element = %s;\n"
"p += len;\n"
"ret += len;\n"
"len -= len;\n"
"}\n",
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name,
forwstr,
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name,
name, have_ellipsis->label);
} else {
fprintf(codefile,
"else {\n"
"e = ASN1_PARSE_ERROR;\n"
"%s;\n"
"}\n",
forwstr);
}
break;
}
case TUTCTime:
decode_primitive ("utctime", name, forwstr);
break;
case TUTF8String:
decode_primitive ("utf8string", name, forwstr);
break;
case TPrintableString:
decode_primitive ("printable_string", name, forwstr);
break;
case TIA5String:
decode_primitive ("ia5_string", name, forwstr);
break;
case TBMPString:
decode_primitive ("bmp_string", name, forwstr);
break;
case TUniversalString:
decode_primitive ("universal_string", name, forwstr);
break;
case TNull:
fprintf (codefile, "/* NULL */\n");
break;
case TOID:
decode_primitive ("oid", name, forwstr);
break;
default :
abort ();
}
return 0;
}
void
generate_type_decode (const Symbol *s)
{
int preserve = preserve_type(s->name) ? TRUE : FALSE;
fprintf (headerfile,
"int "
"decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
s->gen_name, s->gen_name);
fprintf (codefile, "int\n"
"decode_%s(const unsigned char *p,"
" size_t len, %s *data, size_t *size)\n"
"{\n",
s->gen_name, s->gen_name);
switch (s->type->type) {
case TInteger:
case TBoolean:
case TOctetString:
case TOID:
case TGeneralizedTime:
case TGeneralString:
case TUTF8String:
case TPrintableString:
case TIA5String:
case TBMPString:
case TUniversalString:
case TUTCTime:
case TNull:
case TEnumerated:
case TBitString:
case TSequence:
case TSequenceOf:
case TSet:
case TSetOf:
case TTag:
case TType:
case TChoice:
fprintf (codefile,
"size_t ret = 0;\n"
"size_t l;\n"
"int e;\n");
if (preserve)
fprintf (codefile, "const unsigned char *begin = p;\n");
fprintf (codefile, "\n");
fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
decode_type ("data", s->type, 0, "goto fail", "Top");
if (preserve)
fprintf (codefile,
"data->_save.data = calloc(1, ret);\n"
"if (data->_save.data == NULL) { \n"
"e = ENOMEM; goto fail; \n"
"}\n"
"data->_save.length = ret;\n"
"memcpy(data->_save.data, begin, ret);\n");
fprintf (codefile,
"if(size) *size = ret;\n"
"return 0;\n");
fprintf (codefile,
"fail:\n"
"free_%s(data);\n"
"return e;\n",
s->gen_name);
break;
default:
abort ();
}
fprintf (codefile, "}\n\n");
}
+532
View File
@@ -0,0 +1,532 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_encode.c,v 1.19 2005/08/23 11:52:16 lha Exp $");
static void
encode_primitive (const char *typename, const char *name)
{
fprintf (codefile,
"e = der_put_%s(p, len, %s, &l);\n"
"if (e) return e;\np -= l; len -= l; ret += l;\n\n",
typename,
name);
}
const char *
classname(Der_class class)
{
const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
"ASN1_C_CONTEXT", "ASN1_C_PRIV" };
if(class < ASN1_C_UNIV || class > ASN1_C_PRIVATE)
return "???";
return cn[class];
}
const char *
valuename(Der_class class, int value)
{
static char s[32];
struct {
int value;
const char *s;
} *p, values[] = {
#define X(Y) { Y, #Y }
X(UT_BMPString),
X(UT_BitString),
X(UT_Boolean),
X(UT_EmbeddedPDV),
X(UT_Enumerated),
X(UT_External),
X(UT_GeneralString),
X(UT_GeneralizedTime),
X(UT_GraphicString),
X(UT_IA5String),
X(UT_Integer),
X(UT_Null),
X(UT_NumericString),
X(UT_OID),
X(UT_ObjectDescriptor),
X(UT_OctetString),
X(UT_PrintableString),
X(UT_Real),
X(UT_RelativeOID),
X(UT_Sequence),
X(UT_Set),
X(UT_TeletexString),
X(UT_UTCTime),
X(UT_UTF8String),
X(UT_UniversalString),
X(UT_VideotexString),
X(UT_VisibleString),
#undef X
{ -1, NULL }
};
if(class == ASN1_C_UNIV) {
for(p = values; p->value != -1; p++)
if(p->value == value)
return p->s;
}
snprintf(s, sizeof(s), "%d", value);
return s;
}
static int
encode_type (const char *name, const Type *t, const char *tmpstr)
{
int constructed = 1;
switch (t->type) {
case TType:
#if 0
encode_type (name, t->symbol->type);
#endif
fprintf (codefile,
"e = encode_%s(p, len, %s, &l);\n"
"if (e) return e;\np -= l; len -= l; ret += l;\n\n",
t->symbol->gen_name, name);
break;
case TInteger:
if(t->members) {
char *s;
asprintf(&s, "(const int*)%s", name);
if(s == NULL)
errx(1, "out of memory");
encode_primitive ("integer", s);
free(s);
} else if (t->range == NULL) {
encode_primitive ("heim_integer", name);
} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
encode_primitive ("integer", name);
} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
encode_primitive ("unsigned", name);
} else if (t->range->min == 0 && t->range->max == INT_MAX) {
encode_primitive ("unsigned", name);
} else
errx(1, "%s: unsupported range %d -> %d",
name, t->range->min, t->range->max);
constructed = 0;
break;
case TBoolean:
encode_primitive ("boolean", name);
constructed = 0;
break;
case TOctetString:
encode_primitive ("octet_string", name);
constructed = 0;
break;
case TBitString: {
Member *m;
int pos;
int rest;
if (ASN1_TAILQ_EMPTY(t->members)) {
encode_primitive("bit_string", name);
constructed = 0;
break;
}
fprintf (codefile, "{\n"
"unsigned char c = 0;\n");
if (!rfc1510_bitstring)
fprintf (codefile,
"int bit_set = 0;\n");
#if 0
pos = t->members->prev->val;
/* fix for buggy MIT (and OSF?) code */
if (pos > 31)
abort ();
#endif
/*
* It seems that if we do not always set pos to 31 here, the MIT
* code will do the wrong thing.
*
* I hate ASN.1 (and DER), but I hate it even more when everybody
* has to screw it up differently.
*/
pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
if (rfc1510_bitstring) {
if (pos < 31)
pos = 31;
rest = 7 - (pos % 8);
} else
rest = 0;
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
while (m->val / 8 < pos / 8) {
if (!rfc1510_bitstring)
fprintf (codefile,
"if (c != 0 || bit_set) {\n");
fprintf (codefile,
"if (len < 1) return ASN1_OVERFLOW;\n"
"*p-- = c; len--; ret++;\n"
"c = 0;\n");
if (!rfc1510_bitstring)
fprintf (codefile,
"bit_set = 1;\n"
"}\n");
pos -= 8;
}
fprintf (codefile,
"if((%s)->%s) {\n"
"c |= 1<<%d;\n",
name, m->gen_name, 7 - m->val % 8);
if (!rfc1510_bitstring)
rest = 7 - m->val % 8;
fprintf (codefile,
"}\n");
}
if (!rfc1510_bitstring)
fprintf (codefile,
"if (c != 0 || bit_set) {\n");
fprintf (codefile,
"if (len < 1) return ASN1_OVERFLOW;\n"
"*p-- = c; len--; ret++;\n");
if (!rfc1510_bitstring)
fprintf (codefile,
"}\n");
fprintf (codefile,
"if (len < 1) return ASN1_OVERFLOW;\n"
"*p-- = %d;\n"
"len -= 1;\n"
"ret += 1;\n"
"}\n\n",
rest);
constructed = 0;
break;
}
case TEnumerated : {
encode_primitive ("enumerated", name);
constructed = 0;
break;
}
case TSet:
case TSequence: {
Member *m;
if (t->members == NULL)
break;
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
char *s;
if (m->ellipsis)
continue;
asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
if (s == NULL)
errx(1, "malloc");
fprintf(codefile, "/* %s */\n", m->name);
if (m->optional)
fprintf (codefile,
"if(%s) ",
s);
else if(m->defval)
gen_compare_defval(s + 1, m->defval);
fprintf (codefile, "{\n");
fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
fprintf (codefile, "ret = 0;\n");
encode_type (s, m->type, m->gen_name);
fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
fprintf (codefile, "}\n");
free (s);
}
break;
}
case TSetOf: {
fprintf(codefile,
"{\n"
"struct heim_octet_string *val;\n"
"size_t elen, totallen = 0;\n"
"int eret;\n");
fprintf(codefile,
"val = malloc(sizeof(val[0]) * (%s)->len);\n"
"if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
name, name);
fprintf(codefile,
"for(i = 0; i < (%s)->len; i++) {\n",
name);
fprintf(codefile,
"ASN1_MALLOC_ENCODE(%s, val[i].data, "
"val[i].length, &(%s)->val[i], &elen, eret);\n",
t->subtype->symbol->gen_name,
name);
fprintf(codefile,
"if(eret) {\n"
"i--;\n"
"while (i >= 0) {\n"
"free(val[i].data);\n"
"i--;\n"
"}\n"
"free(val);\n"
"return eret;\n"
"}\n"
"totallen += elen;\n"
"}\n");
fprintf(codefile,
"if (totallen > len) {\n"
"for (i = 0; i < (%s)->len; i++) {\n"
"free(val[i].data);\n"
"}\n"
"free(val);\n"
"return ASN1_OVERFLOW;\n"
"}\n",
name);
fprintf(codefile,
"qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
name);
fprintf (codefile,
"for(i = (%s)->len - 1; i >= 0; --i) {\n"
"p -= val[i].length;\n"
"ret += val[i].length;\n"
"memcpy(p + 1, val[i].data, val[i].length);\n"
"free(val[i].data);\n"
"}\n"
"free(val);\n"
"}\n",
name);
break;
}
case TSequenceOf: {
char *n;
char *sname;
fprintf (codefile,
"for(i = (%s)->len - 1; i >= 0; --i) {\n"
"size_t %s_for_oldret = ret;\n"
"ret = 0;\n",
name, tmpstr);
asprintf (&n, "&(%s)->val[i]", name);
if (n == NULL)
errx(1, "malloc");
asprintf (&sname, "%s_S_Of", tmpstr);
if (sname == NULL)
errx(1, "malloc");
encode_type (n, t->subtype, sname);
fprintf (codefile,
"ret += %s_for_oldret;\n"
"}\n",
tmpstr);
free (n);
free (sname);
break;
}
case TGeneralizedTime:
encode_primitive ("generalized_time", name);
constructed = 0;
break;
case TGeneralString:
encode_primitive ("general_string", name);
constructed = 0;
break;
case TTag: {
char *tname;
int c;
asprintf (&tname, "%s_tag", tmpstr);
if (tname == NULL)
errx(1, "malloc");
c = encode_type (name, t->subtype, tname);
fprintf (codefile,
"e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
"if (e) return e;\np -= l; len -= l; ret += l;\n\n",
classname(t->tag.tagclass),
c ? "CONS" : "PRIM",
valuename(t->tag.tagclass, t->tag.tagvalue));
free (tname);
break;
}
case TChoice:{
Member *m, *have_ellipsis = NULL;
char *s;
if (t->members == NULL)
break;
fprintf(codefile, "\n");
asprintf (&s, "(%s)", name);
if (s == NULL)
errx(1, "malloc");
fprintf(codefile, "switch(%s->element) {\n", s);
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
char *s2;
if (m->ellipsis) {
have_ellipsis = m;
continue;
}
fprintf (codefile, "case %s: {", m->label);
asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
s, m->gen_name);
if (s2 == NULL)
errx(1, "malloc");
if (m->optional)
fprintf (codefile, "if(%s) {\n", s2);
fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
fprintf (codefile, "ret = 0;\n");
constructed = encode_type (s2, m->type, m->gen_name);
fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
if(m->optional)
fprintf (codefile, "}\n");
fprintf(codefile, "break;\n");
fprintf(codefile, "}\n");
free (s2);
}
free (s);
if (have_ellipsis) {
fprintf(codefile,
"case %s: {\n"
"if (len < (%s)->u.%s.length)\n"
"return ASN1_OVERFLOW;\n"
"p -= (%s)->u.%s.length;\n"
"ret += (%s)->u.%s.length;\n"
"memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
"break;\n"
"}\n",
have_ellipsis->label,
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name,
name, have_ellipsis->gen_name);
}
fprintf(codefile, "};\n");
break;
}
case TOID:
encode_primitive ("oid", name);
constructed = 0;
break;
case TUTCTime:
encode_primitive ("utctime", name);
constructed = 0;
break;
case TUTF8String:
encode_primitive ("utf8string", name);
constructed = 0;
break;
case TPrintableString:
encode_primitive ("printable_string", name);
constructed = 0;
break;
case TIA5String:
encode_primitive ("ia5_string", name);
constructed = 0;
break;
case TBMPString:
encode_primitive ("bmp_string", name);
constructed = 0;
break;
case TUniversalString:
encode_primitive ("universal_string", name);
constructed = 0;
break;
case TNull:
fprintf (codefile, "/* NULL */\n");
constructed = 0;
break;
default:
abort ();
}
return constructed;
}
void
generate_type_encode (const Symbol *s)
{
fprintf (headerfile,
"int "
"encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
s->gen_name, s->gen_name);
fprintf (codefile, "int\n"
"encode_%s(unsigned char *p, size_t len,"
" const %s *data, size_t *size)\n"
"{\n",
s->gen_name, s->gen_name);
switch (s->type->type) {
case TInteger:
case TBoolean:
case TOctetString:
case TGeneralizedTime:
case TGeneralString:
case TUTCTime:
case TUTF8String:
case TPrintableString:
case TIA5String:
case TBMPString:
case TUniversalString:
case TNull:
case TBitString:
case TEnumerated:
case TOID:
case TSequence:
case TSequenceOf:
case TSet:
case TSetOf:
case TTag:
case TType:
case TChoice:
fprintf (codefile,
"size_t ret = 0;\n"
"size_t l;\n"
"int i, e;\n\n");
fprintf(codefile, "i = 0;\n"); /* hack to avoid `unused variable' */
encode_type("data", s->type, "Top");
fprintf (codefile, "*size = ret;\n"
"return 0;\n");
break;
default:
abort ();
}
fprintf (codefile, "}\n\n");
}
+191
View File
@@ -0,0 +1,191 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_free.c,v 1.16 2006/10/14 05:33:58 lha Exp $");
static void
free_primitive (const char *typename, const char *name)
{
fprintf (codefile, "der_free_%s(%s);\n", typename, name);
}
static void
free_type (const char *name, const Type *t, int preserve)
{
switch (t->type) {
case TType:
#if 0
free_type (name, t->symbol->type, preserve);
#endif
fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
break;
case TInteger:
if (t->range == NULL && t->members == NULL) {
free_primitive ("heim_integer", name);
break;
}
case TBoolean:
case TEnumerated :
case TNull:
case TGeneralizedTime:
case TUTCTime:
break;
case TBitString:
if (ASN1_TAILQ_EMPTY(t->members))
free_primitive("bit_string", name);
break;
case TOctetString:
free_primitive ("octet_string", name);
break;
case TChoice:
case TSet:
case TSequence: {
Member *m, *have_ellipsis = NULL;
if (t->members == NULL)
break;
if ((t->type == TSequence || t->type == TChoice) && preserve)
fprintf(codefile, "der_free_octet_string(&data->_save);\n");
if(t->type == TChoice)
fprintf(codefile, "switch((%s)->element) {\n", name);
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *s;
if (m->ellipsis){
have_ellipsis = m;
continue;
}
if(t->type == TChoice)
fprintf(codefile, "case %s:\n", m->label);
asprintf (&s, "%s(%s)->%s%s",
m->optional ? "" : "&", name,
t->type == TChoice ? "u." : "", m->gen_name);
if (s == NULL)
errx(1, "malloc");
if(m->optional)
fprintf(codefile, "if(%s) {\n", s);
free_type (s, m->type, FALSE);
if(m->optional)
fprintf(codefile,
"free(%s);\n"
"%s = NULL;\n"
"}\n",s, s);
free (s);
if(t->type == TChoice)
fprintf(codefile, "break;\n");
}
if(t->type == TChoice) {
if (have_ellipsis)
fprintf(codefile,
"case %s:\n"
"der_free_octet_string(&(%s)->u.%s);\n"
"break;",
have_ellipsis->label,
name, have_ellipsis->gen_name);
fprintf(codefile, "}\n");
}
break;
}
case TSetOf:
case TSequenceOf: {
char *n;
fprintf (codefile, "while((%s)->len){\n", name);
asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name);
if (n == NULL)
errx(1, "malloc");
free_type(n, t->subtype, FALSE);
fprintf(codefile,
"(%s)->len--;\n"
"}\n",
name);
fprintf(codefile,
"free((%s)->val);\n"
"(%s)->val = NULL;\n", name, name);
free(n);
break;
}
case TGeneralString:
free_primitive ("general_string", name);
break;
case TUTF8String:
free_primitive ("utf8string", name);
break;
case TPrintableString:
free_primitive ("printable_string", name);
break;
case TIA5String:
free_primitive ("ia5_string", name);
break;
case TBMPString:
free_primitive ("bmp_string", name);
break;
case TUniversalString:
free_primitive ("universal_string", name);
break;
case TTag:
free_type (name, t->subtype, preserve);
break;
case TOID :
free_primitive ("oid", name);
break;
default :
abort ();
}
}
void
generate_type_free (const Symbol *s)
{
int preserve = preserve_type(s->name) ? TRUE : FALSE;
fprintf (headerfile,
"void free_%s (%s *);\n",
s->gen_name, s->gen_name);
fprintf (codefile, "void\n"
"free_%s(%s *data)\n"
"{\n",
s->gen_name, s->gen_name);
free_type ("data", s->type, preserve);
fprintf (codefile, "}\n\n");
}
+140
View File
@@ -0,0 +1,140 @@
/*
* Copyright (c) 1997, 1999, 2000, 2003 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_glue.c,v 1.9 2005/07/12 06:27:29 lha Exp $");
static void
generate_2int (const Type *t, const char *gen_name)
{
Member *m;
fprintf (headerfile,
"unsigned %s2int(%s);\n",
gen_name, gen_name);
fprintf (codefile,
"unsigned %s2int(%s f)\n"
"{\n"
"unsigned r = 0;\n",
gen_name, gen_name);
ASN1_TAILQ_FOREACH(m, t->members, members) {
fprintf (codefile, "if(f.%s) r |= (1U << %d);\n",
m->gen_name, m->val);
}
fprintf (codefile, "return r;\n"
"}\n\n");
}
static void
generate_int2 (const Type *t, const char *gen_name)
{
Member *m;
fprintf (headerfile,
"%s int2%s(unsigned);\n",
gen_name, gen_name);
fprintf (codefile,
"%s int2%s(unsigned n)\n"
"{\n"
"\t%s flags;\n\n",
gen_name, gen_name, gen_name);
if(t->members) {
ASN1_TAILQ_FOREACH(m, t->members, members) {
fprintf (codefile, "\tflags.%s = (n >> %d) & 1;\n",
m->gen_name, m->val);
}
}
fprintf (codefile, "\treturn flags;\n"
"}\n\n");
}
/*
* This depends on the bit string being declared in increasing order
*/
static void
generate_units (const Type *t, const char *gen_name)
{
Member *m;
fprintf (headerfile,
"const struct units * asn1_%s_units(void);",
gen_name);
fprintf (codefile,
"static struct units %s_units[] = {\n",
gen_name);
if(t->members) {
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
fprintf (codefile,
"\t{\"%s\",\t1U << %d},\n", m->gen_name, m->val);
}
}
fprintf (codefile,
"\t{NULL,\t0}\n"
"};\n\n");
fprintf (codefile,
"const struct units * asn1_%s_units(void){\n"
"return %s_units;\n"
"}\n\n",
gen_name, gen_name);
}
void
generate_glue (const Type *t, const char *gen_name)
{
switch(t->type) {
case TTag:
generate_glue(t->subtype, gen_name);
break;
case TBitString :
if (!ASN1_TAILQ_EMPTY(t->members)) {
generate_2int (t, gen_name);
generate_int2 (t, gen_name);
generate_units (t, gen_name);
}
break;
default :
break;
}
}
+281
View File
@@ -0,0 +1,281 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_length.c,v 1.21 2006/10/14 05:28:28 lha Exp $");
static void
length_primitive (const char *typename,
const char *name,
const char *variable)
{
fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name);
}
static size_t
length_tag(unsigned int tag)
{
size_t len = 0;
if(tag <= 30)
return 1;
while(tag) {
tag /= 128;
len++;
}
return len + 1;
}
static int
length_type (const char *name, const Type *t,
const char *variable, const char *tmpstr)
{
switch (t->type) {
case TType:
#if 0
length_type (name, t->symbol->type);
#endif
fprintf (codefile, "%s += length_%s(%s);\n",
variable, t->symbol->gen_name, name);
break;
case TInteger:
if(t->members) {
char *s;
asprintf(&s, "(const int*)%s", name);
if(s == NULL)
errx (1, "out of memory");
length_primitive ("integer", s, variable);
free(s);
} else if (t->range == NULL) {
length_primitive ("heim_integer", name, variable);
} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
length_primitive ("integer", name, variable);
} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
length_primitive ("unsigned", name, variable);
} else if (t->range->min == 0 && t->range->max == INT_MAX) {
length_primitive ("unsigned", name, variable);
} else
errx(1, "%s: unsupported range %d -> %d",
name, t->range->min, t->range->max);
break;
case TBoolean:
fprintf (codefile, "%s += 1;\n", variable);
break;
case TEnumerated :
length_primitive ("enumerated", name, variable);
break;
case TOctetString:
length_primitive ("octet_string", name, variable);
break;
case TBitString: {
if (ASN1_TAILQ_EMPTY(t->members))
length_primitive("bit_string", name, variable);
else {
if (!rfc1510_bitstring) {
Member *m;
int pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
fprintf(codefile,
"do {\n");
ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
while (m->val / 8 < pos / 8) {
pos -= 8;
}
fprintf (codefile,
"if((%s)->%s) { %s += %d; break; }\n",
name, m->gen_name, variable, (pos + 8) / 8);
}
fprintf(codefile,
"} while(0);\n");
fprintf (codefile, "%s += 1;\n", variable);
} else {
fprintf (codefile, "%s += 5;\n", variable);
}
}
break;
}
case TSet:
case TSequence:
case TChoice: {
Member *m, *have_ellipsis = NULL;
if (t->members == NULL)
break;
if(t->type == TChoice)
fprintf (codefile, "switch((%s)->element) {\n", name);
ASN1_TAILQ_FOREACH(m, t->members, members) {
char *s;
if (m->ellipsis) {
have_ellipsis = m;
continue;
}
if(t->type == TChoice)
fprintf(codefile, "case %s:\n", m->label);
asprintf (&s, "%s(%s)->%s%s",
m->optional ? "" : "&", name,
t->type == TChoice ? "u." : "", m->gen_name);
if (s == NULL)
errx(1, "malloc");
if (m->optional)
fprintf (codefile, "if(%s)", s);
else if(m->defval)
gen_compare_defval(s + 1, m->defval);
fprintf (codefile, "{\n"
"size_t %s_oldret = %s;\n"
"%s = 0;\n", tmpstr, variable, variable);
length_type (s, m->type, "ret", m->gen_name);
fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
fprintf (codefile, "}\n");
free (s);
if(t->type == TChoice)
fprintf(codefile, "break;\n");
}
if(t->type == TChoice) {
if (have_ellipsis)
fprintf(codefile,
"case %s:\n"
"ret += (%s)->u.%s.length;\n"
"break;\n",
have_ellipsis->label,
name,
have_ellipsis->gen_name);
fprintf (codefile, "}\n"); /* switch */
}
break;
}
case TSetOf:
case TSequenceOf: {
char *n;
char *sname;
fprintf (codefile,
"{\n"
"int %s_oldret = %s;\n"
"int i;\n"
"%s = 0;\n",
tmpstr, variable, variable);
fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name);
fprintf (codefile, "int %s_for_oldret = %s;\n"
"%s = 0;\n", tmpstr, variable, variable);
asprintf (&n, "&(%s)->val[i]", name);
if (n == NULL)
errx(1, "malloc");
asprintf (&sname, "%s_S_Of", tmpstr);
if (sname == NULL)
errx(1, "malloc");
length_type(n, t->subtype, variable, sname);
fprintf (codefile, "%s += %s_for_oldret;\n",
variable, tmpstr);
fprintf (codefile, "}\n");
fprintf (codefile,
"%s += %s_oldret;\n"
"}\n", variable, tmpstr);
free(n);
free(sname);
break;
}
case TGeneralizedTime:
length_primitive ("generalized_time", name, variable);
break;
case TGeneralString:
length_primitive ("general_string", name, variable);
break;
case TUTCTime:
length_primitive ("utctime", name, variable);
break;
case TUTF8String:
length_primitive ("utf8string", name, variable);
break;
case TPrintableString:
length_primitive ("printable_string", name, variable);
break;
case TIA5String:
length_primitive ("ia5_string", name, variable);
break;
case TBMPString:
length_primitive ("bmp_string", name, variable);
break;
case TUniversalString:
length_primitive ("universal_string", name, variable);
break;
case TNull:
fprintf (codefile, "/* NULL */\n");
break;
case TTag:{
char *tname;
asprintf(&tname, "%s_tag", tmpstr);
if (tname == NULL)
errx(1, "malloc");
length_type (name, t->subtype, variable, tname);
fprintf (codefile, "ret += %lu + der_length_len (ret);\n",
(unsigned long)length_tag(t->tag.tagvalue));
free(tname);
break;
}
case TOID:
length_primitive ("oid", name, variable);
break;
default :
abort ();
}
return 0;
}
void
generate_type_length (const Symbol *s)
{
fprintf (headerfile,
"size_t length_%s(const %s *);\n",
s->gen_name, s->gen_name);
fprintf (codefile,
"size_t\n"
"length_%s(const %s *data)\n"
"{\n"
"size_t ret = 0;\n",
s->gen_name, s->gen_name);
length_type ("data", s->type, "ret", "Top");
fprintf (codefile, "return ret;\n}\n\n");
}
+89
View File
@@ -0,0 +1,89 @@
/*
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: gen_locl.h,v 1.14 2006/09/05 12:29:18 lha Exp $ */
#ifndef __GEN_LOCL_H__
#define __GEN_LOCL_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <err.h>
#include <roken.h>
#include "hash.h"
#include "symbol.h"
#include "asn1-common.h"
#include "der.h"
void generate_type (const Symbol *);
void generate_constant (const Symbol *);
void generate_type_encode (const Symbol *);
void generate_type_decode (const Symbol *);
void generate_type_free (const Symbol *);
void generate_type_length (const Symbol *);
void generate_type_copy (const Symbol *);
void generate_type_seq (const Symbol *);
void generate_glue (const Type *, const char*);
const char *classname(Der_class);
const char *valuename(Der_class, int);
void gen_compare_defval(const char *, struct value *);
void gen_assign_defval(const char *, struct value *);
void init_generate (const char *, const char *);
const char *get_filename (void);
void close_generate(void);
void add_import(const char *);
int yyparse(void);
int preserve_type(const char *);
int seq_type(const char *);
extern FILE *headerfile, *codefile, *logfile;
extern int dce_fix;
extern int rfc1510_bitstring;
extern int error_flag;
#endif /* __GEN_LOCL_H__ */
+119
View File
@@ -0,0 +1,119 @@
/*
* Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
RCSID("$Id: gen_seq.c,v 1.4 2006/10/04 10:18:10 lha Exp $");
void
generate_type_seq (const Symbol *s)
{
char *subname;
Type *type;
if (!seq_type(s->name))
return;
type = s->type;
while(type->type == TTag)
type = type->subtype;
if (type->type != TSequenceOf) {
printf("%s not seq of %d\n", s->name, (int)type->type);
return;
}
/*
* Require the subtype to be a type so we can name it and use
* copy_/free_
*/
if (type->subtype->type != TType) {
fprintf(stderr, "%s subtype is not a type, can't generate "
"sequence code for this case: %d\n",
s->name, (int)type->subtype->type);
exit(1);
}
subname = type->subtype->symbol->gen_name;
fprintf (headerfile,
"int add_%s (%s *, const %s *);\n"
"int remove_%s (%s *, unsigned int);\n",
s->gen_name, s->gen_name, subname,
s->gen_name, s->gen_name);
fprintf (codefile, "int\n"
"add_%s(%s *data, const %s *element)\n"
"{\n",
s->gen_name, s->gen_name, subname);
fprintf (codefile,
"int ret;\n"
"void *ptr;\n"
"\n"
"ptr = realloc(data->val, \n"
"\t(data->len + 1) * sizeof(data->val[0]));\n"
"if (ptr == NULL) return ENOMEM;\n"
"data->val = ptr;\n\n"
"ret = copy_%s(element, &data->val[data->len]);\n"
"if (ret) return ret;\n"
"data->len++;\n"
"return 0;\n",
subname);
fprintf (codefile, "}\n\n");
fprintf (codefile, "int\n"
"remove_%s(%s *data, unsigned int element)\n"
"{\n",
s->gen_name, s->gen_name);
fprintf (codefile,
"void *ptr;\n"
"\n"
"if (data->len == 0 || element >= data->len)\n"
"\treturn ASN1_OVERRUN;\n"
"free_%s(&data->val[element]);\n"
"data->len--;\n"
/* don't move if its the last element */
"if (element < data->len)\n"
"\tmemmove(&data->val[element], &data->val[element + 1], \n"
"\t\tsizeof(data->val[0]) * data->len);\n"
/* resize but don't care about failures since it doesn't matter */
"ptr = realloc(data->val, data->len * sizeof(data->val[0]));\n"
"if (ptr) data->val = ptr;\n"
"return 0;\n",
subname);
fprintf (codefile, "}\n\n");
}
+206
View File
@@ -0,0 +1,206 @@
/*
* Copyright (c) 1997 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Hash table functions
*/
#include "gen_locl.h"
RCSID("$Id: hash.c,v 1.11 2006/04/07 22:16:00 lha Exp $");
static Hashentry *_search(Hashtab * htab, /* The hash table */
void *ptr); /* And key */
Hashtab *
hashtabnew(int sz,
int (*cmp) (void *, void *),
unsigned (*hash) (void *))
{
Hashtab *htab;
int i;
assert(sz > 0);
htab = (Hashtab *) malloc(sizeof(Hashtab) + (sz - 1) * sizeof(Hashentry *));
if (htab == NULL)
return NULL;
for (i = 0; i < sz; ++i)
htab->tab[i] = NULL;
htab->cmp = cmp;
htab->hash = hash;
htab->sz = sz;
return htab;
}
/* Intern search function */
static Hashentry *
_search(Hashtab * htab, void *ptr)
{
Hashentry *hptr;
assert(htab && ptr);
for (hptr = htab->tab[(*htab->hash) (ptr) % htab->sz];
hptr;
hptr = hptr->next)
if ((*htab->cmp) (ptr, hptr->ptr) == 0)
break;
return hptr;
}
/* Search for element in hash table */
void *
hashtabsearch(Hashtab * htab, void *ptr)
{
Hashentry *tmp;
tmp = _search(htab, ptr);
return tmp ? tmp->ptr : tmp;
}
/* add element to hash table */
/* if already there, set new value */
/* !NULL if succesful */
void *
hashtabadd(Hashtab * htab, void *ptr)
{
Hashentry *h = _search(htab, ptr);
Hashentry **tabptr;
assert(htab && ptr);
if (h)
free((void *) h->ptr);
else {
h = (Hashentry *) malloc(sizeof(Hashentry));
if (h == NULL) {
return NULL;
}
tabptr = &htab->tab[(*htab->hash) (ptr) % htab->sz];
h->next = *tabptr;
*tabptr = h;
h->prev = tabptr;
if (h->next)
h->next->prev = &h->next;
}
h->ptr = ptr;
return h;
}
/* delete element with key key. Iff freep, free Hashentry->ptr */
int
_hashtabdel(Hashtab * htab, void *ptr, int freep)
{
Hashentry *h;
assert(htab && ptr);
h = _search(htab, ptr);
if (h) {
if (freep)
free(h->ptr);
if ((*(h->prev) = h->next))
h->next->prev = h->prev;
free(h);
return 0;
} else
return -1;
}
/* Do something for each element */
void
hashtabforeach(Hashtab * htab, int (*func) (void *ptr, void *arg),
void *arg)
{
Hashentry **h, *g;
assert(htab);
for (h = htab->tab; h < &htab->tab[htab->sz]; ++h)
for (g = *h; g; g = g->next)
if ((*func) (g->ptr, arg))
return;
}
/* standard hash-functions for strings */
unsigned
hashadd(const char *s)
{ /* Standard hash function */
unsigned i;
assert(s);
for (i = 0; *s; ++s)
i += *s;
return i;
}
unsigned
hashcaseadd(const char *s)
{ /* Standard hash function */
unsigned i;
assert(s);
for (i = 0; *s; ++s)
i += toupper((unsigned char)*s);
return i;
}
#define TWELVE (sizeof(unsigned))
#define SEVENTYFIVE (6*sizeof(unsigned))
#define HIGH_BITS (~((unsigned)(~0) >> TWELVE))
unsigned
hashjpw(const char *ss)
{ /* another hash function */
unsigned h = 0;
unsigned g;
const unsigned char *s = (const unsigned char *)ss;
for (; *s; ++s) {
h = (h << TWELVE) + *s;
if ((g = h & HIGH_BITS))
h = (h ^ (g >> SEVENTYFIVE)) & ~HIGH_BITS;
}
return h;
}
+87
View File
@@ -0,0 +1,87 @@
/*
* Copyright (c) 1997 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* hash.h. Header file for hash table functions
*/
/* $Id: hash.h,v 1.3 1999/12/02 17:05:02 joda Exp $ */
struct hashentry { /* Entry in bucket */
struct hashentry **prev;
struct hashentry *next;
void *ptr;
};
typedef struct hashentry Hashentry;
struct hashtab { /* Hash table */
int (*cmp)(void *, void *); /* Compare function */
unsigned (*hash)(void *); /* hash function */
int sz; /* Size */
Hashentry *tab[1]; /* The table */
};
typedef struct hashtab Hashtab;
/* prototypes */
Hashtab *hashtabnew(int sz,
int (*cmp)(void *, void *),
unsigned (*hash)(void *)); /* Make new hash table */
void *hashtabsearch(Hashtab *htab, /* The hash table */
void *ptr); /* The key */
void *hashtabadd(Hashtab *htab, /* The hash table */
void *ptr); /* The element */
int _hashtabdel(Hashtab *htab, /* The table */
void *ptr, /* Key */
int freep); /* Free data part? */
void hashtabforeach(Hashtab *htab,
int (*func)(void *ptr, void *arg),
void *arg);
unsigned hashadd(const char *s); /* Standard hash function */
unsigned hashcaseadd(const char *s); /* Standard hash function */
unsigned hashjpw(const char *s); /* another hash function */
/* macros */
/* Don't free space */
#define hashtabdel(htab,key) _hashtabdel(htab,key,FALSE)
#define hashtabfree(htab,key) _hashtabdel(htab,key,TRUE) /* Do! */
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2003-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef __HEIM_ANY_H__
#define __HEIM_ANY_H__ 1
int encode_heim_any(unsigned char *, size_t, const heim_any *, size_t *);
int decode_heim_any(const unsigned char *, size_t, heim_any *, size_t *);
void free_heim_any(heim_any *);
size_t length_heim_any(const heim_any *);
int copy_heim_any(const heim_any *, heim_any *);
int encode_heim_any_set(unsigned char *, size_t,
const heim_any_set *, size_t *);
int decode_heim_any_set(const unsigned char *, size_t,
heim_any_set *,size_t *);
void free_heim_any_set(heim_any_set *);
size_t length_heim_any_set(const heim_any_set *);
int copy_heim_any_set(const heim_any_set *, heim_any_set *);
int heim_any_cmp(const heim_any_set *, const heim_any_set *);
#endif /* __HEIM_ANY_H__ */
+629
View File
@@ -0,0 +1,629 @@
-- $Id: k5.asn1,v 1.50 2006/09/11 13:28:59 lha Exp $
KERBEROS5 DEFINITIONS ::=
BEGIN
NAME-TYPE ::= INTEGER {
KRB5_NT_UNKNOWN(0), -- Name type not known
KRB5_NT_PRINCIPAL(1), -- Just the name of the principal as in
KRB5_NT_SRV_INST(2), -- Service and other unique instance (krbtgt)
KRB5_NT_SRV_HST(3), -- Service with host name as instance
KRB5_NT_SRV_XHST(4), -- Service with host as remaining components
KRB5_NT_UID(5), -- Unique ID
KRB5_NT_X500_PRINCIPAL(6), -- PKINIT
KRB5_NT_SMTP_NAME(7), -- Name in form of SMTP email name
KRB5_NT_ENTERPRISE_PRINCIPAL(10), -- Windows 2000 UPN
KRB5_NT_ENT_PRINCIPAL_AND_ID(-130), -- Windows 2000 UPN and SID
KRB5_NT_MS_PRINCIPAL(-128), -- NT 4 style name
KRB5_NT_MS_PRINCIPAL_AND_ID(-129) -- NT style name and SID
}
-- message types
MESSAGE-TYPE ::= INTEGER {
krb-as-req(10), -- Request for initial authentication
krb-as-rep(11), -- Response to KRB_AS_REQ request
krb-tgs-req(12), -- Request for authentication based on TGT
krb-tgs-rep(13), -- Response to KRB_TGS_REQ request
krb-ap-req(14), -- application request to server
krb-ap-rep(15), -- Response to KRB_AP_REQ_MUTUAL
krb-safe(20), -- Safe (checksummed) application message
krb-priv(21), -- Private (encrypted) application message
krb-cred(22), -- Private (encrypted) message to forward credentials
krb-error(30) -- Error response
}
-- pa-data types
PADATA-TYPE ::= INTEGER {
KRB5-PADATA-NONE(0),
KRB5-PADATA-TGS-REQ(1),
KRB5-PADATA-AP-REQ(1),
KRB5-PADATA-ENC-TIMESTAMP(2),
KRB5-PADATA-PW-SALT(3),
KRB5-PADATA-ENC-UNIX-TIME(5),
KRB5-PADATA-SANDIA-SECUREID(6),
KRB5-PADATA-SESAME(7),
KRB5-PADATA-OSF-DCE(8),
KRB5-PADATA-CYBERSAFE-SECUREID(9),
KRB5-PADATA-AFS3-SALT(10),
KRB5-PADATA-ETYPE-INFO(11),
KRB5-PADATA-SAM-CHALLENGE(12), -- (sam/otp)
KRB5-PADATA-SAM-RESPONSE(13), -- (sam/otp)
KRB5-PADATA-PK-AS-REQ-19(14), -- (PKINIT-19)
KRB5-PADATA-PK-AS-REP-19(15), -- (PKINIT-19)
KRB5-PADATA-PK-AS-REQ-WIN(15), -- (PKINIT - old number)
KRB5-PADATA-PK-AS-REQ(16), -- (PKINIT-25)
KRB5-PADATA-PK-AS-REP(17), -- (PKINIT-25)
KRB5-PADATA-PA-PK-OCSP-RESPONSE(18),
KRB5-PADATA-ETYPE-INFO2(19),
KRB5-PADATA-USE-SPECIFIED-KVNO(20),
KRB5-PADATA-SAM-REDIRECT(21), -- (sam/otp)
KRB5-PADATA-GET-FROM-TYPED-DATA(22),
KRB5-PADATA-SAM-ETYPE-INFO(23),
KRB5-PADATA-SERVER-REFERRAL(25),
KRB5-PADATA-TD-KRB-PRINCIPAL(102), -- PrincipalName
KRB5-PADATA-PK-TD-TRUSTED-CERTIFIERS(104), -- PKINIT
KRB5-PADATA-PK-TD-CERTIFICATE-INDEX(105), -- PKINIT
KRB5-PADATA-TD-APP-DEFINED-ERROR(106), -- application specific
KRB5-PADATA-TD-REQ-NONCE(107), -- INTEGER
KRB5-PADATA-TD-REQ-SEQ(108), -- INTEGER
KRB5-PADATA-PA-PAC-REQUEST(128), -- jbrezak@exchange.microsoft.com
KRB5-PADATA-PK-AS-09-BINDING(132), -- client send this to
-- tell KDC that is supports
-- the asCheckSum in the
-- PK-AS-REP
KRB5-PADATA-S4U2SELF(-17)
}
AUTHDATA-TYPE ::= INTEGER {
KRB5-AUTHDATA-IF-RELEVANT(1),
KRB5-AUTHDATA-INTENDED-FOR_SERVER(2),
KRB5-AUTHDATA-INTENDED-FOR-APPLICATION-CLASS(3),
KRB5-AUTHDATA-KDC-ISSUED(4),
KRB5-AUTHDATA-AND-OR(5),
KRB5-AUTHDATA-MANDATORY-TICKET-EXTENSIONS(6),
KRB5-AUTHDATA-IN-TICKET-EXTENSIONS(7),
KRB5-AUTHDATA-MANDATORY-FOR-KDC(8),
KRB5-AUTHDATA-OSF-DCE(64),
KRB5-AUTHDATA-SESAME(65),
KRB5-AUTHDATA-OSF-DCE-PKI-CERTID(66),
KRB5-AUTHDATA-WIN2K-PAC(128),
KRB5-AUTHDATA-GSS-API-ETYPE-NEGOTIATION(129), -- Authenticator only
KRB5-AUTHDATA-SIGNTICKET(-17)
}
-- checksumtypes
CKSUMTYPE ::= INTEGER {
CKSUMTYPE_NONE(0),
CKSUMTYPE_CRC32(1),
CKSUMTYPE_RSA_MD4(2),
CKSUMTYPE_RSA_MD4_DES(3),
CKSUMTYPE_DES_MAC(4),
CKSUMTYPE_DES_MAC_K(5),
CKSUMTYPE_RSA_MD4_DES_K(6),
CKSUMTYPE_RSA_MD5(7),
CKSUMTYPE_RSA_MD5_DES(8),
CKSUMTYPE_RSA_MD5_DES3(9),
CKSUMTYPE_SHA1_OTHER(10),
CKSUMTYPE_HMAC_SHA1_DES3(12),
CKSUMTYPE_SHA1(14),
CKSUMTYPE_HMAC_SHA1_96_AES_128(15),
CKSUMTYPE_HMAC_SHA1_96_AES_256(16),
CKSUMTYPE_GSSAPI(0x8003),
CKSUMTYPE_HMAC_MD5(-138), -- unofficial microsoft number
CKSUMTYPE_HMAC_MD5_ENC(-1138) -- even more unofficial
}
--enctypes
ENCTYPE ::= INTEGER {
ETYPE_NULL(0),
ETYPE_DES_CBC_CRC(1),
ETYPE_DES_CBC_MD4(2),
ETYPE_DES_CBC_MD5(3),
ETYPE_DES3_CBC_MD5(5),
ETYPE_OLD_DES3_CBC_SHA1(7),
ETYPE_SIGN_DSA_GENERATE(8),
ETYPE_ENCRYPT_RSA_PRIV(9),
ETYPE_ENCRYPT_RSA_PUB(10),
ETYPE_DES3_CBC_SHA1(16), -- with key derivation
ETYPE_AES128_CTS_HMAC_SHA1_96(17),
ETYPE_AES256_CTS_HMAC_SHA1_96(18),
ETYPE_ARCFOUR_HMAC_MD5(23),
ETYPE_ARCFOUR_HMAC_MD5_56(24),
ETYPE_ENCTYPE_PK_CROSS(48),
-- these are for Heimdal internal use
ETYPE_DES_CBC_NONE(-0x1000),
ETYPE_DES3_CBC_NONE(-0x1001),
ETYPE_DES_CFB64_NONE(-0x1002),
ETYPE_DES_PCBC_NONE(-0x1003),
ETYPE_DIGEST_MD5_NONE(-0x1004), -- private use, lukeh@padl.com
ETYPE_CRAM_MD5_NONE(-0x1005) -- private use, lukeh@padl.com
}
-- this is sugar to make something ASN1 does not have: unsigned
krb5uint32 ::= INTEGER (0..4294967295)
krb5int32 ::= INTEGER (-2147483648..2147483647)
KerberosString ::= GeneralString
Realm ::= GeneralString
PrincipalName ::= SEQUENCE {
name-type[0] NAME-TYPE,
name-string[1] SEQUENCE OF GeneralString
}
-- this is not part of RFC1510
Principal ::= SEQUENCE {
name[0] PrincipalName,
realm[1] Realm
}
HostAddress ::= SEQUENCE {
addr-type[0] krb5int32,
address[1] OCTET STRING
}
-- This is from RFC1510.
--
-- HostAddresses ::= SEQUENCE OF SEQUENCE {
-- addr-type[0] krb5int32,
-- address[1] OCTET STRING
-- }
-- This seems much better.
HostAddresses ::= SEQUENCE OF HostAddress
KerberosTime ::= GeneralizedTime -- Specifying UTC time zone (Z)
AuthorizationDataElement ::= SEQUENCE {
ad-type[0] krb5int32,
ad-data[1] OCTET STRING
}
AuthorizationData ::= SEQUENCE OF AuthorizationDataElement
APOptions ::= BIT STRING {
reserved(0),
use-session-key(1),
mutual-required(2)
}
TicketFlags ::= BIT STRING {
reserved(0),
forwardable(1),
forwarded(2),
proxiable(3),
proxy(4),
may-postdate(5),
postdated(6),
invalid(7),
renewable(8),
initial(9),
pre-authent(10),
hw-authent(11),
transited-policy-checked(12),
ok-as-delegate(13),
anonymous(14)
}
KDCOptions ::= BIT STRING {
reserved(0),
forwardable(1),
forwarded(2),
proxiable(3),
proxy(4),
allow-postdate(5),
postdated(6),
unused7(7),
renewable(8),
unused9(9),
unused10(10),
unused11(11),
request-anonymous(14),
canonicalize(15),
disable-transited-check(26),
renewable-ok(27),
enc-tkt-in-skey(28),
renew(30),
validate(31)
}
LR-TYPE ::= INTEGER {
LR_NONE(0), -- no information
LR_INITIAL_TGT(1), -- last initial TGT request
LR_INITIAL(2), -- last initial request
LR_ISSUE_USE_TGT(3), -- time of newest TGT used
LR_RENEWAL(4), -- time of last renewal
LR_REQUEST(5), -- time of last request (of any type)
LR_PW_EXPTIME(6), -- expiration time of password
LR_ACCT_EXPTIME(7) -- expiration time of account
}
LastReq ::= SEQUENCE OF SEQUENCE {
lr-type[0] LR-TYPE,
lr-value[1] KerberosTime
}
EncryptedData ::= SEQUENCE {
etype[0] ENCTYPE, -- EncryptionType
kvno[1] krb5int32 OPTIONAL,
cipher[2] OCTET STRING -- ciphertext
}
EncryptionKey ::= SEQUENCE {
keytype[0] krb5int32,
keyvalue[1] OCTET STRING
}
-- encoded Transited field
TransitedEncoding ::= SEQUENCE {
tr-type[0] krb5int32, -- must be registered
contents[1] OCTET STRING
}
Ticket ::= [APPLICATION 1] SEQUENCE {
tkt-vno[0] krb5int32,
realm[1] Realm,
sname[2] PrincipalName,
enc-part[3] EncryptedData
}
-- Encrypted part of ticket
EncTicketPart ::= [APPLICATION 3] SEQUENCE {
flags[0] TicketFlags,
key[1] EncryptionKey,
crealm[2] Realm,
cname[3] PrincipalName,
transited[4] TransitedEncoding,
authtime[5] KerberosTime,
starttime[6] KerberosTime OPTIONAL,
endtime[7] KerberosTime,
renew-till[8] KerberosTime OPTIONAL,
caddr[9] HostAddresses OPTIONAL,
authorization-data[10] AuthorizationData OPTIONAL
}
Checksum ::= SEQUENCE {
cksumtype[0] CKSUMTYPE,
checksum[1] OCTET STRING
}
Authenticator ::= [APPLICATION 2] SEQUENCE {
authenticator-vno[0] krb5int32,
crealm[1] Realm,
cname[2] PrincipalName,
cksum[3] Checksum OPTIONAL,
cusec[4] krb5int32,
ctime[5] KerberosTime,
subkey[6] EncryptionKey OPTIONAL,
seq-number[7] krb5uint32 OPTIONAL,
authorization-data[8] AuthorizationData OPTIONAL
}
PA-DATA ::= SEQUENCE {
-- might be encoded AP-REQ
padata-type[1] PADATA-TYPE,
padata-value[2] OCTET STRING
}
ETYPE-INFO-ENTRY ::= SEQUENCE {
etype[0] ENCTYPE,
salt[1] OCTET STRING OPTIONAL,
salttype[2] krb5int32 OPTIONAL
}
ETYPE-INFO ::= SEQUENCE OF ETYPE-INFO-ENTRY
ETYPE-INFO2-ENTRY ::= SEQUENCE {
etype[0] ENCTYPE,
salt[1] KerberosString OPTIONAL,
s2kparams[2] OCTET STRING OPTIONAL
}
ETYPE-INFO2 ::= SEQUENCE OF ETYPE-INFO2-ENTRY
METHOD-DATA ::= SEQUENCE OF PA-DATA
TypedData ::= SEQUENCE {
data-type[0] krb5int32,
data-value[1] OCTET STRING OPTIONAL
}
TYPED-DATA ::= SEQUENCE OF TypedData
KDC-REQ-BODY ::= SEQUENCE {
kdc-options[0] KDCOptions,
cname[1] PrincipalName OPTIONAL, -- Used only in AS-REQ
realm[2] Realm, -- Server's realm
-- Also client's in AS-REQ
sname[3] PrincipalName OPTIONAL,
from[4] KerberosTime OPTIONAL,
till[5] KerberosTime OPTIONAL,
rtime[6] KerberosTime OPTIONAL,
nonce[7] krb5int32,
etype[8] SEQUENCE OF ENCTYPE, -- EncryptionType,
-- in preference order
addresses[9] HostAddresses OPTIONAL,
enc-authorization-data[10] EncryptedData OPTIONAL,
-- Encrypted AuthorizationData encoding
additional-tickets[11] SEQUENCE OF Ticket OPTIONAL
}
KDC-REQ ::= SEQUENCE {
pvno[1] krb5int32,
msg-type[2] MESSAGE-TYPE,
padata[3] METHOD-DATA OPTIONAL,
req-body[4] KDC-REQ-BODY
}
AS-REQ ::= [APPLICATION 10] KDC-REQ
TGS-REQ ::= [APPLICATION 12] KDC-REQ
-- padata-type ::= PA-ENC-TIMESTAMP
-- padata-value ::= EncryptedData - PA-ENC-TS-ENC
PA-ENC-TS-ENC ::= SEQUENCE {
patimestamp[0] KerberosTime, -- client's time
pausec[1] krb5int32 OPTIONAL
}
-- draft-brezak-win2k-krb-authz-01
PA-PAC-REQUEST ::= SEQUENCE {
include-pac[0] BOOLEAN -- Indicates whether a PAC
-- should be included or not
}
-- PacketCable provisioning server location, PKT-SP-SEC-I09-030728.pdf
PROV-SRV-LOCATION ::= GeneralString
KDC-REP ::= SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
padata[2] METHOD-DATA OPTIONAL,
crealm[3] Realm,
cname[4] PrincipalName,
ticket[5] Ticket,
enc-part[6] EncryptedData
}
AS-REP ::= [APPLICATION 11] KDC-REP
TGS-REP ::= [APPLICATION 13] KDC-REP
EncKDCRepPart ::= SEQUENCE {
key[0] EncryptionKey,
last-req[1] LastReq,
nonce[2] krb5int32,
key-expiration[3] KerberosTime OPTIONAL,
flags[4] TicketFlags,
authtime[5] KerberosTime,
starttime[6] KerberosTime OPTIONAL,
endtime[7] KerberosTime,
renew-till[8] KerberosTime OPTIONAL,
srealm[9] Realm,
sname[10] PrincipalName,
caddr[11] HostAddresses OPTIONAL
}
EncASRepPart ::= [APPLICATION 25] EncKDCRepPart
EncTGSRepPart ::= [APPLICATION 26] EncKDCRepPart
AP-REQ ::= [APPLICATION 14] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
ap-options[2] APOptions,
ticket[3] Ticket,
authenticator[4] EncryptedData
}
AP-REP ::= [APPLICATION 15] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
enc-part[2] EncryptedData
}
EncAPRepPart ::= [APPLICATION 27] SEQUENCE {
ctime[0] KerberosTime,
cusec[1] krb5int32,
subkey[2] EncryptionKey OPTIONAL,
seq-number[3] krb5uint32 OPTIONAL
}
KRB-SAFE-BODY ::= SEQUENCE {
user-data[0] OCTET STRING,
timestamp[1] KerberosTime OPTIONAL,
usec[2] krb5int32 OPTIONAL,
seq-number[3] krb5uint32 OPTIONAL,
s-address[4] HostAddress OPTIONAL,
r-address[5] HostAddress OPTIONAL
}
KRB-SAFE ::= [APPLICATION 20] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
safe-body[2] KRB-SAFE-BODY,
cksum[3] Checksum
}
KRB-PRIV ::= [APPLICATION 21] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
enc-part[3] EncryptedData
}
EncKrbPrivPart ::= [APPLICATION 28] SEQUENCE {
user-data[0] OCTET STRING,
timestamp[1] KerberosTime OPTIONAL,
usec[2] krb5int32 OPTIONAL,
seq-number[3] krb5uint32 OPTIONAL,
s-address[4] HostAddress OPTIONAL, -- sender's addr
r-address[5] HostAddress OPTIONAL -- recip's addr
}
KRB-CRED ::= [APPLICATION 22] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE, -- KRB_CRED
tickets[2] SEQUENCE OF Ticket,
enc-part[3] EncryptedData
}
KrbCredInfo ::= SEQUENCE {
key[0] EncryptionKey,
prealm[1] Realm OPTIONAL,
pname[2] PrincipalName OPTIONAL,
flags[3] TicketFlags OPTIONAL,
authtime[4] KerberosTime OPTIONAL,
starttime[5] KerberosTime OPTIONAL,
endtime[6] KerberosTime OPTIONAL,
renew-till[7] KerberosTime OPTIONAL,
srealm[8] Realm OPTIONAL,
sname[9] PrincipalName OPTIONAL,
caddr[10] HostAddresses OPTIONAL
}
EncKrbCredPart ::= [APPLICATION 29] SEQUENCE {
ticket-info[0] SEQUENCE OF KrbCredInfo,
nonce[1] krb5int32 OPTIONAL,
timestamp[2] KerberosTime OPTIONAL,
usec[3] krb5int32 OPTIONAL,
s-address[4] HostAddress OPTIONAL,
r-address[5] HostAddress OPTIONAL
}
KRB-ERROR ::= [APPLICATION 30] SEQUENCE {
pvno[0] krb5int32,
msg-type[1] MESSAGE-TYPE,
ctime[2] KerberosTime OPTIONAL,
cusec[3] krb5int32 OPTIONAL,
stime[4] KerberosTime,
susec[5] krb5int32,
error-code[6] krb5int32,
crealm[7] Realm OPTIONAL,
cname[8] PrincipalName OPTIONAL,
realm[9] Realm, -- Correct realm
sname[10] PrincipalName, -- Correct name
e-text[11] GeneralString OPTIONAL,
e-data[12] OCTET STRING OPTIONAL
}
ChangePasswdDataMS ::= SEQUENCE {
newpasswd[0] OCTET STRING,
targname[1] PrincipalName OPTIONAL,
targrealm[2] Realm OPTIONAL
}
EtypeList ::= SEQUENCE OF krb5int32
-- the client's proposed enctype list in
-- decreasing preference order, favorite choice first
krb5-pvno krb5int32 ::= 5 -- current Kerberos protocol version number
-- transited encodings
DOMAIN-X500-COMPRESS krb5int32 ::= 1
-- authorization data primitives
AD-IF-RELEVANT ::= AuthorizationData
AD-KDCIssued ::= SEQUENCE {
ad-checksum[0] Checksum,
i-realm[1] Realm OPTIONAL,
i-sname[2] PrincipalName OPTIONAL,
elements[3] AuthorizationData
}
AD-AND-OR ::= SEQUENCE {
condition-count[0] INTEGER,
elements[1] AuthorizationData
}
AD-MANDATORY-FOR-KDC ::= AuthorizationData
-- PA-SAM-RESPONSE-2/PA-SAM-RESPONSE-2
PA-SAM-TYPE ::= INTEGER {
PA_SAM_TYPE_ENIGMA(1), -- Enigma Logic
PA_SAM_TYPE_DIGI_PATH(2), -- Digital Pathways
PA_SAM_TYPE_SKEY_K0(3), -- S/key where KDC has key 0
PA_SAM_TYPE_SKEY(4), -- Traditional S/Key
PA_SAM_TYPE_SECURID(5), -- Security Dynamics
PA_SAM_TYPE_CRYPTOCARD(6) -- CRYPTOCard
}
PA-SAM-REDIRECT ::= HostAddresses
SAMFlags ::= BIT STRING {
use-sad-as-key(0),
send-encrypted-sad(1),
must-pk-encrypt-sad(2)
}
PA-SAM-CHALLENGE-2-BODY ::= SEQUENCE {
sam-type[0] krb5int32,
sam-flags[1] SAMFlags,
sam-type-name[2] GeneralString OPTIONAL,
sam-track-id[3] GeneralString OPTIONAL,
sam-challenge-label[4] GeneralString OPTIONAL,
sam-challenge[5] GeneralString OPTIONAL,
sam-response-prompt[6] GeneralString OPTIONAL,
sam-pk-for-sad[7] EncryptionKey OPTIONAL,
sam-nonce[8] krb5int32,
sam-etype[9] krb5int32,
...
}
PA-SAM-CHALLENGE-2 ::= SEQUENCE {
sam-body[0] PA-SAM-CHALLENGE-2-BODY,
sam-cksum[1] SEQUENCE OF Checksum, -- (1..MAX)
...
}
PA-SAM-RESPONSE-2 ::= SEQUENCE {
sam-type[0] krb5int32,
sam-flags[1] SAMFlags,
sam-track-id[2] GeneralString OPTIONAL,
sam-enc-nonce-or-sad[3] EncryptedData, -- PA-ENC-SAM-RESPONSE-ENC
sam-nonce[4] krb5int32,
...
}
PA-ENC-SAM-RESPONSE-ENC ::= SEQUENCE {
sam-nonce[0] krb5int32,
sam-sad[1] GeneralString OPTIONAL,
...
}
PA-S4U2Self ::= SEQUENCE {
name[0] PrincipalName,
realm[1] Realm,
cksum[2] Checksum,
auth[3] GeneralString
}
KRB5SignedPathPrincipals ::= SEQUENCE OF Principal
-- never encoded on the wire, just used to checksum over
KRB5SignedPathData ::= SEQUENCE {
encticket[0] EncTicketPart,
delegated[1] KRB5SignedPathPrincipals OPTIONAL
}
KRB5SignedPath ::= SEQUENCE {
-- DERcoded KRB5SignedPathData
-- krbtgt key (etype), KeyUsage = XXX
etype[0] ENCTYPE,
cksum[1] Checksum,
-- srvs delegated though
delegated[2] KRB5SignedPathPrincipals OPTIONAL
}
END
-- etags -r '/\([A-Za-z][-A-Za-z0-9]*\).*::=/\1/' k5.asn1
File diff suppressed because it is too large Load Diff
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: lex.h,v 1.6 2005/07/12 06:27:33 lha Exp $ */
#include <roken.h>
void error_message (const char *, ...)
__attribute__ ((format (printf, 1, 2)));
extern int error_flag;
int yylex(void);
+294
View File
@@ -0,0 +1,294 @@
%{
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: lex.l,v 1.27 2005/09/13 18:17:16 lha Exp $ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#undef ECHO
#include "symbol.h"
#include "parse.h"
#include "lex.h"
#include "gen_locl.h"
static unsigned lineno = 1;
#undef ECHO
static void unterminated(const char *, unsigned);
%}
%%
ABSENT { return kw_ABSENT; }
ABSTRACT-SYNTAX { return kw_ABSTRACT_SYNTAX; }
ALL { return kw_ALL; }
APPLICATION { return kw_APPLICATION; }
AUTOMATIC { return kw_AUTOMATIC; }
BEGIN { return kw_BEGIN; }
BIT { return kw_BIT; }
BMPString { return kw_BMPString; }
BOOLEAN { return kw_BOOLEAN; }
BY { return kw_BY; }
CHARACTER { return kw_CHARACTER; }
CHOICE { return kw_CHOICE; }
CLASS { return kw_CLASS; }
COMPONENT { return kw_COMPONENT; }
COMPONENTS { return kw_COMPONENTS; }
CONSTRAINED { return kw_CONSTRAINED; }
CONTAINING { return kw_CONTAINING; }
DEFAULT { return kw_DEFAULT; }
DEFINITIONS { return kw_DEFINITIONS; }
EMBEDDED { return kw_EMBEDDED; }
ENCODED { return kw_ENCODED; }
END { return kw_END; }
ENUMERATED { return kw_ENUMERATED; }
EXCEPT { return kw_EXCEPT; }
EXPLICIT { return kw_EXPLICIT; }
EXPORTS { return kw_EXPORTS; }
EXTENSIBILITY { return kw_EXTENSIBILITY; }
EXTERNAL { return kw_EXTERNAL; }
FALSE { return kw_FALSE; }
FROM { return kw_FROM; }
GeneralString { return kw_GeneralString; }
GeneralizedTime { return kw_GeneralizedTime; }
GraphicString { return kw_GraphicString; }
IA5String { return kw_IA5String; }
IDENTIFIER { return kw_IDENTIFIER; }
IMPLICIT { return kw_IMPLICIT; }
IMPLIED { return kw_IMPLIED; }
IMPORTS { return kw_IMPORTS; }
INCLUDES { return kw_INCLUDES; }
INSTANCE { return kw_INSTANCE; }
INTEGER { return kw_INTEGER; }
INTERSECTION { return kw_INTERSECTION; }
ISO646String { return kw_ISO646String; }
MAX { return kw_MAX; }
MIN { return kw_MIN; }
MINUS-INFINITY { return kw_MINUS_INFINITY; }
NULL { return kw_NULL; }
NumericString { return kw_NumericString; }
OBJECT { return kw_OBJECT; }
OCTET { return kw_OCTET; }
OF { return kw_OF; }
OPTIONAL { return kw_OPTIONAL; }
ObjectDescriptor { return kw_ObjectDescriptor; }
PATTERN { return kw_PATTERN; }
PDV { return kw_PDV; }
PLUS-INFINITY { return kw_PLUS_INFINITY; }
PRESENT { return kw_PRESENT; }
PRIVATE { return kw_PRIVATE; }
PrintableString { return kw_PrintableString; }
REAL { return kw_REAL; }
RELATIVE_OID { return kw_RELATIVE_OID; }
SEQUENCE { return kw_SEQUENCE; }
SET { return kw_SET; }
SIZE { return kw_SIZE; }
STRING { return kw_STRING; }
SYNTAX { return kw_SYNTAX; }
T61String { return kw_T61String; }
TAGS { return kw_TAGS; }
TRUE { return kw_TRUE; }
TYPE-IDENTIFIER { return kw_TYPE_IDENTIFIER; }
TeletexString { return kw_TeletexString; }
UNION { return kw_UNION; }
UNIQUE { return kw_UNIQUE; }
UNIVERSAL { return kw_UNIVERSAL; }
UTCTime { return kw_UTCTime; }
UTF8String { return kw_UTF8String; }
UniversalString { return kw_UniversalString; }
VideotexString { return kw_VideotexString; }
VisibleString { return kw_VisibleString; }
WITH { return kw_WITH; }
[-,;{}()|] { return *yytext; }
"[" { return *yytext; }
"]" { return *yytext; }
::= { return EEQUAL; }
-- {
int c, start_lineno = lineno;
int f = 0;
while((c = input()) != EOF) {
if(f && c == '-')
break;
if(c == '-') {
f = 1;
continue;
}
if(c == '\n') {
lineno++;
break;
}
f = 0;
}
if(c == EOF)
unterminated("comment", start_lineno);
}
\/\* {
int c, start_lineno = lineno;
int level = 1;
int seen_star = 0;
int seen_slash = 0;
while((c = input()) != EOF) {
if(c == '/') {
if(seen_star) {
if(--level == 0)
break;
seen_star = 0;
continue;
}
seen_slash = 1;
continue;
}
if(seen_star && c == '/') {
if(--level == 0)
break;
seen_star = 0;
continue;
}
if(c == '*') {
if(seen_slash) {
level++;
seen_star = seen_slash = 0;
continue;
}
seen_star = 1;
continue;
}
seen_star = seen_slash = 0;
if(c == '\n') {
lineno++;
continue;
}
}
if(c == EOF)
unterminated("comment", start_lineno);
}
"\"" {
int start_lineno = lineno;
int c;
char buf[1024];
char *p = buf;
int f = 0;
int skip_ws = 0;
while((c = input()) != EOF) {
if(isspace(c) && skip_ws) {
if(c == '\n')
lineno++;
continue;
}
skip_ws = 0;
if(c == '"') {
if(f) {
*p++ = '"';
f = 0;
} else
f = 1;
continue;
}
if(f == 1) {
unput(c);
break;
}
if(c == '\n') {
lineno++;
while(p > buf && isspace((unsigned char)p[-1]))
p--;
skip_ws = 1;
continue;
}
*p++ = c;
}
if(c == EOF)
unterminated("string", start_lineno);
*p++ = '\0';
fprintf(stderr, "string -- %s\n", buf);
yylval.name = estrdup(buf);
return STRING;
}
-?0x[0-9A-Fa-f]+|-?[0-9]+ { char *e, *y = yytext;
yylval.constant = strtol((const char *)yytext,
&e, 0);
if(e == y)
error_message("malformed constant (%s)", yytext);
else
return NUMBER;
}
[A-Za-z][-A-Za-z0-9_]* {
yylval.name = estrdup ((const char *)yytext);
return IDENTIFIER;
}
[ \t] ;
\n { ++lineno; }
\.\.\. { return ELLIPSIS; }
\.\. { return RANGE; }
. { error_message("Ignoring char(%c)\n", *yytext); }
%%
#ifndef yywrap /* XXX */
int
yywrap ()
{
return 1;
}
#endif
void
error_message (const char *format, ...)
{
va_list args;
va_start (args, format);
fprintf (stderr, "%s:%d: ", get_filename(), lineno);
vfprintf (stderr, format, args);
va_end (args);
error_flag++;
}
static void
unterminated(const char *type, unsigned start_lineno)
{
error_message("unterminated %s, possibly started on line %d\n", type, start_lineno);
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: libasn1.h,v 1.11 2005/07/12 06:27:34 lha Exp $ */
#ifndef __LIBASN1_H__
#define __LIBASN1_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "krb5_asn1.h"
#include "der.h"
#include "asn1_err.h"
#include <parse_units.h>
#endif /* __LIBASN1_H__ */
+131
View File
@@ -0,0 +1,131 @@
/*
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
#include <getarg.h>
#include "lex.h"
RCSID("$Id: main.c,v 1.16 2006/09/05 12:27:29 lha Exp $");
extern FILE *yyin;
static getarg_strings preserve;
static getarg_strings seq;
int
preserve_type(const char *p)
{
int i;
for (i = 0; i < preserve.num_strings; i++)
if (strcmp(preserve.strings[i], p) == 0)
return 1;
return 0;
}
int
seq_type(const char *p)
{
int i;
for (i = 0; i < seq.num_strings; i++)
if (strcmp(seq.strings[i], p) == 0)
return 1;
return 0;
}
int dce_fix;
int rfc1510_bitstring;
int version_flag;
int help_flag;
struct getargs args[] = {
{ "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring },
{ "decode-dce-ber", 0, arg_flag, &dce_fix },
{ "preserve-binary", 0, arg_strings, &preserve },
{ "sequence", 0, arg_strings, &seq },
{ "version", 0, arg_flag, &version_flag },
{ "help", 0, arg_flag, &help_flag }
};
int num_args = sizeof(args) / sizeof(args[0]);
static void
usage(int code)
{
arg_printusage(args, num_args, NULL, "[asn1-file [name]]");
exit(code);
}
int error_flag;
int
main(int argc, char **argv)
{
int ret;
const char *file;
const char *name = NULL;
int optidx = 0;
setprogname(argv[0]);
if(getarg(args, num_args, argc, argv, &optidx))
usage(1);
if(help_flag)
usage(0);
if(version_flag) {
print_version(NULL);
exit(0);
}
if (argc == optidx) {
file = "stdin";
name = "stdin";
yyin = stdin;
} else {
file = argv[optidx];
yyin = fopen (file, "r");
if (yyin == NULL)
err (1, "open %s", file);
if (argc == optidx + 1) {
char *p;
name = estrdup(file);
p = strrchr(name, '.');
if (p)
*p = '\0';
} else
name = argv[optidx + 1];
}
init_generate (file, name);
initsym ();
ret = yyparse ();
if(ret != 0 || error_flag != 0)
exit(1);
close_generate ();
return 0;
}
File diff suppressed because it is too large Load Diff
+239
View File
@@ -0,0 +1,239 @@
/* A Bison parser, made by GNU Bison 2.1. */
/* Skeleton parser for Yacc-like parsing with Bison,
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, when this file is copied by Bison into a
Bison output file, you may use that output file without restriction.
This special exception was added by the Free Software Foundation
in version 1.24 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
kw_ABSENT = 258,
kw_ABSTRACT_SYNTAX = 259,
kw_ALL = 260,
kw_APPLICATION = 261,
kw_AUTOMATIC = 262,
kw_BEGIN = 263,
kw_BIT = 264,
kw_BMPString = 265,
kw_BOOLEAN = 266,
kw_BY = 267,
kw_CHARACTER = 268,
kw_CHOICE = 269,
kw_CLASS = 270,
kw_COMPONENT = 271,
kw_COMPONENTS = 272,
kw_CONSTRAINED = 273,
kw_CONTAINING = 274,
kw_DEFAULT = 275,
kw_DEFINITIONS = 276,
kw_EMBEDDED = 277,
kw_ENCODED = 278,
kw_END = 279,
kw_ENUMERATED = 280,
kw_EXCEPT = 281,
kw_EXPLICIT = 282,
kw_EXPORTS = 283,
kw_EXTENSIBILITY = 284,
kw_EXTERNAL = 285,
kw_FALSE = 286,
kw_FROM = 287,
kw_GeneralString = 288,
kw_GeneralizedTime = 289,
kw_GraphicString = 290,
kw_IA5String = 291,
kw_IDENTIFIER = 292,
kw_IMPLICIT = 293,
kw_IMPLIED = 294,
kw_IMPORTS = 295,
kw_INCLUDES = 296,
kw_INSTANCE = 297,
kw_INTEGER = 298,
kw_INTERSECTION = 299,
kw_ISO646String = 300,
kw_MAX = 301,
kw_MIN = 302,
kw_MINUS_INFINITY = 303,
kw_NULL = 304,
kw_NumericString = 305,
kw_OBJECT = 306,
kw_OCTET = 307,
kw_OF = 308,
kw_OPTIONAL = 309,
kw_ObjectDescriptor = 310,
kw_PATTERN = 311,
kw_PDV = 312,
kw_PLUS_INFINITY = 313,
kw_PRESENT = 314,
kw_PRIVATE = 315,
kw_PrintableString = 316,
kw_REAL = 317,
kw_RELATIVE_OID = 318,
kw_SEQUENCE = 319,
kw_SET = 320,
kw_SIZE = 321,
kw_STRING = 322,
kw_SYNTAX = 323,
kw_T61String = 324,
kw_TAGS = 325,
kw_TRUE = 326,
kw_TYPE_IDENTIFIER = 327,
kw_TeletexString = 328,
kw_UNION = 329,
kw_UNIQUE = 330,
kw_UNIVERSAL = 331,
kw_UTCTime = 332,
kw_UTF8String = 333,
kw_UniversalString = 334,
kw_VideotexString = 335,
kw_VisibleString = 336,
kw_WITH = 337,
RANGE = 338,
EEQUAL = 339,
ELLIPSIS = 340,
IDENTIFIER = 341,
referencename = 342,
STRING = 343,
NUMBER = 344
};
#endif
/* Tokens. */
#define kw_ABSENT 258
#define kw_ABSTRACT_SYNTAX 259
#define kw_ALL 260
#define kw_APPLICATION 261
#define kw_AUTOMATIC 262
#define kw_BEGIN 263
#define kw_BIT 264
#define kw_BMPString 265
#define kw_BOOLEAN 266
#define kw_BY 267
#define kw_CHARACTER 268
#define kw_CHOICE 269
#define kw_CLASS 270
#define kw_COMPONENT 271
#define kw_COMPONENTS 272
#define kw_CONSTRAINED 273
#define kw_CONTAINING 274
#define kw_DEFAULT 275
#define kw_DEFINITIONS 276
#define kw_EMBEDDED 277
#define kw_ENCODED 278
#define kw_END 279
#define kw_ENUMERATED 280
#define kw_EXCEPT 281
#define kw_EXPLICIT 282
#define kw_EXPORTS 283
#define kw_EXTENSIBILITY 284
#define kw_EXTERNAL 285
#define kw_FALSE 286
#define kw_FROM 287
#define kw_GeneralString 288
#define kw_GeneralizedTime 289
#define kw_GraphicString 290
#define kw_IA5String 291
#define kw_IDENTIFIER 292
#define kw_IMPLICIT 293
#define kw_IMPLIED 294
#define kw_IMPORTS 295
#define kw_INCLUDES 296
#define kw_INSTANCE 297
#define kw_INTEGER 298
#define kw_INTERSECTION 299
#define kw_ISO646String 300
#define kw_MAX 301
#define kw_MIN 302
#define kw_MINUS_INFINITY 303
#define kw_NULL 304
#define kw_NumericString 305
#define kw_OBJECT 306
#define kw_OCTET 307
#define kw_OF 308
#define kw_OPTIONAL 309
#define kw_ObjectDescriptor 310
#define kw_PATTERN 311
#define kw_PDV 312
#define kw_PLUS_INFINITY 313
#define kw_PRESENT 314
#define kw_PRIVATE 315
#define kw_PrintableString 316
#define kw_REAL 317
#define kw_RELATIVE_OID 318
#define kw_SEQUENCE 319
#define kw_SET 320
#define kw_SIZE 321
#define kw_STRING 322
#define kw_SYNTAX 323
#define kw_T61String 324
#define kw_TAGS 325
#define kw_TRUE 326
#define kw_TYPE_IDENTIFIER 327
#define kw_TeletexString 328
#define kw_UNION 329
#define kw_UNIQUE 330
#define kw_UNIVERSAL 331
#define kw_UTCTime 332
#define kw_UTF8String 333
#define kw_UniversalString 334
#define kw_VideotexString 335
#define kw_VisibleString 336
#define kw_WITH 337
#define RANGE 338
#define EEQUAL 339
#define ELLIPSIS 340
#define IDENTIFIER 341
#define referencename 342
#define STRING 343
#define NUMBER 344
#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
#line 65 "parse.y"
typedef union YYSTYPE {
int constant;
struct value *value;
struct range range;
char *name;
Type *type;
Member *member;
struct objid *objid;
char *defval;
struct string_list *sl;
struct tagtype tag;
struct memhead *members;
struct constraint_spec *constraint_spec;
} YYSTYPE;
/* Line 1447 of yacc.c. */
#line 231 "parse.h"
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif
extern YYSTYPE yylval;
+970
View File
@@ -0,0 +1,970 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: parse.y,v 1.27 2005/12/14 09:44:36 lha Exp $ */
%{
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbol.h"
#include "lex.h"
#include "gen_locl.h"
#include "der.h"
RCSID("$Id: parse.y,v 1.27 2005/12/14 09:44:36 lha Exp $");
static Type *new_type (Typetype t);
static struct constraint_spec *new_constraint_spec(enum ctype);
static Type *new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype);
void yyerror (const char *);
static struct objid *new_objid(const char *label, int value);
static void add_oid_to_tail(struct objid *, struct objid *);
static void fix_labels(Symbol *s);
struct string_list {
char *string;
struct string_list *next;
};
%}
%union {
int constant;
struct value *value;
struct range range;
char *name;
Type *type;
Member *member;
struct objid *objid;
char *defval;
struct string_list *sl;
struct tagtype tag;
struct memhead *members;
struct constraint_spec *constraint_spec;
}
%token kw_ABSENT
%token kw_ABSTRACT_SYNTAX
%token kw_ALL
%token kw_APPLICATION
%token kw_AUTOMATIC
%token kw_BEGIN
%token kw_BIT
%token kw_BMPString
%token kw_BOOLEAN
%token kw_BY
%token kw_CHARACTER
%token kw_CHOICE
%token kw_CLASS
%token kw_COMPONENT
%token kw_COMPONENTS
%token kw_CONSTRAINED
%token kw_CONTAINING
%token kw_DEFAULT
%token kw_DEFINITIONS
%token kw_EMBEDDED
%token kw_ENCODED
%token kw_END
%token kw_ENUMERATED
%token kw_EXCEPT
%token kw_EXPLICIT
%token kw_EXPORTS
%token kw_EXTENSIBILITY
%token kw_EXTERNAL
%token kw_FALSE
%token kw_FROM
%token kw_GeneralString
%token kw_GeneralizedTime
%token kw_GraphicString
%token kw_IA5String
%token kw_IDENTIFIER
%token kw_IMPLICIT
%token kw_IMPLIED
%token kw_IMPORTS
%token kw_INCLUDES
%token kw_INSTANCE
%token kw_INTEGER
%token kw_INTERSECTION
%token kw_ISO646String
%token kw_MAX
%token kw_MIN
%token kw_MINUS_INFINITY
%token kw_NULL
%token kw_NumericString
%token kw_OBJECT
%token kw_OCTET
%token kw_OF
%token kw_OPTIONAL
%token kw_ObjectDescriptor
%token kw_PATTERN
%token kw_PDV
%token kw_PLUS_INFINITY
%token kw_PRESENT
%token kw_PRIVATE
%token kw_PrintableString
%token kw_REAL
%token kw_RELATIVE_OID
%token kw_SEQUENCE
%token kw_SET
%token kw_SIZE
%token kw_STRING
%token kw_SYNTAX
%token kw_T61String
%token kw_TAGS
%token kw_TRUE
%token kw_TYPE_IDENTIFIER
%token kw_TeletexString
%token kw_UNION
%token kw_UNIQUE
%token kw_UNIVERSAL
%token kw_UTCTime
%token kw_UTF8String
%token kw_UniversalString
%token kw_VideotexString
%token kw_VisibleString
%token kw_WITH
%token RANGE
%token EEQUAL
%token ELLIPSIS
%token <name> IDENTIFIER referencename
%token <name> STRING
%token <constant> NUMBER
%type <constant> SignedNumber
%type <constant> Class tagenv
%type <value> Value
%type <value> BuiltinValue
%type <value> IntegerValue
%type <value> BooleanValue
%type <value> ObjectIdentifierValue
%type <value> CharacterStringValue
%type <value> NullValue
%type <value> DefinedValue
%type <value> ReferencedValue
%type <value> Valuereference
%type <type> Type
%type <type> BuiltinType
%type <type> BitStringType
%type <type> BooleanType
%type <type> ChoiceType
%type <type> ConstrainedType
%type <type> EnumeratedType
%type <type> IntegerType
%type <type> NullType
%type <type> OctetStringType
%type <type> SequenceType
%type <type> SequenceOfType
%type <type> SetType
%type <type> SetOfType
%type <type> TaggedType
%type <type> ReferencedType
%type <type> DefinedType
%type <type> UsefulType
%type <type> ObjectIdentifierType
%type <type> CharacterStringType
%type <type> RestrictedCharactedStringType
%type <tag> Tag
%type <member> ComponentType
%type <member> NamedBit
%type <member> NamedNumber
%type <member> NamedType
%type <members> ComponentTypeList
%type <members> Enumerations
%type <members> NamedBitList
%type <members> NamedNumberList
%type <objid> objid objid_list objid_element objid_opt
%type <range> range
%type <sl> referencenames
%type <constraint_spec> Constraint
%type <constraint_spec> ConstraintSpec
%type <constraint_spec> GeneralConstraint
%type <constraint_spec> ContentsConstraint
%type <constraint_spec> UserDefinedConstraint
%start ModuleDefinition
%%
ModuleDefinition: IDENTIFIER kw_DEFINITIONS TagDefault ExtensionDefault
EEQUAL kw_BEGIN ModuleBody kw_END
{
checkundefined();
}
;
TagDefault : kw_EXPLICIT kw_TAGS
| kw_IMPLICIT kw_TAGS
{ error_message("implicit tagging is not supported"); }
| kw_AUTOMATIC kw_TAGS
{ error_message("automatic tagging is not supported"); }
| /* empty */
;
ExtensionDefault: kw_EXTENSIBILITY kw_IMPLIED
{ error_message("no extensibility options supported"); }
| /* empty */
;
ModuleBody : /* Exports */ Imports AssignmentList
| /* empty */
;
Imports : kw_IMPORTS SymbolsImported ';'
| /* empty */
;
SymbolsImported : SymbolsFromModuleList
| /* empty */
;
SymbolsFromModuleList: SymbolsFromModule
| SymbolsFromModuleList SymbolsFromModule
;
SymbolsFromModule: referencenames kw_FROM IDENTIFIER objid_opt
{
struct string_list *sl;
for(sl = $1; sl != NULL; sl = sl->next) {
Symbol *s = addsym(sl->string);
s->stype = Stype;
}
add_import($3);
}
;
AssignmentList : Assignment
| Assignment AssignmentList
;
Assignment : TypeAssignment
| ValueAssignment
;
referencenames : IDENTIFIER ',' referencenames
{
$$ = emalloc(sizeof(*$$));
$$->string = $1;
$$->next = $3;
}
| IDENTIFIER
{
$$ = emalloc(sizeof(*$$));
$$->string = $1;
$$->next = NULL;
}
;
TypeAssignment : IDENTIFIER EEQUAL Type
{
Symbol *s = addsym ($1);
s->stype = Stype;
s->type = $3;
fix_labels(s);
generate_type (s);
}
;
Type : BuiltinType
| ReferencedType
| ConstrainedType
;
BuiltinType : BitStringType
| BooleanType
| CharacterStringType
| ChoiceType
| EnumeratedType
| IntegerType
| NullType
| ObjectIdentifierType
| OctetStringType
| SequenceType
| SequenceOfType
| SetType
| SetOfType
| TaggedType
;
BooleanType : kw_BOOLEAN
{
$$ = new_tag(ASN1_C_UNIV, UT_Boolean,
TE_EXPLICIT, new_type(TBoolean));
}
;
range : '(' Value RANGE Value ')'
{
if($2->type != integervalue ||
$4->type != integervalue)
error_message("Non-integer value used in range");
$$.min = $2->u.integervalue;
$$.max = $4->u.integervalue;
}
;
IntegerType : kw_INTEGER
{
$$ = new_tag(ASN1_C_UNIV, UT_Integer,
TE_EXPLICIT, new_type(TInteger));
}
| kw_INTEGER range
{
$$ = new_type(TInteger);
$$->range = emalloc(sizeof(*$$->range));
*($$->range) = $2;
$$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$);
}
| kw_INTEGER '{' NamedNumberList '}'
{
$$ = new_type(TInteger);
$$->members = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$);
}
;
NamedNumberList : NamedNumber
{
$$ = emalloc(sizeof(*$$));
ASN1_TAILQ_INIT($$);
ASN1_TAILQ_INSERT_HEAD($$, $1, members);
}
| NamedNumberList ',' NamedNumber
{
ASN1_TAILQ_INSERT_TAIL($1, $3, members);
$$ = $1;
}
| NamedNumberList ',' ELLIPSIS
{ $$ = $1; } /* XXX used for Enumerations */
;
NamedNumber : IDENTIFIER '(' SignedNumber ')'
{
$$ = emalloc(sizeof(*$$));
$$->name = $1;
$$->gen_name = estrdup($1);
output_name ($$->gen_name);
$$->val = $3;
$$->optional = 0;
$$->ellipsis = 0;
$$->type = NULL;
}
;
EnumeratedType : kw_ENUMERATED '{' Enumerations '}'
{
$$ = new_type(TInteger);
$$->members = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Enumerated, TE_EXPLICIT, $$);
}
;
Enumerations : NamedNumberList /* XXX */
;
BitStringType : kw_BIT kw_STRING
{
$$ = new_type(TBitString);
$$->members = emalloc(sizeof(*$$->members));
ASN1_TAILQ_INIT($$->members);
$$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$);
}
| kw_BIT kw_STRING '{' NamedBitList '}'
{
$$ = new_type(TBitString);
$$->members = $4;
$$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$);
}
;
ObjectIdentifierType: kw_OBJECT kw_IDENTIFIER
{
$$ = new_tag(ASN1_C_UNIV, UT_OID,
TE_EXPLICIT, new_type(TOID));
}
;
OctetStringType : kw_OCTET kw_STRING
{
$$ = new_tag(ASN1_C_UNIV, UT_OctetString,
TE_EXPLICIT, new_type(TOctetString));
}
;
NullType : kw_NULL
{
$$ = new_tag(ASN1_C_UNIV, UT_Null,
TE_EXPLICIT, new_type(TNull));
}
;
SequenceType : kw_SEQUENCE '{' /* ComponentTypeLists */ ComponentTypeList '}'
{
$$ = new_type(TSequence);
$$->members = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Sequence, TE_EXPLICIT, $$);
}
| kw_SEQUENCE '{' '}'
{
$$ = new_type(TSequence);
$$->members = NULL;
$$ = new_tag(ASN1_C_UNIV, UT_Sequence, TE_EXPLICIT, $$);
}
;
SequenceOfType : kw_SEQUENCE kw_OF Type
{
$$ = new_type(TSequenceOf);
$$->subtype = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Sequence, TE_EXPLICIT, $$);
}
;
SetType : kw_SET '{' /* ComponentTypeLists */ ComponentTypeList '}'
{
$$ = new_type(TSet);
$$->members = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Set, TE_EXPLICIT, $$);
}
| kw_SET '{' '}'
{
$$ = new_type(TSet);
$$->members = NULL;
$$ = new_tag(ASN1_C_UNIV, UT_Set, TE_EXPLICIT, $$);
}
;
SetOfType : kw_SET kw_OF Type
{
$$ = new_type(TSetOf);
$$->subtype = $3;
$$ = new_tag(ASN1_C_UNIV, UT_Set, TE_EXPLICIT, $$);
}
;
ChoiceType : kw_CHOICE '{' /* AlternativeTypeLists */ ComponentTypeList '}'
{
$$ = new_type(TChoice);
$$->members = $3;
}
;
ReferencedType : DefinedType
| UsefulType
;
DefinedType : IDENTIFIER
{
Symbol *s = addsym($1);
$$ = new_type(TType);
if(s->stype != Stype && s->stype != SUndefined)
error_message ("%s is not a type\n", $1);
else
$$->symbol = s;
}
;
UsefulType : kw_GeneralizedTime
{
$$ = new_tag(ASN1_C_UNIV, UT_GeneralizedTime,
TE_EXPLICIT, new_type(TGeneralizedTime));
}
| kw_UTCTime
{
$$ = new_tag(ASN1_C_UNIV, UT_UTCTime,
TE_EXPLICIT, new_type(TUTCTime));
}
;
ConstrainedType : Type Constraint
{
/* if (Constraint.type == contentConstrant) {
assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too
if (Constraint.u.constraint.type) {
assert((Constraint.u.constraint.type.length % 8) == 0);
}
}
if (Constraint.u.constraint.encoding) {
type == der-oid|ber-oid
}
*/
}
;
Constraint : '(' ConstraintSpec ')'
{
$$ = $2;
}
ConstraintSpec : GeneralConstraint
GeneralConstraint: ContentsConstraint
| UserDefinedConstraint
;
ContentsConstraint: kw_CONTAINING Type
{
$$ = new_constraint_spec(CT_CONTENTS);
$$->u.content.type = $2;
$$->u.content.encoding = NULL;
}
| kw_ENCODED kw_BY Value
{
if ($3->type != objectidentifiervalue)
error_message("Non-OID used in ENCODED BY constraint");
$$ = new_constraint_spec(CT_CONTENTS);
$$->u.content.type = NULL;
$$->u.content.encoding = $3;
}
| kw_CONTAINING Type kw_ENCODED kw_BY Value
{
if ($5->type != objectidentifiervalue)
error_message("Non-OID used in ENCODED BY constraint");
$$ = new_constraint_spec(CT_CONTENTS);
$$->u.content.type = $2;
$$->u.content.encoding = $5;
}
;
UserDefinedConstraint: kw_CONSTRAINED kw_BY '{' '}'
{
$$ = new_constraint_spec(CT_USER);
}
;
TaggedType : Tag tagenv Type
{
$$ = new_type(TTag);
$$->tag = $1;
$$->tag.tagenv = $2;
if($3->type == TTag && $2 == TE_IMPLICIT) {
$$->subtype = $3->subtype;
free($3);
} else
$$->subtype = $3;
}
;
Tag : '[' Class NUMBER ']'
{
$$.tagclass = $2;
$$.tagvalue = $3;
$$.tagenv = TE_EXPLICIT;
}
;
Class : /* */
{
$$ = ASN1_C_CONTEXT;
}
| kw_UNIVERSAL
{
$$ = ASN1_C_UNIV;
}
| kw_APPLICATION
{
$$ = ASN1_C_APPL;
}
| kw_PRIVATE
{
$$ = ASN1_C_PRIVATE;
}
;
tagenv : /* */
{
$$ = TE_EXPLICIT;
}
| kw_EXPLICIT
{
$$ = TE_EXPLICIT;
}
| kw_IMPLICIT
{
$$ = TE_IMPLICIT;
}
;
ValueAssignment : IDENTIFIER Type EEQUAL Value
{
Symbol *s;
s = addsym ($1);
s->stype = SValue;
s->value = $4;
generate_constant (s);
}
;
CharacterStringType: RestrictedCharactedStringType
;
RestrictedCharactedStringType: kw_GeneralString
{
$$ = new_tag(ASN1_C_UNIV, UT_GeneralString,
TE_EXPLICIT, new_type(TGeneralString));
}
| kw_UTF8String
{
$$ = new_tag(ASN1_C_UNIV, UT_UTF8String,
TE_EXPLICIT, new_type(TUTF8String));
}
| kw_PrintableString
{
$$ = new_tag(ASN1_C_UNIV, UT_PrintableString,
TE_EXPLICIT, new_type(TPrintableString));
}
| kw_IA5String
{
$$ = new_tag(ASN1_C_UNIV, UT_IA5String,
TE_EXPLICIT, new_type(TIA5String));
}
| kw_BMPString
{
$$ = new_tag(ASN1_C_UNIV, UT_BMPString,
TE_EXPLICIT, new_type(TBMPString));
}
| kw_UniversalString
{
$$ = new_tag(ASN1_C_UNIV, UT_UniversalString,
TE_EXPLICIT, new_type(TUniversalString));
}
;
ComponentTypeList: ComponentType
{
$$ = emalloc(sizeof(*$$));
ASN1_TAILQ_INIT($$);
ASN1_TAILQ_INSERT_HEAD($$, $1, members);
}
| ComponentTypeList ',' ComponentType
{
ASN1_TAILQ_INSERT_TAIL($1, $3, members);
$$ = $1;
}
| ComponentTypeList ',' ELLIPSIS
{
struct member *m = ecalloc(1, sizeof(*m));
m->name = estrdup("...");
m->gen_name = estrdup("asn1_ellipsis");
m->ellipsis = 1;
ASN1_TAILQ_INSERT_TAIL($1, m, members);
$$ = $1;
}
;
NamedType : IDENTIFIER Type
{
$$ = emalloc(sizeof(*$$));
$$->name = $1;
$$->gen_name = estrdup($1);
output_name ($$->gen_name);
$$->type = $2;
$$->ellipsis = 0;
}
;
ComponentType : NamedType
{
$$ = $1;
$$->optional = 0;
$$->defval = NULL;
}
| NamedType kw_OPTIONAL
{
$$ = $1;
$$->optional = 1;
$$->defval = NULL;
}
| NamedType kw_DEFAULT Value
{
$$ = $1;
$$->optional = 0;
$$->defval = $3;
}
;
NamedBitList : NamedBit
{
$$ = emalloc(sizeof(*$$));
ASN1_TAILQ_INIT($$);
ASN1_TAILQ_INSERT_HEAD($$, $1, members);
}
| NamedBitList ',' NamedBit
{
ASN1_TAILQ_INSERT_TAIL($1, $3, members);
$$ = $1;
}
;
NamedBit : IDENTIFIER '(' NUMBER ')'
{
$$ = emalloc(sizeof(*$$));
$$->name = $1;
$$->gen_name = estrdup($1);
output_name ($$->gen_name);
$$->val = $3;
$$->optional = 0;
$$->ellipsis = 0;
$$->type = NULL;
}
;
objid_opt : objid
| /* empty */ { $$ = NULL; }
;
objid : '{' objid_list '}'
{
$$ = $2;
}
;
objid_list : /* empty */
{
$$ = NULL;
}
| objid_element objid_list
{
if ($2) {
$$ = $2;
add_oid_to_tail($2, $1);
} else {
$$ = $1;
}
}
;
objid_element : IDENTIFIER '(' NUMBER ')'
{
$$ = new_objid($1, $3);
}
| IDENTIFIER
{
Symbol *s = addsym($1);
if(s->stype != SValue ||
s->value->type != objectidentifiervalue) {
error_message("%s is not an object identifier\n",
s->name);
exit(1);
}
$$ = s->value->u.objectidentifiervalue;
}
| NUMBER
{
$$ = new_objid(NULL, $1);
}
;
Value : BuiltinValue
| ReferencedValue
;
BuiltinValue : BooleanValue
| CharacterStringValue
| IntegerValue
| ObjectIdentifierValue
| NullValue
;
ReferencedValue : DefinedValue
;
DefinedValue : Valuereference
;
Valuereference : IDENTIFIER
{
Symbol *s = addsym($1);
if(s->stype != SValue)
error_message ("%s is not a value\n",
s->name);
else
$$ = s->value;
}
;
CharacterStringValue: STRING
{
$$ = emalloc(sizeof(*$$));
$$->type = stringvalue;
$$->u.stringvalue = $1;
}
;
BooleanValue : kw_TRUE
{
$$ = emalloc(sizeof(*$$));
$$->type = booleanvalue;
$$->u.booleanvalue = 0;
}
| kw_FALSE
{
$$ = emalloc(sizeof(*$$));
$$->type = booleanvalue;
$$->u.booleanvalue = 0;
}
;
IntegerValue : SignedNumber
{
$$ = emalloc(sizeof(*$$));
$$->type = integervalue;
$$->u.integervalue = $1;
}
;
SignedNumber : NUMBER
;
NullValue : kw_NULL
{
}
;
ObjectIdentifierValue: objid
{
$$ = emalloc(sizeof(*$$));
$$->type = objectidentifiervalue;
$$->u.objectidentifiervalue = $1;
}
;
%%
void
yyerror (const char *s)
{
error_message ("%s\n", s);
}
static Type *
new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype)
{
Type *t;
if(oldtype->type == TTag && oldtype->tag.tagenv == TE_IMPLICIT) {
t = oldtype;
oldtype = oldtype->subtype; /* XXX */
} else
t = new_type (TTag);
t->tag.tagclass = tagclass;
t->tag.tagvalue = tagvalue;
t->tag.tagenv = tagenv;
t->subtype = oldtype;
return t;
}
static struct objid *
new_objid(const char *label, int value)
{
struct objid *s;
s = emalloc(sizeof(*s));
s->label = label;
s->value = value;
s->next = NULL;
return s;
}
static void
add_oid_to_tail(struct objid *head, struct objid *tail)
{
struct objid *o;
o = head;
while (o->next)
o = o->next;
o->next = tail;
}
static Type *
new_type (Typetype tt)
{
Type *t = ecalloc(1, sizeof(*t));
t->type = tt;
return t;
}
static struct constraint_spec *
new_constraint_spec(enum ctype ct)
{
struct constraint_spec *c = ecalloc(1, sizeof(*c));
c->ctype = ct;
return c;
}
static void fix_labels2(Type *t, const char *prefix);
static void fix_labels1(struct memhead *members, const char *prefix)
{
Member *m;
if(members == NULL)
return;
ASN1_TAILQ_FOREACH(m, members, members) {
asprintf(&m->label, "%s_%s", prefix, m->gen_name);
if (m->label == NULL)
errx(1, "malloc");
if(m->type != NULL)
fix_labels2(m->type, m->label);
}
}
static void fix_labels2(Type *t, const char *prefix)
{
for(; t; t = t->subtype)
fix_labels1(t->members, prefix);
}
static void
fix_labels(Symbol *s)
{
char *p;
asprintf(&p, "choice_%s", s->gen_name);
if (p == NULL)
errx(1, "malloc");
fix_labels2(s->type, p);
free(p);
}
+81
View File
@@ -0,0 +1,81 @@
-- $Id: pkcs12.asn1,v 1.3 2005/07/23 11:07:39 lha Exp $ --
PKCS12 DEFINITIONS ::=
BEGIN
IMPORTS ContentInfo FROM cms
DigestInfo FROM rfc2459
heim_any, heim_any_set FROM heim;
-- The PFX PDU
id-pkcs-12 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) pkcs(1) pkcs-12(12) }
id-pkcs-12PbeIds OBJECT IDENTIFIER ::= { id-pkcs-12 1}
id-pbeWithSHAAnd128BitRC4 OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 1}
id-pbeWithSHAAnd40BitRC4 OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 2}
id-pbeWithSHAAnd3-KeyTripleDES-CBC OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 3}
id-pbeWithSHAAnd2-KeyTripleDES-CBC OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 4}
id-pbeWithSHAAnd128BitRC2-CBC OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 5}
id-pbewithSHAAnd40BitRC2-CBC OBJECT IDENTIFIER ::= { id-pkcs-12PbeIds 6}
id-pkcs12-bagtypes OBJECT IDENTIFIER ::= { id-pkcs-12 10 1}
id-pkcs12-keyBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 1 }
id-pkcs12-pkcs8ShroudedKeyBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 2 }
id-pkcs12-certBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 3 }
id-pkcs12-crlBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 4 }
id-pkcs12-secretBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 5 }
id-pkcs12-safeContentsBag OBJECT IDENTIFIER ::= { id-pkcs12-bagtypes 6 }
PKCS12-MacData ::= SEQUENCE {
mac DigestInfo,
macSalt OCTET STRING,
iterations INTEGER OPTIONAL
}
PKCS12-PFX ::= SEQUENCE {
version INTEGER,
authSafe ContentInfo,
macData PKCS12-MacData OPTIONAL
}
PKCS12-AuthenticatedSafe ::= SEQUENCE OF ContentInfo
-- Data if unencrypted
-- EncryptedData if password-encrypted
-- EnvelopedData if public key-encrypted
PKCS12-Attribute ::= SEQUENCE {
attrId OBJECT IDENTIFIER,
attrValues -- SET OF -- heim_any_set
}
PKCS12-Attributes ::= SET OF PKCS12-Attribute
PKCS12-SafeBag ::= SEQUENCE {
bagId OBJECT IDENTIFIER,
bagValue [0] heim_any,
bagAttributes PKCS12-Attributes OPTIONAL
}
PKCS12-SafeContents ::= SEQUENCE OF PKCS12-SafeBag
PKCS12-CertBag ::= SEQUENCE {
certType OBJECT IDENTIFIER,
certValue [0] heim_any
}
PKCS12-PBEParams ::= SEQUENCE {
salt OCTET STRING,
iterations INTEGER (0..4294967295) OPTIONAL
}
PKCS12-OctetString ::= OCTET STRING
-- KeyBag ::= PrivateKeyInfo
-- PKCS8ShroudedKeyBag ::= EncryptedPrivateKeyInfo
END
+30
View File
@@ -0,0 +1,30 @@
-- $Id: pkcs8.asn1,v 1.3 2005/09/13 19:41:29 lha Exp $ --
PKCS8 DEFINITIONS ::=
BEGIN
IMPORTS Attribute, AlgorithmIdentifier FROM rfc2459
heim_any, heim_any_set FROM heim;
PKCS8PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
PKCS8PrivateKey ::= OCTET STRING
PKCS8Attributes ::= SET OF Attribute
PKCS8PrivateKeyInfo ::= SEQUENCE {
version INTEGER,
privateKeyAlgorithm PKCS8PrivateKeyAlgorithmIdentifier,
privateKey PKCS8PrivateKey,
attributes [0] IMPLICIT SET OF Attribute OPTIONAL
}
PKCS8EncryptedData ::= OCTET STRING
PKCS8EncryptedPrivateKeyInfo ::= SEQUENCE {
encryptionAlgorithm AlgorithmIdentifier,
encryptedData PKCS8EncryptedData
}
END
+28
View File
@@ -0,0 +1,28 @@
-- $Id: pkcs9.asn1,v 1.5 2006/04/24 08:59:10 lha Exp $ --
PKCS9 DEFINITIONS ::=
BEGIN
-- The PFX PDU
id-pkcs-9 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) pkcs(1) pkcs-9(9) }
id-pkcs9-emailAddress OBJECT IDENTIFIER ::= {id-pkcs-9 1 }
id-pkcs9-contentType OBJECT IDENTIFIER ::= {id-pkcs-9 3 }
id-pkcs9-messageDigest OBJECT IDENTIFIER ::= {id-pkcs-9 4 }
id-pkcs9-signingTime OBJECT IDENTIFIER ::= {id-pkcs-9 5 }
id-pkcs9-countersignature OBJECT IDENTIFIER ::= {id-pkcs-9 6 }
id-pkcs-9-at-friendlyName OBJECT IDENTIFIER ::= {id-pkcs-9 20}
id-pkcs-9-at-localKeyId OBJECT IDENTIFIER ::= {id-pkcs-9 21}
id-pkcs-9-at-certTypes OBJECT IDENTIFIER ::= {id-pkcs-9 22}
id-pkcs-9-at-certTypes-x509 OBJECT IDENTIFIER ::= {id-pkcs-9-at-certTypes 1}
PKCS9-BMPString ::= BMPString
PKCS9-friendlyName ::= SET OF PKCS9-BMPString
END
+161
View File
@@ -0,0 +1,161 @@
-- $Id$ --
PKINIT DEFINITIONS ::= BEGIN
IMPORTS EncryptionKey, PrincipalName, Realm, KerberosTime, Checksum FROM krb5
IssuerAndSerialNumber, ContentInfo FROM cms
SubjectPublicKeyInfo, AlgorithmIdentifier FROM rfc2459
heim_any FROM heim;
id-pkinit OBJECT IDENTIFIER ::=
{ iso (1) org (3) dod (6) internet (1) security (5)
kerberosv5 (2) pkinit (3) }
id-pkauthdata OBJECT IDENTIFIER ::= { id-pkinit 1 }
id-pkdhkeydata OBJECT IDENTIFIER ::= { id-pkinit 2 }
id-pkrkeydata OBJECT IDENTIFIER ::= { id-pkinit 3 }
id-pkekuoid OBJECT IDENTIFIER ::= { id-pkinit 4 }
id-pkkdcekuoid OBJECT IDENTIFIER ::= { id-pkinit 5 }
id-pkinit-san OBJECT IDENTIFIER ::=
{ iso(1) org(3) dod(6) internet(1) security(5) kerberosv5(2)
x509-sanan(2) }
id-pkinit-ms-san OBJECT IDENTIFIER ::=
{ iso(1) org(3) dod(6) internet(1) foo1(4)
foo2(1) foo3(311) foo4(20) foo5(2) foo6(3) }
pa-pk-as-req INTEGER ::= 16
pa-pk-as-rep INTEGER ::= 17
ad-initial-verified-cas INTEGER ::= 9
td-trusted-certifiers INTEGER ::= 104
td-invalid-certificates INTEGER ::= 105
td-dh-parameters INTEGER ::= 109
DHNonce ::= OCTET STRING
TrustedCA ::= SEQUENCE {
caName [0] IMPLICIT OCTET STRING,
certificateSerialNumber [1] INTEGER OPTIONAL,
subjectKeyIdentifier [2] OCTET STRING OPTIONAL,
...
}
ExternalPrincipalIdentifier ::= SEQUENCE {
subjectName [0] IMPLICIT OCTET STRING OPTIONAL,
issuerAndSerialNumber [1] IMPLICIT OCTET STRING OPTIONAL,
subjectKeyIdentifier [2] IMPLICIT OCTET STRING OPTIONAL,
...
}
ExternalPrincipalIdentifiers ::= SEQUENCE OF ExternalPrincipalIdentifier
PA-PK-AS-REQ ::= SEQUENCE {
signedAuthPack [0] IMPLICIT OCTET STRING,
trustedCertifiers [1] ExternalPrincipalIdentifiers OPTIONAL,
kdcPkId [2] IMPLICIT OCTET STRING OPTIONAL,
...
}
PKAuthenticator ::= SEQUENCE {
cusec [0] INTEGER -- (0..999999) --,
ctime [1] KerberosTime,
nonce [2] INTEGER (0..4294967295),
paChecksum [3] OCTET STRING OPTIONAL,
...
}
AuthPack ::= SEQUENCE {
pkAuthenticator [0] PKAuthenticator,
clientPublicValue [1] SubjectPublicKeyInfo OPTIONAL,
supportedCMSTypes [2] SEQUENCE OF AlgorithmIdentifier OPTIONAL,
clientDHNonce [3] DHNonce OPTIONAL,
...
}
TD-TRUSTED-CERTIFIERS ::= ExternalPrincipalIdentifiers
TD-INVALID-CERTIFICATES ::= ExternalPrincipalIdentifiers
KRB5PrincipalName ::= SEQUENCE {
realm [0] Realm,
principalName [1] PrincipalName
}
AD-INITIAL-VERIFIED-CAS ::= SEQUENCE OF ExternalPrincipalIdentifier
DHRepInfo ::= SEQUENCE {
dhSignedData [0] IMPLICIT OCTET STRING,
serverDHNonce [1] DHNonce OPTIONAL
}
PA-PK-AS-REP ::= CHOICE {
dhInfo [0] DHRepInfo,
encKeyPack [1] IMPLICIT OCTET STRING,
...
}
KDCDHKeyInfo ::= SEQUENCE {
subjectPublicKey [0] BIT STRING,
nonce [1] INTEGER (0..4294967295),
dhKeyExpiration [2] KerberosTime OPTIONAL,
...
}
ReplyKeyPack ::= SEQUENCE {
replyKey [0] EncryptionKey,
asChecksum [1] Checksum,
...
}
TD-DH-PARAMETERS ::= SEQUENCE OF AlgorithmIdentifier
-- Windows compat glue --
PKAuthenticator-Win2k ::= SEQUENCE {
kdcName [0] PrincipalName,
kdcRealm [1] Realm,
cusec [2] INTEGER (0..4294967295),
ctime [3] KerberosTime,
nonce [4] INTEGER (-2147483648..2147483647)
}
AuthPack-Win2k ::= SEQUENCE {
pkAuthenticator [0] PKAuthenticator-Win2k,
clientPublicValue [1] SubjectPublicKeyInfo OPTIONAL
}
TrustedCA-Win2k ::= CHOICE {
caName [1] heim_any,
issuerAndSerial [2] IssuerAndSerialNumber
}
PA-PK-AS-REQ-Win2k ::= SEQUENCE {
signed-auth-pack [0] IMPLICIT OCTET STRING,
trusted-certifiers [2] SEQUENCE OF TrustedCA-Win2k OPTIONAL,
kdc-cert [3] IMPLICIT OCTET STRING OPTIONAL,
encryption-cert [4] IMPLICIT OCTET STRING OPTIONAL
}
PA-PK-AS-REP-Win2k ::= CHOICE {
dhSignedData [0] IMPLICIT OCTET STRING,
encKeyPack [1] IMPLICIT OCTET STRING
}
KDCDHKeyInfo-Win2k ::= SEQUENCE {
nonce [0] INTEGER (-2147483648..2147483647),
subjectPublicKey [2] BIT STRING
}
ReplyKeyPack-Win2k ::= SEQUENCE {
replyKey [0] EncryptionKey,
nonce [1] INTEGER (0..4294967295),
...
}
END
+426
View File
@@ -0,0 +1,426 @@
-- $Id$ --
-- Definitions from rfc2459/rfc3280
RFC2459 DEFINITIONS ::= BEGIN
IMPORTS heim_any FROM heim;
Version ::= INTEGER {
rfc3280_version_1(0),
rfc3280_version_2(1),
rfc3280_version_3(2)
}
id-pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) pkcs(1) 1 }
id-pkcs1-rsaEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 1 }
id-pkcs1-md2WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 2 }
id-pkcs1-md5WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 4 }
id-pkcs1-sha1WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 5 }
id-pkcs1-sha256WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 11 }
id-pkcs1-sha384WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 12 }
id-pkcs1-sha512WithRSAEncryption OBJECT IDENTIFIER ::= { id-pkcs-1 13 }
id-pkcs-2 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) pkcs(1) 2 }
id-pkcs2-md2 OBJECT IDENTIFIER ::= { id-pkcs-2 2 }
id-pkcs2-md4 OBJECT IDENTIFIER ::= { id-pkcs-2 4 }
id-pkcs2-md5 OBJECT IDENTIFIER ::= { id-pkcs-2 5 }
id-rsa-digestAlgorithm OBJECT IDENTIFIER ::=
{ iso(1) member-body(2) us(840) rsadsi(113549) 2 }
id-rsa-digest-md2 OBJECT IDENTIFIER ::= { id-rsa-digestAlgorithm 2 }
id-rsa-digest-md4 OBJECT IDENTIFIER ::= { id-rsa-digestAlgorithm 4 }
id-rsa-digest-md5 OBJECT IDENTIFIER ::= { id-rsa-digestAlgorithm 5 }
id-pkcs-3 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) pkcs(1) 3 }
id-pkcs3-rc2-cbc OBJECT IDENTIFIER ::= { id-pkcs-3 2 }
id-pkcs3-rc4 OBJECT IDENTIFIER ::= { id-pkcs-3 4 }
id-pkcs3-des-ede3-cbc OBJECT IDENTIFIER ::= { id-pkcs-3 7 }
id-rsadsi-encalg OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
rsadsi(113549) 3 }
id-rsadsi-rc2-cbc OBJECT IDENTIFIER ::= { id-rsadsi-encalg 2 }
id-rsadsi-des-ede3-cbc OBJECT IDENTIFIER ::= { id-rsadsi-encalg 7 }
id-secsig-sha-1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
oiw(14) secsig(3) algorithm(2) 26 }
id-nistAlgorithm OBJECT IDENTIFIER ::= {
joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) 4 }
id-nist-aes-algs OBJECT IDENTIFIER ::= { id-nistAlgorithm 1 }
id-aes-128-cbc OBJECT IDENTIFIER ::= { id-nist-aes-algs 2 }
id-aes-192-cbc OBJECT IDENTIFIER ::= { id-nist-aes-algs 22 }
id-aes-256-cbc OBJECT IDENTIFIER ::= { id-nist-aes-algs 42 }
id-nist-sha-algs OBJECT IDENTIFIER ::= { id-nistAlgorithm 2 }
id-sha256 OBJECT IDENTIFIER ::= { id-nist-sha-algs 1 }
id-sha224 OBJECT IDENTIFIER ::= { id-nist-sha-algs 4 }
id-sha384 OBJECT IDENTIFIER ::= { id-nist-sha-algs 2 }
id-sha512 OBJECT IDENTIFIER ::= { id-nist-sha-algs 3 }
id-dhpublicnumber OBJECT IDENTIFIER ::= {
iso(1) member-body(2) us(840) ansi-x942(10046)
number-type(2) 1 }
id-x9-57 OBJECT IDENTIFIER ::= {
iso(1) member-body(2) us(840) ansi-x942(10046)
4 }
id-dsa OBJECT IDENTIFIER ::= { id-x9-57 1 }
id-dsa-with-sha1 OBJECT IDENTIFIER ::= { id-x9-57 3 }
-- x.520 names types
id-x520-at OBJECT IDENTIFIER ::= { joint-iso-ccitt(2) ds(5) 4 }
id-at-commonName OBJECT IDENTIFIER ::= { id-x520-at 3 }
id-at-surname OBJECT IDENTIFIER ::= { id-x520-at 4 }
id-at-serialNumber OBJECT IDENTIFIER ::= { id-x520-at 5 }
id-at-countryName OBJECT IDENTIFIER ::= { id-x520-at 6 }
id-at-localityName OBJECT IDENTIFIER ::= { id-x520-at 7 }
id-at-stateOrProvinceName OBJECT IDENTIFIER ::= { id-x520-at 8 }
id-at-organizationName OBJECT IDENTIFIER ::= { id-x520-at 10 }
id-at-organizationalUnitName OBJECT IDENTIFIER ::= { id-x520-at 11 }
id-at-name OBJECT IDENTIFIER ::= { id-x520-at 41 }
id-at-givenName OBJECT IDENTIFIER ::= { id-x520-at 42 }
id-at-initials OBJECT IDENTIFIER ::= { id-x520-at 43 }
id-at-generationQualifier OBJECT IDENTIFIER ::= { id-x520-at 44 }
id-at-pseudonym OBJECT IDENTIFIER ::= { id-x520-at 65 }
-- RFC 2247
id-Userid OBJECT IDENTIFIER ::=
{ 0 9 2342 19200300 100 1 1 }
id-domainComponent OBJECT IDENTIFIER ::=
{ 0 9 2342 19200300 100 1 25 }
-- rfc3280
id-x509-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29}
AlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER,
parameters heim_any OPTIONAL
}
AttributeType ::= OBJECT IDENTIFIER
AttributeValue ::= heim_any
TeletexStringx ::= [UNIVERSAL 20] IMPLICIT OCTET STRING
DirectoryString ::= CHOICE {
ia5String IA5String,
teletexString TeletexStringx,
printableString PrintableString,
universalString UniversalString,
utf8String UTF8String,
bmpString BMPString
}
Attribute ::= SEQUENCE {
type AttributeType,
value SET OF -- AttributeValue -- heim_any
}
AttributeTypeAndValue ::= SEQUENCE {
type AttributeType,
value DirectoryString
}
RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
Name ::= CHOICE {
rdnSequence RDNSequence
}
CertificateSerialNumber ::= INTEGER
Time ::= CHOICE {
utcTime UTCTime,
generalTime GeneralizedTime
}
Validity ::= SEQUENCE {
notBefore Time,
notAfter Time
}
UniqueIdentifier ::= BIT STRING
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
Extension ::= SEQUENCE {
extnID OBJECT IDENTIFIER,
critical BOOLEAN OPTIONAL, -- DEFAULT FALSE XXX
extnValue OCTET STRING
}
Extensions ::= SEQUENCE OF Extension -- SIZE (1..MAX)
TBSCertificate ::= SEQUENCE {
version [0] Version OPTIONAL, -- EXPLICIT nnn DEFAULT 1,
serialNumber CertificateSerialNumber,
signature AlgorithmIdentifier,
issuer Name,
validity Validity,
subject Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID [1] IMPLICIT BIT STRING -- UniqueIdentifier -- OPTIONAL,
-- If present, version shall be v2 or v3
subjectUniqueID [2] IMPLICIT BIT STRING -- UniqueIdentifier -- OPTIONAL,
-- If present, version shall be v2 or v3
extensions [3] EXPLICIT Extensions OPTIONAL
-- If present, version shall be v3
}
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate,
signatureAlgorithm AlgorithmIdentifier,
signatureValue BIT STRING
}
Certificates ::= SEQUENCE OF Certificate
ValidationParms ::= SEQUENCE {
seed BIT STRING,
pgenCounter INTEGER
}
DomainParameters ::= SEQUENCE {
p INTEGER, -- odd prime, p=jq +1
g INTEGER, -- generator, g
q INTEGER, -- factor of p-1
j INTEGER OPTIONAL, -- subgroup factor
validationParms ValidationParms OPTIONAL -- ValidationParms
}
DHPublicKey ::= INTEGER
OtherName ::= SEQUENCE {
type-id OBJECT IDENTIFIER,
value [0] EXPLICIT heim_any
}
GeneralName ::= CHOICE {
otherName [0] IMPLICIT -- OtherName -- SEQUENCE {
type-id OBJECT IDENTIFIER,
value [0] EXPLICIT heim_any
},
rfc822Name [1] IMPLICIT IA5String,
dNSName [2] IMPLICIT IA5String,
-- x400Address [3] IMPLICIT ORAddress,--
directoryName [4] IMPLICIT -- Name -- CHOICE {
rdnSequence RDNSequence
},
-- ediPartyName [5] IMPLICIT EDIPartyName, --
uniformResourceIdentifier [6] IMPLICIT IA5String,
iPAddress [7] IMPLICIT OCTET STRING,
registeredID [8] IMPLICIT OBJECT IDENTIFIER
}
GeneralNames ::= SEQUENCE -- SIZE (1..MAX) -- OF GeneralName
id-x509-ce-keyUsage OBJECT IDENTIFIER ::= { id-x509-ce 15 }
KeyUsage ::= BIT STRING {
digitalSignature (0),
nonRepudiation (1),
keyEncipherment (2),
dataEncipherment (3),
keyAgreement (4),
keyCertSign (5),
cRLSign (6),
encipherOnly (7),
decipherOnly (8)
}
id-x509-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-x509-ce 35 }
KeyIdentifier ::= OCTET STRING
AuthorityKeyIdentifier ::= SEQUENCE {
keyIdentifier [0] IMPLICIT OCTET STRING OPTIONAL,
authorityCertIssuer [1] IMPLICIT -- GeneralName --
SEQUENCE -- SIZE (1..MAX) -- OF GeneralName OPTIONAL,
authorityCertSerialNumber [2] IMPLICIT INTEGER OPTIONAL
}
id-x509-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-x509-ce 14 }
SubjectKeyIdentifier ::= KeyIdentifier
id-x509-ce-basicConstraints OBJECT IDENTIFIER ::= { id-x509-ce 19 }
BasicConstraints ::= SEQUENCE {
cA BOOLEAN OPTIONAL -- DEFAULT FALSE --,
pathLenConstraint INTEGER (0..4294967295) OPTIONAL
}
id-x509-ce-nameConstraints OBJECT IDENTIFIER ::= { id-x509-ce 30 }
BaseDistance ::= INTEGER -- (0..MAX) --
GeneralSubtree ::= SEQUENCE {
base GeneralName,
minimum [0] IMPLICIT -- BaseDistance -- INTEGER OPTIONAL -- DEFAULT 0 --,
maximum [1] IMPLICIT -- BaseDistance -- INTEGER OPTIONAL
}
GeneralSubtrees ::= SEQUENCE -- SIZE (1..MAX) -- OF GeneralSubtree
NameConstraints ::= SEQUENCE {
permittedSubtrees [0] IMPLICIT -- GeneralSubtrees -- SEQUENCE OF GeneralSubtree OPTIONAL,
excludedSubtrees [1] IMPLICIT -- GeneralSubtrees -- SEQUENCE OF GeneralSubtree OPTIONAL
}
id-x509-ce-privateKeyUsagePeriod OBJECT IDENTIFIER ::= { id-x509-ce 16 }
id-x509-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-x509-ce 32 }
id-x509-ce-policyMappings OBJECT IDENTIFIER ::= { id-x509-ce 33 }
id-x509-ce-subjectAltName OBJECT IDENTIFIER ::= { id-x509-ce 17 }
id-x509-ce-issuerAltName OBJECT IDENTIFIER ::= { id-x509-ce 18 }
id-x509-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-x509-ce 9 }
id-x509-ce-policyConstraints OBJECT IDENTIFIER ::= { id-x509-ce 36 }
id-x509-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-x509-ce 37}
ExtKeyUsage ::= SEQUENCE OF OBJECT IDENTIFIER
id-x509-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-x509-ce 31 }
id-x509-ce-deltaCRLIndicator OBJECT IDENTIFIER ::= { id-x509-ce 27 }
id-x509-ce-issuingDistributionPoint OBJECT IDENTIFIER ::= { id-x509-ce 28 }
id-x509-ce-holdInstructionCode OBJECT IDENTIFIER ::= { id-x509-ce 23 }
id-x509-ce-invalidityDate OBJECT IDENTIFIER ::= { id-x509-ce 24 }
id-x509-ce-certificateIssuer OBJECT IDENTIFIER ::= { id-x509-ce 29 }
id-x509-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-x509-ce 54 }
-- rfc3279
DSASigValue ::= SEQUENCE {
r INTEGER,
s INTEGER
}
DSAPublicKey ::= INTEGER
DSAParams ::= SEQUENCE {
p INTEGER,
q INTEGER,
g INTEGER
}
-- really pkcs1
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e
}
RSAPrivateKey ::= SEQUENCE {
version INTEGER (0..4294967295),
modulus INTEGER, -- n
publicExponent INTEGER, -- e
privateExponent INTEGER, -- d
prime1 INTEGER, -- p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1)
exponent2 INTEGER, -- d mod (q-1)
coefficient INTEGER -- (inverse of q) mod p
}
DigestInfo ::= SEQUENCE {
digestAlgorithm AlgorithmIdentifier,
digest OCTET STRING
}
-- some ms ext
-- szOID_ENROLL_CERTTYPE_EXTENSION "1.3.6.1.4.1.311.20.2" is Encoded as a
-- UNICODESTRING (0x1E tag)
-- szOID_CERTIFICATE_TEMPLATE "1.3.6.1.4.1.311.21.7" is Encoded as:
-- TemplateVersion ::= INTEGER (0..4294967295)
-- CertificateTemplate ::= SEQUENCE {
-- templateID OBJECT IDENTIFIER,
-- templateMajorVersion TemplateVersion,
-- templateMinorVersion TemplateVersion OPTIONAL
-- }
--
-- CRL
--
TBSCRLCertList ::= SEQUENCE {
version Version OPTIONAL, -- if present, MUST be v2
signature AlgorithmIdentifier,
issuer Name,
thisUpdate Time,
nextUpdate Time OPTIONAL,
revokedCertificates SEQUENCE OF SEQUENCE {
userCertificate CertificateSerialNumber,
revocationDate Time,
crlEntryExtensions Extensions OPTIONAL
-- if present, MUST be v2
} OPTIONAL,
crlExtensions [0] EXPLICIT Extensions OPTIONAL
-- if present, MUST be v2
}
CRLCertificateList ::= SEQUENCE {
tbsCertList TBSCRLCertList,
signatureAlgorithm AlgorithmIdentifier,
signatureValue BIT STRING
}
id-x509-ce-cRLNumber OBJECT IDENTIFIER ::= { id-x509-ce 20 }
id-x509-ce-freshestCRL OBJECT IDENTIFIER ::= { id-x509-ce 46 }
id-x509-ce-cRLReason OBJECT IDENTIFIER ::= { id-x509-ce 21 }
CRLReason ::= ENUMERATED {
unspecified (0),
keyCompromise (1),
cACompromise (2),
affiliationChanged (3),
superseded (4),
cessationOfOperation (5),
certificateHold (6),
removeFromCRL (8),
privilegeWithdrawn (9),
aACompromise (10)
}
-- RFC 3820 Proxy Certificate Profile
id-pkix-pe OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
dod(6) internet(1) security(5) mechanisms(5) pkix(7) 1 }
id-pe-proxyCertInfo OBJECT IDENTIFIER ::= { id-pkix-pe 14 }
ProxyPolicy ::= SEQUENCE {
policyLanguage OBJECT IDENTIFIER,
policy OCTET STRING OPTIONAL
}
ProxyCertInfo ::= SEQUENCE {
pCPathLenConstraint INTEGER (0..4294967295) OPTIONAL, -- really MAX
proxyPolicy ProxyPolicy
}
END
+110
View File
@@ -0,0 +1,110 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "gen_locl.h"
#include "lex.h"
RCSID("$Id: symbol.c,v 1.10 2005/07/12 06:27:39 lha Exp $");
static Hashtab *htab;
static int
cmp(void *a, void *b)
{
Symbol *s1 = (Symbol *) a;
Symbol *s2 = (Symbol *) b;
return strcmp(s1->name, s2->name);
}
static unsigned
hash(void *a)
{
Symbol *s = (Symbol *) a;
return hashjpw(s->name);
}
void
initsym(void)
{
htab = hashtabnew(101, cmp, hash);
}
void
output_name(char *s)
{
char *p;
for (p = s; *p; ++p)
if (*p == '-')
*p = '_';
}
Symbol *
addsym(char *name)
{
Symbol key, *s;
key.name = name;
s = (Symbol *) hashtabsearch(htab, (void *) &key);
if (s == NULL) {
s = (Symbol *) emalloc(sizeof(*s));
s->name = name;
s->gen_name = estrdup(name);
output_name(s->gen_name);
s->stype = SUndefined;
hashtabadd(htab, s);
}
return s;
}
static int
checkfunc(void *ptr, void *arg)
{
Symbol *s = ptr;
if (s->stype == SUndefined) {
error_message("%s is still undefined\n", s->name);
*(int *) arg = 1;
}
return 0;
}
int
checkundefined(void)
{
int f = 0;
hashtabforeach(htab, checkfunc, &f);
return f;
}
+160
View File
@@ -0,0 +1,160 @@
/*
* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: symbol.h,v 1.13 2005/12/06 19:59:52 lha Exp $ */
#ifndef _SYMBOL_H
#define _SYMBOL_H
#include "asn1_queue.h"
enum typetype {
TBitString,
TBoolean,
TChoice,
TEnumerated,
TGeneralString,
TGeneralizedTime,
TIA5String,
TInteger,
TNull,
TOID,
TOctetString,
TPrintableString,
TSequence,
TSequenceOf,
TSet,
TSetOf,
TTag,
TType,
TUTCTime,
TUTF8String,
TBMPString,
TUniversalString
};
typedef enum typetype Typetype;
struct type;
struct value {
enum { booleanvalue,
nullvalue,
integervalue,
stringvalue,
objectidentifiervalue
} type;
union {
int booleanvalue;
int integervalue;
char *stringvalue;
struct objid *objectidentifiervalue;
} u;
};
struct member {
char *name;
char *gen_name;
char *label;
int val;
int optional;
int ellipsis;
struct type *type;
ASN1_TAILQ_ENTRY(member) members;
struct value *defval;
};
typedef struct member Member;
ASN1_TAILQ_HEAD(memhead, member);
struct symbol;
struct tagtype {
int tagclass;
int tagvalue;
enum { TE_IMPLICIT, TE_EXPLICIT } tagenv;
};
struct range {
int min;
int max;
};
enum ctype { CT_CONTENTS, CT_USER } ;
struct constraint_spec;
struct type {
Typetype type;
struct memhead *members;
struct symbol *symbol;
struct type *subtype;
struct tagtype tag;
struct range *range;
struct constraint_spec *constraint;
};
typedef struct type Type;
struct constraint_spec {
enum ctype ctype;
union {
struct {
Type *type;
struct value *encoding;
} content;
} u;
};
struct objid {
const char *label;
int value;
struct objid *next;
};
struct symbol {
char *name;
char *gen_name;
enum { SUndefined, SValue, Stype } stype;
struct value *value;
Type *type;
};
typedef struct symbol Symbol;
void initsym (void);
Symbol *addsym (char *);
void output_name (char *);
int checkundefined(void);
#endif
+88
View File
@@ -0,0 +1,88 @@
-- $Id: test.asn1,v 1.9 2006/09/05 14:00:44 lha Exp $ --
TEST DEFINITIONS ::=
BEGIN
IMPORTS heim_any FROM heim;
TESTLargeTag ::= SEQUENCE {
foo[127] INTEGER (-2147483648..2147483647)
}
TESTSeq ::= SEQUENCE {
tag0[0] INTEGER (-2147483648..2147483647),
tag1[1] TESTLargeTag,
tagless INTEGER (-2147483648..2147483647),
tag3[2] INTEGER (-2147483648..2147483647)
}
TESTChoice1 ::= CHOICE {
i1[1] INTEGER (-2147483648..2147483647),
i2[2] INTEGER (-2147483648..2147483647),
...
}
TESTChoice2 ::= CHOICE {
i1[1] INTEGER (-2147483648..2147483647),
...
}
TESTInteger ::= INTEGER (-2147483648..2147483647)
TESTInteger2 ::= [4] IMPLICIT TESTInteger
TESTInteger3 ::= [5] IMPLICIT TESTInteger2
TESTImplicit ::= SEQUENCE {
ti1[0] IMPLICIT INTEGER (-2147483648..2147483647),
ti2[1] IMPLICIT SEQUENCE {
foo[127] INTEGER (-2147483648..2147483647)
},
ti3[2] IMPLICIT [5] IMPLICIT [4] IMPLICIT INTEGER (-2147483648..2147483647)
}
TESTImplicit2 ::= SEQUENCE {
ti1[0] IMPLICIT TESTInteger,
ti2[1] IMPLICIT TESTLargeTag,
ti3[2] IMPLICIT TESTInteger3
}
TESTAllocInner ::= SEQUENCE {
ai[0] TESTInteger
}
TESTAlloc ::= SEQUENCE {
tagless TESTAllocInner OPTIONAL,
three [1] INTEGER (-2147483648..2147483647),
tagless2 heim_any OPTIONAL
}
TESTCONTAINING ::= OCTET STRING ( CONTAINING INTEGER )
TESTENCODEDBY ::= OCTET STRING ( ENCODED BY
{ joint-iso-itu-t(2) asn(1) ber-derived(2) distinguished-encoding(1) }
)
TESTDer OBJECT IDENTIFIER ::= {
joint-iso-itu-t(2) asn(1) ber-derived(2) distinguished-encoding(1)
}
TESTCONTAININGENCODEDBY ::= OCTET STRING ( CONTAINING INTEGER ENCODED BY
{ joint-iso-itu-t(2) asn(1) ber-derived(2) distinguished-encoding(1) }
)
TESTCONTAININGENCODEDBY2 ::= OCTET STRING (
CONTAINING INTEGER ENCODED BY TESTDer
)
TESTValue1 INTEGER ::= 1
TESTUSERCONSTRAINED ::= OCTET STRING (CONSTRAINED BY { -- meh -- })
-- TESTUSERCONSTRAINED2 ::= OCTET STRING (CONSTRAINED BY { TESTInteger })
-- TESTUSERCONSTRAINED3 ::= OCTET STRING (CONSTRAINED BY { INTEGER })
-- TESTUSERCONSTRAINED4 ::= OCTET STRING (CONSTRAINED BY { INTEGER : 1 })
TESTSeqOf ::= SEQUENCE OF TESTInteger
END
+14
View File
@@ -0,0 +1,14 @@
# $Id: test.gen,v 1.2 2005/07/12 06:27:41 lha Exp $
# Sample for TESTSeq in test.asn1
#
UNIV CONS Sequence 23
CONTEXT CONS 0 3
UNIV PRIM Integer 1 01
CONTEXT CONS 1 8
UNIV CONS Sequence 6
CONTEXT CONS 127 3
UNIV PRIM Integer 1 01
UNIV PRIM Integer 1 01
CONTEXT CONS 2 3
UNIV PRIM Integer 1 01
+86
View File
@@ -0,0 +1,86 @@
/*
* Copyright (c) 1997 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "der_locl.h"
RCSID("$Id: timegm.c,v 1.11 2006/10/19 16:19:32 lha Exp $");
static int
is_leap(unsigned y)
{
y += 1900;
return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
}
/*
* This is a simplifed version of _der_timegm that doesn't accept out
* of bound values that timegm(3) normally accepts but those are not
* valid in asn1 encodings.
*/
time_t
_der_timegm (struct tm *tm)
{
static const unsigned ndays[2][12] ={
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
time_t res = 0;
unsigned i;
if (tm->tm_year < 0)
return -1;
if (tm->tm_mon < 0 || tm->tm_mon > 11)
return -1;
if (tm->tm_mday < 1 || tm->tm_mday > ndays[is_leap(tm->tm_year)][tm->tm_mon])
return -1;
if (tm->tm_hour < 0 || tm->tm_hour > 23)
return -1;
if (tm->tm_min < 0 || tm->tm_min > 59)
return -1;
if (tm->tm_sec < 0 || tm->tm_sec > 59)
return -1;
for (i = 70; i < tm->tm_year; ++i)
res += is_leap(i) ? 366 : 365;
for (i = 0; i < tm->tm_mon; ++i)
res += ndays[is_leap(tm->tm_year)][i];
res += tm->tm_mday - 1;
res *= 24;
res += tm->tm_hour;
res *= 60;
res += tm->tm_min;
res *= 60;
res += tm->tm_sec;
return res;
}
+172
View File
@@ -0,0 +1,172 @@
/*
* Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: com_err.c,v 1.19 2005/04/24 19:42:39 lha Exp $");
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <roken.h>
#include "com_err.h"
struct et_list *_et_list = NULL;
const char *
error_message (long code)
{
static char msg[128];
const char *p = com_right(_et_list, code);
if (p == NULL) {
if (code < 0)
snprintf(msg, sizeof(msg), "Unknown error %ld", code);
else
p = strerror(code);
}
if (p != NULL && *p != '\0') {
strlcpy(msg, p, sizeof(msg));
} else
snprintf(msg, sizeof(msg), "Unknown error %ld", code);
return msg;
}
int
init_error_table(const char **msgs, long base, int count)
{
initialize_error_table_r(&_et_list, msgs, count, base);
return 0;
}
static void
default_proc (const char *whoami, long code, const char *fmt, va_list args)
__attribute__((__format__(__printf__, 3, 0)));
static void
default_proc (const char *whoami, long code, const char *fmt, va_list args)
{
if (whoami)
fprintf(stderr, "%s: ", whoami);
if (code)
fprintf(stderr, "%s ", error_message(code));
if (fmt)
vfprintf(stderr, fmt, args);
fprintf(stderr, "\r\n"); /* ??? */
}
static errf com_err_hook = default_proc;
void
com_err_va (const char *whoami,
long code,
const char *fmt,
va_list args)
{
(*com_err_hook) (whoami, code, fmt, args);
}
void
com_err (const char *whoami,
long code,
const char *fmt,
...)
{
va_list ap;
va_start(ap, fmt);
com_err_va (whoami, code, fmt, ap);
va_end(ap);
}
errf
set_com_err_hook (errf new)
{
errf old = com_err_hook;
if (new)
com_err_hook = new;
else
com_err_hook = default_proc;
return old;
}
errf
reset_com_err_hook (void)
{
return set_com_err_hook(NULL);
}
#define ERRCODE_RANGE 8 /* # of bits to shift table number */
#define BITS_PER_CHAR 6 /* # bits to shift per character in name */
static const char char_set[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
static char buf[6];
const char *
error_table_name(int num)
{
int ch;
int i;
char *p;
/* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
p = buf;
num >>= ERRCODE_RANGE;
/* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
num &= 077777777;
/* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
for (i = 4; i >= 0; i--) {
ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
if (ch != 0)
*p++ = char_set[ch-1];
}
*p = '\0';
return(buf);
}
void
add_to_error_table(struct et_list *new_table)
{
struct et_list *et;
for (et = _et_list; et; et = et->next) {
if (et->table->base == new_table->table->base)
return;
}
new_table->next = _et_list;
_et_list = new_table;
}
@@ -0,0 +1,66 @@
/*
* Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: com_err.h,v 1.11 2005/07/07 14:58:07 lha Exp $ */
/* MIT compatible com_err library */
#ifndef __COM_ERR_H__
#define __COM_ERR_H__
#include <com_right.h>
#include <stdarg.h>
#if !defined(__GNUC__) && !defined(__attribute__)
#define __attribute__(X)
#endif
typedef void (*errf) (const char *, long, const char *, va_list);
const char * error_message (long);
int init_error_table (const char**, long, int);
void com_err_va (const char *, long, const char *, va_list)
__attribute__((format(printf, 3, 0)));
void com_err (const char *, long, const char *, ...)
__attribute__((format(printf, 3, 4)));
errf set_com_err_hook (errf);
errf reset_com_err_hook (void);
const char *error_table_name (int num);
void add_to_error_table (struct et_list *new_table);
#endif /* __COM_ERR_H__ */
@@ -0,0 +1,58 @@
/*
* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: com_right.h,v 1.12 2005/02/03 08:43:01 lha Exp $ */
#ifndef __COM_RIGHT_H__
#define __COM_RIGHT_H__
#ifdef __STDC__
#include <stdarg.h>
#endif
struct error_table {
char const * const * msgs;
long base;
int n_msgs;
};
struct et_list {
struct et_list *next;
struct error_table *table;
};
extern struct et_list *_et_list;
const char *com_right (struct et_list *list, long code);
void initialize_error_table_r (struct et_list **, const char **, int, long);
void free_error_table (struct et_list *);
#endif /* __COM_RIGHT_H__ */
@@ -0,0 +1,236 @@
/*
* Copyright (c) 1998-2002 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#undef ROKEN_RENAME
#include "compile_et.h"
#include <getarg.h>
RCSID("$Id: compile_et.c,v 1.19 2005/06/16 19:21:00 lha Exp $");
#include <roken.h>
#include <err.h>
#include "parse.h"
int numerror;
extern FILE *yyin;
extern void yyparse(void);
long base_id;
int number;
char *prefix;
char *id_str;
char name[128];
char Basename[128];
#ifdef YYDEBUG
extern int yydebug = 1;
#endif
char *filename;
char hfn[128];
char cfn[128];
struct error_code *codes = NULL;
static int
generate_c(void)
{
int n;
struct error_code *ec;
FILE *c_file = fopen(cfn, "w");
if(c_file == NULL)
return 1;
fprintf(c_file, "/* Generated from %s */\n", filename);
if(id_str)
fprintf(c_file, "/* %s */\n", id_str);
fprintf(c_file, "\n");
fprintf(c_file, "#include <stddef.h>\n");
fprintf(c_file, "#include <com_err.h>\n");
fprintf(c_file, "#include \"%s\"\n", hfn);
fprintf(c_file, "\n");
fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);
for(ec = codes, n = 0; ec; ec = ec->next, n++) {
while(n < ec->number) {
fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
n, name, n);
n++;
}
fprintf(c_file, "\t/* %03d */ \"%s\",\n", ec->number, ec->string);
}
fprintf(c_file, "\tNULL\n");
fprintf(c_file, "};\n");
fprintf(c_file, "\n");
fprintf(c_file, "#define num_errors %d\n", number);
fprintf(c_file, "\n");
fprintf(c_file,
"void initialize_%s_error_table_r(struct et_list **list)\n",
name);
fprintf(c_file, "{\n");
fprintf(c_file,
" initialize_error_table_r(list, %s_error_strings, "
"num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
fprintf(c_file, "}\n");
fprintf(c_file, "\n");
fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
fprintf(c_file, "{\n");
fprintf(c_file,
" init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
"num_errors);\n", name, name);
fprintf(c_file, "}\n");
fclose(c_file);
return 0;
}
static int
generate_h(void)
{
struct error_code *ec;
char fn[128];
FILE *h_file = fopen(hfn, "w");
char *p;
if(h_file == NULL)
return 1;
snprintf(fn, sizeof(fn), "__%s__", hfn);
for(p = fn; *p; p++)
if(!isalnum((unsigned char)*p))
*p = '_';
fprintf(h_file, "/* Generated from %s */\n", filename);
if(id_str)
fprintf(h_file, "/* %s */\n", id_str);
fprintf(h_file, "\n");
fprintf(h_file, "#ifndef %s\n", fn);
fprintf(h_file, "#define %s\n", fn);
fprintf(h_file, "\n");
fprintf(h_file, "struct et_list;\n");
fprintf(h_file, "\n");
fprintf(h_file,
"void initialize_%s_error_table_r(struct et_list **);\n",
name);
fprintf(h_file, "\n");
fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",
name, name);
fprintf(h_file, "\n");
fprintf(h_file, "typedef enum %s_error_number{\n", name);
for(ec = codes; ec; ec = ec->next) {
fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number,
(ec->next != NULL) ? "," : "");
}
fprintf(h_file, "} %s_error_number;\n", name);
fprintf(h_file, "\n");
fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);
fprintf(h_file, "\n");
fprintf(h_file, "#endif /* %s */\n", fn);
fclose(h_file);
return 0;
}
static int
generate(void)
{
return generate_c() || generate_h();
}
int version_flag;
int help_flag;
struct getargs args[] = {
{ "version", 0, arg_flag, &version_flag },
{ "help", 0, arg_flag, &help_flag }
};
int num_args = sizeof(args) / sizeof(args[0]);
static void
usage(int code)
{
arg_printusage(args, num_args, NULL, "error-table");
exit(code);
}
int
main(int argc, char **argv)
{
char *p;
int optidx = 0;
setprogname(argv[0]);
if(getarg(args, num_args, argc, argv, &optidx))
usage(1);
if(help_flag)
usage(0);
if(version_flag) {
print_version(NULL);
exit(0);
}
if(optidx == argc)
usage(1);
filename = argv[optidx];
yyin = fopen(filename, "r");
if(yyin == NULL)
err(1, "%s", filename);
p = strrchr(filename, '/');
if(p)
p++;
else
p = filename;
strlcpy(Basename, p, sizeof(Basename));
Basename[strcspn(Basename, ".")] = '\0';
snprintf(hfn, sizeof(hfn), "%s.h", Basename);
snprintf(cfn, sizeof(cfn), "%s.c", Basename);
yyparse();
if(numerror)
return 1;
return generate();
}
@@ -0,0 +1,80 @@
/*
* Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: compile_et.h,v 1.8 2005/06/16 19:21:26 lha Exp $ */
#ifndef __COMPILE_ET_H__
#define __COMPILE_ET_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <roken.h>
extern long base_id;
extern int number;
extern char *prefix;
extern char name[128];
extern char *id_str;
extern char *filename;
extern int numerror;
struct error_code {
unsigned number;
char *name;
char *string;
struct error_code *next, **tail;
};
extern struct error_code *codes;
#define APPEND(L, V) \
do { \
if((L) == NULL) { \
(L) = (V); \
(L)->tail = &(V)->next; \
(L)->next = NULL; \
}else{ \
*(L)->tail = (V); \
(L)->tail = &(V)->next; \
} \
}while(0)
#endif /* __COMPILE_ET_H__ */
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (c) 1997, 1998, 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: error.c,v 1.15 2001/02/28 20:00:13 joda Exp $");
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <com_right.h>
const char *
com_right(struct et_list *list, long code)
{
struct et_list *p;
for (p = list; p; p = p->next) {
if (code >= p->table->base && code < p->table->base + p->table->n_msgs)
return p->table->msgs[code - p->table->base];
}
return NULL;
}
struct foobar {
struct et_list etl;
struct error_table et;
};
void
initialize_error_table_r(struct et_list **list,
const char **messages,
int num_errors,
long base)
{
struct et_list *et, **end;
struct foobar *f;
for (end = list, et = *list; et; end = &et->next, et = et->next)
if (et->table->msgs == messages)
return;
f = malloc(sizeof(*f));
if (f == NULL)
return;
et = &f->etl;
et->table = &f->et;
et->table->msgs = messages;
et->table->n_msgs = num_errors;
et->table->base = base;
et->next = NULL;
*end = et;
}
void
free_error_table(struct et_list *et)
{
while(et){
struct et_list *p = et;
et = et->next;
free(p);
}
}
File diff suppressed because it is too large Load Diff
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: lex.h,v 1.1 2000/06/22 00:42:52 assar Exp $ */
void error_message (const char *, ...)
__attribute__ ((format (printf, 1, 2)));
int yylex(void);
+128
View File
@@ -0,0 +1,128 @@
%{
/*
* Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This is to handle the definition of this symbol in some AIX
* headers, which will conflict with the definition that lex will
* generate for it. It's only a problem for AIX lex.
*/
#undef ECHO
#include "compile_et.h"
#include "parse.h"
#include "lex.h"
RCSID("$Id: lex.l,v 1.8 2005/05/16 08:52:54 lha Exp $");
static unsigned lineno = 1;
static int getstring(void);
#define YY_NO_UNPUT
#undef ECHO
%}
%%
et { return ET; }
error_table { return ET; }
ec { return EC; }
error_code { return EC; }
prefix { return PREFIX; }
index { return INDEX; }
id { return ID; }
end { return END; }
[0-9]+ { yylval.number = atoi(yytext); return NUMBER; }
#[^\n]* ;
[ \t] ;
\n { lineno++; }
\" { return getstring(); }
[a-zA-Z0-9_]+ { yylval.string = strdup(yytext); return STRING; }
. { return *yytext; }
%%
#ifndef yywrap /* XXX */
int
yywrap ()
{
return 1;
}
#endif
static int
getstring(void)
{
char x[128];
int i = 0;
int c;
int quote = 0;
while(i < sizeof(x) - 1 && (c = input()) != EOF){
if(quote) {
x[i++] = c;
quote = 0;
continue;
}
if(c == '\n'){
error_message("unterminated string");
lineno++;
break;
}
if(c == '\\'){
quote++;
continue;
}
if(c == '\"')
break;
x[i++] = c;
}
x[i] = '\0';
yylval.string = strdup(x);
if (yylval.string == NULL)
err(1, "malloc");
return STRING;
}
void
error_message (const char *format, ...)
{
va_list args;
va_start (args, format);
fprintf (stderr, "%s:%d:", filename, lineno);
vfprintf (stderr, format, args);
va_end (args);
numerror++;
}
File diff suppressed because it is too large Load Diff
+15
View File
@@ -0,0 +1,15 @@
typedef union {
char *string;
int number;
} YYSTYPE;
#define ET 257
#define INDEX 258
#define PREFIX 259
#define EC 260
#define ID 261
#define END 262
#define STRING 263
#define NUMBER 264
extern YYSTYPE yylval;
+173
View File
@@ -0,0 +1,173 @@
%{
/*
* Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "compile_et.h"
#include "lex.h"
RCSID("$Id: parse.y,v 1.15 2005/06/16 19:21:42 lha Exp $");
void yyerror (char *s);
static long name2number(const char *str);
extern char *yytext;
/* This is for bison */
#if !defined(alloca) && !defined(HAVE_ALLOCA)
#define alloca(x) malloc(x)
#endif
%}
%union {
char *string;
int number;
}
%token ET INDEX PREFIX EC ID END
%token <string> STRING
%token <number> NUMBER
%%
file : /* */
| header statements
;
header : id et
| et
;
id : ID STRING
{
id_str = $2;
}
;
et : ET STRING
{
base_id = name2number($2);
strlcpy(name, $2, sizeof(name));
free($2);
}
| ET STRING STRING
{
base_id = name2number($2);
strlcpy(name, $3, sizeof(name));
free($2);
free($3);
}
;
statements : statement
| statements statement
;
statement : INDEX NUMBER
{
number = $2;
}
| PREFIX STRING
{
free(prefix);
asprintf (&prefix, "%s_", $2);
if (prefix == NULL)
errx(1, "malloc");
free($2);
}
| PREFIX
{
prefix = realloc(prefix, 1);
if (prefix == NULL)
errx(1, "malloc");
*prefix = '\0';
}
| EC STRING ',' STRING
{
struct error_code *ec = malloc(sizeof(*ec));
if (ec == NULL)
errx(1, "malloc");
ec->next = NULL;
ec->number = number;
if(prefix && *prefix != '\0') {
asprintf (&ec->name, "%s%s", prefix, $2);
if (ec->name == NULL)
errx(1, "malloc");
free($2);
} else
ec->name = $2;
ec->string = $4;
APPEND(codes, ec);
number++;
}
| END
{
YYACCEPT;
}
;
%%
static long
name2number(const char *str)
{
const char *p;
long num = 0;
const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789_";
if(strlen(str) > 4) {
yyerror("table name too long");
return 0;
}
for(p = str; *p; p++){
char *q = strchr(x, *p);
if(q == NULL) {
yyerror("invalid character in table name");
return 0;
}
num = (num << 6) + (q - x) + 1;
}
num <<= 8;
if(num > 0x7fffffff)
num = -(0xffffffff - num + 1);
return num;
}
void
yyerror (char *s)
{
error_message ("%s\n", s);
}
+124
View File
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2003 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: aes.c,v 1.5 2005/06/18 22:46:35 lha Exp $");
#endif
#ifdef KRB5
#include <krb5-types.h>
#endif
#include <string.h>
#include "rijndael-alg-fst.h"
#include "aes.h"
int
AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
{
key->rounds = rijndaelKeySetupEnc(key->key, userkey, bits);
if (key->rounds == 0)
return -1;
return 0;
}
int
AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
{
key->rounds = rijndaelKeySetupDec(key->key, userkey, bits);
if (key->rounds == 0)
return -1;
return 0;
}
void
AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
{
rijndaelEncrypt(key->key, key->rounds, in, out);
}
void
AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
{
rijndaelDecrypt(key->key, key->rounds, in, out);
}
void
AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
unsigned long size, const AES_KEY *key,
unsigned char *iv, int forward_encrypt)
{
unsigned char tmp[AES_BLOCK_SIZE];
int i;
if (forward_encrypt) {
while (size >= AES_BLOCK_SIZE) {
for (i = 0; i < AES_BLOCK_SIZE; i++)
tmp[i] = in[i] ^ iv[i];
AES_encrypt(tmp, out, key);
memcpy(iv, out, AES_BLOCK_SIZE);
size -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (size) {
for (i = 0; i < size; i++)
tmp[i] = in[i] ^ iv[i];
for (i = size; i < AES_BLOCK_SIZE; i++)
tmp[i] = iv[i];
AES_encrypt(tmp, out, key);
memcpy(iv, out, AES_BLOCK_SIZE);
}
} else {
while (size >= AES_BLOCK_SIZE) {
memcpy(tmp, in, AES_BLOCK_SIZE);
AES_decrypt(tmp, out, key);
for (i = 0; i < AES_BLOCK_SIZE; i++)
out[i] ^= iv[i];
memcpy(iv, tmp, AES_BLOCK_SIZE);
size -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (size) {
memcpy(tmp, in, AES_BLOCK_SIZE);
AES_decrypt(tmp, out, key);
for (i = 0; i < size; i++)
out[i] ^= iv[i];
memcpy(iv, tmp, AES_BLOCK_SIZE);
}
}
}
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2003-2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: aes.h,v 1.6 2006/05/05 11:06:35 lha Exp $ */
#ifndef HEIM_AES_H
#define HEIM_AES_H 1
/* symbol renaming */
#define AES_set_encrypt_key hc_AES_set_encrypt_key
#define AES_set_decrypt_key hc_AES_decrypt_key
#define AES_encrypt hc_AES_encrypt
#define AES_decrypt hc_AES_decrypt
#define AES_cbc_encrypt hc_AES_cbc_encrypt
/*
*
*/
#define AES_BLOCK_SIZE 16
#define AES_MAXNR 14
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
typedef struct aes_key {
uint32_t key[(AES_MAXNR+1)*4];
int rounds;
} AES_KEY;
int AES_set_encrypt_key(const unsigned char *, const int, AES_KEY *);
int AES_set_decrypt_key(const unsigned char *, const int, AES_KEY *);
void AES_encrypt(const unsigned char *, unsigned char *, const AES_KEY *);
void AES_decrypt(const unsigned char *, unsigned char *, const AES_KEY *);
void AES_cbc_encrypt(const unsigned char *, unsigned char *,
const unsigned long, const AES_KEY *,
unsigned char *, int);
#endif /* HEIM_AES_H */
+121
View File
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: bn.h,v 1.3 2006/01/13 08:27:50 lha Exp $
*/
#ifndef _HEIM_BN_H
#define _HEIM_BN_H 1
/* symbol renaming */
#define BN_GENCB_call hc_BN_GENCB_call
#define BN_GENCB_set hc_BN_GENCB_set
#define BN_bin2bn hc_BN_bin2bn
#define BN_bn2bin hc_BN_bn2bin
#define BN_bn2hex hc_BN_bn2hex
#define BN_clear hc_BN_clear
#define BN_clear_bit hc_BN_clear_bit
#define BN_clear_free hc_BN_clear_free
#define BN_cmp hc_BN_cmp
#define BN_dup hc_BN_dup
#define BN_free hc_BN_free
#define BN_is_negative hc_BN_is_negative
#define BN_get_word hc_BN_get_word
#define BN_hex2bn hc_BN_hex2bn
#define BN_is_bit_set hc_BN_is_bit_set
#define BN_new hc_BN_new
#define BN_num_bits hc_BN_num_bits
#define BN_num_bytes hc_BN_num_bytes
#define BN_rand hc_BN_rand
#define BN_set_bit hc_BN_set_bit
#define BN_set_negative hc_BN_set_negative
#define BN_set_word hc_BN_set_word
#define BN_uadd hc_BN_uadd
/*
*
*/
typedef void BIGNUM;
typedef struct BN_GENCB BN_GENCB;
typedef void BN_CTX;
typedef void BN_MONT_CTX;
typedef void BN_BLINDING;
struct BN_GENCB {
unsigned int ver;
void *arg;
union {
int (*cb_2)(int, int, BN_GENCB *);
} cb;
};
/*
*
*/
BIGNUM *BN_new(void);
void BN_free(BIGNUM *);
void BN_clear_free(BIGNUM *);
void BN_clear(BIGNUM *);
BIGNUM *BN_dup(const BIGNUM *);
int BN_num_bits(const BIGNUM *);
int BN_num_bytes(const BIGNUM *);
int BN_cmp(const BIGNUM *, const BIGNUM *);
void BN_set_negative(BIGNUM *, int);
int BN_is_negative(BIGNUM *);
int BN_is_bit_set(const BIGNUM *, int);
int BN_set_bit(BIGNUM *, int);
int BN_clear_bit(BIGNUM *, int);
int BN_set_word(BIGNUM *, unsigned long);
unsigned long BN_get_word(const BIGNUM *);
BIGNUM *BN_bin2bn(const void *,int len,BIGNUM *);
int BN_bn2bin(const BIGNUM *, void *);
int BN_hex2bn(BIGNUM **, const char *);
char * BN_bn2hex(const BIGNUM *);
int BN_uadd(BIGNUM *, const BIGNUM *, const BIGNUM *);
int BN_rand(BIGNUM *, int, int, int);
void BN_GENCB_set(BN_GENCB *, int (*)(int, int, BN_GENCB *), void *);
int BN_GENCB_call(BN_GENCB *, int, int);
#endif
+196
View File
@@ -0,0 +1,196 @@
/* GENERATE FILE from gen-des.pl, do not edit */
/* pc1_c_3 bit pattern 5 13 21 */
static int pc1_c_3[8] = {
0x00000000, 0x00000010, 0x00001000, 0x00001010,
0x00100000, 0x00100010, 0x00101000, 0x00101010
};
/* pc1_c_4 bit pattern 1 9 17 25 */
static int pc1_c_4[16] = {
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
};
/* pc1_d_3 bit pattern 49 41 33 */
static int pc1_d_3[8] = {
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100
};
/* pc1_d_4 bit pattern 57 53 45 37 */
static int pc1_d_4[16] = {
0x00000000, 0x00100000, 0x00001000, 0x00101000,
0x00000010, 0x00100010, 0x00001010, 0x00101010,
0x00000001, 0x00100001, 0x00001001, 0x00101001,
0x00000011, 0x00100011, 0x00001011, 0x00101011
};
/* pc2_c_1 bit pattern 5 24 7 16 6 10 */
static int pc2_c_1[64] = {
0x00000000, 0x00004000, 0x00040000, 0x00044000,
0x00000100, 0x00004100, 0x00040100, 0x00044100,
0x00020000, 0x00024000, 0x00060000, 0x00064000,
0x00020100, 0x00024100, 0x00060100, 0x00064100,
0x00000001, 0x00004001, 0x00040001, 0x00044001,
0x00000101, 0x00004101, 0x00040101, 0x00044101,
0x00020001, 0x00024001, 0x00060001, 0x00064001,
0x00020101, 0x00024101, 0x00060101, 0x00064101,
0x00080000, 0x00084000, 0x000c0000, 0x000c4000,
0x00080100, 0x00084100, 0x000c0100, 0x000c4100,
0x000a0000, 0x000a4000, 0x000e0000, 0x000e4000,
0x000a0100, 0x000a4100, 0x000e0100, 0x000e4100,
0x00080001, 0x00084001, 0x000c0001, 0x000c4001,
0x00080101, 0x00084101, 0x000c0101, 0x000c4101,
0x000a0001, 0x000a4001, 0x000e0001, 0x000e4001,
0x000a0101, 0x000a4101, 0x000e0101, 0x000e4101
};
/* pc2_c_2 bit pattern 20 18 12 3 15 23 */
static int pc2_c_2[64] = {
0x00000000, 0x00000002, 0x00000200, 0x00000202,
0x00200000, 0x00200002, 0x00200200, 0x00200202,
0x00001000, 0x00001002, 0x00001200, 0x00001202,
0x00201000, 0x00201002, 0x00201200, 0x00201202,
0x00000040, 0x00000042, 0x00000240, 0x00000242,
0x00200040, 0x00200042, 0x00200240, 0x00200242,
0x00001040, 0x00001042, 0x00001240, 0x00001242,
0x00201040, 0x00201042, 0x00201240, 0x00201242,
0x00000010, 0x00000012, 0x00000210, 0x00000212,
0x00200010, 0x00200012, 0x00200210, 0x00200212,
0x00001010, 0x00001012, 0x00001210, 0x00001212,
0x00201010, 0x00201012, 0x00201210, 0x00201212,
0x00000050, 0x00000052, 0x00000250, 0x00000252,
0x00200050, 0x00200052, 0x00200250, 0x00200252,
0x00001050, 0x00001052, 0x00001250, 0x00001252,
0x00201050, 0x00201052, 0x00201250, 0x00201252
};
/* pc2_c_3 bit pattern 1 9 19 2 14 22 */
static int pc2_c_3[64] = {
0x00000000, 0x00000004, 0x00000400, 0x00000404,
0x00400000, 0x00400004, 0x00400400, 0x00400404,
0x00000020, 0x00000024, 0x00000420, 0x00000424,
0x00400020, 0x00400024, 0x00400420, 0x00400424,
0x00008000, 0x00008004, 0x00008400, 0x00008404,
0x00408000, 0x00408004, 0x00408400, 0x00408404,
0x00008020, 0x00008024, 0x00008420, 0x00008424,
0x00408020, 0x00408024, 0x00408420, 0x00408424,
0x00800000, 0x00800004, 0x00800400, 0x00800404,
0x00c00000, 0x00c00004, 0x00c00400, 0x00c00404,
0x00800020, 0x00800024, 0x00800420, 0x00800424,
0x00c00020, 0x00c00024, 0x00c00420, 0x00c00424,
0x00808000, 0x00808004, 0x00808400, 0x00808404,
0x00c08000, 0x00c08004, 0x00c08400, 0x00c08404,
0x00808020, 0x00808024, 0x00808420, 0x00808424,
0x00c08020, 0x00c08024, 0x00c08420, 0x00c08424
};
/* pc2_c_4 bit pattern 11 13 4 17 21 8 */
static int pc2_c_4[64] = {
0x00000000, 0x00010000, 0x00000008, 0x00010008,
0x00000080, 0x00010080, 0x00000088, 0x00010088,
0x00100000, 0x00110000, 0x00100008, 0x00110008,
0x00100080, 0x00110080, 0x00100088, 0x00110088,
0x00000800, 0x00010800, 0x00000808, 0x00010808,
0x00000880, 0x00010880, 0x00000888, 0x00010888,
0x00100800, 0x00110800, 0x00100808, 0x00110808,
0x00100880, 0x00110880, 0x00100888, 0x00110888,
0x00002000, 0x00012000, 0x00002008, 0x00012008,
0x00002080, 0x00012080, 0x00002088, 0x00012088,
0x00102000, 0x00112000, 0x00102008, 0x00112008,
0x00102080, 0x00112080, 0x00102088, 0x00112088,
0x00002800, 0x00012800, 0x00002808, 0x00012808,
0x00002880, 0x00012880, 0x00002888, 0x00012888,
0x00102800, 0x00112800, 0x00102808, 0x00112808,
0x00102880, 0x00112880, 0x00102888, 0x00112888
};
/* pc2_d_1 bit pattern 51 35 31 52 39 45 */
static int pc2_d_1[64] = {
0x00000000, 0x00000080, 0x00002000, 0x00002080,
0x00000001, 0x00000081, 0x00002001, 0x00002081,
0x00200000, 0x00200080, 0x00202000, 0x00202080,
0x00200001, 0x00200081, 0x00202001, 0x00202081,
0x00020000, 0x00020080, 0x00022000, 0x00022080,
0x00020001, 0x00020081, 0x00022001, 0x00022081,
0x00220000, 0x00220080, 0x00222000, 0x00222080,
0x00220001, 0x00220081, 0x00222001, 0x00222081,
0x00000002, 0x00000082, 0x00002002, 0x00002082,
0x00000003, 0x00000083, 0x00002003, 0x00002083,
0x00200002, 0x00200082, 0x00202002, 0x00202082,
0x00200003, 0x00200083, 0x00202003, 0x00202083,
0x00020002, 0x00020082, 0x00022002, 0x00022082,
0x00020003, 0x00020083, 0x00022003, 0x00022083,
0x00220002, 0x00220082, 0x00222002, 0x00222082,
0x00220003, 0x00220083, 0x00222003, 0x00222083
};
/* pc2_d_2 bit pattern 50 32 43 36 29 48 */
static int pc2_d_2[64] = {
0x00000000, 0x00000010, 0x00800000, 0x00800010,
0x00010000, 0x00010010, 0x00810000, 0x00810010,
0x00000200, 0x00000210, 0x00800200, 0x00800210,
0x00010200, 0x00010210, 0x00810200, 0x00810210,
0x00100000, 0x00100010, 0x00900000, 0x00900010,
0x00110000, 0x00110010, 0x00910000, 0x00910010,
0x00100200, 0x00100210, 0x00900200, 0x00900210,
0x00110200, 0x00110210, 0x00910200, 0x00910210,
0x00000004, 0x00000014, 0x00800004, 0x00800014,
0x00010004, 0x00010014, 0x00810004, 0x00810014,
0x00000204, 0x00000214, 0x00800204, 0x00800214,
0x00010204, 0x00010214, 0x00810204, 0x00810214,
0x00100004, 0x00100014, 0x00900004, 0x00900014,
0x00110004, 0x00110014, 0x00910004, 0x00910014,
0x00100204, 0x00100214, 0x00900204, 0x00900214,
0x00110204, 0x00110214, 0x00910204, 0x00910214
};
/* pc2_d_3 bit pattern 41 38 47 33 40 42 */
static int pc2_d_3[64] = {
0x00000000, 0x00000400, 0x00001000, 0x00001400,
0x00080000, 0x00080400, 0x00081000, 0x00081400,
0x00000020, 0x00000420, 0x00001020, 0x00001420,
0x00080020, 0x00080420, 0x00081020, 0x00081420,
0x00004000, 0x00004400, 0x00005000, 0x00005400,
0x00084000, 0x00084400, 0x00085000, 0x00085400,
0x00004020, 0x00004420, 0x00005020, 0x00005420,
0x00084020, 0x00084420, 0x00085020, 0x00085420,
0x00000800, 0x00000c00, 0x00001800, 0x00001c00,
0x00080800, 0x00080c00, 0x00081800, 0x00081c00,
0x00000820, 0x00000c20, 0x00001820, 0x00001c20,
0x00080820, 0x00080c20, 0x00081820, 0x00081c20,
0x00004800, 0x00004c00, 0x00005800, 0x00005c00,
0x00084800, 0x00084c00, 0x00085800, 0x00085c00,
0x00004820, 0x00004c20, 0x00005820, 0x00005c20,
0x00084820, 0x00084c20, 0x00085820, 0x00085c20
};
/* pc2_d_4 bit pattern 49 37 30 46 34 44 */
static int pc2_d_4[64] = {
0x00000000, 0x00000100, 0x00040000, 0x00040100,
0x00000040, 0x00000140, 0x00040040, 0x00040140,
0x00400000, 0x00400100, 0x00440000, 0x00440100,
0x00400040, 0x00400140, 0x00440040, 0x00440140,
0x00008000, 0x00008100, 0x00048000, 0x00048100,
0x00008040, 0x00008140, 0x00048040, 0x00048140,
0x00408000, 0x00408100, 0x00448000, 0x00448100,
0x00408040, 0x00408140, 0x00448040, 0x00448140,
0x00000008, 0x00000108, 0x00040008, 0x00040108,
0x00000048, 0x00000148, 0x00040048, 0x00040148,
0x00400008, 0x00400108, 0x00440008, 0x00440108,
0x00400048, 0x00400148, 0x00440048, 0x00440148,
0x00008008, 0x00008108, 0x00048008, 0x00048108,
0x00008048, 0x00008148, 0x00048048, 0x00048148,
0x00408008, 0x00408108, 0x00448008, 0x00448108,
0x00408048, 0x00408148, 0x00448048, 0x00448148
};
static unsigned char odd_parity[256] = {
1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254,
};
+967
View File
@@ -0,0 +1,967 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* The document that got me started for real was "Efficient
* Implementation of the Data Encryption Standard" by Dag Arne Osvik.
* I never got to the PC1 transformation was working, instead I used
* table-lookup was used for all key schedule setup. The document was
* very useful since it de-mystified other implementations for me.
*
* The core DES function (SBOX + P transformation) is from Richard
* Outerbridge public domain DES implementation. My sanity is saved
* thanks to his work. Thank you Richard.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: des.c,v 1.18 2006/04/24 14:26:19 lha Exp $");
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <krb5-types.h>
#include <assert.h>
#include "des.h"
#include "ui.h"
static void desx(uint32_t [2], DES_key_schedule *, int);
static void IP(uint32_t [2]);
static void FP(uint32_t [2]);
#include "des-tables.h"
#define ROTATE_LEFT28(x,one) \
if (one) { \
x = ( ((x)<<(1)) & 0xffffffe) | ((x) >> 27); \
} else { \
x = ( ((x)<<(2)) & 0xffffffc) | ((x) >> 26); \
}
/*
*
*/
int
DES_set_odd_parity(DES_cblock *key)
{
int i;
for (i = 0; i < DES_CBLOCK_LEN; i++)
(*key)[i] = odd_parity[(*key)[i]];
return 0;
}
/*
*
*/
/* FIPS 74 */
static DES_cblock weak_keys[] = {
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, /* weak keys */
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, /* semi-weak keys */
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}
};
int
DES_is_weak_key(DES_cblock *key)
{
int i;
for (i = 0; i < sizeof(weak_keys)/sizeof(weak_keys[0]); i++) {
if (memcmp(weak_keys[i], key, DES_CBLOCK_LEN) == 0)
return 1;
}
return 0;
}
/*
*
*/
int
DES_set_key(DES_cblock *key, DES_key_schedule *ks)
{
uint32_t t1, t2;
uint32_t c, d;
int shifts[16] = { 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
uint32_t *k = &ks->ks[0];
int i;
t1 = (*key)[0] << 24 | (*key)[1] << 16 | (*key)[2] << 8 | (*key)[3];
t2 = (*key)[4] << 24 | (*key)[5] << 16 | (*key)[6] << 8 | (*key)[7];
c = (pc1_c_3[(t1 >> (5 )) & 0x7] << 3)
| (pc1_c_3[(t1 >> (5 + 8 )) & 0x7] << 2)
| (pc1_c_3[(t1 >> (5 + 8 + 8 )) & 0x7] << 1)
| (pc1_c_3[(t1 >> (5 + 8 + 8 + 8)) & 0x7] << 0)
| (pc1_c_4[(t2 >> (4 )) & 0xf] << 3)
| (pc1_c_4[(t2 >> (4 + 8 )) & 0xf] << 2)
| (pc1_c_4[(t2 >> (4 + 8 + 8 )) & 0xf] << 1)
| (pc1_c_4[(t2 >> (4 + 8 + 8 + 8)) & 0xf] << 0);
d = (pc1_d_3[(t2 >> (1 )) & 0x7] << 3)
| (pc1_d_3[(t2 >> (1 + 8 )) & 0x7] << 2)
| (pc1_d_3[(t2 >> (1 + 8 + 8 )) & 0x7] << 1)
| (pc1_d_3[(t2 >> (1 + 8 + 8 + 8)) & 0x7] << 0)
| (pc1_d_4[(t1 >> (1 )) & 0xf] << 3)
| (pc1_d_4[(t1 >> (1 + 8 )) & 0xf] << 2)
| (pc1_d_4[(t1 >> (1 + 8 + 8 )) & 0xf] << 1)
| (pc1_d_4[(t1 >> (1 + 8 + 8 + 8)) & 0xf] << 0);
for (i = 0; i < 16; i++) {
uint32_t kc, kd;
ROTATE_LEFT28(c, shifts[i]);
ROTATE_LEFT28(d, shifts[i]);
kc = pc2_c_1[(c >> 22) & 0x3f] |
pc2_c_2[((c >> 16) & 0x30) | ((c >> 15) & 0xf)] |
pc2_c_3[((c >> 9 ) & 0x3c) | ((c >> 8 ) & 0x3)] |
pc2_c_4[((c >> 2 ) & 0x20) | ((c >> 1) & 0x18) | (c & 0x7)];
kd = pc2_d_1[(d >> 22) & 0x3f] |
pc2_d_2[((d >> 15) & 0x30) | ((d >> 14) & 0xf)] |
pc2_d_3[ (d >> 7 ) & 0x3f] |
pc2_d_4[((d >> 1 ) & 0x3c) | ((d ) & 0x3)];
/* Change to byte order used by the S boxes */
*k = (kc & 0x00fc0000L) << 6;
*k |= (kc & 0x00000fc0L) << 10;
*k |= (kd & 0x00fc0000L) >> 10;
*k++ |= (kd & 0x00000fc0L) >> 6;
*k = (kc & 0x0003f000L) << 12;
*k |= (kc & 0x0000003fL) << 16;
*k |= (kd & 0x0003f000L) >> 4;
*k++ |= (kd & 0x0000003fL);
}
return 0;
}
/*
*
*/
int
DES_set_key_checked(DES_cblock *key, DES_key_schedule *ks)
{
if (DES_is_weak_key(key)) {
memset(ks, 0, sizeof(*ks));
return 1;
}
return DES_set_key(key, ks);
}
/*
* Compatibility function for eay libdes
*/
int
DES_key_sched(DES_cblock *key, DES_key_schedule *ks)
{
return DES_set_key(key, ks);
}
/*
*
*/
static void
load(const unsigned char *b, uint32_t v[2])
{
v[0] = b[0] << 24;
v[0] |= b[1] << 16;
v[0] |= b[2] << 8;
v[0] |= b[3] << 0;
v[1] = b[4] << 24;
v[1] |= b[5] << 16;
v[1] |= b[6] << 8;
v[1] |= b[7] << 0;
}
static void
store(const uint32_t v[2], unsigned char *b)
{
b[0] = (v[0] >> 24) & 0xff;
b[1] = (v[0] >> 16) & 0xff;
b[2] = (v[0] >> 8) & 0xff;
b[3] = (v[0] >> 0) & 0xff;
b[4] = (v[1] >> 24) & 0xff;
b[5] = (v[1] >> 16) & 0xff;
b[6] = (v[1] >> 8) & 0xff;
b[7] = (v[1] >> 0) & 0xff;
}
/*
*
*/
void
DES_encrypt(uint32_t u[2], DES_key_schedule *ks, int forward_encrypt)
{
IP(u);
desx(u, ks, forward_encrypt);
FP(u);
}
/*
*
*/
void
DES_ecb_encrypt(DES_cblock *input, DES_cblock *output,
DES_key_schedule *ks, int forward_encrypt)
{
uint32_t u[2];
load(*input, u);
DES_encrypt(u, ks, forward_encrypt);
store(u, *output);
}
/*
*
*/
void
DES_cbc_encrypt(const void *in, void *out, long length,
DES_key_schedule *ks, DES_cblock *iv, int forward_encrypt)
{
const unsigned char *input = in;
unsigned char *output = out;
uint32_t u[2];
uint32_t uiv[2];
load(*iv, uiv);
if (forward_encrypt) {
while (length >= DES_CBLOCK_LEN) {
load(input, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
uiv[0] = u[0]; uiv[1] = u[1];
store(u, output);
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
store(u, output);
}
} else {
uint32_t t[2];
while (length >= DES_CBLOCK_LEN) {
load(input, u);
t[0] = u[0]; t[1] = u[1];
DES_encrypt(u, ks, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
store(u, output);
uiv[0] = t[0]; uiv[1] = t[1];
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
DES_encrypt(u, ks, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
store(u, output);
}
}
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
}
/*
*
*/
void
DES_pcbc_encrypt(const void *in, void *out, long length,
DES_key_schedule *ks, DES_cblock *iv, int forward_encrypt)
{
const unsigned char *input = in;
unsigned char *output = out;
uint32_t u[2];
uint32_t uiv[2];
load(*iv, uiv);
if (forward_encrypt) {
uint32_t t[2];
while (length >= DES_CBLOCK_LEN) {
load(input, u);
t[0] = u[0]; t[1] = u[1];
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
uiv[0] = u[0] ^ t[0]; uiv[1] = u[1] ^ t[1];
store(u, output);
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
store(u, output);
}
} else {
uint32_t t[2];
while (length >= DES_CBLOCK_LEN) {
load(input, u);
t[0] = u[0]; t[1] = u[1];
DES_encrypt(u, ks, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
store(u, output);
uiv[0] = t[0] ^ u[0]; uiv[1] = t[1] ^ u[1];
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
DES_encrypt(u, ks, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
}
}
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
}
/*
*
*/
static void
_des3_encrypt(uint32_t u[2], DES_key_schedule *ks1, DES_key_schedule *ks2,
DES_key_schedule *ks3, int forward_encrypt)
{
IP(u);
if (forward_encrypt) {
desx(u, ks1, 1); /* IP + FP cancel out each other */
desx(u, ks2, 0);
desx(u, ks3, 1);
} else {
desx(u, ks3, 0);
desx(u, ks2, 1);
desx(u, ks1, 0);
}
FP(u);
}
/*
*
*/
void
DES_ecb3_encrypt(DES_cblock *input,
DES_cblock *output,
DES_key_schedule *ks1,
DES_key_schedule *ks2,
DES_key_schedule *ks3,
int forward_encrypt)
{
uint32_t u[2];
load(*input, u);
_des3_encrypt(u, ks1, ks2, ks3, forward_encrypt);
store(u, *output);
return;
}
/*
*
*/
void
DES_ede3_cbc_encrypt(const void *in, void *out,
long length, DES_key_schedule *ks1,
DES_key_schedule *ks2, DES_key_schedule *ks3,
DES_cblock *iv, int forward_encrypt)
{
const unsigned char *input = in;
unsigned char *output = out;
uint32_t u[2];
uint32_t uiv[2];
load(*iv, uiv);
if (forward_encrypt) {
while (length >= DES_CBLOCK_LEN) {
load(input, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
_des3_encrypt(u, ks1, ks2, ks3, 1);
uiv[0] = u[0]; uiv[1] = u[1];
store(u, output);
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
_des3_encrypt(u, ks1, ks2, ks3, 1);
store(u, output);
}
} else {
uint32_t t[2];
while (length >= DES_CBLOCK_LEN) {
load(input, u);
t[0] = u[0]; t[1] = u[1];
_des3_encrypt(u, ks1, ks2, ks3, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
store(u, output);
uiv[0] = t[0]; uiv[1] = t[1];
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
output += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
_des3_encrypt(u, ks1, ks2, ks3, 0);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
store(u, output);
}
}
store(uiv, *iv);
uiv[0] = 0; u[0] = 0; uiv[1] = 0; u[1] = 0;
}
/*
*
*/
void
DES_cfb64_encrypt(const void *in, void *out,
long length, DES_key_schedule *ks, DES_cblock *iv,
int *num, int forward_encrypt)
{
const unsigned char *input = in;
unsigned char *output = out;
unsigned char tmp[DES_CBLOCK_LEN];
uint32_t uiv[2];
load(*iv, uiv);
assert(*num >= 0 && *num < DES_CBLOCK_LEN);
if (forward_encrypt) {
int i = *num;
while (length > 0) {
if (i == 0)
DES_encrypt(uiv, ks, 1);
store(uiv, tmp);
for (; i < DES_CBLOCK_LEN && i < length; i++) {
output[i] = tmp[i] ^ input[i];
}
if (i == DES_CBLOCK_LEN)
load(output, uiv);
output += i;
input += i;
length -= i;
if (i == DES_CBLOCK_LEN)
i = 0;
}
store(uiv, *iv);
*num = i;
} else {
int i = *num;
unsigned char c;
while (length > 0) {
if (i == 0) {
DES_encrypt(uiv, ks, 1);
store(uiv, tmp);
}
for (; i < DES_CBLOCK_LEN && i < length; i++) {
c = input[i];
output[i] = tmp[i] ^ input[i];
(*iv)[i] = c;
}
output += i;
input += i;
length -= i;
if (i == DES_CBLOCK_LEN) {
i = 0;
load(*iv, uiv);
}
}
store(uiv, *iv);
*num = i;
}
}
/*
*
*/
uint32_t
DES_cbc_cksum(const void *in, DES_cblock *output,
long length, DES_key_schedule *ks, DES_cblock *iv)
{
const unsigned char *input = in;
uint32_t uiv[2];
uint32_t u[2] = { 0, 0 };
load(*iv, uiv);
while (length >= DES_CBLOCK_LEN) {
load(input, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
uiv[0] = u[0]; uiv[1] = u[1];
length -= DES_CBLOCK_LEN;
input += DES_CBLOCK_LEN;
}
if (length) {
unsigned char tmp[DES_CBLOCK_LEN];
memcpy(tmp, input, length);
memset(tmp + length, 0, DES_CBLOCK_LEN - length);
load(tmp, u);
u[0] ^= uiv[0]; u[1] ^= uiv[1];
DES_encrypt(u, ks, 1);
}
if (output)
store(u, *output);
uiv[0] = 0; u[0] = 0; uiv[1] = 0;
return u[1];
}
/*
*
*/
static unsigned char
bitswap8(unsigned char b)
{
unsigned char r = 0;
int i;
for (i = 0; i < 8; i++) {
r = r << 1 | (b & 1);
b = b >> 1;
}
return r;
}
void
DES_string_to_key(const char *str, DES_cblock *key)
{
const unsigned char *s;
unsigned char *k;
DES_key_schedule ks;
size_t i, len;
memset(key, 0, sizeof(*key));
k = *key;
s = (const unsigned char *)str;
len = strlen(str);
for (i = 0; i < len; i++) {
if ((i % 16) < 8)
k[i % 8] ^= s[i] << 1;
else
k[7 - (i % 8)] ^= bitswap8(s[i]);
}
DES_set_odd_parity(key);
if (DES_is_weak_key(key))
k[7] ^= 0xF0;
DES_set_key(key, &ks);
DES_cbc_cksum(s, key, len, &ks, key);
memset(&ks, 0, sizeof(ks));
DES_set_odd_parity(key);
if (DES_is_weak_key(key))
k[7] ^= 0xF0;
}
/*
*
*/
int
DES_read_password(DES_cblock *key, char *prompt, int verify)
{
char buf[512];
int ret;
ret = UI_UTIL_read_pw_string(buf, sizeof(buf) - 1, prompt, verify);
if (ret == 0)
DES_string_to_key(buf, key);
return ret;
}
/*
*
*/
void
_DES_ipfp_test(void)
{
DES_cblock k = "\x01\x02\x04\x08\x10\x20\x40\x80", k2;
uint32_t u[2] = { 1, 0 };
IP(u);
FP(u);
IP(u);
FP(u);
if (u[0] != 1 || u[1] != 0)
abort();
load(k, u);
store(u, k2);
if (memcmp(k, k2, 8) != 0)
abort();
}
/* D3DES (V5.09) -
*
* A portable, public domain, version of the Data Encryption Standard.
*
* Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
* Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
* code; Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
* Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
* for humouring me on.
*
* Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
* (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
*/
static uint32_t SP1[64] = {
0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
static uint32_t SP2[64] = {
0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
static uint32_t SP3[64] = {
0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
static uint32_t SP4[64] = {
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
static uint32_t SP5[64] = {
0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
static uint32_t SP6[64] = {
0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
static uint32_t SP7[64] = {
0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
static uint32_t SP8[64] = {
0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
static void
IP(uint32_t v[2])
{
uint32_t work;
work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
v[1] ^= work;
v[0] ^= (work << 4);
work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
v[1] ^= work;
v[0] ^= (work << 16);
work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
v[0] ^= work;
v[1] ^= (work << 2);
work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
v[0] ^= work;
v[1] ^= (work << 8);
v[1] = ((v[1] << 1) | ((v[1] >> 31) & 1L)) & 0xffffffffL;
work = (v[0] ^ v[1]) & 0xaaaaaaaaL;
v[0] ^= work;
v[1] ^= work;
v[0] = ((v[0] << 1) | ((v[0] >> 31) & 1L)) & 0xffffffffL;
}
static void
FP(uint32_t v[2])
{
uint32_t work;
v[0] = (v[0] << 31) | (v[0] >> 1);
work = (v[1] ^ v[0]) & 0xaaaaaaaaL;
v[1] ^= work;
v[0] ^= work;
v[1] = (v[1] << 31) | (v[1] >> 1);
work = ((v[1] >> 8) ^ v[0]) & 0x00ff00ffL;
v[0] ^= work;
v[1] ^= (work << 8);
work = ((v[1] >> 2) ^ v[0]) & 0x33333333L;
v[0] ^= work;
v[1] ^= (work << 2);
work = ((v[0] >> 16) ^ v[1]) & 0x0000ffffL;
v[1] ^= work;
v[0] ^= (work << 16);
work = ((v[0] >> 4) ^ v[1]) & 0x0f0f0f0fL;
v[1] ^= work;
v[0] ^= (work << 4);
}
static void
desx(uint32_t block[2], DES_key_schedule *ks, int forward_encrypt)
{
uint32_t *keys;
uint32_t fval, work, right, left;
int round;
left = block[0];
right = block[1];
if (forward_encrypt) {
keys = &ks->ks[0];
for( round = 0; round < 8; round++ ) {
work = (right << 28) | (right >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = right ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
left ^= fval;
work = (left << 28) | (left >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = left ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
right ^= fval;
}
} else {
keys = &ks->ks[30];
for( round = 0; round < 8; round++ ) {
work = (right << 28) | (right >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = right ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
left ^= fval;
work = (left << 28) | (left >> 4);
keys -= 4;
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = left ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
right ^= fval;
keys -= 4;
}
}
block[0] = right;
block[1] = left;
}
+124
View File
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: des.h,v 1.25 2006/01/08 21:47:28 lha Exp $ */
#ifndef _DESperate_H
#define _DESperate_H 1
/* symbol renaming */
#define DES_set_odd_parity hc_DES_set_odd_parity
#define DES_is_weak_key hc_DES_is_weak_key
#define DES_key_sched hc_DES_key_sched
#define DES_set_key hc_DES_set_key
#define DES_set_key_checked hc_DES_set_key_checked
#define DES_set_key_sched hc_DES_set_key_sched
#define DES_new_random_key hc_DES_new_random_key
#define DES_string_to_key hc_DES_string_to_key
#define DES_read_password hc_DES_read_password
#define DES_rand_data hc_DES_rand_data
#define DES_set_random_generator_seed hc_DES_set_random_generator_seed
#define DES_generate_random_block hc_DES_generate_random_block
#define DES_set_sequence_number hc_DES_set_sequence_number
#define DES_init_random_number_generator hc_DES_init_random_number_generator
#define DES_random_key hc_DES_random_key
#define DES_encrypt hc_DES_encrypt
#define DES_ecb_encrypt hc_DES_ecb_encrypt
#define DES_ecb3_encrypt hc_DES_ecb3_encrypt
#define DES_pcbc_encrypt hc_DES_pcbc_encrypt
#define DES_cbc_encrypt hc_DES_cbc_encrypt
#define DES_cbc_cksum hc_DES_cbc_cksum
#define DES_ede3_cbc_encrypt hc_DES_ede3_cbc_encrypt
#define DES_cfb64_encrypt hc_DES_cfb64_encrypt
#define _DES_ipfp_test _hc_DES_ipfp_test
/*
*
*/
#define DES_CBLOCK_LEN 8
#define DES_KEY_SZ 8
#define DES_ENCRYPT 1
#define DES_DECRYPT 0
typedef unsigned char DES_cblock[DES_CBLOCK_LEN];
typedef struct DES_key_schedule
{
uint32_t ks[32];
} DES_key_schedule;
/*
*
*/
int DES_set_odd_parity(DES_cblock *);
int DES_is_weak_key(DES_cblock *);
int DES_set_key(DES_cblock *, DES_key_schedule *);
int DES_set_key_checked(DES_cblock *, DES_key_schedule *);
int DES_key_sched(DES_cblock *, DES_key_schedule *);
int DES_new_random_key(DES_cblock *);
void DES_string_to_key(const char *, DES_cblock *);
int DES_read_password(DES_cblock *, char *, int);
void DES_rand_data(void *, int);
void DES_set_random_generator_seed(DES_cblock *);
void DES_generate_random_block(DES_cblock *);
void DES_set_sequence_number(void *);
void DES_init_random_number_generator(DES_cblock *);
void DES_random_key(DES_cblock *);
void DES_encrypt(uint32_t [2], DES_key_schedule *, int);
void DES_ecb_encrypt(DES_cblock *, DES_cblock *, DES_key_schedule *, int);
void DES_ecb3_encrypt(DES_cblock *,DES_cblock *, DES_key_schedule *,
DES_key_schedule *, DES_key_schedule *, int);
void DES_pcbc_encrypt(const void *, void *, long,
DES_key_schedule *, DES_cblock *, int);
void DES_cbc_encrypt(const void *, void *, long,
DES_key_schedule *, DES_cblock *, int);
void DES_ede3_cbc_encrypt(const void *, void *, long,
DES_key_schedule *, DES_key_schedule *,
DES_key_schedule *, DES_cblock *, int);
void DES_cfb64_encrypt(const void *, void *, long,
DES_key_schedule *, DES_cblock *, int *, int);
uint32_t DES_cbc_cksum(const void *, DES_cblock *,
long, DES_key_schedule *, DES_cblock *);
void _DES_ipfp_test(void);
#endif /* _DESperate_H */
+141
View File
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: dh.h,v 1.6 2006/05/06 13:11:15 lha Exp $
*/
#ifndef _HEIM_DH_H
#define _HEIM_DH_H 1
/* symbol renaming */
#define DH_null_method hc_DH_null_method
#define DH_imath_method hc_DH_imath_method
#define DH_new hc_DH_new
#define DH_new_method hc_DH_new_method
#define DH_free hc_DH_free
#define DH_up_ref hc_DH_up_ref
#define DH_size hc_DH_size
#define DH_set_default_method hc_DH_set_default_method
#define DH_get_default_method hc_DH_get_default_method
#define DH_set_method hc_DH_set_method
#define DH_get_method hc_DH_get_method
#define DH_set_ex_data hc_DH_set_ex_data
#define DH_get_ex_data hc_DH_get_ex_data
#define DH_generate_parameters_ex hc_DH_generate_parameters_ex
#define DH_check_pubkey hc_DH_check_pubkey
#define DH_generate_key hc_DH_generate_key
#define DH_compute_key hc_DH_compute_key
/*
*
*/
typedef struct DH DH;
typedef struct DH_METHOD DH_METHOD;
#include <hcrypto/bn.h>
#include <hcrypto/engine.h>
struct DH_METHOD {
const char *name;
int (*generate_key)(DH *);
int (*compute_key)(unsigned char *,const BIGNUM *,DH *);
int (*bn_mod_exp)(const DH *, BIGNUM *, const BIGNUM *,
const BIGNUM *, const BIGNUM *, BN_CTX *,
BN_MONT_CTX *);
int (*init)(DH *);
int (*finish)(DH *);
int flags;
void *app_data;
int (*generate_params)(DH *, int, int, BN_GENCB *);
};
struct DH {
int pad;
int version;
BIGNUM *p;
BIGNUM *g;
long length;
BIGNUM *pub_key;
BIGNUM *priv_key;
int flags;
void *method_mont_p;
BIGNUM *q;
BIGNUM *j;
void *seed;
int seedlen;
BIGNUM *counter;
int references;
struct CRYPTO_EX_DATA {
void *sk;
int dummy;
} ex_data;
const DH_METHOD *meth;
ENGINE *engine;
};
/* DH_check_pubkey return codes in `codes' argument. */
#define DH_CHECK_PUBKEY_TOO_SMALL 1
#define DH_CHECK_PUBKEY_TOO_LARGE 2
/*
*
*/
const DH_METHOD *DH_null_method(void);
const DH_METHOD *DH_imath_method(void);
DH * DH_new(void);
DH * DH_new_method(ENGINE *);
void DH_free(DH *);
int DH_up_ref(DH *);
int DH_size(const DH *);
void DH_set_default_method(const DH_METHOD *);
const DH_METHOD *
DH_get_default_method(void);
int DH_set_method(DH *, const DH_METHOD *);
int DH_set_ex_data(DH *, int, void *);
void * DH_get_ex_data(DH *, int);
int DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
int DH_check_pubkey(const DH *, const BIGNUM *, int *);
int DH_generate_key(DH *);
int DH_compute_key(unsigned char *,const BIGNUM *,DH *);
#endif /* _HEIM_DH_H */
+140
View File
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: dsa.h,v 1.2 2006/01/13 15:26:52 lha Exp $
*/
#ifndef _HEIM_DSA_H
#define _HEIM_DSA_H 1
#include <hcrypto/bn.h>
/* symbol renaming */
#define DSA_null_method hc_DSA_null_method
#define DSA_new hc_DSA_new
#define DSA_free hc_DSA_free
#define DSA_up_ref hc_DSA_up_ref
#define DSA_set_default_method hc_DSA_set_default_method
#define DSA_get_default_method hc_DSA_get_default_method
#define DSA_set_method hc_DSA_set_method
#define DSA_get_method hc_DSA_get_method
#define DSA_set_app_data hc_DSA_set_app_data
#define DSA_get_app_data hc_DSA_get_app_data
#define DSA_size hc_DSA_size
#define DSA_verify hc_DSA_verify
/*
*
*/
typedef struct DSA DSA;
typedef struct DSA_METHOD DSA_METHOD;
typedef struct DSA_SIG DSA_SIG;
struct DSA_SIG {
BIGNUM *r;
BIGNUM *s;
};
struct DSA_METHOD {
const char *name;
DSA_SIG * (*dsa_do_sign)(const unsigned char *, int, DSA *);
int (*dsa_sign_setup)(DSA *, BN_CTX *, BIGNUM **, BIGNUM **);
int (*dsa_do_verify)(const unsigned char *, int, DSA_SIG *, DSA *);
int (*dsa_mod_exp)(DSA *, BIGNUM *, BIGNUM *, BIGNUM *,
BIGNUM *, BIGNUM *, BIGNUM *, BN_CTX *,
BN_MONT_CTX *);
int (*bn_mod_exp)(DSA *, BIGNUM *, BIGNUM *, const BIGNUM *,
const BIGNUM *, BN_CTX *,
BN_MONT_CTX *);
int (*init)(DSA *);
int (*finish)(DSA *);
int flags;
void *app_data;
};
struct DSA {
int pad;
long version;
int write_params;
BIGNUM *p;
BIGNUM *q;
BIGNUM *g;
BIGNUM *pub_key;
BIGNUM *priv_key;
BIGNUM *kinv;
BIGNUM *r;
int flags;
void *method_mont_p;
int references;
struct dsa_CRYPTO_EX_DATA {
void *sk;
int dummy;
} ex_data;
const DSA_METHOD *meth;
void *engine;
};
/*
*
*/
const DSA_METHOD *DSA_null_method(void);
/*
*
*/
DSA * DSA_new(void);
void DSA_free(DSA *);
int DSA_up_ref(DSA *);
void DSA_set_default_method(const DSA_METHOD *);
const DSA_METHOD * DSA_get_default_method(void);
const DSA_METHOD * DSA_get_method(const DSA *);
int DSA_set_method(DSA *, const DSA_METHOD *);
void DSA_set_app_data(DSA *, void *arg);
void * DSA_get_app_data(DSA *);
int DSA_size(const DSA *);
int DSA_verify(int, const unsigned char *, int,
const unsigned char *, int, DSA *);
#endif /* _HEIM_DSA_H */
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: engine.h,v 1.6 2006/05/06 12:34:36 lha Exp $
*/
#ifndef _HEIM_ENGINE_H
#define _HEIM_ENGINE_H 1
/* symbol renaming */
#define ENGINE_add_conf_module hc_ENGINE_add_conf_module
#define ENGINE_by_dso hc_ENGINE_by_dso
#define ENGINE_by_id hc_ENGINE_by_id
#define ENGINE_finish hc_ENGINE_finish
#define ENGINE_get_DH hc_ENGINE_get_DH
#define ENGINE_get_RSA hc_ENGINE_get_RSA
#define ENGINE_get_RAND hc_ENGINE_get_RAND
#define ENGINE_get_id hc_ENGINE_get_id
#define ENGINE_get_name hc_ENGINE_get_name
#define ENGINE_load_builtin_engines hc_ENGINE_load_builtin_engines
#define ENGINE_set_DH hc_ENGINE_set_DH
#define ENGINE_set_RSA hc_ENGINE_set_RSA
#define ENGINE_set_id hc_ENGINE_set_id
#define ENGINE_set_name hc_ENGINE_set_name
#define ENGINE_set_destroy_function hc_ENGINE_set_destroy_function
#define ENGINE_up_ref hc_ENGINE_up_ref
#define ENGINE_get_default_DH hc_ENGINE_get_default_DH
#define ENGINE_get_default_RSA hc_ENGINE_get_default_RSA
#define ENGINE_set_default_DH hc_ENGINE_set_default_DH
#define ENGINE_set_default_RSA hc_ENGINE_set_default_RSA
/*
*
*/
typedef struct hc_engine ENGINE;
#include <hcrypto/rsa.h>
#include <hcrypto/dsa.h>
#include <hcrypto/dh.h>
#include <hcrypto/rand.h>
#define OPENSSL_DYNAMIC_VERSION (unsigned long)0x00020000
typedef int (*openssl_bind_engine)(ENGINE *, const char *, const void *);
typedef unsigned long (*openssl_v_check)(unsigned long);
void ENGINE_add_conf_module(void);
void ENGINE_load_builtin_engines(void);
ENGINE *ENGINE_by_id(const char *);
ENGINE *ENGINE_by_dso(const char *, const char *);
int ENGINE_finish(ENGINE *);
int ENGINE_up_ref(ENGINE *);
int ENGINE_set_id(ENGINE *, const char *);
int ENGINE_set_name(ENGINE *, const char *);
int ENGINE_set_RSA(ENGINE *, const RSA_METHOD *);
int ENGINE_set_DH(ENGINE *, const DH_METHOD *);
int ENGINE_set_destroy_function(ENGINE *, void (*)(ENGINE *));
const char * ENGINE_get_id(const ENGINE *);
const char * ENGINE_get_name(const ENGINE *);
const RSA_METHOD * ENGINE_get_RSA(const ENGINE *);
const DH_METHOD * ENGINE_get_DH(const ENGINE *);
const RAND_METHOD * ENGINE_get_RAND(const ENGINE *);
int ENGINE_set_default_RSA(ENGINE *);
ENGINE * ENGINE_get_default_RSA(void);
int ENGINE_set_default_DH(ENGINE *);
ENGINE * ENGINE_get_default_DH(void);
#endif /* _HEIM_ENGINE_H */
+905
View File
@@ -0,0 +1,905 @@
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <evp.h>
#include <krb5-types.h>
#include <aes.h>
#include <des.h>
#include <sha.h>
#include <rc2.h>
#include <rc4.h>
#include <md2.h>
#include <md4.h>
#include <md5.h>
typedef int (*evp_md_init)(EVP_MD_CTX *);
typedef int (*evp_md_update)(EVP_MD_CTX *,const void *, size_t);
typedef int (*evp_md_final)(void *, EVP_MD_CTX *);
typedef int (*evp_md_cleanup)(EVP_MD_CTX *);
struct hc_evp_md {
int hash_size;
int block_size;
int ctx_size;
evp_md_init init;
evp_md_update update;
evp_md_final final;
evp_md_cleanup cleanup;
};
/*
*
*/
size_t
EVP_MD_size(const EVP_MD *md)
{
return md->hash_size;
}
size_t
EVP_MD_block_size(const EVP_MD *md)
{
return md->block_size;
}
EVP_MD_CTX *
EVP_MD_CTX_create(void)
{
return calloc(1, sizeof(EVP_MD_CTX));
}
void
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}
void
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
{
EVP_MD_CTX_cleanup(ctx);
free(ctx);
}
int
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
{
if (ctx->md && ctx->md->cleanup)
(ctx->md->cleanup)(ctx);
ctx->md = NULL;
ctx->engine = NULL;
free(ctx->ptr);
return 1;
}
const EVP_MD *
EVP_MD_CTX_md(EVP_MD_CTX *ctx)
{
return ctx->md;
}
size_t
EVP_MD_CTX_size(EVP_MD_CTX *ctx)
{
return EVP_MD_size(ctx->md);
}
size_t
EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
{
return EVP_MD_block_size(ctx->md);
}
int
EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
{
if (ctx->md != md || ctx->engine != engine) {
EVP_MD_CTX_cleanup(ctx);
ctx->md = md;
ctx->engine = engine;
ctx->ptr = calloc(1, md->ctx_size);
if (ctx->ptr == NULL)
return 0;
}
(ctx->md->init)(ctx->ptr);
return 1;
}
int
EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
{
(ctx->md->update)(ctx->ptr, data, size);
return 1;
}
int
EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
{
(ctx->md->final)(hash, ctx->ptr);
if (size)
*size = ctx->md->hash_size;
return 1;
}
int
EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
const EVP_MD *md, ENGINE *engine)
{
EVP_MD_CTX *ctx;
int ret;
ctx = EVP_MD_CTX_create();
if (ctx == NULL)
return 0;
ret = EVP_DigestInit_ex(ctx, md, engine);
if (ret != 1)
return ret;
ret = EVP_DigestUpdate(ctx, data, dsize);
if (ret != 1)
return ret;
ret = EVP_DigestFinal_ex(ctx, hash, hsize);
if (ret != 1)
return ret;
EVP_MD_CTX_destroy(ctx);
return 1;
}
/*
*
*/
const EVP_MD *
EVP_sha256(void)
{
static const struct hc_evp_md sha256 = {
32,
64,
sizeof(SHA256_CTX),
(evp_md_init)SHA256_Init,
(evp_md_update)SHA256_Update,
(evp_md_final)SHA256_Final,
NULL
};
return &sha256;
}
static const struct hc_evp_md sha1 = {
20,
64,
sizeof(SHA_CTX),
(evp_md_init)SHA1_Init,
(evp_md_update)SHA1_Update,
(evp_md_final)SHA1_Final,
NULL
};
const EVP_MD *
EVP_sha1(void)
{
return &sha1;
}
const EVP_MD *
EVP_sha(void)
{
return &sha1;
}
const EVP_MD *
EVP_md5(void)
{
static const struct hc_evp_md md5 = {
16,
64,
sizeof(MD5_CTX),
(evp_md_init)MD5_Init,
(evp_md_update)MD5_Update,
(evp_md_final)MD5_Final,
NULL
};
return &md5;
}
const EVP_MD *
EVP_md4(void)
{
static const struct hc_evp_md md4 = {
16,
64,
sizeof(MD4_CTX),
(evp_md_init)MD4_Init,
(evp_md_update)MD4_Update,
(evp_md_final)MD4_Final,
NULL
};
return &md4;
}
const EVP_MD *
EVP_md2(void)
{
static const struct hc_evp_md md2 = {
16,
16,
sizeof(MD2_CTX),
(evp_md_init)MD2_Init,
(evp_md_update)MD2_Update,
(evp_md_final)MD2_Final,
NULL
};
return &md2;
}
/*
*
*/
static void
null_Init (void *m)
{
}
static void
null_Update (void *m, const void * data, size_t size)
{
}
static void
null_Final(void *res, struct md5 *m)
{
}
const EVP_MD *
EVP_md_null(void)
{
static const struct hc_evp_md null = {
0,
0,
0,
(evp_md_init)null_Init,
(evp_md_update)null_Update,
(evp_md_final)null_Final,
NULL
};
return &null;
}
#if 0
void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
int EVP_SignFinal(EVP_MD_CTX *, void *, size_t *, EVP_PKEY *);
int EVP_VerifyFinal(EVP_MD_CTX *, const void *, size_t, EVP_PKEY *);
#endif
/*
*
*/
size_t
EVP_CIPHER_block_size(const EVP_CIPHER *c)
{
return c->block_size;
}
size_t
EVP_CIPHER_key_length(const EVP_CIPHER *c)
{
return c->key_len;
}
size_t
EVP_CIPHER_iv_length(const EVP_CIPHER *c)
{
return c->iv_len;
}
void
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
{
memset(c, 0, sizeof(*c));
}
int
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
{
if (c->cipher && c->cipher->cleanup)
c->cipher->cleanup(c);
if (c->cipher_data) {
free(c->cipher_data);
c->cipher_data = NULL;
}
return 1;
}
#if 0
int
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
{
return 0;
}
int
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
{
return 0;
}
#endif
const EVP_CIPHER *
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
{
return ctx->cipher;
}
size_t
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_block_size(ctx->cipher);
}
size_t
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_key_length(ctx->cipher);
}
size_t
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_iv_length(ctx->cipher);
}
unsigned long
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
{
return ctx->cipher->flags;
}
int
EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
}
void *
EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
{
return ctx->app_data;
}
void
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
{
ctx->app_data = data;
}
int
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
const void *key, const void *iv, int encp)
{
if (encp == -1)
encp = ctx->encrypt;
else
ctx->encrypt = (encp ? 1 : 0);
if (c && (c != ctx->cipher)) {
EVP_CIPHER_CTX_cleanup(ctx);
ctx->cipher = c;
ctx->key_len = c->key_len;
ctx->cipher_data = malloc(c->ctx_size);
if (ctx->cipher_data == NULL && c->ctx_size != 0)
return 0;
} else if (ctx->cipher == NULL) {
/* reuse of cipher, but not any cipher ever set! */
return 0;
}
switch (EVP_CIPHER_CTX_flags(ctx)) {
case EVP_CIPH_CBC_MODE:
assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
if (iv)
memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
break;
default:
return 0;
}
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
ctx->cipher->init(ctx, key, iv, encp);
return 1;
}
int
EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
{
return ctx->cipher->do_cipher(ctx, out, in, size);
}
/*
*
*/
static int
enc_null_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
return 1;
}
static int
enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
memmove(out, in, size);
return 1;
}
static int
enc_null_cleanup(EVP_CIPHER_CTX *ctx)
{
return 1;
}
const EVP_CIPHER *
EVP_enc_null(void)
{
static const EVP_CIPHER enc_null = {
0,
0,
0,
0,
EVP_CIPH_CBC_MODE,
enc_null_init,
enc_null_do_cipher,
enc_null_cleanup,
0,
NULL,
NULL,
NULL,
NULL
};
return &enc_null;
}
/*
*
*/
struct rc2_cbc {
unsigned int maximum_effective_key;
RC2_KEY key;
};
static int
rc2_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
struct rc2_cbc *k = ctx->cipher_data;
k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
RC2_set_key(&k->key,
EVP_CIPHER_CTX_key_length(ctx),
key,
k->maximum_effective_key);
return 1;
}
static int
rc2_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
struct rc2_cbc *k = ctx->cipher_data;
RC2_cbc_encrypt(in, out, size, &k->key, ctx->iv, ctx->encrypt);
return 1;
}
static int
rc2_cleanup(EVP_CIPHER_CTX *ctx)
{
memset(ctx->cipher_data, 0, sizeof(struct rc2_cbc));
return 1;
}
const EVP_CIPHER *
EVP_rc2_cbc(void)
{
static const EVP_CIPHER rc2_cbc = {
0,
RC2_BLOCK_SIZE,
RC2_KEY_LENGTH,
RC2_BLOCK_SIZE,
EVP_CIPH_CBC_MODE,
rc2_init,
rc2_do_cipher,
rc2_cleanup,
sizeof(struct rc2_cbc),
NULL,
NULL,
NULL,
NULL
};
return &rc2_cbc;
}
const EVP_CIPHER *
EVP_rc2_40_cbc(void)
{
static const EVP_CIPHER rc2_40_cbc = {
0,
RC2_BLOCK_SIZE,
5,
RC2_BLOCK_SIZE,
EVP_CIPH_CBC_MODE,
rc2_init,
rc2_do_cipher,
rc2_cleanup,
sizeof(struct rc2_cbc),
NULL,
NULL,
NULL,
NULL
};
return &rc2_40_cbc;
}
const EVP_CIPHER *
EVP_rc2_64_cbc(void)
{
static const EVP_CIPHER rc2_64_cbc = {
0,
RC2_BLOCK_SIZE,
8,
RC2_BLOCK_SIZE,
EVP_CIPH_CBC_MODE,
rc2_init,
rc2_do_cipher,
rc2_cleanup,
sizeof(struct rc2_cbc),
NULL,
NULL,
NULL,
NULL
};
return &rc2_64_cbc;
}
/*
*
*/
const EVP_CIPHER *
EVP_rc4(void)
{
printf("evp rc4\n");
abort();
return NULL;
}
const EVP_CIPHER *
EVP_rc4_40(void)
{
printf("evp rc4_40\n");
abort();
return NULL;
}
/*
*
*/
struct des_ede3_cbc {
DES_key_schedule ks[3];
};
static int
des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
struct des_ede3_cbc *k = ctx->cipher_data;
DES_key_sched((DES_cblock *)(key), &k->ks[0]);
DES_key_sched((DES_cblock *)(key + 8), &k->ks[1]);
DES_key_sched((DES_cblock *)(key + 16), &k->ks[2]);
return 1;
}
static int
des_ede3_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
struct des_ede3_cbc *k = ctx->cipher_data;
DES_ede3_cbc_encrypt(in, out, size,
&k->ks[0], &k->ks[1], &k->ks[2],
(DES_cblock *)ctx->iv, ctx->encrypt);
return 1;
}
static int
des_ede3_cbc_cleanup(EVP_CIPHER_CTX *ctx)
{
memset(ctx->cipher_data, 0, sizeof(struct des_ede3_cbc));
return 1;
}
const EVP_CIPHER *
EVP_des_ede3_cbc(void)
{
static const EVP_CIPHER des_ede3_cbc = {
0,
8,
24,
8,
EVP_CIPH_CBC_MODE,
des_ede3_cbc_init,
des_ede3_cbc_do_cipher,
des_ede3_cbc_cleanup,
sizeof(struct des_ede3_cbc),
NULL,
NULL,
NULL,
NULL
};
return &des_ede3_cbc;
}
/*
*
*/
static int
aes_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
AES_KEY *k = ctx->cipher_data;
if (ctx->encrypt)
AES_set_encrypt_key(key, ctx->cipher->key_len * 8, k);
else
AES_set_decrypt_key(key, ctx->cipher->key_len * 8, k);
return 1;
}
static int
aes_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
AES_KEY *k = ctx->cipher_data;
AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
return 1;
}
static int
aes_cleanup(EVP_CIPHER_CTX *ctx)
{
memset(ctx->cipher_data, 0, sizeof(AES_KEY));
return 1;
}
const EVP_CIPHER *
EVP_aes_128_cbc(void)
{
static const EVP_CIPHER aes_128_cbc = {
0,
16,
16,
16,
EVP_CIPH_CBC_MODE,
aes_init,
aes_do_cipher,
aes_cleanup,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_128_cbc;
}
const EVP_CIPHER *
EVP_aes_192_cbc(void)
{
static const EVP_CIPHER aes_192_cbc = {
0,
16,
24,
16,
EVP_CIPH_CBC_MODE,
aes_init,
aes_do_cipher,
aes_cleanup,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_192_cbc;
}
const EVP_CIPHER *
EVP_aes_256_cbc(void)
{
static const EVP_CIPHER aes_256_cbc = {
0,
16,
32,
16,
EVP_CIPH_CBC_MODE,
aes_init,
aes_do_cipher,
aes_cleanup,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_256_cbc;
}
/*
*
*/
static const struct cipher_name {
const char *name;
const EVP_CIPHER *(*func)(void);
} cipher_name[] = {
{ "des-ede3-cbc", EVP_des_ede3_cbc },
{ "aes-128-cbc", EVP_aes_128_cbc },
{ "aes-192-cbc", EVP_aes_192_cbc },
{ "aes-256-cbc", EVP_aes_256_cbc }
};
const EVP_CIPHER *
EVP_get_cipherbyname(const char *name)
{
int i;
for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
if (strcasecmp(cipher_name[i].name, name) == 0)
return (*cipher_name[i].func)();
}
return NULL;
}
/*
*
*/
#ifndef min
#define min(a,b) (((a)>(b))?(b):(a))
#endif
int
EVP_BytesToKey(const EVP_CIPHER *type,
const EVP_MD *md,
const void *salt,
const void *data, size_t datalen,
unsigned int count,
void *keydata,
void *ivdata)
{
int ivlen, keylen, first = 0;
unsigned int mds = 0, i;
unsigned char *key = keydata;
unsigned char *iv = ivdata;
unsigned char *buf;
EVP_MD_CTX c;
keylen = EVP_CIPHER_key_length(type);
ivlen = EVP_CIPHER_iv_length(type);
if (data == NULL)
return keylen;
buf = malloc(EVP_MD_size(md));
if (buf == NULL)
return -1;
EVP_MD_CTX_init(&c);
first = 1;
while (1) {
EVP_DigestInit_ex(&c, md, NULL);
if (!first)
EVP_DigestUpdate(&c, buf, mds);
first = 0;
EVP_DigestUpdate(&c,data,datalen);
#define PKCS5_SALT_LEN 8
if (salt)
EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
EVP_DigestFinal_ex(&c, buf, &mds);
assert(mds == EVP_MD_size(md));
for (i = 1; i < count; i++) {
EVP_DigestInit_ex(&c, md, NULL);
EVP_DigestUpdate(&c, buf, mds);
EVP_DigestFinal_ex(&c, buf, &mds);
assert(mds == EVP_MD_size(md));
}
i = 0;
if (keylen) {
size_t sz = min(keylen, mds);
if (key) {
memcpy(key, buf, sz);
key += sz;
}
keylen -= sz;
i += sz;
}
if (ivlen && mds > i) {
size_t sz = min(ivlen, (mds - i));
if (iv) {
memcpy(iv, &buf[i], sz);
iv += sz;
}
ivlen -= sz;
}
if (keylen == 0 && ivlen == 0)
break;
}
EVP_MD_CTX_cleanup(&c);
free(buf);
return EVP_CIPHER_key_length(type);
}
/*
*
*/
void
OpenSSL_add_all_algorithms(void)
{
return;
}
void
OpenSSL_add_all_algorithms_conf(void)
{
return;
}
void
OpenSSL_add_all_algorithms_noconf(void)
{
return;
}
+255
View File
@@ -0,0 +1,255 @@
/*
* Copyright (c) 2005 - 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: evp.h,v 1.11 2006/10/07 17:21:24 lha Exp $ */
#ifndef HEIM_EVP_H
#define HEIM_EVP_H 1
#include <hcrypto/engine.h>
/* symbol renaming */
#define EVP_CIPHER_CTX_block_size hc_EVP_CIPHER_CTX_block_size
#define EVP_CIPHER_CTX_cipher hc_EVP_CIPHER_CTX_cipher
#define EVP_CIPHER_CTX_cleanup hc_EVP_CIPHER_CTX_cleanup
#define EVP_CIPHER_CTX_flags hc_EVP_CIPHER_CTX_flags
#define EVP_CIPHER_CTX_get_app_data hc_EVP_CIPHER_CTX_get_app_data
#define EVP_CIPHER_CTX_init hc_EVP_CIPHER_CTX_init
#define EVP_CIPHER_CTX_iv_length hc_EVP_CIPHER_CTX_iv_length
#define EVP_CIPHER_CTX_key_length hc_EVP_CIPHER_CTX_key_length
#define EVP_CIPHER_CTX_mode hc_EVP_CIPHER_CTX_mode
#define EVP_CIPHER_CTX_set_app_data hc_EVP_CIPHER_CTX_set_app_data
#define EVP_CIPHER_CTX_set_key_length hc_EVP_CIPHER_CTX_set_key_length
#define EVP_CIPHER_CTX_set_padding hc_EVP_CIPHER_CTX_set_padding
#define EVP_CIPHER_block_size hc_EVP_CIPHER_block_size
#define EVP_CIPHER_iv_length hc_EVP_CIPHER_iv_length
#define EVP_CIPHER_key_length hc_EVP_CIPHER_key_length
#define EVP_Cipher hc_EVP_Cipher
#define EVP_CipherInit_ex hc_EVP_CipherInit_ex
#define EVP_Digest hc_EVP_Digest
#define EVP_DigestFinal_ex hc_EVP_DigestFinal_ex
#define EVP_DigestInit_ex hc_EVP_DigestInit_ex
#define EVP_DigestUpdate hc_EVP_DigestUpdate
#define EVP_MD_CTX_block_size hc_EVP_MD_CTX_block_size
#define EVP_MD_CTX_cleanup hc_EVP_MD_CTX_cleanup
#define EVP_MD_CTX_create hc_EVP_MD_CTX_create
#define EVP_MD_CTX_init hc_EVP_MD_CTX_init
#define EVP_MD_CTX_destroy hc_EVP_MD_CTX_destroy
#define EVP_MD_CTX_md hc_EVP_MD_CTX_md
#define EVP_MD_CTX_size hc_EVP_MD_CTX_size
#define EVP_MD_block_size hc_EVP_MD_block_size
#define EVP_MD_size hc_EVP_MD_size
#define EVP_aes_128_cbc hc_EVP_aes_128_cbc
#define EVP_aes_192_cbc hc_EVP_aes_192_cbc
#define EVP_aes_256_cbc hc_EVP_aes_256_cbc
#define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
#define EVP_enc_null hc_EVP_enc_null
#define EVP_md2 hc_EVP_md2
#define EVP_md4 hc_EVP_md4
#define EVP_md5 hc_EVP_md5
#define EVP_md_null hc_EVP_md_null
#define EVP_rc2_40_cbc hc_EVP_rc2_40_cbc
#define EVP_rc2_64_cbc hc_EVP_rc2_64_cbc
#define EVP_rc2_cbc hc_EVP_rc2_cbc
#define EVP_rc4 hc_EVP_rc4
#define EVP_rc4_40 hc_EVP_rc4_40
#define EVP_sha hc_EVP_sha
#define EVP_sha1 hc_EVP_sha1
#define EVP_sha256 hc_EVP_sha256
#define PKCS5_PBKDF2_HMAC_SHA1 hc_PKCS5_PBKDF2_HMAC_SHA1
#define EVP_BytesToKey hc_EVP_BytesToKey
#define EVP_get_cipherbyname hc_EVP_get_cipherbyname
#define OpenSSL_add_all_algorithms hc_OpenSSL_add_all_algorithms
#define OpenSSL_add_all_algorithms_conf hc_OpenSSL_add_all_algorithms_conf
#define OpenSSL_add_all_algorithms_noconf hc_OpenSSL_add_all_algorithms_noconf
/*
*
*/
typedef struct hc_EVP_MD_CTX EVP_MD_CTX;
typedef struct hc_evp_pkey EVP_PKEY;
typedef struct hc_evp_md EVP_MD;
typedef struct hc_CIPHER EVP_CIPHER;
typedef struct hc_CIPHER_CTX EVP_CIPHER_CTX;
#define EVP_MAX_IV_LENGTH 16
#define EVP_MAX_BLOCK_LENGTH 32
#define EVP_MAX_MD_SIZE 64
struct hc_CIPHER {
int nid;
int block_size;
int key_len;
int iv_len;
unsigned long flags;
/* The lowest 3 bits is used as integer field for the mode the
* cipher is used in (use EVP_CIPHER.._mode() to extract the
* mode). The rest of the flag field is a bitfield.
*/
#define EVP_CIPH_CBC_MODE 2
#define EVP_CIPH_MODE 0x7
#define EVP_CIPH_ALWAYS_CALL_INIT 0x20
int (*init)(EVP_CIPHER_CTX*,const unsigned char*,const unsigned char*,int);
int (*do_cipher)(EVP_CIPHER_CTX *, unsigned char *,
const unsigned char *, unsigned int);
int (*cleanup)(EVP_CIPHER_CTX *);
int ctx_size;
void *set_asn1_parameters;
void *get_asn1_parameters;
void *ctrl;
void *app_data;
};
struct hc_CIPHER_CTX {
const EVP_CIPHER *cipher;
ENGINE *engine;
int encrypt;
int buf_len;
unsigned char oiv[EVP_MAX_IV_LENGTH];
unsigned char iv[EVP_MAX_IV_LENGTH];
unsigned char buf[EVP_MAX_BLOCK_LENGTH];
int num;
void *app_data;
int key_len;
unsigned long flags;
void *cipher_data;
int final_used;
int block_mask;
unsigned char final[EVP_MAX_BLOCK_LENGTH];
};
struct hc_EVP_MD_CTX {
const EVP_MD *md;
ENGINE *engine;
void *ptr;
};
/*
* Avaible crypto algs
*/
const EVP_MD *EVP_md_null(void);
const EVP_MD *EVP_md2(void);
const EVP_MD *EVP_md4(void);
const EVP_MD *EVP_md5(void);
const EVP_MD *EVP_sha(void);
const EVP_MD *EVP_sha1(void);
const EVP_MD *EVP_sha256(void);
const EVP_CIPHER * EVP_aes_128_cbc(void);
const EVP_CIPHER * EVP_aes_192_cbc(void);
const EVP_CIPHER * EVP_aes_256_cbc(void);
const EVP_CIPHER * EVP_des_ede3_cbc(void);
const EVP_CIPHER * EVP_enc_null(void);
const EVP_CIPHER * EVP_rc2_40_cbc(void);
const EVP_CIPHER * EVP_rc2_64_cbc(void);
const EVP_CIPHER * EVP_rc2_cbc(void);
const EVP_CIPHER * EVP_rc4(void);
const EVP_CIPHER * EVP_rc4_40(void);
/*
*
*/
size_t EVP_MD_size(const EVP_MD *);
size_t EVP_MD_block_size(const EVP_MD *);
const EVP_MD *
EVP_MD_CTX_md(EVP_MD_CTX *);
size_t EVP_MD_CTX_size(EVP_MD_CTX *);
size_t EVP_MD_CTX_block_size(EVP_MD_CTX *);
EVP_MD_CTX *
EVP_MD_CTX_create(void);
void EVP_MD_CTX_init(EVP_MD_CTX *);
void EVP_MD_CTX_destroy(EVP_MD_CTX *);
int EVP_MD_CTX_cleanup(EVP_MD_CTX *);
int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
int EVP_DigestUpdate(EVP_MD_CTX *,const void *, size_t);
int EVP_DigestFinal_ex(EVP_MD_CTX *, void *, unsigned int *);
int EVP_Digest(const void *, size_t, void *, unsigned int *,
const EVP_MD *, ENGINE *);
/*
*
*/
const EVP_CIPHER *
EVP_get_cipherbyname(const char *);
size_t EVP_CIPHER_block_size(const EVP_CIPHER *);
size_t EVP_CIPHER_key_length(const EVP_CIPHER *);
size_t EVP_CIPHER_iv_length(const EVP_CIPHER *);
void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *);
int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
unsigned long
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *);
int EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *);
const EVP_CIPHER *
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *);
size_t EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *);
size_t EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *);
size_t EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *);
void * EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *);
void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *, void *);
int EVP_CipherInit_ex(EVP_CIPHER_CTX *,const EVP_CIPHER *, ENGINE *,
const void *, const void *, int);
int EVP_Cipher(EVP_CIPHER_CTX *,void *,const void *,size_t);
int PKCS5_PBKDF2_HMAC_SHA1(const void *, size_t, const void *, size_t,
unsigned long, size_t, void *);
int EVP_BytesToKey(const EVP_CIPHER *, const EVP_MD *,
const void *, const void *, size_t,
unsigned int, void *, void *);
/*
*
*/
void OpenSSL_add_all_algorithms(void);
void OpenSSL_add_all_algorithms_conf(void);
void OpenSSL_add_all_algorithms_noconf(void);
#endif /* HEIM_EVP_H */
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 1999 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of KTH nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* $Id: hash.h,v 1.4 2006/05/05 11:06:49 lha Exp $ */
/* stuff in common between md4, md5, and sha1 */
#ifndef __hash_h__
#define __hash_h__
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#ifdef KRB5
#include <krb5-types.h>
#endif
#ifndef min
#define min(a,b) (((a)>(b))?(b):(a))
#endif
/* Vector Crays doesn't have a good 32-bit type, or more precisely,
int32_t as defined by <bind/bitypes.h> isn't 32 bits, and we don't
want to depend in being able to redefine this type. To cope with
this we have to clamp the result in some places to [0,2^32); no
need to do this on other machines. Did I say this was a mess?
*/
#ifdef _CRAY
#define CRAYFIX(X) ((X) & 0xffffffff)
#else
#define CRAYFIX(X) (X)
#endif
static inline uint32_t
cshift (uint32_t x, unsigned int n)
{
x = CRAYFIX(x);
return CRAYFIX((x << n) | (x >> (32 - n)));
}
#endif /* __hash_h__ */
+122
View File
@@ -0,0 +1,122 @@
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hmac.h>
void
HMAC_CTX_init(HMAC_CTX *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}
void
HMAC_CTX_cleanup(HMAC_CTX *ctx)
{
if (ctx->buf) {
memset(ctx->buf, 0, ctx->key_length);
free(ctx->buf);
ctx->buf = NULL;
}
if (ctx->opad) {
memset(ctx->ipad, 0, ctx->key_length);
free(ctx->opad);
ctx->opad = NULL;
}
if (ctx->ipad) {
memset(ctx->ipad, 0, ctx->key_length);
free(ctx->ipad);
ctx->ipad = NULL;
}
if (ctx->ctx) {
EVP_MD_CTX_destroy(ctx->ctx);
ctx->ctx = NULL;
}
}
size_t
HMAC_size(const HMAC_CTX *ctx)
{
return EVP_MD_size(ctx->md);
}
void
HMAC_Init_ex(HMAC_CTX *ctx,
const void *key,
size_t keylen,
const EVP_MD *md,
ENGINE *engine)
{
unsigned char *p;
size_t i;
if (ctx->md != md) {
ctx->md = md;
if (ctx->buf)
free (ctx->buf);
ctx->key_length = EVP_MD_size(ctx->md);
ctx->buf = malloc(ctx->key_length);
}
#if 0
ctx->engine = engine;
#endif
if (keylen > EVP_MD_block_size(ctx->md)) {
EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
key = ctx->buf;
keylen = EVP_MD_size(ctx->md);
}
if (ctx->opad)
free(ctx->opad);
if (ctx->ipad)
free(ctx->ipad);
ctx->opad = malloc(EVP_MD_block_size(ctx->md));
ctx->ipad = malloc(EVP_MD_block_size(ctx->md));
memset(ctx->ipad, 0x36, EVP_MD_block_size(ctx->md));
memset(ctx->opad, 0x5c, EVP_MD_block_size(ctx->md));
for (i = 0, p = ctx->ipad; i < keylen; i++)
p[i] ^= ((const unsigned char *)key)[i];
for (i = 0, p = ctx->opad; i < keylen; i++)
p[i] ^= ((const unsigned char *)key)[i];
ctx->ctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
}
void
HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len)
{
EVP_DigestUpdate(ctx->ctx, data, len);
}
void
HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len)
{
EVP_DigestFinal_ex(ctx->ctx, ctx->buf, NULL);
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
EVP_DigestUpdate(ctx->ctx, ctx->opad, EVP_MD_block_size(ctx->md));
EVP_DigestUpdate(ctx->ctx, ctx->buf, ctx->key_length);
EVP_DigestFinal_ex(ctx->ctx, md, len);
}
void *
HMAC(const EVP_MD *md,
const void *key, size_t key_size,
const void *data, size_t data_size,
void *hash, unsigned int *hash_len)
{
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, key_size, md, NULL);
HMAC_Update(&ctx, data, data_size);
HMAC_Final(&ctx, hash, hash_len);
HMAC_CTX_cleanup(&ctx);
return hash;
}
+82
View File
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: hmac.h,v 1.3 2006/01/13 15:26:52 lha Exp $ */
#ifndef HEIM_HMAC_H
#define HEIM_HMAC_H 1
#include <hcrypto/evp.h>
/* symbol renaming */
#define HMAC_CTX_init hc_HMAC_CTX_init
#define HMAC_CTX_cleanup hc_HMAC_CTX_cleanup
#define HMAC_size hc_HMAC_size
#define HMAC_Init_ex hc_HMAC_Init_ex
#define HMAC_Update hc_HMAC_Update
#define HMAC_Final hc_HMAC_Final
#define HMAC hc_HMAC
/*
*
*/
#define HMAC_MAX_MD_CBLOCK 64
typedef struct hc_HMAC_CTX HMAC_CTX;
struct hc_HMAC_CTX {
const EVP_MD *md;
ENGINE *engine;
EVP_MD_CTX *ctx;
size_t key_length;
void *opad;
void *ipad;
void *buf;
};
void HMAC_CTX_init(HMAC_CTX *);
void HMAC_CTX_cleanup(HMAC_CTX *ctx);
size_t HMAC_size(const HMAC_CTX *ctx);
void HMAC_Init_ex(HMAC_CTX *, const void *, size_t,
const EVP_MD *, ENGINE *);
void HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len);
void HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len);
void * HMAC(const EVP_MD *evp_md, const void *key, size_t key_len,
const void *data, size_t n, void *md, unsigned int *md_len);
#endif /* HEIM_HMAC_H */
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: md2.c,v 1.1 2006/01/08 21:47:28 lha Exp $");
#endif
#include "hash.h"
#include "md2.h"
static const unsigned char subst[256] = {
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
31, 26, 219, 153, 141, 51, 159, 17, 131, 20
};
void
MD2_Init (struct md2 *m)
{
memset(m, 0, sizeof(*m));
}
static void
calc(struct md2 *m, const void *v)
{
unsigned char x[48], L;
const unsigned char *p = v;
int i, j, t;
L = m->checksum[15];
for (i = 0; i < 16; i++)
L = m->checksum[i] ^= subst[p[i] ^ L];
for (i = 0; i < 16; i++) {
x[i] = m->state[i];
x[i + 16] = p[i];
x[i + 32] = x[i] ^ p[i];
}
t = 0;
for (i = 0; i < 18; i++) {
for (j = 0; j < 48; j++)
t = x[j] ^= subst[t];
t = (t + i) & 0xff;
}
memcpy(m->state, x, 16);
memset(x, 0, sizeof(x));
}
void
MD2_Update (struct md2 *m, const void *v, size_t len)
{
size_t idx = m->len & 0xf;
const unsigned char *p = v;
m->len += len;
if (len + idx >= 16) {
if (idx) {
memcpy(m->data + idx, p, 16 - idx);
calc(m, m->data);
p += 16;
len -= 16 - idx;
}
while (len >= 16) {
calc(m, p);
p += 16;
len -= 16;
}
idx = 0;
}
memcpy(m->data + idx, p, len);
}
void
MD2_Final (void *res, struct md2 *m)
{
unsigned char pad[16];
size_t padlen;
padlen = 16 - (m->len % 16);
memset(pad, padlen, padlen);
MD2_Update(m, pad, padlen);
memcpy(pad, m->checksum, 16);
MD2_Update(m, pad, 16);
memcpy(res, m->state, MD2_DIGEST_LENGTH);
memset(m, 0, sizeof(m));
}
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: md2.h,v 1.1 2006/01/08 21:47:28 lha Exp $ */
#ifndef HEIM_MD2_H
#define HEIM_MD2_H 1
/* symbol renaming */
#define MD2_Init hc_MD2_Init
#define MD2_Update hc_MD2_Update
#define MD2_Final hc_MD2_Final
/*
*
*/
#define MD2_DIGEST_LENGTH 16
struct md2 {
size_t len;
unsigned char data[16]; /* stored unalligned data between Update's */
unsigned char checksum[16];
unsigned char state[16]; /* lower 16 bytes of X */
};
typedef struct md2 MD2_CTX;
void MD2_Init (struct md2 *m);
void MD2_Update (struct md2 *m, const void *p, size_t len);
void MD2_Final (void *res, struct md2 *m);
#endif /* HEIM_MD2_H */
+250
View File
@@ -0,0 +1,250 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: md4.c,v 1.18 2006/05/05 10:22:04 lha Exp $");
#endif
#include "hash.h"
#include "md4.h"
#define A m->counter[0]
#define B m->counter[1]
#define C m->counter[2]
#define D m->counter[3]
#define X data
void
MD4_Init (struct md4 *m)
{
m->sz[0] = 0;
m->sz[1] = 0;
D = 0x10325476;
C = 0x98badcfe;
B = 0xefcdab89;
A = 0x67452301;
}
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
#define G(x,y,z) ((x & y) | (x & z) | (y & z))
#define H(x,y,z) (x ^ y ^ z)
#define DOIT(a,b,c,d,k,s,i,OP) \
a = cshift(a + OP(b,c,d) + X[k] + i, s)
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
static inline void
calc (struct md4 *m, uint32_t *data)
{
uint32_t AA, BB, CC, DD;
AA = A;
BB = B;
CC = C;
DD = D;
/* Round 1 */
DO1(A,B,C,D,0,3,0);
DO1(D,A,B,C,1,7,0);
DO1(C,D,A,B,2,11,0);
DO1(B,C,D,A,3,19,0);
DO1(A,B,C,D,4,3,0);
DO1(D,A,B,C,5,7,0);
DO1(C,D,A,B,6,11,0);
DO1(B,C,D,A,7,19,0);
DO1(A,B,C,D,8,3,0);
DO1(D,A,B,C,9,7,0);
DO1(C,D,A,B,10,11,0);
DO1(B,C,D,A,11,19,0);
DO1(A,B,C,D,12,3,0);
DO1(D,A,B,C,13,7,0);
DO1(C,D,A,B,14,11,0);
DO1(B,C,D,A,15,19,0);
/* Round 2 */
DO2(A,B,C,D,0,3,0x5A827999);
DO2(D,A,B,C,4,5,0x5A827999);
DO2(C,D,A,B,8,9,0x5A827999);
DO2(B,C,D,A,12,13,0x5A827999);
DO2(A,B,C,D,1,3,0x5A827999);
DO2(D,A,B,C,5,5,0x5A827999);
DO2(C,D,A,B,9,9,0x5A827999);
DO2(B,C,D,A,13,13,0x5A827999);
DO2(A,B,C,D,2,3,0x5A827999);
DO2(D,A,B,C,6,5,0x5A827999);
DO2(C,D,A,B,10,9,0x5A827999);
DO2(B,C,D,A,14,13,0x5A827999);
DO2(A,B,C,D,3,3,0x5A827999);
DO2(D,A,B,C,7,5,0x5A827999);
DO2(C,D,A,B,11,9,0x5A827999);
DO2(B,C,D,A,15,13,0x5A827999);
/* Round 3 */
DO3(A,B,C,D,0,3,0x6ED9EBA1);
DO3(D,A,B,C,8,9,0x6ED9EBA1);
DO3(C,D,A,B,4,11,0x6ED9EBA1);
DO3(B,C,D,A,12,15,0x6ED9EBA1);
DO3(A,B,C,D,2,3,0x6ED9EBA1);
DO3(D,A,B,C,10,9,0x6ED9EBA1);
DO3(C,D,A,B,6,11,0x6ED9EBA1);
DO3(B,C,D,A,14,15,0x6ED9EBA1);
DO3(A,B,C,D,1,3,0x6ED9EBA1);
DO3(D,A,B,C,9,9,0x6ED9EBA1);
DO3(C,D,A,B,5,11,0x6ED9EBA1);
DO3(B,C,D,A,13,15,0x6ED9EBA1);
DO3(A,B,C,D,3,3,0x6ED9EBA1);
DO3(D,A,B,C,11,9,0x6ED9EBA1);
DO3(C,D,A,B,7,11,0x6ED9EBA1);
DO3(B,C,D,A,15,15,0x6ED9EBA1);
A += AA;
B += BB;
C += CC;
D += DD;
}
/*
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
*/
#if defined(WORDS_BIGENDIAN)
static inline uint32_t
swap_uint32_t (uint32_t t)
{
uint32_t temp1, temp2;
temp1 = cshift(t, 16);
temp2 = temp1 >> 8;
temp1 &= 0x00ff00ff;
temp2 &= 0x00ff00ff;
temp1 <<= 8;
return temp1 | temp2;
}
#endif
struct x32{
unsigned int a:32;
unsigned int b:32;
};
void
MD4_Update (struct md4 *m, const void *v, size_t len)
{
const unsigned char *p = v;
size_t old_sz = m->sz[0];
size_t offset;
m->sz[0] += len * 8;
if (m->sz[0] < old_sz)
++m->sz[1];
offset = (old_sz / 8) % 64;
while(len > 0) {
size_t l = min(len, 64 - offset);
memcpy(m->save + offset, p, l);
offset += l;
p += l;
len -= l;
if(offset == 64) {
#if defined(WORDS_BIGENDIAN)
int i;
uint32_t current[16];
struct x32 *u = (struct x32*)m->save;
for(i = 0; i < 8; i++){
current[2*i+0] = swap_uint32_t(u[i].a);
current[2*i+1] = swap_uint32_t(u[i].b);
}
calc(m, current);
#else
calc(m, (uint32_t*)m->save);
#endif
offset = 0;
}
}
}
void
MD4_Final (void *res, struct md4 *m)
{
unsigned char zeros[72];
unsigned offset = (m->sz[0] / 8) % 64;
unsigned int dstart = (120 - offset - 1) % 64 + 1;
*zeros = 0x80;
memset (zeros + 1, 0, sizeof(zeros) - 1);
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
MD4_Update (m, zeros, dstart + 8);
{
int i;
unsigned char *r = (unsigned char *)res;
for (i = 0; i < 4; ++i) {
r[4*i] = m->counter[i] & 0xFF;
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
}
}
#if 0
{
int i;
uint32_t *r = (uint32_t *)res;
for (i = 0; i < 4; ++i)
r[i] = swap_uint32_t (m->counter[i]);
}
#endif
}
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: md4.h,v 1.11 2006/05/05 11:07:01 lha Exp $ */
#ifndef HEIM_MD4_H
#define HEIM_MD4_H 1
/* symbol renaming */
#define MD4_Init hc_MD4_Init
#define MD4_Update hc_MD4_Update
#define MD4_Final hc_MD4_Final
/*
*
*/
#define MD4_DIGEST_LENGTH 16
struct md4 {
unsigned int sz[2];
uint32_t counter[4];
unsigned char save[64];
};
typedef struct md4 MD4_CTX;
void MD4_Init (struct md4 *m);
void MD4_Update (struct md4 *m, const void *p, size_t len);
void MD4_Final (void *res, struct md4 *m);
#endif /* HEIM_MD4_H */
+274
View File
@@ -0,0 +1,274 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: md5.c,v 1.18 2006/05/05 10:22:35 lha Exp $");
#endif
#include "hash.h"
#include "md5.h"
#define A m->counter[0]
#define B m->counter[1]
#define C m->counter[2]
#define D m->counter[3]
#define X data
void
MD5_Init (struct md5 *m)
{
m->sz[0] = 0;
m->sz[1] = 0;
D = 0x10325476;
C = 0x98badcfe;
B = 0xefcdab89;
A = 0x67452301;
}
#define F(x,y,z) CRAYFIX((x & y) | (~x & z))
#define G(x,y,z) CRAYFIX((x & z) | (y & ~z))
#define H(x,y,z) (x ^ y ^ z)
#define I(x,y,z) CRAYFIX(y ^ (x | ~z))
#define DOIT(a,b,c,d,k,s,i,OP) \
a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
#define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
static inline void
calc (struct md5 *m, uint32_t *data)
{
uint32_t AA, BB, CC, DD;
AA = A;
BB = B;
CC = C;
DD = D;
/* Round 1 */
DO1(A,B,C,D,0,7,0xd76aa478);
DO1(D,A,B,C,1,12,0xe8c7b756);
DO1(C,D,A,B,2,17,0x242070db);
DO1(B,C,D,A,3,22,0xc1bdceee);
DO1(A,B,C,D,4,7,0xf57c0faf);
DO1(D,A,B,C,5,12,0x4787c62a);
DO1(C,D,A,B,6,17,0xa8304613);
DO1(B,C,D,A,7,22,0xfd469501);
DO1(A,B,C,D,8,7,0x698098d8);
DO1(D,A,B,C,9,12,0x8b44f7af);
DO1(C,D,A,B,10,17,0xffff5bb1);
DO1(B,C,D,A,11,22,0x895cd7be);
DO1(A,B,C,D,12,7,0x6b901122);
DO1(D,A,B,C,13,12,0xfd987193);
DO1(C,D,A,B,14,17,0xa679438e);
DO1(B,C,D,A,15,22,0x49b40821);
/* Round 2 */
DO2(A,B,C,D,1,5,0xf61e2562);
DO2(D,A,B,C,6,9,0xc040b340);
DO2(C,D,A,B,11,14,0x265e5a51);
DO2(B,C,D,A,0,20,0xe9b6c7aa);
DO2(A,B,C,D,5,5,0xd62f105d);
DO2(D,A,B,C,10,9,0x2441453);
DO2(C,D,A,B,15,14,0xd8a1e681);
DO2(B,C,D,A,4,20,0xe7d3fbc8);
DO2(A,B,C,D,9,5,0x21e1cde6);
DO2(D,A,B,C,14,9,0xc33707d6);
DO2(C,D,A,B,3,14,0xf4d50d87);
DO2(B,C,D,A,8,20,0x455a14ed);
DO2(A,B,C,D,13,5,0xa9e3e905);
DO2(D,A,B,C,2,9,0xfcefa3f8);
DO2(C,D,A,B,7,14,0x676f02d9);
DO2(B,C,D,A,12,20,0x8d2a4c8a);
/* Round 3 */
DO3(A,B,C,D,5,4,0xfffa3942);
DO3(D,A,B,C,8,11,0x8771f681);
DO3(C,D,A,B,11,16,0x6d9d6122);
DO3(B,C,D,A,14,23,0xfde5380c);
DO3(A,B,C,D,1,4,0xa4beea44);
DO3(D,A,B,C,4,11,0x4bdecfa9);
DO3(C,D,A,B,7,16,0xf6bb4b60);
DO3(B,C,D,A,10,23,0xbebfbc70);
DO3(A,B,C,D,13,4,0x289b7ec6);
DO3(D,A,B,C,0,11,0xeaa127fa);
DO3(C,D,A,B,3,16,0xd4ef3085);
DO3(B,C,D,A,6,23,0x4881d05);
DO3(A,B,C,D,9,4,0xd9d4d039);
DO3(D,A,B,C,12,11,0xe6db99e5);
DO3(C,D,A,B,15,16,0x1fa27cf8);
DO3(B,C,D,A,2,23,0xc4ac5665);
/* Round 4 */
DO4(A,B,C,D,0,6,0xf4292244);
DO4(D,A,B,C,7,10,0x432aff97);
DO4(C,D,A,B,14,15,0xab9423a7);
DO4(B,C,D,A,5,21,0xfc93a039);
DO4(A,B,C,D,12,6,0x655b59c3);
DO4(D,A,B,C,3,10,0x8f0ccc92);
DO4(C,D,A,B,10,15,0xffeff47d);
DO4(B,C,D,A,1,21,0x85845dd1);
DO4(A,B,C,D,8,6,0x6fa87e4f);
DO4(D,A,B,C,15,10,0xfe2ce6e0);
DO4(C,D,A,B,6,15,0xa3014314);
DO4(B,C,D,A,13,21,0x4e0811a1);
DO4(A,B,C,D,4,6,0xf7537e82);
DO4(D,A,B,C,11,10,0xbd3af235);
DO4(C,D,A,B,2,15,0x2ad7d2bb);
DO4(B,C,D,A,9,21,0xeb86d391);
A += AA;
B += BB;
C += CC;
D += DD;
}
/*
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
*/
#if defined(WORDS_BIGENDIAN)
static inline uint32_t
swap_uint32_t (uint32_t t)
{
uint32_t temp1, temp2;
temp1 = cshift(t, 16);
temp2 = temp1 >> 8;
temp1 &= 0x00ff00ff;
temp2 &= 0x00ff00ff;
temp1 <<= 8;
return temp1 | temp2;
}
#endif
struct x32{
unsigned int a:32;
unsigned int b:32;
};
void
MD5_Update (struct md5 *m, const void *v, size_t len)
{
const unsigned char *p = v;
size_t old_sz = m->sz[0];
size_t offset;
m->sz[0] += len * 8;
if (m->sz[0] < old_sz)
++m->sz[1];
offset = (old_sz / 8) % 64;
while(len > 0){
size_t l = min(len, 64 - offset);
memcpy(m->save + offset, p, l);
offset += l;
p += l;
len -= l;
if(offset == 64){
#if defined(WORDS_BIGENDIAN)
int i;
uint32_t current[16];
struct x32 *u = (struct x32*)m->save;
for(i = 0; i < 8; i++){
current[2*i+0] = swap_uint32_t(u[i].a);
current[2*i+1] = swap_uint32_t(u[i].b);
}
calc(m, current);
#else
calc(m, (uint32_t*)m->save);
#endif
offset = 0;
}
}
}
void
MD5_Final (void *res, struct md5 *m)
{
unsigned char zeros[72];
unsigned offset = (m->sz[0] / 8) % 64;
unsigned int dstart = (120 - offset - 1) % 64 + 1;
*zeros = 0x80;
memset (zeros + 1, 0, sizeof(zeros) - 1);
zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
MD5_Update (m, zeros, dstart + 8);
{
int i;
unsigned char *r = (unsigned char *)res;
for (i = 0; i < 4; ++i) {
r[4*i] = m->counter[i] & 0xFF;
r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
}
}
#if 0
{
int i;
uint32_t *r = (uint32_t *)res;
for (i = 0; i < 4; ++i)
r[i] = swap_uint32_t (m->counter[i]);
}
#endif
}
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: md5.h,v 1.11 2006/05/05 11:07:11 lha Exp $ */
#ifndef HEIM_MD5_H
#define HEIM_MD5_H 1
/* symbol renaming */
#define MD5_Init hc_MD5_Init
#define MD5_Update hc_MD5_Update
#define MD5_Final hc_MD5_Final
/*
*
*/
#define MD5_DIGEST_LENGTH 16
struct md5 {
unsigned int sz[2];
uint32_t counter[4];
unsigned char save[64];
};
typedef struct md5 MD5_CTX;
void MD5_Init (struct md5 *m);
void MD5_Update (struct md5 *m, const void *p, size_t len);
void MD5_Final (void *res, struct md5 *m); /* uint32_t res[4] */
#endif /* HEIM_MD5_H */
+57
View File
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: pkcs12.h,v 1.2 2006/01/13 15:26:52 lha Exp $
*/
#ifndef _HEIM_PKCS12_H
#define _HEIM_PKCS12_H 1
/* symbol renaming */
#define PKCS12_key_gen hc_PKCS12_key_gen
/*
*
*/
#include <hcrypto/evp.h>
#define PKCS12_KEY_ID 1
#define PKCS12_IV_ID 2
int PKCS12_key_gen(const void *, size_t, const void *,
size_t, int, int, size_t, void *, const EVP_MD *);
#endif /* _HEIM_PKCS12_H */
+116
View File
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
RCSID("$Id: pkcs5.c,v 1.3 2006/05/05 10:23:11 lha Exp $");
#ifdef KRB5
#include <krb5-types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <evp.h>
#include <hmac.h>
#include <roken.h>
int
PKCS5_PBKDF2_HMAC_SHA1(const void * password, size_t password_len,
const void * salt, size_t salt_len,
unsigned long iter,
size_t keylen, void *key)
{
size_t datalen, leftofkey, checksumsize;
char *data, *tmpcksum;
uint32_t keypart;
const EVP_MD *md;
unsigned long i;
int j;
char *p;
unsigned int hmacsize;
md = EVP_sha1();
checksumsize = EVP_MD_size(md);
datalen = salt_len + 4;
tmpcksum = malloc(checksumsize + datalen);
if (tmpcksum == NULL)
return 0;
data = &tmpcksum[checksumsize];
memcpy(data, salt, salt_len);
keypart = 1;
leftofkey = keylen;
p = key;
while (leftofkey) {
int len;
if (leftofkey > checksumsize)
len = checksumsize;
else
len = leftofkey;
data[datalen - 4] = (keypart >> 24) & 0xff;
data[datalen - 3] = (keypart >> 16) & 0xff;
data[datalen - 2] = (keypart >> 8) & 0xff;
data[datalen - 1] = (keypart) & 0xff;
HMAC(md, password, password_len, data, datalen,
tmpcksum, &hmacsize);
memcpy(p, tmpcksum, len);
for (i = 1; i < iter; i++) {
HMAC(md, password, password_len, tmpcksum, checksumsize,
tmpcksum, &hmacsize);
for (j = 0; j < len; j++)
p[j] ^= tmpcksum[j];
}
p += len;
leftofkey -= len;
keypart++;
}
free(tmpcksum);
return 1;
}
+153
View File
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
RCSID("$Id: rand-unix.c,v 1.2 2006/10/21 21:09:14 lha Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <rand.h>
#include <roken.h>
/*
* Unix /dev/random
*/
static int
get_device_fd(int flags)
{
static const char *rnd_devices[] = {
"/dev/urandom",
"/dev/random",
"/dev/srandom",
"/dev/arandom",
NULL
};
const char **p;
for(p = rnd_devices; *p; p++) {
int fd = open(*p, flags | O_NDELAY);
if(fd >= 0)
return fd;
}
return -1;
}
static void
unix_seed(const void *indata, int size)
{
int fd;
if (size <= 0)
return;
fd = get_device_fd(O_WRONLY);
if (fd < 0)
return;
write(fd, indata, size);
close(fd);
}
static int
unix_bytes(unsigned char *outdata, int size)
{
ssize_t count;
int fd;
if (size <= 0)
return 0;
fd = get_device_fd(O_RDONLY);
if (fd < 0)
return 0;
while (size > 0) {
count = read (fd, outdata, size);
if (count < 0 && errno == EINTR)
continue;
else if (count <= 0) {
close(fd);
return 0;
}
outdata += count;
size -= count;
}
close(fd);
return 1;
}
static void
unix_cleanup(void)
{
}
static void
unix_add(const void *indata, int size, double entropi)
{
unix_seed(indata, size);
}
static int
unix_pseudorand(unsigned char *outdata, int size)
{
return unix_bytes(outdata, size);
}
static int
unix_status(void)
{
int fd;
fd = get_device_fd(O_RDONLY);
if (fd < 0)
return 0;
close(fd);
return 1;
}
const RAND_METHOD hc_rand_unix_method = {
unix_seed,
unix_bytes,
unix_cleanup,
unix_add,
unix_pseudorand,
unix_status
};
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
RCSID("$Id: rand.c,v 1.7 2006/10/16 10:23:01 lha Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <rand.h>
#include <roken.h>
extern RAND_METHOD hc_rand_unix_method;
static const RAND_METHOD *selected_meth = &hc_rand_unix_method;
void
RAND_seed(const void *indata, size_t size)
{
(*selected_meth->seed)(indata, size);
}
int
RAND_bytes(void *outdata, size_t size)
{
return (*selected_meth->bytes)(outdata, size);
}
void
RAND_cleanup(void)
{
(*selected_meth->cleanup)();
}
void
RAND_add(const void *indata, size_t size, double entropi)
{
(*selected_meth->add)(indata, size, entropi);
}
int
RAND_pseudo_bytes(void *outdata, size_t size)
{
return (*selected_meth->pseudorand)(outdata, size);
}
int
RAND_status(void)
{
return (*selected_meth->status)();
}
int
RAND_set_rand_method(const RAND_METHOD *meth)
{
selected_meth = meth;
return 1;
}
const RAND_METHOD *
RAND_get_rand_method(void)
{
return selected_meth;
}
int
RAND_set_rand_engine(ENGINE *engine)
{
return 1;
}
int
RAND_load_file(const char *filename, size_t size)
{
return 1;
}
int
RAND_write_file(const char *filename)
{
return 1;
}
int
RAND_egd(const char *filename)
{
return 1;
}
+96
View File
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: rand.h,v 1.4 2006/04/17 13:23:04 lha Exp $
*/
#ifndef _HEIM_RAND_H
#define _HEIM_RAND_H 1
typedef struct RAND_METHOD RAND_METHOD;
#include <hcrypto/bn.h>
#include <hcrypto/engine.h>
/* symbol renaming */
#define RAND_bytes hc_RAND_bytes
#define RAND_pseudo_bytes hc_RAND_pseudo_bytes
#define RAND_seed hc_RAND_seed
#define RAND_cleanup hc_RAND_cleanup
#define RAND_add hc_RAND_add
#define RAND_set_rand_method hc_RAND_set_rand_method
#define RAND_get_rand_method hc_RAND_get_rand_method
#define RAND_set_rand_engine hc_RAND_set_rand_engine
#define RAND_load_file hc_RAND_load_file
#define RAND_write_file hc_RAND_write_file
#define RAND_status hc_RAND_status
#define RAND_egd hc_RAND_egd
/*
*
*/
struct RAND_METHOD
{
void (*seed)(const void *, int);
int (*bytes)(unsigned char *, int);
void (*cleanup)(void);
void (*add)(const void *, int, double);
int (*pseudorand)(unsigned char *, int);
int (*status)(void);
};
/*
*
*/
int RAND_bytes(void *, size_t num);
int RAND_pseudo_bytes(void *, size_t);
void RAND_seed(const void *, size_t);
void RAND_cleanup(void);
void RAND_add(const void *, size_t, double);
int RAND_set_rand_method(const RAND_METHOD *);
const RAND_METHOD *
RAND_get_rand_method(void);
int RAND_set_rand_engine(ENGINE *);
int RAND_load_file(const char *, size_t);
int RAND_write_file(const char *);
int RAND_status(void);
int RAND_egd(const char *);
#endif /* _HEIM_RAND_H */
+245
View File
@@ -0,0 +1,245 @@
/*
* Copyright (c) 2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: rc2.c,v 1.7 2006/04/09 17:03:21 lha Exp $");
#endif
#include "rc2.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Implemented from Peter Gutmann's "Specification for Ron Rivests Cipher No.2"
* rfc2268 and "On the Design and Security of RC2" was also useful.
*/
static unsigned int Sbox[256] = {
0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed,
0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e,
0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13,
0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b,
0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c,
0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1,
0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57,
0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7,
0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7,
0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74,
0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc,
0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a,
0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae,
0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c,
0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0,
0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77,
0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad
};
void
RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
{
unsigned char k[128];
int j, T8, TM;
if (len <= 0)
abort();
if (len > 128)
len = 128;
if (bits <= 0 || bits > 1024)
bits = 1024;
for (j = 0; j < len; j++)
k[j] = data[j];
for (; j < 128; j++)
k[j] = Sbox[(k[j - len] + k[j - 1]) & 0xff];
T8 = (bits + 7) / 8;
j = (8*T8 - bits);
TM = 0xff >> j;
k[128 - T8] = Sbox[k[128 - T8] & TM];
for (j = 127 - T8; j >= 0; j--)
k[j] = Sbox[k[j + 1] ^ k[j + T8]];
for (j = 0; j < 64; j++)
key->data[j] = k[(j * 2) + 0] | (k[(j * 2) + 1] << 8);
memset(k, 0, sizeof(k));
}
#define ROT16L(w,n) ((w<<n)|(w>>(16-n)))
#define ROT16R(w,n) ((w>>n)|(w<<(16-n)))
void
RC2_encryptc(unsigned char *in, unsigned char *out, const RC2_KEY *key)
{
int i, j;
int w0, w1, w2, w3;
int t0, t1, t2, t3;
w0 = in[0] | (in[1] << 8);
w1 = in[2] | (in[3] << 8);
w2 = in[4] | (in[5] << 8);
w3 = in[6] | (in[7] << 8);
for (i = 0; i < 16; i++) {
j = i * 4;
t0 = (w0 + (w1 & ~w3) + (w2 & w3) + key->data[j + 0]) & 0xffff;
w0 = ROT16L(t0, 1);
t1 = (w1 + (w2 & ~w0) + (w3 & w0) + key->data[j + 1]) & 0xffff;
w1 = ROT16L(t1, 2);
t2 = (w2 + (w3 & ~w1) + (w0 & w1) + key->data[j + 2]) & 0xffff;
w2 = ROT16L(t2, 3);
t3 = (w3 + (w0 & ~w2) + (w1 & w2) + key->data[j + 3]) & 0xffff;
w3 = ROT16L(t3, 5);
if(i == 4 || i == 10) {
w0 += key->data[w3 & 63];
w1 += key->data[w0 & 63];
w2 += key->data[w1 & 63];
w3 += key->data[w2 & 63];
}
}
out[0] = w0 & 0xff;
out[1] = (w0 >> 8) & 0xff;
out[2] = w1 & 0xff;
out[3] = (w1 >> 8) & 0xff;
out[4] = w2 & 0xff;
out[5] = (w2 >> 8) & 0xff;
out[6] = w3 & 0xff;
out[7] = (w3 >> 8) & 0xff;
}
void
RC2_decryptc(unsigned char *in, unsigned char *out, const RC2_KEY *key)
{
int i, j;
int w0, w1, w2, w3;
int t0, t1, t2, t3;
w0 = in[0] | (in[1] << 8);
w1 = in[2] | (in[3] << 8);
w2 = in[4] | (in[5] << 8);
w3 = in[6] | (in[7] << 8);
for (i = 15; i >= 0; i--) {
j = i * 4;
if(i == 4 || i == 10) {
w3 = (w3 - key->data[w2 & 63]) & 0xffff;
w2 = (w2 - key->data[w1 & 63]) & 0xffff;
w1 = (w1 - key->data[w0 & 63]) & 0xffff;
w0 = (w0 - key->data[w3 & 63]) & 0xffff;
}
t3 = ROT16R(w3, 5);
w3 = (t3 - (w0 & ~w2) - (w1 & w2) - key->data[j + 3]) & 0xffff;
t2 = ROT16R(w2, 3);
w2 = (t2 - (w3 & ~w1) - (w0 & w1) - key->data[j + 2]) & 0xffff;
t1 = ROT16R(w1, 2);
w1 = (t1 - (w2 & ~w0) - (w3 & w0) - key->data[j + 1]) & 0xffff;
t0 = ROT16R(w0, 1);
w0 = (t0 - (w1 & ~w3) - (w2 & w3) - key->data[j + 0]) & 0xffff;
}
out[0] = w0 & 0xff;
out[1] = (w0 >> 8) & 0xff;
out[2] = w1 & 0xff;
out[3] = (w1 >> 8) & 0xff;
out[4] = w2 & 0xff;
out[5] = (w2 >> 8) & 0xff;
out[6] = w3 & 0xff;
out[7] = (w3 >> 8) & 0xff;
}
void
RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long size,
RC2_KEY *key, unsigned char *iv, int forward_encrypt)
{
unsigned char tmp[RC2_BLOCK_SIZE];
int i;
if (forward_encrypt) {
while (size >= RC2_BLOCK_SIZE) {
for (i = 0; i < RC2_BLOCK_SIZE; i++)
tmp[i] = in[i] ^ iv[i];
RC2_encryptc(tmp, out, key);
memcpy(iv, out, RC2_BLOCK_SIZE);
size -= RC2_BLOCK_SIZE;
in += RC2_BLOCK_SIZE;
out += RC2_BLOCK_SIZE;
}
if (size) {
for (i = 0; i < size; i++)
tmp[i] = in[i] ^ iv[i];
for (i = size; i < RC2_BLOCK_SIZE; i++)
tmp[i] = iv[i];
RC2_encryptc(tmp, out, key);
memcpy(iv, out, RC2_BLOCK_SIZE);
}
} else {
while (size >= RC2_BLOCK_SIZE) {
memcpy(tmp, in, RC2_BLOCK_SIZE);
RC2_decryptc(tmp, out, key);
for (i = 0; i < RC2_BLOCK_SIZE; i++)
out[i] ^= iv[i];
memcpy(iv, tmp, RC2_BLOCK_SIZE);
size -= RC2_BLOCK_SIZE;
in += RC2_BLOCK_SIZE;
out += RC2_BLOCK_SIZE;
}
if (size) {
memcpy(tmp, in, RC2_BLOCK_SIZE);
RC2_decryptc(tmp, out, key);
for (i = 0; i < size; i++)
out[i] ^= iv[i];
memcpy(iv, tmp, RC2_BLOCK_SIZE);
}
}
}
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: rc2.h,v 1.2 2006/01/08 21:47:29 lha Exp $ */
/* symbol renaming */
#define RC2_set_key hc_RC2_set_key
#define RC2_encryptc hc_RC2_encryptc
#define RC2_decryptc hc_RC2_decryptc
#define RC2_cbc_encrypt hc_RC2_cbc_encrypt
/*
*
*/
#define RC2_ENCRYPT 1
#define RC2_DECRYPT 0
#define RC2_BLOCK_SIZE 8
#define RC2_BLOCK RC2_BLOCK_SIZE
#define RC2_KEY_LENGTH 16
typedef struct rc2_key {
unsigned int data[64];
} RC2_KEY;
#ifdef __cplusplus
extern "C" {
#endif
void RC2_set_key(RC2_KEY *, int, const unsigned char *,int);
void RC2_encryptc(unsigned char *, unsigned char *, const RC2_KEY *);
void RC2_decryptc(unsigned char *, unsigned char *, const RC2_KEY *);
void RC2_cbc_encrypt(const unsigned char *, unsigned char *, long,
RC2_KEY *, unsigned char *, int);
#ifdef __cplusplus
}
#endif
+82
View File
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* implemented from description in draft-kaukonen-cipher-arcfour-03.txt */
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: rc4.c,v 1.1 2004/03/25 16:40:59 lha Exp $");
#endif
#include <rc4.h>
#define SWAP(k,x,y) \
{ unsigned int _t; \
_t = k->state[x]; \
k->state[x] = k->state[y]; \
k->state[y] = _t; \
}
void
RC4_set_key(RC4_KEY *key, const int len, unsigned char *data)
{
int i, j;
for (i = 0; i < 256; i++)
key->state[i] = i;
for (i = 0, j = 0; i < 256; i++) {
j = (j + key->state[i] + data[i % len]) % 256;
SWAP(key, i, j);
}
key->x = key->y = 0;
}
void
RC4(RC4_KEY *key, const int len, const unsigned char *in, unsigned char *out)
{
int i, t;
unsigned x, y;
x = key->x;
y = key->y;
for (i = 0; i < len; i++) {
x = (x + 1) % 256;
y = (y + key->state[x]) % 256;
SWAP(key, x, y);
t = (key->state[x] + key->state[y]) % 256;
*out++ = key->state[t] ^ *in++;
}
key->x = x;
key->y = y;
}
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2004 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: rc4.h,v 1.4 2006/01/08 21:47:29 lha Exp $ */
/* symbol renaming */
#define RC4_set_key hc_RC4_set_key
#define RC4 hc_RC4
typedef struct rc4_key {
unsigned int x, y;
unsigned int state[256];
} RC4_KEY;
void RC4_set_key(RC4_KEY *, const int, unsigned char *);
void RC4(RC4_KEY *, const int, const unsigned char *, unsigned char *);
File diff suppressed because it is too large Load Diff
+46
View File
@@ -0,0 +1,46 @@
/* $NetBSD: rijndael-alg-fst.h,v 1.2 2000/10/02 17:19:15 itojun Exp $ */
/* $KAME: rijndael-alg-fst.h,v 1.5 2003/07/15 10:47:16 itojun Exp $ */
/**
* rijndael-alg-fst.h
*
* @version 3.0 (December 2000)
*
* Optimised ANSI C code for the Rijndael cipher (now AES)
*
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
* @author Paulo Barreto <paulo.barreto@terra.com.br>
*
* This code is hereby placed in the public domain.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __RIJNDAEL_ALG_FST_H
#define __RIJNDAEL_ALG_FST_H
/* symbol renaming */
#define rijndaelKeySetupEnc _hc_rijndaelKeySetupEnc
#define rijndaelKeySetupDec _hc_rijndaelKeySetupDec
#define rijndaelEncrypt _hc_rijndaelEncrypt
#define rijndaelDecrypt _hc_rijndaelDecrypt
#define RIJNDAEL_MAXKC (256/32)
#define RIJNDAEL_MAXKB (256/8)
#define RIJNDAEL_MAXNR 14
int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
int rijndaelKeySetupDec(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], int keyBits);
void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t pt[16], uint8_t ct[16]);
void rijndaelDecrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, const uint8_t ct[16], uint8_t pt[16]);
#endif /* __RIJNDAEL_ALG_FST_H */
+509
View File
@@ -0,0 +1,509 @@
/*
* Copyright (c) 1995, 1996, 1997, 1999 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: rnd_keys.c,v 1.71 2006/05/05 10:24:31 lha Exp $");
#endif
#ifdef KRB5
#include <krb5-types.h>
#endif
#include <des.h>
#include <stdlib.h>
#include <string.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#elif defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#else
#include <time.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
/*
* Generate "random" data by checksumming a file.
*
* Returns -1 if there were any problems with permissions or I/O
* errors.
*/
static
int
sumFile (const char *name, int len, void *res)
{
uint32_t sum[2] = { 0, 0 };
uint32_t buf[1024*2];
int fd, i;
fd = open (name, 0);
if (fd < 0)
return -1;
while (len > 0)
{
int n = read(fd, buf, sizeof(buf));
if (n < 0)
{
close(fd);
return n;
}
for (i = 0; i < (n/sizeof(buf[0])); i++)
{
sum[0] += buf[i];
i++;
sum[1] += buf[i];
}
len -= n;
}
close (fd);
memcpy (res, &sum, sizeof(sum));
return 0;
}
#if 0
static
int
md5sumFile (const char *name, int len, int32_t sum[4])
{
int32_t buf[1024*2];
int fd, cnt;
struct md5 md5;
fd = open (name, 0);
if (fd < 0)
return -1;
md5_init(&md5);
while (len > 0)
{
int n = read(fd, buf, sizeof(buf));
if (n < 0)
{
close(fd);
return n;
}
md5_update(&md5, buf, n);
len -= n;
}
md5_finito(&md5, (unsigned char *)sum);
close (fd);
return 0;
}
#endif
/*
* Create a sequence of random 64 bit blocks.
* The sequence is indexed with a long long and
* based on an initial des key used as a seed.
*/
static DES_key_schedule sequence_seed;
static uint32_t sequence_index[2];
/*
* Random number generator based on ideas from truerand in cryptolib
* as described on page 424 in Applied Cryptography 2 ed. by Bruce
* Schneier.
*/
static volatile int counter;
static volatile unsigned char *gdata; /* Global data */
static volatile int igdata; /* Index into global data */
static int gsize;
#if !defined(WIN32) && !defined(__EMX__) && !defined(__OS2__) && !defined(__CYGWIN32__)
/* Visual C++ 4.0 (Windows95/NT) */
static
RETSIGTYPE
sigALRM(int sig)
{
if (igdata < gsize)
gdata[igdata++] ^= counter & 0xff;
#ifndef HAVE_SIGACTION
signal(SIGALRM, sigALRM); /* Reinstall SysV signal handler */
#endif
SIGRETURN(0);
}
#endif
#if !defined(HAVE_RANDOM) && defined(HAVE_RAND)
#ifndef srandom
#define srandom srand
#endif
#ifndef random
#define random rand
#endif
#endif
#if !defined(HAVE_SETITIMER) || defined(WIN32) || defined(__EMX__) || defined(__OS2__) || defined(__CYGWIN32__)
static void
des_not_rand_data(unsigned char *data, int size)
{
int i;
srandom (time (NULL));
for(i = 0; i < size; ++i)
data[i] ^= random() % 0x100;
}
#endif
#if !defined(WIN32) && !defined(__EMX__) && !defined(__OS2__) && !defined(__CYGWIN32__)
#ifndef HAVE_SETITIMER
static void
pacemaker(struct timeval *tv)
{
fd_set fds;
pid_t pid;
pid = getppid();
while(1){
FD_ZERO(&fds);
FD_SET(0, &fds);
select(1, &fds, NULL, NULL, tv);
kill(pid, SIGALRM);
}
}
#endif
#ifdef HAVE_SIGACTION
/* XXX ugly hack, should perhaps use function from roken */
static RETSIGTYPE
(*fake_signal(int sig, RETSIGTYPE (*f)(int)))(int)
{
struct sigaction sa, osa;
sa.sa_handler = f;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(sig, &sa, &osa);
return osa.sa_handler;
}
#define signal(S, F) fake_signal((S), (F))
#endif
/*
* Generate size bytes of "random" data using timed interrupts.
* It takes about 40ms/byte random data.
* It's not neccessary to be root to run it.
*/
void
DES_rand_data(void *outdata, int size)
{
unsigned char *data = outdata;
struct itimerval tv, otv;
RETSIGTYPE (*osa)(int);
int i, j;
#ifndef HAVE_SETITIMER
RETSIGTYPE (*ochld)(int);
pid_t pid;
#endif
const char *rnd_devices[] = {"/dev/random",
"/dev/srandom",
"/dev/urandom",
"/dev/arandom",
NULL};
const char **p;
for(p = rnd_devices; *p; p++) {
int fd = open(*p, O_RDONLY | O_NDELAY);
if(fd >= 0 && read(fd, data, size) == size) {
close(fd);
return;
}
close(fd);
}
/* Paranoia? Initialize data from /dev/mem if we can read it. */
if (size >= 8)
sumFile("/dev/mem", (1024*1024*2), data);
gdata = data;
gsize = size;
igdata = 0;
osa = signal(SIGALRM, sigALRM);
/* Start timer */
tv.it_value.tv_sec = 0;
tv.it_value.tv_usec = 10 * 1000; /* 10 ms */
tv.it_interval = tv.it_value;
#ifdef HAVE_SETITIMER
setitimer(ITIMER_REAL, &tv, &otv);
#else
ochld = signal(SIGCHLD, SIG_IGN);
pid = fork();
if(pid == -1){
signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
des_not_rand_data(data, size);
return;
}
if(pid == 0)
pacemaker(&tv.it_interval);
#endif
for(i = 0; i < 4; i++) {
for (igdata = 0; igdata < size;) /* igdata++ in sigALRM */
counter++;
for (j = 0; j < size; j++) /* Only use 2 bits each lap */
gdata[j] = (gdata[j]>>2) | (gdata[j]<<6);
}
#ifdef HAVE_SETITIMER
setitimer(ITIMER_REAL, &otv, 0);
#else
kill(pid, SIGKILL);
while(waitpid(pid, NULL, 0) != pid);
signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
#endif
signal(SIGALRM, osa != SIG_ERR ? osa : SIG_DFL);
}
#else
void
DES_rand_data(unsigned char *p, int s)
{
des_not_rand_data (p, s);
}
#endif
void
DES_generate_random_block(DES_cblock *block)
{
DES_rand_data((unsigned char *)block, sizeof(*block));
}
#define DES_rand_data_key hc_DES_rand_data_key
void
DES_rand_data_key(DES_cblock *key);
/*
* Generate a "random" DES key.
*/
void
DES_rand_data_key(DES_cblock *key)
{
unsigned char data[8];
DES_key_schedule sched;
do {
DES_rand_data(data, sizeof(data));
DES_rand_data((unsigned char*)key, sizeof(DES_cblock));
DES_set_odd_parity(key);
DES_set_key(key, &sched);
DES_ecb_encrypt(&data, key, &sched, DES_ENCRYPT);
memset(&data, 0, sizeof(data));
memset(&sched, 0, sizeof(sched));
DES_set_odd_parity(key);
} while(DES_is_weak_key(key));
}
/*
* Generate "random" data by checksumming /dev/mem
*
* It's neccessary to be root to run it. Returns -1 if there were any
* problems with permissions.
*/
#define DES_mem_rand8 hc_DES_mem_rand8
int
DES_mem_rand8(unsigned char *data);
int
DES_mem_rand8(unsigned char *data)
{
return 1;
}
/*
* In case the generator does not get initialized use this as fallback.
*/
static int initialized;
static void
do_initialize(void)
{
DES_cblock default_seed;
do {
DES_generate_random_block(&default_seed);
DES_set_odd_parity(&default_seed);
} while (DES_is_weak_key(&default_seed));
DES_init_random_number_generator(&default_seed);
}
#define zero_long_long(ll) do { ll[0] = ll[1] = 0; } while (0)
#define incr_long_long(ll) do { if (++ll[0] == 0) ++ll[1]; } while (0)
#define set_sequence_number(ll) \
memcpy((char *)sequence_index, (ll), sizeof(sequence_index));
/*
* Set the sequnce number to this value (a long long).
*/
void
DES_set_sequence_number(void *ll)
{
set_sequence_number(ll);
}
/*
* Set the generator seed and reset the sequence number to 0.
*/
void
DES_set_random_generator_seed(DES_cblock *seed)
{
DES_set_key(seed, &sequence_seed);
zero_long_long(sequence_index);
initialized = 1;
}
/*
* Generate a sequence of random des keys
* using the random block sequence, fixup
* parity and skip weak keys.
*/
int
DES_new_random_key(DES_cblock *key)
{
if (!initialized)
do_initialize();
do {
DES_ecb_encrypt((DES_cblock *) sequence_index,
key,
&sequence_seed,
DES_ENCRYPT);
incr_long_long(sequence_index);
/* random key must have odd parity and not be weak */
DES_set_odd_parity(key);
} while (DES_is_weak_key(key));
return(0);
}
/*
* des_init_random_number_generator:
*
* Initialize the sequence of random 64 bit blocks. The input seed
* can be a secret key since it should be well hidden and is also not
* kept.
*
*/
void
DES_init_random_number_generator(DES_cblock *seed)
{
struct timeval now;
DES_cblock uniq;
DES_cblock new_key;
gettimeofday(&now, (struct timezone *)0);
DES_generate_random_block(&uniq);
/* Pick a unique random key from the shared sequence. */
DES_set_random_generator_seed(seed);
set_sequence_number((unsigned char *)&uniq);
DES_new_random_key(&new_key);
/* Select a new nonshared sequence, */
DES_set_random_generator_seed(&new_key);
/* and use the current time to pick a key for the new sequence. */
set_sequence_number((unsigned char *)&now);
DES_new_random_key(&new_key);
DES_set_random_generator_seed(&new_key);
}
/* This is for backwards compatibility. */
void
DES_random_key(DES_cblock *ret)
{
DES_new_random_key(ret);
}
#ifdef TESTRUN
int
main()
{
unsigned char data[8];
int i;
while (1)
{
if (sumFile("/dev/mem", (1024*1024*8), data) != 0)
{ perror("sumFile"); exit(1); }
for (i = 0; i < 8; i++)
printf("%02x", data[i]);
printf("\n");
}
}
#endif
#ifdef TESTRUN2
int
main()
{
DES_cblock data;
int i;
while (1)
{
do_initialize();
DES_random_key(data);
for (i = 0; i < 8; i++)
printf("%02x", data[i]);
printf("\n");
}
}
#endif
+168
View File
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: rsa.h,v 1.5 2006/05/07 11:34:02 lha Exp $
*/
#ifndef _HEIM_RSA_H
#define _HEIM_RSA_H 1
/* symbol renaming */
#define RSA_null_method hc_RSA_null_method
#define RSA_imath_method hc_RSA_imath_method
#define RSA_new hc_RSA_new
#define RSA_new_method hc_RSA_new_method
#define RSA_free hc_RSA_free
#define RSA_up_ref hc_RSA_up_ref
#define RSA_set_default_method hc_RSA_set_default_method
#define RSA_get_default_method hc_RSA_get_default_method
#define RSA_set_method hc_RSA_set_method
#define RSA_get_method hc_RSA_get_method
#define RSA_set_app_data hc_RSA_set_app_data
#define RSA_get_app_data hc_RSA_get_app_data
#define RSA_check_key hc_RSA_check_key
#define RSA_size hc_RSA_size
#define RSA_public_encrypt hc_RSA_public_encrypt
#define RSA_public_decrypt hc_RSA_public_decrypt
#define RSA_private_encrypt hc_RSA_private_encrypt
#define RSA_private_decrypt hc_RSA_private_decrypt
#define RSA_sign hc_RSA_sign
#define RSA_verify hc_RSA_verify
#define d2i_RSAPrivateKey hc_d2i_RSAPrivateKey
#define i2d_RSAPublicKey hc_i2d_RSAPublicKey
/*
*
*/
typedef struct RSA RSA;
typedef struct RSA_METHOD RSA_METHOD;
#include <hcrypto/bn.h>
#include <hcrypto/engine.h>
struct RSA_METHOD {
const char *name;
int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
void *rsa_mod_exp;
void *bn_mod_exp;
int (*init)(RSA *rsa);
int (*finish)(RSA *rsa);
int flags;
char *app_data;
int (*rsa_sign)(int, const unsigned char *, unsigned int,
unsigned char *, unsigned int *, const RSA *);
int (*rsa_verify)(int, const unsigned char *, unsigned int,
unsigned char *, unsigned int, const RSA *);
int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
};
struct RSA {
int pad;
long version;
const RSA_METHOD *meth;
void *engine;
BIGNUM *n;
BIGNUM *e;
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *dmp1;
BIGNUM *dmq1;
BIGNUM *iqmp;
struct rsa_CRYPTO_EX_DATA {
void *sk;
int dummy;
} ex_data;
int references;
int flags;
void *_method_mod_n;
void *_method_mod_p;
void *_method_mod_q;
char *bignum_data;
void *blinding;
void *mt_blinding;
};
#define RSA_FLAG_SIGN_VER 0x40
#define RSA_PKCS1_PADDING 1
#define RSA_PKCS1_PADDING_SIZE 11
/*
*
*/
const RSA_METHOD *RSA_null_method(void);
const RSA_METHOD *RSA_imath_method(void);
/*
*
*/
RSA * RSA_new(void);
RSA * RSA_new_method(ENGINE *);
void RSA_free(RSA *);
int RSA_up_ref(RSA *);
void RSA_set_default_method(const RSA_METHOD *);
const RSA_METHOD * RSA_get_default_method(void);
const RSA_METHOD * RSA_get_method(const RSA *);
int RSA_set_method(RSA *, const RSA_METHOD *);
int RSA_set_app_data(RSA *, void *arg);
void * RSA_get_app_data(RSA *);
int RSA_check_key(const RSA *);
int RSA_size(const RSA *);
int RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
int RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
int RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
int RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
int RSA_sign(int, const unsigned char *, unsigned int,
unsigned char *, unsigned int *, RSA *);
int RSA_verify(int, const unsigned char *, unsigned int,
unsigned char *, unsigned int, RSA *);
RSA * d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t);
int i2d_RSAPublicKey(RSA *, unsigned char **);
#endif /* _HEIM_RSA_H */
+300
View File
@@ -0,0 +1,300 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: sha.c,v 1.19 2006/05/05 10:25:00 lha Exp $");
#endif
#include "hash.h"
#include "sha.h"
#define A m->counter[0]
#define B m->counter[1]
#define C m->counter[2]
#define D m->counter[3]
#define E m->counter[4]
#define X data
void
SHA1_Init (struct sha *m)
{
m->sz[0] = 0;
m->sz[1] = 0;
A = 0x67452301;
B = 0xefcdab89;
C = 0x98badcfe;
D = 0x10325476;
E = 0xc3d2e1f0;
}
#define F0(x,y,z) CRAYFIX((x & y) | (~x & z))
#define F1(x,y,z) (x ^ y ^ z)
#define F2(x,y,z) ((x & y) | (x & z) | (y & z))
#define F3(x,y,z) F1(x,y,z)
#define K0 0x5a827999
#define K1 0x6ed9eba1
#define K2 0x8f1bbcdc
#define K3 0xca62c1d6
#define DO(t,f,k) \
do { \
uint32_t temp; \
\
temp = cshift(AA, 5) + f(BB,CC,DD) + EE + data[t] + k; \
EE = DD; \
DD = CC; \
CC = cshift(BB, 30); \
BB = AA; \
AA = temp; \
} while(0)
static inline void
calc (struct sha *m, uint32_t *in)
{
uint32_t AA, BB, CC, DD, EE;
uint32_t data[80];
int i;
AA = A;
BB = B;
CC = C;
DD = D;
EE = E;
for (i = 0; i < 16; ++i)
data[i] = in[i];
for (i = 16; i < 80; ++i)
data[i] = cshift(data[i-3] ^ data[i-8] ^ data[i-14] ^ data[i-16], 1);
/* t=[0,19] */
DO(0,F0,K0);
DO(1,F0,K0);
DO(2,F0,K0);
DO(3,F0,K0);
DO(4,F0,K0);
DO(5,F0,K0);
DO(6,F0,K0);
DO(7,F0,K0);
DO(8,F0,K0);
DO(9,F0,K0);
DO(10,F0,K0);
DO(11,F0,K0);
DO(12,F0,K0);
DO(13,F0,K0);
DO(14,F0,K0);
DO(15,F0,K0);
DO(16,F0,K0);
DO(17,F0,K0);
DO(18,F0,K0);
DO(19,F0,K0);
/* t=[20,39] */
DO(20,F1,K1);
DO(21,F1,K1);
DO(22,F1,K1);
DO(23,F1,K1);
DO(24,F1,K1);
DO(25,F1,K1);
DO(26,F1,K1);
DO(27,F1,K1);
DO(28,F1,K1);
DO(29,F1,K1);
DO(30,F1,K1);
DO(31,F1,K1);
DO(32,F1,K1);
DO(33,F1,K1);
DO(34,F1,K1);
DO(35,F1,K1);
DO(36,F1,K1);
DO(37,F1,K1);
DO(38,F1,K1);
DO(39,F1,K1);
/* t=[40,59] */
DO(40,F2,K2);
DO(41,F2,K2);
DO(42,F2,K2);
DO(43,F2,K2);
DO(44,F2,K2);
DO(45,F2,K2);
DO(46,F2,K2);
DO(47,F2,K2);
DO(48,F2,K2);
DO(49,F2,K2);
DO(50,F2,K2);
DO(51,F2,K2);
DO(52,F2,K2);
DO(53,F2,K2);
DO(54,F2,K2);
DO(55,F2,K2);
DO(56,F2,K2);
DO(57,F2,K2);
DO(58,F2,K2);
DO(59,F2,K2);
/* t=[60,79] */
DO(60,F3,K3);
DO(61,F3,K3);
DO(62,F3,K3);
DO(63,F3,K3);
DO(64,F3,K3);
DO(65,F3,K3);
DO(66,F3,K3);
DO(67,F3,K3);
DO(68,F3,K3);
DO(69,F3,K3);
DO(70,F3,K3);
DO(71,F3,K3);
DO(72,F3,K3);
DO(73,F3,K3);
DO(74,F3,K3);
DO(75,F3,K3);
DO(76,F3,K3);
DO(77,F3,K3);
DO(78,F3,K3);
DO(79,F3,K3);
A += AA;
B += BB;
C += CC;
D += DD;
E += EE;
}
/*
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
*/
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
static inline uint32_t
swap_uint32_t (uint32_t t)
{
#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
uint32_t temp1, temp2;
temp1 = cshift(t, 16);
temp2 = temp1 >> 8;
temp1 &= 0x00ff00ff;
temp2 &= 0x00ff00ff;
temp1 <<= 8;
return temp1 | temp2;
}
#endif
struct x32{
unsigned int a:32;
unsigned int b:32;
};
void
SHA1_Update (struct sha *m, const void *v, size_t len)
{
const unsigned char *p = v;
size_t old_sz = m->sz[0];
size_t offset;
m->sz[0] += len * 8;
if (m->sz[0] < old_sz)
++m->sz[1];
offset = (old_sz / 8) % 64;
while(len > 0){
size_t l = min(len, 64 - offset);
memcpy(m->save + offset, p, l);
offset += l;
p += l;
len -= l;
if(offset == 64){
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
int i;
uint32_t current[16];
struct x32 *u = (struct x32*)m->save;
for(i = 0; i < 8; i++){
current[2*i+0] = swap_uint32_t(u[i].a);
current[2*i+1] = swap_uint32_t(u[i].b);
}
calc(m, current);
#else
calc(m, (uint32_t*)m->save);
#endif
offset = 0;
}
}
}
void
SHA1_Final (void *res, struct sha *m)
{
unsigned char zeros[72];
unsigned offset = (m->sz[0] / 8) % 64;
unsigned int dstart = (120 - offset - 1) % 64 + 1;
*zeros = 0x80;
memset (zeros + 1, 0, sizeof(zeros) - 1);
zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
SHA1_Update (m, zeros, dstart + 8);
{
int i;
unsigned char *r = (unsigned char*)res;
for (i = 0; i < 5; ++i) {
r[4*i+3] = m->counter[i] & 0xFF;
r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
r[4*i] = (m->counter[i] >> 24) & 0xFF;
}
}
#if 0
{
int i;
uint32_t *r = (uint32_t *)res;
for (i = 0; i < 5; ++i)
r[i] = swap_uint32_t (m->counter[i]);
}
#endif
}
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: sha.h,v 1.11 2006/05/05 11:06:21 lha Exp $ */
#ifndef HEIM_SHA_H
#define HEIM_SHA_H 1
/* symbol renaming */
#define SHA1_Init hc_SHA1_Init
#define SHA1_Update hc_SHA1_Update
#define SHA1_Final hc_SHA1_Final
#define SHA256_Init hc_SHA256_Init
#define SHA256_Update hc_SHA256_Update
#define SHA256_Final hc_SHA256_Final
/*
* SHA-1
*/
#define SHA_DIGEST_LENGTH 20
struct sha {
unsigned int sz[2];
uint32_t counter[5];
unsigned char save[64];
};
typedef struct sha SHA_CTX;
void SHA1_Init (struct sha *m);
void SHA1_Update (struct sha *m, const void *v, size_t len);
void SHA1_Final (void *res, struct sha *m);
/*
* SHA-2 256
*/
#define SHA256_DIGEST_LENGTH 32
struct hc_sha256state {
unsigned int sz[2];
uint32_t counter[8];
unsigned char save[64];
};
typedef struct hc_sha256state SHA256_CTX;
void SHA256_Init (SHA256_CTX *);
void SHA256_Update (SHA256_CTX *, const void *, size_t);
void SHA256_Final (void *, SHA256_CTX *);
#endif /* HEIM_SHA_H */
+233
View File
@@ -0,0 +1,233 @@
/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
RCSID("$Id: sha256.c,v 1.2 2006/05/05 10:25:37 lha Exp $");
#endif
#include "hash.h"
#include "sha.h"
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define ROTR(x,n) (((x)>>(n)) | ((x) << (32 - (n))))
#define Sigma0(x) (ROTR(x,2) ^ ROTR(x,13) ^ ROTR(x,22))
#define Sigma1(x) (ROTR(x,6) ^ ROTR(x,11) ^ ROTR(x,25))
#define sigma0(x) (ROTR(x,7) ^ ROTR(x,18) ^ ((x)>>3))
#define sigma1(x) (ROTR(x,17) ^ ROTR(x,19) ^ ((x)>>10))
#define A m->counter[0]
#define B m->counter[1]
#define C m->counter[2]
#define D m->counter[3]
#define E m->counter[4]
#define F m->counter[5]
#define G m->counter[6]
#define H m->counter[7]
static const uint32_t constant_256[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
void
SHA256_Init (SHA256_CTX *m)
{
m->sz[0] = 0;
m->sz[1] = 0;
A = 0x6a09e667;
B = 0xbb67ae85;
C = 0x3c6ef372;
D = 0xa54ff53a;
E = 0x510e527f;
F = 0x9b05688c;
G = 0x1f83d9ab;
H = 0x5be0cd19;
}
static void
calc (SHA256_CTX *m, uint32_t *in)
{
uint32_t AA, BB, CC, DD, EE, FF, GG, HH;
uint32_t data[64];
int i;
AA = A;
BB = B;
CC = C;
DD = D;
EE = E;
FF = F;
GG = G;
HH = H;
for (i = 0; i < 16; ++i)
data[i] = in[i];
for (i = 16; i < 64; ++i)
data[i] = sigma1(data[i-2]) + data[i-7] +
sigma0(data[i-15]) + data[i - 16];
for (i = 0; i < 64; i++) {
uint32_t T1, T2;
T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_256[i] + data[i];
T2 = Sigma0(AA) + Maj(AA,BB,CC);
HH = GG;
GG = FF;
FF = EE;
EE = DD + T1;
DD = CC;
CC = BB;
BB = AA;
AA = T1 + T2;
}
A += AA;
B += BB;
C += CC;
D += DD;
E += EE;
F += FF;
G += GG;
H += HH;
}
/*
* From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
*/
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
static inline uint32_t
swap_uint32_t (uint32_t t)
{
#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
uint32_t temp1, temp2;
temp1 = cshift(t, 16);
temp2 = temp1 >> 8;
temp1 &= 0x00ff00ff;
temp2 &= 0x00ff00ff;
temp1 <<= 8;
return temp1 | temp2;
}
#endif
struct x32{
unsigned int a:32;
unsigned int b:32;
};
void
SHA256_Update (SHA256_CTX *m, const void *v, size_t len)
{
const unsigned char *p = v;
size_t old_sz = m->sz[0];
size_t offset;
m->sz[0] += len * 8;
if (m->sz[0] < old_sz)
++m->sz[1];
offset = (old_sz / 8) % 64;
while(len > 0){
size_t l = min(len, 64 - offset);
memcpy(m->save + offset, p, l);
offset += l;
p += l;
len -= l;
if(offset == 64){
#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
int i;
uint32_t current[16];
struct x32 *u = (struct x32*)m->save;
for(i = 0; i < 8; i++){
current[2*i+0] = swap_uint32_t(u[i].a);
current[2*i+1] = swap_uint32_t(u[i].b);
}
calc(m, current);
#else
calc(m, (uint32_t*)m->save);
#endif
offset = 0;
}
}
}
void
SHA256_Final (void *res, SHA256_CTX *m)
{
unsigned char zeros[72];
unsigned offset = (m->sz[0] / 8) % 64;
unsigned int dstart = (120 - offset - 1) % 64 + 1;
*zeros = 0x80;
memset (zeros + 1, 0, sizeof(zeros) - 1);
zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
SHA256_Update (m, zeros, dstart + 8);
{
int i;
unsigned char *r = (unsigned char*)res;
for (i = 0; i < 8; ++i) {
r[4*i+3] = m->counter[i] & 0xFF;
r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
r[4*i] = (m->counter[i] >> 24) & 0xFF;
}
}
}
+164
View File
@@ -0,0 +1,164 @@
/*
* Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: ui.c,v 1.6 2006/09/22 15:45:57 lha Exp $");
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <roken.h>
#include <ui.h>
static sig_atomic_t intr_flag;
static void
intr(int sig)
{
intr_flag++;
}
#ifndef NSIG
#define NSIG 47
#endif
static int
read_string(const char *preprompt, const char *prompt,
char *buf, size_t len, int echo)
{
struct sigaction sigs[NSIG];
int oksigs[NSIG];
struct sigaction sa;
FILE *tty;
int ret = 0;
int of = 0;
int i;
int c;
char *p;
struct termios t_new, t_old;
memset(&oksigs, 0, sizeof(oksigs));
memset(&sa, 0, sizeof(sa));
sa.sa_handler = intr;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
if (i != SIGALRM)
if (sigaction(i, &sa, &sigs[i]) == 0)
oksigs[i] = 1;
if((tty = fopen("/dev/tty", "r")) == NULL)
tty = stdin;
fprintf(stderr, "%s%s", preprompt, prompt);
fflush(stderr);
if(echo == 0){
tcgetattr(fileno(tty), &t_old);
memcpy(&t_new, &t_old, sizeof(t_new));
t_new.c_lflag &= ~ECHO;
tcsetattr(fileno(tty), TCSANOW, &t_new);
}
intr_flag = 0;
p = buf;
while(intr_flag == 0){
c = getc(tty);
if(c == EOF){
if(!ferror(tty))
ret = 1;
break;
}
if(c == '\n')
break;
if(of == 0)
*p++ = c;
of = (p == buf + len);
}
if(of)
p--;
*p = 0;
if(echo == 0){
printf("\n");
tcsetattr(fileno(tty), TCSANOW, &t_old);
}
if(tty != stdin)
fclose(tty);
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
if (oksigs[i])
sigaction(i, &sigs[i], NULL);
if(ret)
return -3;
if(intr_flag)
return -2;
if(of)
return -1;
return 0;
}
int
UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
{
int ret;
ret = read_string("", prompt, buf, length, 0);
if (ret)
return ret;
if (verify) {
char *buf2;
buf2 = malloc(length);
if (buf2 == NULL)
return 1;
ret = read_string("Verify password - ", prompt, buf2, length, 0);
if (ret) {
free(buf2);
return ret;
}
if (strcmp(buf2, buf) != 0)
ret = 1;
free(buf2);
}
return ret;
}
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $Id: ui.h,v 1.1 2006/01/08 21:47:29 lha Exp $ */
#ifndef _HEIM_UI_H
#define _HEIM_UI_H 1
/* symbol renaming */
#define UI_UTIL_read_pw_string hc_UI_UTIL_read_pw_string
int UI_UTIL_read_pw_string(char *, int, const char *, int); /* XXX */
#endif /* _HEIM_UI_H */

Some files were not shown because too many files have changed in this diff Show More