2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 #include <pkinit_asn1.h>
38 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
39 _krb5_pk_octetstring2key(krb5_context context
,
43 const heim_octet_string
*c_n
,
44 const heim_octet_string
*k_n
,
47 struct _krb5_encryption_type
*et
= _krb5_find_enctype(type
);
49 size_t keylen
, offset
;
51 unsigned char counter
;
52 unsigned char shaoutput
[SHA_DIGEST_LENGTH
];
56 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
57 N_("encryption type %d not supported", ""),
59 return KRB5_PROG_ETYPE_NOSUPP
;
61 keylen
= (et
->keytype
->bits
+ 7) / 8;
63 keydata
= malloc(keylen
);
65 return krb5_enomem(context
);
67 m
= EVP_MD_CTX_create();
70 return krb5_enomem(context
);
77 EVP_DigestInit_ex(m
, EVP_sha1(), NULL
);
78 EVP_DigestUpdate(m
, &counter
, 1);
79 EVP_DigestUpdate(m
, dhdata
, dhsize
);
82 EVP_DigestUpdate(m
, c_n
->data
, c_n
->length
);
84 EVP_DigestUpdate(m
, k_n
->data
, k_n
->length
);
86 EVP_DigestFinal_ex(m
, shaoutput
, NULL
);
88 memcpy((unsigned char *)keydata
+ offset
,
90 min(keylen
- offset
, sizeof(shaoutput
)));
92 offset
+= sizeof(shaoutput
);
94 } while(offset
< keylen
);
95 memset(shaoutput
, 0, sizeof(shaoutput
));
97 EVP_MD_CTX_destroy(m
);
99 ret
= krb5_random_to_key(context
, type
, keydata
, keylen
, key
);
100 memset(keydata
, 0, sizeof(keylen
));
105 static krb5_error_code
106 encode_uvinfo(krb5_context context
, krb5_const_principal p
, krb5_data
*data
)
108 KRB5PrincipalName pn
;
112 pn
.principalName
= p
->name
;
115 ASN1_MALLOC_ENCODE(KRB5PrincipalName
, data
->data
, data
->length
,
118 krb5_data_zero(data
);
119 krb5_set_error_message(context
, ret
,
120 N_("Failed to encode KRB5PrincipalName", ""));
123 if (data
->length
!= size
)
124 krb5_abortx(context
, "asn1 compiler internal error");
128 static krb5_error_code
129 encode_otherinfo(krb5_context context
,
130 const AlgorithmIdentifier
*ai
,
131 krb5_const_principal client
,
132 krb5_const_principal server
,
133 krb5_enctype enctype
,
134 const krb5_data
*as_req
,
135 const krb5_data
*pk_as_rep
,
136 const Ticket
*ticket
,
139 PkinitSP80056AOtherInfo otherinfo
;
140 PkinitSuppPubInfo pubinfo
;
145 krb5_data_zero(other
);
146 memset(&otherinfo
, 0, sizeof(otherinfo
));
147 memset(&pubinfo
, 0, sizeof(pubinfo
));
149 pubinfo
.enctype
= enctype
;
150 pubinfo
.as_REQ
= *as_req
;
151 pubinfo
.pk_as_rep
= *pk_as_rep
;
152 pubinfo
.ticket
= *ticket
;
153 ASN1_MALLOC_ENCODE(PkinitSuppPubInfo
, pub
.data
, pub
.length
,
154 &pubinfo
, &size
, ret
);
156 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
159 if (pub
.length
!= size
)
160 krb5_abortx(context
, "asn1 compiler internal error");
162 ret
= encode_uvinfo(context
, client
, &otherinfo
.partyUInfo
);
167 ret
= encode_uvinfo(context
, server
, &otherinfo
.partyVInfo
);
169 free(otherinfo
.partyUInfo
.data
);
174 otherinfo
.algorithmID
= *ai
;
175 otherinfo
.suppPubInfo
= &pub
;
177 ASN1_MALLOC_ENCODE(PkinitSP80056AOtherInfo
, other
->data
, other
->length
,
178 &otherinfo
, &size
, ret
);
179 free(otherinfo
.partyUInfo
.data
);
180 free(otherinfo
.partyVInfo
.data
);
183 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
186 if (other
->length
!= size
)
187 krb5_abortx(context
, "asn1 compiler internal error");
194 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
195 _krb5_pk_kdf(krb5_context context
,
196 const struct AlgorithmIdentifier
*ai
,
199 krb5_const_principal client
,
200 krb5_const_principal server
,
201 krb5_enctype enctype
,
202 const krb5_data
*as_req
,
203 const krb5_data
*pk_as_rep
,
204 const Ticket
*ticket
,
207 struct _krb5_encryption_type
*et
;
210 size_t keylen
, offset
;
212 unsigned char *keydata
;
213 unsigned char shaoutput
[SHA512_DIGEST_LENGTH
];
217 if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha1
, &ai
->algorithm
) == 0) {
219 } else if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha256
, &ai
->algorithm
) == 0) {
221 } else if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha512
, &ai
->algorithm
) == 0) {
224 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
225 N_("KDF not supported", ""));
226 return KRB5_PROG_ETYPE_NOSUPP
;
228 if (ai
->parameters
!= NULL
&&
229 (ai
->parameters
->length
!= 2 ||
230 memcmp(ai
->parameters
->data
, "\x05\x00", 2) != 0))
232 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
233 N_("kdf params not NULL or the NULL-type",
235 return KRB5_PROG_ETYPE_NOSUPP
;
238 et
= _krb5_find_enctype(enctype
);
240 krb5_set_error_message(context
, KRB5_PROG_ETYPE_NOSUPP
,
241 N_("encryption type %d not supported", ""),
243 return KRB5_PROG_ETYPE_NOSUPP
;
245 keylen
= (et
->keytype
->bits
+ 7) / 8;
247 keydata
= malloc(keylen
);
249 return krb5_enomem(context
);
251 ret
= encode_otherinfo(context
, ai
, client
, server
,
252 enctype
, as_req
, pk_as_rep
, ticket
, &other
);
258 m
= EVP_MD_CTX_create();
262 return krb5_enomem(context
);
268 unsigned char cdata
[4];
270 EVP_DigestInit_ex(m
, md
, NULL
);
271 _krb5_put_int(cdata
, counter
, 4);
272 EVP_DigestUpdate(m
, cdata
, 4);
273 EVP_DigestUpdate(m
, dhdata
, dhsize
);
274 EVP_DigestUpdate(m
, other
.data
, other
.length
);
276 EVP_DigestFinal_ex(m
, shaoutput
, NULL
);
278 memcpy((unsigned char *)keydata
+ offset
,
280 min(keylen
- offset
, EVP_MD_CTX_size(m
)));
282 offset
+= EVP_MD_CTX_size(m
);
284 } while(offset
< keylen
);
285 memset(shaoutput
, 0, sizeof(shaoutput
));
287 EVP_MD_CTX_destroy(m
);
290 ret
= krb5_random_to_key(context
, enctype
, keydata
, keylen
, key
);
291 memset(keydata
, 0, sizeof(keylen
));