| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | /*-
 | 
					
						
							| 
									
										
										
										
											2021-03-11 21:27:36 +08:00
										 |  |  |  * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-04-02 17:16:53 +08:00
										 |  |  |  * Copyright Nokia 2007-2019 | 
					
						
							|  |  |  |  * Copyright Siemens AG 2015-2019 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-23 16:58:36 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:00:04 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | #include <openssl/rand.h>
 | 
					
						
							|  |  |  | #include <openssl/evp.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-30 01:42:33 +08:00
										 |  |  | #include <openssl/hmac.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* explicit #includes not strictly needed since implied by the above: */ | 
					
						
							|  |  |  | #include <openssl/asn1t.h>
 | 
					
						
							|  |  |  | #include <openssl/crmf.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:00:04 +08:00
										 |  |  | #include <openssl/params.h>
 | 
					
						
							|  |  |  | #include <openssl/core_names.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:58:53 +08:00
										 |  |  | #include "internal/sizes.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "crmf_local.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | /*-
 | 
					
						
							|  |  |  |  * creates and initializes OSSL_CRMF_PBMPARAMETER (section 4.4) | 
					
						
							| 
									
										
										
										
											2020-05-13 17:58:52 +08:00
										 |  |  |  * |slen| SHOULD be at least 8 (16 is common) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |  * |owfnid| e.g., NID_sha256 | 
					
						
							| 
									
										
										
										
											2020-05-13 17:58:52 +08:00
										 |  |  |  * |itercnt| MUST be >= 100 (e.g., 500) and <= OSSL_CRMF_PBM_MAX_ITERATION_COUNT | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |  * |macnid| e.g., NID_hmac_sha1 | 
					
						
							|  |  |  |  * returns pointer to OSSL_CRMF_PBMPARAMETER on success, NULL on error | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-15 17:55:50 +08:00
										 |  |  | OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(OSSL_LIB_CTX *libctx, size_t slen, | 
					
						
							| 
									
										
										
										
											2020-05-13 17:58:52 +08:00
										 |  |  |                                            int owfnid, size_t itercnt, | 
					
						
							|  |  |  |                                            int macnid) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     OSSL_CRMF_PBMPARAMETER *pbm = NULL; | 
					
						
							|  |  |  |     unsigned char *salt = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 20:07:22 +08:00
										 |  |  |     if ((pbm = OSSL_CRMF_PBMPARAMETER_new()) == NULL) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * salt contains a randomly generated value used in computing the key | 
					
						
							|  |  |  |      * of the MAC process.  The salt SHOULD be at least 8 octets (64 | 
					
						
							|  |  |  |      * bits) long. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-10 20:07:22 +08:00
										 |  |  |     if ((salt = OPENSSL_malloc(slen)) == NULL) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2021-05-31 14:31:18 +08:00
										 |  |  |     if (RAND_bytes_ex(libctx, salt, slen, 0) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_FAILURE_OBTAINING_RANDOM); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!ASN1_OCTET_STRING_set(pbm->salt, salt, (int)slen)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * owf identifies the hash algorithm and associated parameters used to | 
					
						
							|  |  |  |      * compute the key used in the MAC process.  All implementations MUST | 
					
						
							|  |  |  |      * support SHA-1. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (!X509_ALGOR_set0(pbm->owf, OBJ_nid2obj(owfnid), V_ASN1_UNDEF, NULL)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_SETTING_OWF_ALGOR_FAILURE); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * iterationCount identifies the number of times the hash is applied | 
					
						
							|  |  |  |      * during the key computation process.  The iterationCount MUST be a | 
					
						
							| 
									
										
										
										
											2019-12-14 03:07:08 +08:00
										 |  |  |      * minimum of 100. Many people suggest using values as high as 1000 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |      * iterations as the minimum value.  The trade off here is between | 
					
						
							|  |  |  |      * protection of the password from attacks and the time spent by the | 
					
						
							|  |  |  |      * server processing all of the different iterations in deriving | 
					
						
							|  |  |  |      * passwords.  Hashing is generally considered a cheap operation but | 
					
						
							|  |  |  |      * this may not be true with all hash functions in the future. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (itercnt < 100) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_ITERATIONCOUNT_BELOW_100); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2020-05-13 17:58:52 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (itercnt > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_BAD_PBM_ITERATIONCOUNT); | 
					
						
							| 
									
										
										
										
											2020-05-13 17:58:52 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 16:48:01 +08:00
										 |  |  |     if (!ASN1_INTEGER_set(pbm->iterationCount, (long)itercnt)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_CRMFERROR); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * mac identifies the algorithm and associated parameters of the MAC | 
					
						
							|  |  |  |      * function to be used.  All implementations MUST support HMAC-SHA1 [HMAC]. | 
					
						
							|  |  |  |      * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11]. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (!X509_ALGOR_set0(pbm->mac, OBJ_nid2obj(macnid), V_ASN1_UNDEF, NULL)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_SETTING_MAC_ALGOR_FAILURE); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OPENSSL_free(salt); | 
					
						
							|  |  |  |     return pbm; | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     OPENSSL_free(salt); | 
					
						
							|  |  |  |     OSSL_CRMF_PBMPARAMETER_free(pbm); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*-
 | 
					
						
							|  |  |  |  * calculates the PBM based on the settings of the given OSSL_CRMF_PBMPARAMETER | 
					
						
							|  |  |  |  * |pbmp| identifies the algorithms, salt to use | 
					
						
							|  |  |  |  * |msg| message to apply the PBM for | 
					
						
							|  |  |  |  * |msglen| length of the message | 
					
						
							|  |  |  |  * |sec| key to use | 
					
						
							|  |  |  |  * |seclen| length of the key | 
					
						
							| 
									
										
										
										
											2021-03-30 01:42:33 +08:00
										 |  |  |  * |out| pointer to the computed mac, will be set on success | 
					
						
							|  |  |  |  * |outlen| if not NULL, will set variable to the length of the mac on success | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |  * returns 1 on success, 0 on error | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-07-06 18:23:51 +08:00
										 |  |  | /* could be combined with other MAC calculations in the library */ | 
					
						
							| 
									
										
										
										
											2020-10-15 17:55:50 +08:00
										 |  |  | int OSSL_CRMF_pbm_new(OSSL_LIB_CTX *libctx, const char *propq, | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |                       const OSSL_CRMF_PBMPARAMETER *pbmp, | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |                       const unsigned char *msg, size_t msglen, | 
					
						
							|  |  |  |                       const unsigned char *sec, size_t seclen, | 
					
						
							| 
									
										
										
										
											2019-06-05 00:00:04 +08:00
										 |  |  |                       unsigned char **out, size_t *outlen) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int mac_nid, hmac_md_nid = NID_undef; | 
					
						
							| 
									
										
										
										
											2021-03-10 19:58:53 +08:00
										 |  |  |     char mdname[OSSL_MAX_NAME_SIZE]; | 
					
						
							|  |  |  |     char hmac_mdname[OSSL_MAX_NAME_SIZE]; | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |     EVP_MD *owf = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |     EVP_MD_CTX *ctx = NULL; | 
					
						
							|  |  |  |     unsigned char basekey[EVP_MAX_MD_SIZE]; | 
					
						
							|  |  |  |     unsigned int bklen = EVP_MAX_MD_SIZE; | 
					
						
							|  |  |  |     int64_t iterations; | 
					
						
							|  |  |  |     unsigned char *mac_res = 0; | 
					
						
							|  |  |  |     int ok = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 00:00:04 +08:00
										 |  |  |     if (out == NULL || pbmp == NULL || pbmp->mac == NULL | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |             || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-10 20:07:22 +08:00
										 |  |  |     if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * owf identifies the hash algorithm and associated parameters used to | 
					
						
							|  |  |  |      * compute the key used in the MAC process.  All implementations MUST | 
					
						
							|  |  |  |      * support SHA-1. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-10 19:58:53 +08:00
										 |  |  |     OBJ_obj2txt(mdname, sizeof(mdname), pbmp->owf->algorithm, 0); | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |     if ((owf = EVP_MD_fetch(libctx, mdname, propq)) == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 20:07:22 +08:00
										 |  |  |     if ((ctx = EVP_MD_CTX_new()) == NULL) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* compute the basekey of the salted secret */ | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |     if (!EVP_DigestInit_ex(ctx, owf, NULL)) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     /* first the secret */ | 
					
						
							|  |  |  |     if (!EVP_DigestUpdate(ctx, sec, seclen)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     /* then the salt */ | 
					
						
							|  |  |  |     if (!EVP_DigestUpdate(ctx, pbmp->salt->data, pbmp->salt->length)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     if (!EVP_DigestFinal_ex(ctx, basekey, &bklen)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     if (!ASN1_INTEGER_get_int64(&iterations, pbmp->iterationCount) | 
					
						
							|  |  |  |             || iterations < 100 /* min from RFC */ | 
					
						
							|  |  |  |             || iterations > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_BAD_PBM_ITERATIONCOUNT); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* the first iteration was already done above */ | 
					
						
							|  |  |  |     while (--iterations > 0) { | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |         if (!EVP_DigestInit_ex(ctx, owf, NULL)) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         if (!EVP_DigestUpdate(ctx, basekey, bklen)) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         if (!EVP_DigestFinal_ex(ctx, basekey, &bklen)) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * mac identifies the algorithm and associated parameters of the MAC | 
					
						
							|  |  |  |      * function to be used.  All implementations MUST support HMAC-SHA1 [HMAC]. | 
					
						
							|  |  |  |      * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11]. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     mac_nid = OBJ_obj2nid(pbmp->mac->algorithm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, mac_nid, NULL, &hmac_md_nid, NULL) | 
					
						
							| 
									
										
										
										
											2021-11-10 12:39:54 +08:00
										 |  |  |         || OBJ_obj2txt(hmac_mdname, sizeof(hmac_mdname), | 
					
						
							| 
									
										
										
										
											2022-09-18 03:51:48 +08:00
										 |  |  |                        OBJ_nid2obj(hmac_md_nid), 0) <= 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-06 18:23:51 +08:00
										 |  |  |     /* could be generalized to allow non-HMAC: */ | 
					
						
							| 
									
										
										
										
											2021-03-30 01:42:33 +08:00
										 |  |  |     if (EVP_Q_mac(libctx, "HMAC", propq, hmac_mdname, NULL, basekey, bklen, | 
					
						
							| 
									
										
										
										
											2021-06-23 00:11:03 +08:00
										 |  |  |                   msg, msglen, mac_res, EVP_MAX_MD_SIZE, outlen) == NULL) | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ok = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     OPENSSL_cleanse(basekey, bklen); | 
					
						
							| 
									
										
										
										
											2020-08-13 23:44:54 +08:00
										 |  |  |     EVP_MD_free(owf); | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |     EVP_MD_CTX_free(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ok == 1) { | 
					
						
							| 
									
										
										
										
											2019-06-05 00:00:04 +08:00
										 |  |  |         *out = mac_res; | 
					
						
							| 
									
										
										
										
											2018-11-16 21:05:31 +08:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OPENSSL_free(mac_res); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pbmp != NULL && pbmp->mac != NULL) { | 
					
						
							|  |  |  |         char buf[128]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (OBJ_obj2txt(buf, sizeof(buf), pbmp->mac->algorithm, 0)) | 
					
						
							|  |  |  |             ERR_add_error_data(1, buf); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |