xref: /freebsd/crypto/heimdal/lib/krb5/crypto-pk.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1*ae771770SStanislav Sedov /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3*ae771770SStanislav Sedov  * (Royal Institute of Technology, Stockholm, Sweden).
4*ae771770SStanislav Sedov  * All rights reserved.
5*ae771770SStanislav Sedov  *
6*ae771770SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
7*ae771770SStanislav Sedov  * modification, are permitted provided that the following conditions
8*ae771770SStanislav Sedov  * are met:
9*ae771770SStanislav Sedov  *
10*ae771770SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
11*ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
12*ae771770SStanislav Sedov  *
13*ae771770SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
14*ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
15*ae771770SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
16*ae771770SStanislav Sedov  *
17*ae771770SStanislav Sedov  * 3. Neither the name of the Institute nor the names of its contributors
18*ae771770SStanislav Sedov  *    may be used to endorse or promote products derived from this software
19*ae771770SStanislav Sedov  *    without specific prior written permission.
20*ae771770SStanislav Sedov  *
21*ae771770SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22*ae771770SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*ae771770SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*ae771770SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25*ae771770SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*ae771770SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*ae771770SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*ae771770SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*ae771770SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*ae771770SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*ae771770SStanislav Sedov  * SUCH DAMAGE.
32*ae771770SStanislav Sedov  */
33*ae771770SStanislav Sedov 
34*ae771770SStanislav Sedov #include "krb5_locl.h"
35*ae771770SStanislav Sedov 
36*ae771770SStanislav Sedov #include <pkinit_asn1.h>
37*ae771770SStanislav Sedov 
38*ae771770SStanislav Sedov krb5_error_code
_krb5_pk_octetstring2key(krb5_context context,krb5_enctype type,const void * dhdata,size_t dhsize,const heim_octet_string * c_n,const heim_octet_string * k_n,krb5_keyblock * key)39*ae771770SStanislav Sedov _krb5_pk_octetstring2key(krb5_context context,
40*ae771770SStanislav Sedov 			 krb5_enctype type,
41*ae771770SStanislav Sedov 			 const void *dhdata,
42*ae771770SStanislav Sedov 			 size_t dhsize,
43*ae771770SStanislav Sedov 			 const heim_octet_string *c_n,
44*ae771770SStanislav Sedov 			 const heim_octet_string *k_n,
45*ae771770SStanislav Sedov 			 krb5_keyblock *key)
46*ae771770SStanislav Sedov {
47*ae771770SStanislav Sedov     struct _krb5_encryption_type *et = _krb5_find_enctype(type);
48*ae771770SStanislav Sedov     krb5_error_code ret;
49*ae771770SStanislav Sedov     size_t keylen, offset;
50*ae771770SStanislav Sedov     void *keydata;
51*ae771770SStanislav Sedov     unsigned char counter;
52*ae771770SStanislav Sedov     unsigned char shaoutput[SHA_DIGEST_LENGTH];
53*ae771770SStanislav Sedov     EVP_MD_CTX *m;
54*ae771770SStanislav Sedov 
55*ae771770SStanislav Sedov     if(et == NULL) {
56*ae771770SStanislav Sedov 	krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
57*ae771770SStanislav Sedov 			       N_("encryption type %d not supported", ""),
58*ae771770SStanislav Sedov 			       type);
59*ae771770SStanislav Sedov 	return KRB5_PROG_ETYPE_NOSUPP;
60*ae771770SStanislav Sedov     }
61*ae771770SStanislav Sedov     keylen = (et->keytype->bits + 7) / 8;
62*ae771770SStanislav Sedov 
63*ae771770SStanislav Sedov     keydata = malloc(keylen);
64*ae771770SStanislav Sedov     if (keydata == NULL) {
65*ae771770SStanislav Sedov 	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
66*ae771770SStanislav Sedov 	return ENOMEM;
67*ae771770SStanislav Sedov     }
68*ae771770SStanislav Sedov 
69*ae771770SStanislav Sedov     m = EVP_MD_CTX_create();
70*ae771770SStanislav Sedov     if (m == NULL) {
71*ae771770SStanislav Sedov 	free(keydata);
72*ae771770SStanislav Sedov 	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
73*ae771770SStanislav Sedov 	return ENOMEM;
74*ae771770SStanislav Sedov     }
75*ae771770SStanislav Sedov 
76*ae771770SStanislav Sedov     counter = 0;
77*ae771770SStanislav Sedov     offset = 0;
78*ae771770SStanislav Sedov     do {
79*ae771770SStanislav Sedov 
80*ae771770SStanislav Sedov 	EVP_DigestInit_ex(m, EVP_sha1(), NULL);
81*ae771770SStanislav Sedov 	EVP_DigestUpdate(m, &counter, 1);
82*ae771770SStanislav Sedov 	EVP_DigestUpdate(m, dhdata, dhsize);
83*ae771770SStanislav Sedov 
84*ae771770SStanislav Sedov 	if (c_n)
85*ae771770SStanislav Sedov 	    EVP_DigestUpdate(m, c_n->data, c_n->length);
86*ae771770SStanislav Sedov 	if (k_n)
87*ae771770SStanislav Sedov 	    EVP_DigestUpdate(m, k_n->data, k_n->length);
88*ae771770SStanislav Sedov 
89*ae771770SStanislav Sedov 	EVP_DigestFinal_ex(m, shaoutput, NULL);
90*ae771770SStanislav Sedov 
91*ae771770SStanislav Sedov 	memcpy((unsigned char *)keydata + offset,
92*ae771770SStanislav Sedov 	       shaoutput,
93*ae771770SStanislav Sedov 	       min(keylen - offset, sizeof(shaoutput)));
94*ae771770SStanislav Sedov 
95*ae771770SStanislav Sedov 	offset += sizeof(shaoutput);
96*ae771770SStanislav Sedov 	counter++;
97*ae771770SStanislav Sedov     } while(offset < keylen);
98*ae771770SStanislav Sedov     memset(shaoutput, 0, sizeof(shaoutput));
99*ae771770SStanislav Sedov 
100*ae771770SStanislav Sedov     EVP_MD_CTX_destroy(m);
101*ae771770SStanislav Sedov 
102*ae771770SStanislav Sedov     ret = krb5_random_to_key(context, type, keydata, keylen, key);
103*ae771770SStanislav Sedov     memset(keydata, 0, sizeof(keylen));
104*ae771770SStanislav Sedov     free(keydata);
105*ae771770SStanislav Sedov     return ret;
106*ae771770SStanislav Sedov }
107*ae771770SStanislav Sedov 
108*ae771770SStanislav Sedov static krb5_error_code
encode_uvinfo(krb5_context context,krb5_const_principal p,krb5_data * data)109*ae771770SStanislav Sedov encode_uvinfo(krb5_context context, krb5_const_principal p, krb5_data *data)
110*ae771770SStanislav Sedov {
111*ae771770SStanislav Sedov     KRB5PrincipalName pn;
112*ae771770SStanislav Sedov     krb5_error_code ret;
113*ae771770SStanislav Sedov     size_t size = 0;
114*ae771770SStanislav Sedov 
115*ae771770SStanislav Sedov     pn.principalName = p->name;
116*ae771770SStanislav Sedov     pn.realm = p->realm;
117*ae771770SStanislav Sedov 
118*ae771770SStanislav Sedov     ASN1_MALLOC_ENCODE(KRB5PrincipalName, data->data, data->length,
119*ae771770SStanislav Sedov 		       &pn, &size, ret);
120*ae771770SStanislav Sedov     if (ret) {
121*ae771770SStanislav Sedov 	krb5_data_zero(data);
122*ae771770SStanislav Sedov 	krb5_set_error_message(context, ret,
123*ae771770SStanislav Sedov 			       N_("Failed to encode KRB5PrincipalName", ""));
124*ae771770SStanislav Sedov 	return ret;
125*ae771770SStanislav Sedov     }
126*ae771770SStanislav Sedov     if (data->length != size)
127*ae771770SStanislav Sedov 	krb5_abortx(context, "asn1 compiler internal error");
128*ae771770SStanislav Sedov     return 0;
129*ae771770SStanislav Sedov }
130*ae771770SStanislav Sedov 
131*ae771770SStanislav Sedov static krb5_error_code
encode_otherinfo(krb5_context context,const AlgorithmIdentifier * ai,krb5_const_principal client,krb5_const_principal server,krb5_enctype enctype,const krb5_data * as_req,const krb5_data * pk_as_rep,const Ticket * ticket,krb5_data * other)132*ae771770SStanislav Sedov encode_otherinfo(krb5_context context,
133*ae771770SStanislav Sedov 		 const AlgorithmIdentifier *ai,
134*ae771770SStanislav Sedov 		 krb5_const_principal client,
135*ae771770SStanislav Sedov 		 krb5_const_principal server,
136*ae771770SStanislav Sedov 		 krb5_enctype enctype,
137*ae771770SStanislav Sedov 		 const krb5_data *as_req,
138*ae771770SStanislav Sedov 		 const krb5_data *pk_as_rep,
139*ae771770SStanislav Sedov 		 const Ticket *ticket,
140*ae771770SStanislav Sedov 		 krb5_data *other)
141*ae771770SStanislav Sedov {
142*ae771770SStanislav Sedov     PkinitSP80056AOtherInfo otherinfo;
143*ae771770SStanislav Sedov     PkinitSuppPubInfo pubinfo;
144*ae771770SStanislav Sedov     krb5_error_code ret;
145*ae771770SStanislav Sedov     krb5_data pub;
146*ae771770SStanislav Sedov     size_t size = 0;
147*ae771770SStanislav Sedov 
148*ae771770SStanislav Sedov     krb5_data_zero(other);
149*ae771770SStanislav Sedov     memset(&otherinfo, 0, sizeof(otherinfo));
150*ae771770SStanislav Sedov     memset(&pubinfo, 0, sizeof(pubinfo));
151*ae771770SStanislav Sedov 
152*ae771770SStanislav Sedov     pubinfo.enctype = enctype;
153*ae771770SStanislav Sedov     pubinfo.as_REQ = *as_req;
154*ae771770SStanislav Sedov     pubinfo.pk_as_rep = *pk_as_rep;
155*ae771770SStanislav Sedov     pubinfo.ticket = *ticket;
156*ae771770SStanislav Sedov     ASN1_MALLOC_ENCODE(PkinitSuppPubInfo, pub.data, pub.length,
157*ae771770SStanislav Sedov 		       &pubinfo, &size, ret);
158*ae771770SStanislav Sedov     if (ret) {
159*ae771770SStanislav Sedov 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
160*ae771770SStanislav Sedov 	return ret;
161*ae771770SStanislav Sedov     }
162*ae771770SStanislav Sedov     if (pub.length != size)
163*ae771770SStanislav Sedov 	krb5_abortx(context, "asn1 compiler internal error");
164*ae771770SStanislav Sedov 
165*ae771770SStanislav Sedov     ret = encode_uvinfo(context, client, &otherinfo.partyUInfo);
166*ae771770SStanislav Sedov     if (ret) {
167*ae771770SStanislav Sedov 	free(pub.data);
168*ae771770SStanislav Sedov 	return ret;
169*ae771770SStanislav Sedov     }
170*ae771770SStanislav Sedov     ret = encode_uvinfo(context, server, &otherinfo.partyVInfo);
171*ae771770SStanislav Sedov     if (ret) {
172*ae771770SStanislav Sedov 	free(otherinfo.partyUInfo.data);
173*ae771770SStanislav Sedov 	free(pub.data);
174*ae771770SStanislav Sedov 	return ret;
175*ae771770SStanislav Sedov     }
176*ae771770SStanislav Sedov 
177*ae771770SStanislav Sedov     otherinfo.algorithmID = *ai;
178*ae771770SStanislav Sedov     otherinfo.suppPubInfo = &pub;
179*ae771770SStanislav Sedov 
180*ae771770SStanislav Sedov     ASN1_MALLOC_ENCODE(PkinitSP80056AOtherInfo, other->data, other->length,
181*ae771770SStanislav Sedov 		       &otherinfo, &size, ret);
182*ae771770SStanislav Sedov     free(otherinfo.partyUInfo.data);
183*ae771770SStanislav Sedov     free(otherinfo.partyVInfo.data);
184*ae771770SStanislav Sedov     free(pub.data);
185*ae771770SStanislav Sedov     if (ret) {
186*ae771770SStanislav Sedov 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
187*ae771770SStanislav Sedov 	return ret;
188*ae771770SStanislav Sedov     }
189*ae771770SStanislav Sedov     if (other->length != size)
190*ae771770SStanislav Sedov 	krb5_abortx(context, "asn1 compiler internal error");
191*ae771770SStanislav Sedov 
192*ae771770SStanislav Sedov     return 0;
193*ae771770SStanislav Sedov }
194*ae771770SStanislav Sedov 
195*ae771770SStanislav Sedov 
196*ae771770SStanislav Sedov 
197*ae771770SStanislav Sedov krb5_error_code
_krb5_pk_kdf(krb5_context context,const struct AlgorithmIdentifier * ai,const void * dhdata,size_t dhsize,krb5_const_principal client,krb5_const_principal server,krb5_enctype enctype,const krb5_data * as_req,const krb5_data * pk_as_rep,const Ticket * ticket,krb5_keyblock * key)198*ae771770SStanislav Sedov _krb5_pk_kdf(krb5_context context,
199*ae771770SStanislav Sedov 	     const struct AlgorithmIdentifier *ai,
200*ae771770SStanislav Sedov 	     const void *dhdata,
201*ae771770SStanislav Sedov 	     size_t dhsize,
202*ae771770SStanislav Sedov 	     krb5_const_principal client,
203*ae771770SStanislav Sedov 	     krb5_const_principal server,
204*ae771770SStanislav Sedov 	     krb5_enctype enctype,
205*ae771770SStanislav Sedov 	     const krb5_data *as_req,
206*ae771770SStanislav Sedov 	     const krb5_data *pk_as_rep,
207*ae771770SStanislav Sedov 	     const Ticket *ticket,
208*ae771770SStanislav Sedov 	     krb5_keyblock *key)
209*ae771770SStanislav Sedov {
210*ae771770SStanislav Sedov     struct _krb5_encryption_type *et;
211*ae771770SStanislav Sedov     krb5_error_code ret;
212*ae771770SStanislav Sedov     krb5_data other;
213*ae771770SStanislav Sedov     size_t keylen, offset;
214*ae771770SStanislav Sedov     uint32_t counter;
215*ae771770SStanislav Sedov     unsigned char *keydata;
216*ae771770SStanislav Sedov     unsigned char shaoutput[SHA512_DIGEST_LENGTH];
217*ae771770SStanislav Sedov     const EVP_MD *md;
218*ae771770SStanislav Sedov     EVP_MD_CTX *m;
219*ae771770SStanislav Sedov 
220*ae771770SStanislav Sedov     if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha1, &ai->algorithm) == 0) {
221*ae771770SStanislav Sedov         md = EVP_sha1();
222*ae771770SStanislav Sedov     } else if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha256, &ai->algorithm) == 0) {
223*ae771770SStanislav Sedov         md = EVP_sha256();
224*ae771770SStanislav Sedov     } else if (der_heim_oid_cmp(&asn1_oid_id_pkinit_kdf_ah_sha512, &ai->algorithm) == 0) {
225*ae771770SStanislav Sedov         md = EVP_sha512();
226*ae771770SStanislav Sedov     } else {
227*ae771770SStanislav Sedov 	krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
228*ae771770SStanislav Sedov 			       N_("KDF not supported", ""));
229*ae771770SStanislav Sedov 	return KRB5_PROG_ETYPE_NOSUPP;
230*ae771770SStanislav Sedov     }
231*ae771770SStanislav Sedov     if (ai->parameters != NULL &&
232*ae771770SStanislav Sedov 	(ai->parameters->length != 2 ||
233*ae771770SStanislav Sedov 	 memcmp(ai->parameters->data, "\x05\x00", 2) != 0))
234*ae771770SStanislav Sedov 	{
235*ae771770SStanislav Sedov 	    krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
236*ae771770SStanislav Sedov 				   N_("kdf params not NULL or the NULL-type",
237*ae771770SStanislav Sedov 				      ""));
238*ae771770SStanislav Sedov 	    return KRB5_PROG_ETYPE_NOSUPP;
239*ae771770SStanislav Sedov 	}
240*ae771770SStanislav Sedov 
241*ae771770SStanislav Sedov     et = _krb5_find_enctype(enctype);
242*ae771770SStanislav Sedov     if(et == NULL) {
243*ae771770SStanislav Sedov 	krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
244*ae771770SStanislav Sedov 			       N_("encryption type %d not supported", ""),
245*ae771770SStanislav Sedov 			       enctype);
246*ae771770SStanislav Sedov 	return KRB5_PROG_ETYPE_NOSUPP;
247*ae771770SStanislav Sedov     }
248*ae771770SStanislav Sedov     keylen = (et->keytype->bits + 7) / 8;
249*ae771770SStanislav Sedov 
250*ae771770SStanislav Sedov     keydata = malloc(keylen);
251*ae771770SStanislav Sedov     if (keydata == NULL) {
252*ae771770SStanislav Sedov 	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
253*ae771770SStanislav Sedov 	return ENOMEM;
254*ae771770SStanislav Sedov     }
255*ae771770SStanislav Sedov 
256*ae771770SStanislav Sedov     ret = encode_otherinfo(context, ai, client, server,
257*ae771770SStanislav Sedov 			   enctype, as_req, pk_as_rep, ticket, &other);
258*ae771770SStanislav Sedov     if (ret) {
259*ae771770SStanislav Sedov 	free(keydata);
260*ae771770SStanislav Sedov 	return ret;
261*ae771770SStanislav Sedov     }
262*ae771770SStanislav Sedov 
263*ae771770SStanislav Sedov     m = EVP_MD_CTX_create();
264*ae771770SStanislav Sedov     if (m == NULL) {
265*ae771770SStanislav Sedov 	free(keydata);
266*ae771770SStanislav Sedov 	free(other.data);
267*ae771770SStanislav Sedov 	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
268*ae771770SStanislav Sedov 	return ENOMEM;
269*ae771770SStanislav Sedov     }
270*ae771770SStanislav Sedov 
271*ae771770SStanislav Sedov     offset = 0;
272*ae771770SStanislav Sedov     counter = 1;
273*ae771770SStanislav Sedov     do {
274*ae771770SStanislav Sedov 	unsigned char cdata[4];
275*ae771770SStanislav Sedov 
276*ae771770SStanislav Sedov 	EVP_DigestInit_ex(m, md, NULL);
277*ae771770SStanislav Sedov 	_krb5_put_int(cdata, counter, 4);
278*ae771770SStanislav Sedov 	EVP_DigestUpdate(m, cdata, 4);
279*ae771770SStanislav Sedov 	EVP_DigestUpdate(m, dhdata, dhsize);
280*ae771770SStanislav Sedov 	EVP_DigestUpdate(m, other.data, other.length);
281*ae771770SStanislav Sedov 
282*ae771770SStanislav Sedov 	EVP_DigestFinal_ex(m, shaoutput, NULL);
283*ae771770SStanislav Sedov 
284*ae771770SStanislav Sedov 	memcpy((unsigned char *)keydata + offset,
285*ae771770SStanislav Sedov 	       shaoutput,
286*ae771770SStanislav Sedov 	       min(keylen - offset, EVP_MD_CTX_size(m)));
287*ae771770SStanislav Sedov 
288*ae771770SStanislav Sedov 	offset += EVP_MD_CTX_size(m);
289*ae771770SStanislav Sedov 	counter++;
290*ae771770SStanislav Sedov     } while(offset < keylen);
291*ae771770SStanislav Sedov     memset(shaoutput, 0, sizeof(shaoutput));
292*ae771770SStanislav Sedov 
293*ae771770SStanislav Sedov     EVP_MD_CTX_destroy(m);
294*ae771770SStanislav Sedov     free(other.data);
295*ae771770SStanislav Sedov 
296*ae771770SStanislav Sedov     ret = krb5_random_to_key(context, enctype, keydata, keylen, key);
297*ae771770SStanislav Sedov     memset(keydata, 0, sizeof(keylen));
298*ae771770SStanislav Sedov     free(keydata);
299*ae771770SStanislav Sedov 
300*ae771770SStanislav Sedov     return ret;
301*ae771770SStanislav Sedov }
302