Path: blob/main/crypto/krb5/src/tests/asn.1/make-vectors.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/asn.1/make-vectors.c - Generate ASN.1 test vectors using asn1c */2/*3* Copyright (C) 2011 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* This program generates test vectors using asn1c, to be included in other28* test programs which exercise the krb5 ASN.1 encoder and decoder functions.29* It is intended to be used via "make test-vectors". Currently, test vectors30* are only generated for a subset of newer ASN.1 objects.31*/3233#include <PrincipalName.h>34#include <KRB5PrincipalName.h>35#include <OtherInfo.h>36#include <PkinitSuppPubInfo.h>37#include <OTP-TOKENINFO.h>38#include <PA-OTP-CHALLENGE.h>39#include <PA-OTP-REQUEST.h>40#include <PA-OTP-ENC-REQUEST.h>41#include <AD-CAMMAC.h>42#include <SPAKESecondFactor.h>43#include <PA-SPAKE.h>4445static unsigned char buf[8192];46static size_t buf_pos;4748/* PrincipalName and KRB5PrincipalName */49static KerberosString_t comp_1 = { "hftsai", 6 };50static KerberosString_t comp_2 = { "extra", 5 };51static KerberosString_t *comps[] = { &comp_1, &comp_2 };52static PrincipalName_t princ = { 1, { comps, 2, 2 } };53static KRB5PrincipalName_t krb5princ = { { "ATHENA.MIT.EDU", 14 },54{ 1, { comps, 2, 2 } } };5556/* OtherInfo */57static unsigned int krb5_arcs[] = { 1, 2, 840, 113554, 1, 2, 2 };58static OCTET_STRING_t krb5data_ostring = { "krb5data", 8 };59static OtherInfo_t other_info = {60{ 0 }, { 0 }, { 0 }, /* Initialized in main() */61&krb5data_ostring, NULL62};6364/* PkinitSuppPubInfo */65static PkinitSuppPubInfo_t supp_pub_info = { 1, { "krb5data", 8 },66{ "krb5data", 8 } };6768/* Minimal OTP-TOKENINFO */69static OTP_TOKENINFO_t token_info_1 = { { "\0\0\0\0", 4, 0 } };7071/* Maximal OTP-TOKENINFO */72static UTF8String_t vendor = { "Examplecorp", 11 };73static OCTET_STRING_t challenge = { "hark!", 5 };74static Int32_t otp_length = 10;75static OTPFormat_t otp_format; /* Initialized to 2 in main(). */76static OCTET_STRING_t token_id = { "yourtoken", 9 };77static AnyURI_t otp_alg = { "urn:ietf:params:xml:ns:keyprov:pskc:hotp", 40 };78static unsigned int sha256_arcs[] = { 2, 16, 840, 1, 101, 3, 4, 2, 1 };79static unsigned int sha1_arcs[] = { 1, 3, 14, 3, 2, 26 };80static AlgorithmIdentifier_t alg_sha256, alg_sha1; /* Initialized in main(). */81static AlgorithmIdentifier_t *algs[] = { &alg_sha256, &alg_sha1 };82static struct supportedHashAlg hash_algs = { algs, 2, 2 };83static Int32_t iter_count = 1000;84/* Flags are nextOTP | combine | collect-pin | must-encrypt-nonce |85* separate-pin-required | check-digit */86static OTP_TOKENINFO_t token_info_2 = { { "\x77\0\0\0", 4, 0 }, &vendor,87&challenge, &otp_length, &otp_format,88&token_id, &otp_alg, &hash_algs,89&iter_count };9091/* Minimal PA-OTP-CHALLENGE */92static OTP_TOKENINFO_t *tinfo_1[] = { &token_info_1 };93static PA_OTP_CHALLENGE_t challenge_1 = { { "minnonce", 8 }, NULL,94{ { tinfo_1, 1, 1 } } };9596/* Maximal PA-OTP-CHALLENGE */97static OTP_TOKENINFO_t *tinfo_2[] = { &token_info_1, &token_info_2 };98static UTF8String_t service = { "testservice", 11 };99static KerberosString_t salt = { "keysalt", 7 };100static OCTET_STRING_t s2kparams = { "1234", 4 };101static PA_OTP_CHALLENGE_t challenge_2 = { { "maxnonce", 8 }, &service,102{ { tinfo_2, 2, 2 } }, &salt,103&s2kparams };104105/* Minimal PA-OTP-REQUEST */106static UInt32_t kvno = 5;107static PA_OTP_REQUEST_t request_1 = { { "\0\0\0\0", 4, 0 }, NULL,108{ 0, &kvno,109{ "krbASN.1 test message", 21 } } };110111/* Maximal PA-OTP-REQUEST */112/* Flags are nextOTP | combine */113static OCTET_STRING_t nonce = { "nonce", 5 };114static OCTET_STRING_t otp_value = { "frogs", 5 };115static UTF8String_t otp_pin = { "myfirstpin", 10 };116/* Corresponds to Unix time 771228197 */117static KerberosTime_t otp_time = { "19940610060317Z", 15 };118static OCTET_STRING_t counter = { "346", 3 };119static PA_OTP_REQUEST_t request_2 = { { "\x60\0\0\0", 4, 0 }, &nonce,120{ 0, &kvno,121{ "krbASN.1 test message", 21 } },122&alg_sha256, &iter_count, &otp_value,123&otp_pin, &challenge, &otp_time,124&counter, &otp_format, &token_id,125&otp_alg, &vendor };126127/* PA-OTP-ENC-REQUEST */128static PA_OTP_ENC_REQUEST_t enc_request = { { "krb5data", 8 } };129130/*131* There is no ASN.1 name for a single authorization data element, so asn1c132* declares it as "struct Member" in an inner scope. This structure must be133* laid out identically to that one.134*/135struct ad_element {136Int32_t ad_type;137OCTET_STRING_t ad_data;138asn_struct_ctx_t _asn_ctx;139};140141/* Authorization data elements and lists, for use in CAMMAC */142static struct ad_element ad_1 = { 1, { "ad1", 3 } };143static struct ad_element ad_2 = { 2, { "ad2", 3 } };144static struct ad_element *adlist_1[] = { &ad_1 };145static struct ad_element *adlist_2[] = { &ad_1, &ad_2 };146147/* Minimal Verifier */148static Verifier_t verifier_1 = { Verifier_PR_mac,149{ { NULL, NULL, NULL,150{ 1, { "cksum1", 6 } } } } };151152/* Maximal Verifier */153static Int32_t enctype = 16;154static Verifier_t verifier_2 = { Verifier_PR_mac,155{ { &princ, &kvno, &enctype,156{ 1, { "cksum2", 6 } } } } };157158/* Minimal CAMMAC */159static AD_CAMMAC_t cammac_1 = { { { (void *)adlist_1, 1, 1 } },160NULL, NULL, NULL };161162/* Maximal CAMMAC */163static Verifier_MAC_t vmac_1 = { &princ, &kvno, &enctype,164{ 1, { "cksumkdc", 8 } } };165static Verifier_MAC_t vmac_2 = { &princ, &kvno, &enctype,166{ 1, { "cksumsvc", 8 } } };167static Verifier_t *verifiers[] = { &verifier_1, &verifier_2 };168static struct other_verifiers overfs = { { verifiers, 2, 2 } };169static AD_CAMMAC_t cammac_2 = { { { (void *)adlist_2, 2, 2 } },170&vmac_1, &vmac_2, &overfs };171172/* SPAKESecondFactor */173static SPAKESecondFactor_t factor_1 = { 1, NULL };174static OCTET_STRING_t factor_data = { "fdata", 5 };175static SPAKESecondFactor_t factor_2 = { 2, &factor_data };176177/* PA-SPAKE (support) */178static Int32_t group_1 = 1, group_2 = 2, *groups[] = { &group_1, &group_2 };179static PA_SPAKE_t pa_spake_1 = { PA_SPAKE_PR_support,180{ .support = { { groups, 2, 2 } } } };181182/* PA-SPAKE (challenge) */183static SPAKESecondFactor_t *factors[2] = { &factor_1, &factor_2 };184static PA_SPAKE_t pa_spake_2 = { PA_SPAKE_PR_challenge,185{ .challenge = { 1, { "T value", 7 },186{ factors, 2, 2 } } } };187188/* PA-SPAKE (response) */189UInt32_t enctype_5 = 5;190static PA_SPAKE_t pa_spake_3 = { PA_SPAKE_PR_response,191{ .response = { { "S value", 7 },192{ 0, &enctype_5,193{ "krbASN.1 test message",19421 } } } } };195196/* PA-SPAKE (encdata) */197static PA_SPAKE_t pa_spake_4 = { PA_SPAKE_PR_encdata,198{ .encdata = { 0, &enctype_5,199{ "krbASN.1 test message",20021 } } } };201202static int203consume(const void *data, size_t size, void *dummy)204{205memcpy(buf + buf_pos, data, size);206buf_pos += size;207return 0;208}209210/* Display a C string literal representing the contents of buf, and211* reinitialize buf_pos for the next encoding operation. */212static void213printbuf(void)214{215size_t i;216217for (i = 0; i < buf_pos; i++) {218printf("%02X", buf[i]);219if (i + 1 < buf_pos)220printf(" ");221}222buf_pos = 0;223}224225int226main(void)227{228/* Initialize values which can't use static initializers. */229asn_long2INTEGER(&otp_format, 2); /* Alphanumeric */230OBJECT_IDENTIFIER_set_arcs(&alg_sha256.algorithm, sha256_arcs,231sizeof(*sha256_arcs),232sizeof(sha256_arcs) / sizeof(*sha256_arcs));233OBJECT_IDENTIFIER_set_arcs(&alg_sha1.algorithm, sha1_arcs,234sizeof(*sha1_arcs),235sizeof(sha1_arcs) / sizeof(*sha1_arcs));236OBJECT_IDENTIFIER_set_arcs(&other_info.algorithmID.algorithm, krb5_arcs,237sizeof(*krb5_arcs),238sizeof(krb5_arcs) / sizeof(*krb5_arcs));239240printf("PrincipalName:\n");241der_encode(&asn_DEF_PrincipalName, &princ, consume, NULL);242printbuf();243244/* Print this encoding and also use it to initialize two fields of245* other_info. */246printf("\nKRB5PrincipalName:\n");247der_encode(&asn_DEF_KRB5PrincipalName, &krb5princ, consume, NULL);248OCTET_STRING_fromBuf(&other_info.partyUInfo, buf, buf_pos);249OCTET_STRING_fromBuf(&other_info.partyVInfo, buf, buf_pos);250printbuf();251252printf("\nOtherInfo:\n");253der_encode(&asn_DEF_OtherInfo, &other_info, consume, NULL);254printbuf();255free(other_info.partyUInfo.buf);256free(other_info.partyVInfo.buf);257258printf("\nPkinitSuppPubInfo:\n");259der_encode(&asn_DEF_PkinitSuppPubInfo, &supp_pub_info, consume, NULL);260printbuf();261262printf("\nMinimal OTP-TOKEN-INFO:\n");263der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_1, consume, NULL);264printbuf();265266printf("\nMaximal OTP-TOKEN-INFO:\n");267der_encode(&asn_DEF_OTP_TOKENINFO, &token_info_2, consume, NULL);268printbuf();269270printf("\nMinimal PA-OTP-CHALLENGE:\n");271der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_1, consume, NULL);272printbuf();273274printf("\nMaximal PA-OTP-CHALLENGE:\n");275der_encode(&asn_DEF_PA_OTP_CHALLENGE, &challenge_2, consume, NULL);276printbuf();277278printf("\nMinimal PA-OTP-REQUEST:\n");279der_encode(&asn_DEF_PA_OTP_REQUEST, &request_1, consume, NULL);280printbuf();281282printf("\nMaximal PA-OTP-REQUEST:\n");283der_encode(&asn_DEF_PA_OTP_REQUEST, &request_2, consume, NULL);284printbuf();285286printf("\nPA-OTP-ENC-REQUEST:\n");287der_encode(&asn_DEF_PA_OTP_ENC_REQUEST, &enc_request, consume, NULL);288printbuf();289290printf("\nMinimal Verifier:\n");291der_encode(&asn_DEF_Verifier, &verifier_1, consume, NULL);292printbuf();293294printf("\nMaximal Verifier:\n");295der_encode(&asn_DEF_Verifier, &verifier_2, consume, NULL);296printbuf();297298printf("\nMinimal AD-CAMMAC:\n");299der_encode(&asn_DEF_AD_CAMMAC, &cammac_1, consume, NULL);300printbuf();301302printf("\nMaximal AD-CAMMAC:\n");303der_encode(&asn_DEF_AD_CAMMAC, &cammac_2, consume, NULL);304printbuf();305306printf("\nMinimal SPAKESecondFactor:\n");307der_encode(&asn_DEF_SPAKESecondFactor, &factor_1, consume, NULL);308printbuf();309310printf("\nMaximal SPAKESecondFactor:\n");311der_encode(&asn_DEF_SPAKESecondFactor, &factor_2, consume, NULL);312printbuf();313314printf("\nPA-SPAKE (support):\n");315der_encode(&asn_DEF_PA_SPAKE, &pa_spake_1, consume, NULL);316printbuf();317318printf("\nPA-SPAKE (challenge):\n");319der_encode(&asn_DEF_PA_SPAKE, &pa_spake_2, consume, NULL);320printbuf();321322printf("\nPA-SPAKE (response):\n");323der_encode(&asn_DEF_PA_SPAKE, &pa_spake_3, consume, NULL);324printbuf();325326printf("\nPA-SPAKE (encdata):\n");327der_encode(&asn_DEF_PA_SPAKE, &pa_spake_4, consume, NULL);328printbuf();329330printf("\n");331return 0;332}333334335