diff --git a/Configure b/Configure index b2410a2bde..8818d589e7 100755 --- a/Configure +++ b/Configure @@ -349,6 +349,7 @@ my @disablables = ( "cmac", "cms", "comp", + "crmf", "crypto-mdebug", "crypto-mdebug-backtrace", "ct", diff --git a/crypto/build.info b/crypto/build.info index 0cca6ab7a3..a1ccad44ea 100644 --- a/crypto/build.info +++ b/crypto/build.info @@ -5,7 +5,7 @@ SUBDIRS=objects buffer bio stack lhash rand evp asn1 pem x509 x509v3 conf \ md2 md4 md5 sha mdc2 gmac hmac ripemd whrlpool poly1305 blake2 \ siphash sm3 des aes rc2 rc4 rc5 idea aria bf cast camellia \ seed sm4 chacha modes bn ec rsa dsa dh sm2 dso engine \ - err comp ocsp cms ts srp cmac ct async kmac ess + err comp ocsp cms ts srp cmac ct async kmac ess crmf LIBS=../libcrypto # The Core diff --git a/crypto/crmf/build.info b/crypto/crmf/build.info new file mode 100644 index 0000000000..7cfa8ec2b0 --- /dev/null +++ b/crypto/crmf/build.info @@ -0,0 +1,2 @@ +LIBS=../../libcrypto +SOURCE[../../libcrypto]=crmf_asn.c crmf_err.c crmf_lib.c crmf_pbm.c diff --git a/crypto/crmf/crmf_asn.c b/crypto/crmf/crmf_asn.c new file mode 100644 index 0000000000..d0ab6e95bc --- /dev/null +++ b/crypto/crmf/crmf_asn.c @@ -0,0 +1,240 @@ +/*- + * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright Nokia 2007-2018 + * Copyright Siemens AG 2015-2018 + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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. + */ + +#include + +#include "crmf_int.h" + +/* explicit #includes not strictly needed since implied by the above: */ +#include + +ASN1_SEQUENCE(OSSL_CRMF_PRIVATEKEYINFO) = { + ASN1_SIMPLE(OSSL_CRMF_PRIVATEKEYINFO, version, ASN1_INTEGER), + ASN1_SIMPLE(OSSL_CRMF_PRIVATEKEYINFO, privateKeyAlgorithm, X509_ALGOR), + ASN1_SIMPLE(OSSL_CRMF_PRIVATEKEYINFO, privateKey, ASN1_OCTET_STRING), + ASN1_IMP_SET_OF_OPT(OSSL_CRMF_PRIVATEKEYINFO, attributes, X509_ATTRIBUTE, 0) +} ASN1_SEQUENCE_END(OSSL_CRMF_PRIVATEKEYINFO) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_PRIVATEKEYINFO) + + +ASN1_CHOICE(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER) = { + ASN1_SIMPLE(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER, value.string, ASN1_UTF8STRING), + ASN1_SIMPLE(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER, value.generalName, GENERAL_NAME) +} ASN1_CHOICE_END(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER) + + +ASN1_SEQUENCE(OSSL_CRMF_ENCKEYWITHID) = { + ASN1_SIMPLE(OSSL_CRMF_ENCKEYWITHID, privateKey, OSSL_CRMF_PRIVATEKEYINFO), + ASN1_OPT(OSSL_CRMF_ENCKEYWITHID, identifier, + OSSL_CRMF_ENCKEYWITHID_IDENTIFIER) +} ASN1_SEQUENCE_END(OSSL_CRMF_ENCKEYWITHID) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_ENCKEYWITHID) + + +ASN1_SEQUENCE(OSSL_CRMF_CERTID) = { + ASN1_SIMPLE(OSSL_CRMF_CERTID, issuer, GENERAL_NAME), + ASN1_SIMPLE(OSSL_CRMF_CERTID, serialNumber, ASN1_INTEGER) +} ASN1_SEQUENCE_END(OSSL_CRMF_CERTID) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_CERTID) +IMPLEMENT_ASN1_DUP_FUNCTION(OSSL_CRMF_CERTID) + + +ASN1_SEQUENCE(OSSL_CRMF_ENCRYPTEDVALUE) = { + ASN1_IMP_OPT(OSSL_CRMF_ENCRYPTEDVALUE, intendedAlg, X509_ALGOR, 0), + ASN1_IMP_OPT(OSSL_CRMF_ENCRYPTEDVALUE, symmAlg, X509_ALGOR, 1), + ASN1_IMP_OPT(OSSL_CRMF_ENCRYPTEDVALUE, encSymmKey, ASN1_BIT_STRING, 2), + ASN1_IMP_OPT(OSSL_CRMF_ENCRYPTEDVALUE, keyAlg, X509_ALGOR, 3), + ASN1_IMP_OPT(OSSL_CRMF_ENCRYPTEDVALUE, valueHint, ASN1_OCTET_STRING, 4), + ASN1_SIMPLE(OSSL_CRMF_ENCRYPTEDVALUE, encValue, ASN1_BIT_STRING) +} ASN1_SEQUENCE_END(OSSL_CRMF_ENCRYPTEDVALUE) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_ENCRYPTEDVALUE) + +ASN1_SEQUENCE(OSSL_CRMF_SINGLEPUBINFO) = { + ASN1_SIMPLE(OSSL_CRMF_SINGLEPUBINFO, pubMethod, ASN1_INTEGER), + ASN1_SIMPLE(OSSL_CRMF_SINGLEPUBINFO, pubLocation, GENERAL_NAME) +} ASN1_SEQUENCE_END(OSSL_CRMF_SINGLEPUBINFO) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_SINGLEPUBINFO) + + +ASN1_SEQUENCE(OSSL_CRMF_PKIPUBLICATIONINFO) = { + ASN1_SIMPLE(OSSL_CRMF_PKIPUBLICATIONINFO, action, ASN1_INTEGER), + ASN1_SEQUENCE_OF_OPT(OSSL_CRMF_PKIPUBLICATIONINFO, pubInfos, + OSSL_CRMF_SINGLEPUBINFO) +} ASN1_SEQUENCE_END(OSSL_CRMF_PKIPUBLICATIONINFO) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_PKIPUBLICATIONINFO) +IMPLEMENT_ASN1_DUP_FUNCTION(OSSL_CRMF_PKIPUBLICATIONINFO) + + +ASN1_SEQUENCE(OSSL_CRMF_PKMACVALUE) = { + ASN1_SIMPLE(OSSL_CRMF_PKMACVALUE, algId, X509_ALGOR), + ASN1_SIMPLE(OSSL_CRMF_PKMACVALUE, value, ASN1_BIT_STRING) +} ASN1_SEQUENCE_END(OSSL_CRMF_PKMACVALUE) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_PKMACVALUE) + + +ASN1_CHOICE(OSSL_CRMF_POPOPRIVKEY) = { + ASN1_IMP(OSSL_CRMF_POPOPRIVKEY, value.thisMessage, ASN1_BIT_STRING, 0), + ASN1_IMP(OSSL_CRMF_POPOPRIVKEY, value.subsequentMessage, ASN1_INTEGER, 1), + ASN1_IMP(OSSL_CRMF_POPOPRIVKEY, value.dhMAC, ASN1_BIT_STRING, 2), + ASN1_IMP(OSSL_CRMF_POPOPRIVKEY, value.agreeMAC, OSSL_CRMF_PKMACVALUE, 3), + /* + * TODO: This is not ASN1_NULL but CMS_ENVELOPEDDATA which should be somehow + * taken from crypto/cms which exists now - this is not used anywhere so far + */ + ASN1_IMP(OSSL_CRMF_POPOPRIVKEY, value.encryptedKey, ASN1_NULL, 4), +} ASN1_CHOICE_END(OSSL_CRMF_POPOPRIVKEY) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPOPRIVKEY) + + +ASN1_SEQUENCE(OSSL_CRMF_PBMPARAMETER) = { + ASN1_SIMPLE(OSSL_CRMF_PBMPARAMETER, salt, ASN1_OCTET_STRING), + ASN1_SIMPLE(OSSL_CRMF_PBMPARAMETER, owf, X509_ALGOR), + ASN1_SIMPLE(OSSL_CRMF_PBMPARAMETER, iterationCount, ASN1_INTEGER), + ASN1_SIMPLE(OSSL_CRMF_PBMPARAMETER, mac, X509_ALGOR) +} ASN1_SEQUENCE_END(OSSL_CRMF_PBMPARAMETER) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_PBMPARAMETER) + + +ASN1_CHOICE(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO) = { + ASN1_EXP(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO, value.sender, + GENERAL_NAME, 0), + ASN1_SIMPLE(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO, value.publicKeyMAC, + OSSL_CRMF_PKMACVALUE) +} ASN1_CHOICE_END(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO) + + +ASN1_SEQUENCE(OSSL_CRMF_POPOSIGNINGKEYINPUT) = { + ASN1_SIMPLE(OSSL_CRMF_POPOSIGNINGKEYINPUT, authInfo, + OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO), + ASN1_SIMPLE(OSSL_CRMF_POPOSIGNINGKEYINPUT, publicKey, X509_PUBKEY) +} ASN1_SEQUENCE_END(OSSL_CRMF_POPOSIGNINGKEYINPUT) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEYINPUT) + + +ASN1_SEQUENCE(OSSL_CRMF_POPOSIGNINGKEY) = { + ASN1_IMP_OPT(OSSL_CRMF_POPOSIGNINGKEY, poposkInput, + OSSL_CRMF_POPOSIGNINGKEYINPUT, 0), + ASN1_SIMPLE(OSSL_CRMF_POPOSIGNINGKEY, algorithmIdentifier, X509_ALGOR), + ASN1_SIMPLE(OSSL_CRMF_POPOSIGNINGKEY, signature, ASN1_BIT_STRING) +} ASN1_SEQUENCE_END(OSSL_CRMF_POPOSIGNINGKEY) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEY) + + +ASN1_CHOICE(OSSL_CRMF_POPO) = { + ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0), + ASN1_IMP(OSSL_CRMF_POPO, value.signature, OSSL_CRMF_POPOSIGNINGKEY, 1), + ASN1_EXP(OSSL_CRMF_POPO, value.keyEncipherment, OSSL_CRMF_POPOPRIVKEY, 2), + ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3) +} ASN1_CHOICE_END(OSSL_CRMF_POPO) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO) + + +ASN1_ADB_TEMPLATE(attributetypeandvalue_default) = ASN1_OPT( + OSSL_CRMF_ATTRIBUTETYPEANDVALUE, value.other, ASN1_ANY); +ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = { + ADB_ENTRY(NID_id_regCtrl_regToken, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.regToken, ASN1_UTF8STRING)), + ADB_ENTRY(NID_id_regCtrl_authenticator, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.authenticator, ASN1_UTF8STRING)), + ADB_ENTRY(NID_id_regCtrl_pkiPublicationInfo, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.pkiPublicationInfo, + OSSL_CRMF_PKIPUBLICATIONINFO)), + ADB_ENTRY(NID_id_regCtrl_oldCertID, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.oldCertID, OSSL_CRMF_CERTID)), + ADB_ENTRY(NID_id_regCtrl_protocolEncrKey, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.protocolEncrKey, X509_PUBKEY)), + ADB_ENTRY(NID_id_regInfo_utf8Pairs, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.utf8Pairs, ASN1_UTF8STRING)), + ADB_ENTRY(NID_id_regInfo_certReq, + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, + value.certReq, OSSL_CRMF_CERTREQUEST)), +} ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0, + &attributetypeandvalue_default_tt, NULL); + + +ASN1_SEQUENCE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = { + ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, type, ASN1_OBJECT), + ASN1_ADB_OBJECT(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +} ASN1_SEQUENCE_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) + +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +IMPLEMENT_ASN1_DUP_FUNCTION(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) + + +ASN1_SEQUENCE(OSSL_CRMF_OPTIONALVALIDITY) = { + ASN1_EXP_OPT(OSSL_CRMF_OPTIONALVALIDITY, notBefore, ASN1_TIME, 0), + ASN1_EXP_OPT(OSSL_CRMF_OPTIONALVALIDITY, notAfter, ASN1_TIME, 1) +} ASN1_SEQUENCE_END(OSSL_CRMF_OPTIONALVALIDITY) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_OPTIONALVALIDITY) + + +ASN1_SEQUENCE(OSSL_CRMF_CERTTEMPLATE) = { + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, version, ASN1_INTEGER, 0), + /* + * serialNumber MUST be omitted. This field is assigned by the CA + * during certificate creation. + */ + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, serialNumber, ASN1_INTEGER, 1), + /* + * signingAlg MUST be omitted. This field is assigned by the CA + * during certificate creation. + */ + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, signingAlg, X509_ALGOR, 2), + ASN1_EXP_OPT(OSSL_CRMF_CERTTEMPLATE, issuer, X509_NAME, 3), + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, validity, + OSSL_CRMF_OPTIONALVALIDITY, 4), + ASN1_EXP_OPT(OSSL_CRMF_CERTTEMPLATE, subject, X509_NAME, 5), + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, publicKey, X509_PUBKEY, 6), + /* issuerUID is deprecated in version 2 */ + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, issuerUID, ASN1_BIT_STRING, 7), + /* subjectUID is deprecated in version 2 */ + ASN1_IMP_OPT(OSSL_CRMF_CERTTEMPLATE, subjectUID, ASN1_BIT_STRING, 8), + ASN1_IMP_SEQUENCE_OF_OPT(OSSL_CRMF_CERTTEMPLATE, extensions, + X509_EXTENSION, 9), +} ASN1_SEQUENCE_END(OSSL_CRMF_CERTTEMPLATE) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_CERTTEMPLATE) + + +ASN1_SEQUENCE(OSSL_CRMF_CERTREQUEST) = { + ASN1_SIMPLE(OSSL_CRMF_CERTREQUEST, certReqId, ASN1_INTEGER), + ASN1_SIMPLE(OSSL_CRMF_CERTREQUEST, certTemplate, OSSL_CRMF_CERTTEMPLATE), + ASN1_SEQUENCE_OF_OPT(OSSL_CRMF_CERTREQUEST, controls, + OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +} ASN1_SEQUENCE_END(OSSL_CRMF_CERTREQUEST) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_CERTREQUEST) +IMPLEMENT_ASN1_DUP_FUNCTION(OSSL_CRMF_CERTREQUEST) + + +ASN1_SEQUENCE(OSSL_CRMF_MSG) = { + ASN1_SIMPLE(OSSL_CRMF_MSG, certReq, OSSL_CRMF_CERTREQUEST), + ASN1_OPT(OSSL_CRMF_MSG, popo, OSSL_CRMF_POPO), + ASN1_SEQUENCE_OF_OPT(OSSL_CRMF_MSG, regInfo, + OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +} ASN1_SEQUENCE_END(OSSL_CRMF_MSG) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_MSG) + + +ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) = + ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, + OSSL_CRMF_MSGS, OSSL_CRMF_MSG) + ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS) +IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_MSGS) + diff --git a/crypto/crmf/crmf_err.c b/crypto/crmf/crmf_err.c new file mode 100644 index 0000000000..f3405b112f --- /dev/null +++ b/crypto/crmf/crmf_err.c @@ -0,0 +1,104 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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 + */ + +#include +#include + +#ifndef OPENSSL_NO_ERR + +static const ERR_STRING_DATA CRMF_str_functs[] = { + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_CRMF_POPOSIGNINGKEY_INIT, 0), + "CRMF_poposigningkey_init"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_CERTID_GEN, 0), + "OSSL_CRMF_CERTID_gen"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, 0), + "OSSL_CRMF_CERTTEMPLATE_fill"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, 0), + "OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, 0), + "OSSL_CRMF_MSGS_verify_popo"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, 0), + "OSSL_CRMF_MSG_create_popo"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, 0), + "OSSL_CRMF_MSG_get0_tmpl"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, 0), + "OSSL_CRMF_MSG_get_certReqId"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO, 0), + "OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, 0), + "OSSL_CRMF_MSG_push0_extension"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, 0), + "OSSL_CRMF_MSG_push0_regCtrl"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, 0), + "OSSL_CRMF_MSG_push0_regInfo"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, 0), + "OSSL_CRMF_MSG_set0_extensions"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO, 0), + "OSSL_CRMF_MSG_set0_SinglePubInfo"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, 0), + "OSSL_CRMF_MSG_set_certReqId"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION, 0), + "OSSL_CRMF_MSG_set_PKIPublicationInfo_action"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY, 0), + "OSSL_CRMF_MSG_set_validity"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_PBMP_NEW, 0), + "OSSL_CRMF_pbmp_new"}, + {ERR_PACK(ERR_LIB_CRMF, CRMF_F_OSSL_CRMF_PBM_NEW, 0), "OSSL_CRMF_pbm_new"}, + {0, NULL} +}; + +static const ERR_STRING_DATA CRMF_str_reasons[] = { + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_BAD_PBM_ITERATIONCOUNT), + "bad pbm iterationcount"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_CRMFERROR), "crmferror"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_ERROR), "error"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_ERROR_DECODING_CERTIFICATE), + "error decoding certificate"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_ERROR_DECRYPTING_CERTIFICATE), + "error decrypting certificate"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY), + "error decrypting symmetric key"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_FAILURE_OBTAINING_RANDOM), + "failure obtaining random"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_ITERATIONCOUNT_BELOW_100), + "iterationcount below 100"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_MALFORMED_IV), "malformed iv"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_NULL_ARGUMENT), "null argument"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_SETTING_MAC_ALGOR_FAILURE), + "setting mac algor failure"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_SETTING_OWF_ALGOR_FAILURE), + "setting owf algor failure"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_ALGORITHM), + "unsupported algorithm"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY), + "unsupported alg for popsigningkey"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_CIPHER), + "unsupported cipher"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO), + "unsupported method for creating popo"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_POPO_METHOD), + "unsupported popo method"}, + {ERR_PACK(ERR_LIB_CRMF, 0, CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED), + "unsupported popo not accepted"}, + {0, NULL} +}; + +#endif + +int ERR_load_CRMF_strings(void) +{ +#ifndef OPENSSL_NO_ERR + if (ERR_func_error_string(CRMF_str_functs[0].error) == NULL) { + ERR_load_strings_const(CRMF_str_functs); + ERR_load_strings_const(CRMF_str_reasons); + } +#endif + return 1; +} diff --git a/crypto/crmf/crmf_int.h b/crypto/crmf/crmf_int.h new file mode 100644 index 0000000000..db1547b0ef --- /dev/null +++ b/crypto/crmf/crmf_int.h @@ -0,0 +1,394 @@ +/*- + * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright Nokia 2007-2018 + * Copyright Siemens AG 2015-2018 + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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. + */ + +#ifndef OSSL_HEADER_CRMF_INT_H +# define OSSL_HEADER_CRMF_INT_H + +# include +# include + +/* explicit #includes not strictly needed since implied by the above: */ +# include +# include +# include +# include + +/*- + * EncryptedValue ::= SEQUENCE { + * intendedAlg [0] AlgorithmIdentifier OPTIONAL, + * -- the intended algorithm for which the value will be used + * symmAlg [1] AlgorithmIdentifier OPTIONAL, + * -- the symmetric algorithm used to encrypt the value + * encSymmKey [2] BIT STRING OPTIONAL, + * -- the (encrypted) symmetric key used to encrypt the value + * keyAlg [3] AlgorithmIdentifier OPTIONAL, + * -- algorithm used to encrypt the symmetric key + * valueHint [4] OCTET STRING OPTIONAL, + * -- a brief description or identifier of the encValue content + * -- (may be meaningful only to the sending entity, and + * -- used only if EncryptedValue might be re-examined + * -- by the sending entity in the future) + * encValue BIT STRING + * -- the encrypted value itself + * } + */ +struct OSSL_crmf_encryptedvalue_st { + X509_ALGOR *intendedAlg; /* 0 */ + X509_ALGOR *symmAlg; /* 1 */ + ASN1_BIT_STRING *encSymmKey; /* 2 */ + X509_ALGOR *keyAlg; /* 3 */ + ASN1_OCTET_STRING *valueHint; /* 4 */ + ASN1_BIT_STRING *encValue; +} /* OSSL_CRMF_ENCRYPTEDVALUE */; + +/*- + * Attributes ::= SET OF Attribute + * => X509_ATTRIBUTE + * + * PrivateKeyInfo ::= SEQUENCE { + * version INTEGER, + * privateKeyAlgorithm AlgorithmIdentifier, + * privateKey OCTET STRING, + * attributes [0] IMPLICIT Attributes OPTIONAL + * } + */ +typedef struct OSSL_crmf_privatekeyinfo_st { + ASN1_INTEGER *version; + X509_ALGOR *privateKeyAlgorithm; + ASN1_OCTET_STRING *privateKey; + STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ +} OSSL_CRMF_PRIVATEKEYINFO; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_PRIVATEKEYINFO) + +/*- + * section 4.2.1 Private Key Info Content Type + * id-ct-encKeyWithID OBJECT IDENTIFIER ::= {id-ct 21} + * + * EncKeyWithID ::= SEQUENCE { + * privateKey PrivateKeyInfo, + * identifier CHOICE { + * string UTF8String, + * generalName GeneralName + * } OPTIONAL + * } + */ +typedef struct OSSL_crmf_enckeywithid_identifier_st { + int type; + union { + ASN1_UTF8STRING *string; + GENERAL_NAME *generalName; + } value; +} OSSL_CRMF_ENCKEYWITHID_IDENTIFIER; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_ENCKEYWITHID_IDENTIFIER) + +typedef struct OSSL_crmf_enckeywithid_st { + OSSL_CRMF_PRIVATEKEYINFO *privateKey; + /* [0] */ + OSSL_CRMF_ENCKEYWITHID_IDENTIFIER *identifier; +} OSSL_CRMF_ENCKEYWITHID; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_ENCKEYWITHID) + +/*- + * CertId ::= SEQUENCE { + * issuer GeneralName, + * serialNumber INTEGER + * } + */ +struct OSSL_crmf_certid_st { + GENERAL_NAME *issuer; + ASN1_INTEGER *serialNumber; +} /* OSSL_CRMF_CERTID */; +DECLARE_ASN1_DUP_FUNCTION(OSSL_CRMF_CERTID) + +/*- + * SinglePubInfo ::= SEQUENCE { + * pubMethod INTEGER { + * dontCare (0), + * x500 (1), + * web (2), + * ldap (3) }, + * pubLocation GeneralName OPTIONAL + * } + */ +struct OSSL_crmf_singlepubinfo_st { + ASN1_INTEGER *pubMethod; + GENERAL_NAME *pubLocation; +} /* OSSL_CRMF_SINGLEPUBINFO */; +DEFINE_STACK_OF(OSSL_CRMF_SINGLEPUBINFO) +typedef STACK_OF(OSSL_CRMF_SINGLEPUBINFO) OSSL_CRMF_PUBINFOS; + + +/*- + * PKIPublicationInfo ::= SEQUENCE { + * action INTEGER { + * dontPublish (0), + * pleasePublish (1) }, + * pubInfos SEQUENCE SIZE (1..MAX) OF SinglePubInfo OPTIONAL + * -- pubInfos MUST NOT be present if action is "dontPublish" + * -- (if action is "pleasePublish" and pubInfos is omitted, + * -- "dontCare" is assumed) + * } + */ +struct OSSL_crmf_pkipublicationinfo_st { + ASN1_INTEGER *action; + OSSL_CRMF_PUBINFOS *pubInfos; +} /* OSSL_CRMF_PKIPUBLICATIONINFO */; +DECLARE_ASN1_DUP_FUNCTION(OSSL_CRMF_PKIPUBLICATIONINFO) + +/*- + * PKMACValue ::= SEQUENCE { + * algId AlgorithmIdentifier, + * -- algorithm value shall be PasswordBasedMac {1 2 840 113533 7 66 13} + * -- parameter value is PBMParameter + * value BIT STRING + * } + */ +typedef struct OSSL_crmf_pkmacvalue_st { + X509_ALGOR *algId; + ASN1_BIT_STRING *value; +} OSSL_CRMF_PKMACVALUE; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_PKMACVALUE) + +/*- + * SubsequentMessage ::= INTEGER { + * encrCert (0), + * -- requests that resulting certificate be encrypted for the + * -- end entity (following which, POP will be proven in a + * -- confirmation message) + * challengeResp (1) + * -- requests that CA engage in challenge-response exchange with + * -- end entity in order to prove private key possession + * } + * + * POPOPrivKey ::= CHOICE { + * thisMessage [0] BIT STRING, -- Deprecated + * -- possession is proven in this message (which contains the private + * -- key itself (encrypted for the CA)) + * subsequentMessage [1] SubsequentMessage, + * -- possession will be proven in a subsequent message + * dhMAC [2] BIT STRING, -- Deprecated + * agreeMAC [3] PKMACValue, + * encryptedKey [4] EnvelopedData + * } + */ + +typedef struct OSSL_crmf_popoprivkey_st { + int type; + union { + ASN1_BIT_STRING *thisMessage; /* 0 */ /* Deprecated */ + ASN1_INTEGER *subsequentMessage; /* 1 */ + ASN1_BIT_STRING *dhMAC; /* 2 */ /* Deprecated */ + OSSL_CRMF_PKMACVALUE *agreeMAC; /* 3 */ + /* + * TODO: This is not ASN1_NULL but CMS_ENVELOPEDDATA which should be + * somehow taken from crypto/cms which exists now + * - this is not used anywhere so far + */ + ASN1_NULL *encryptedKey; /* 4 */ + } value; +} OSSL_CRMF_POPOPRIVKEY; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_POPOPRIVKEY) + +/*- + * PBMParameter ::= SEQUENCE { + * salt OCTET STRING, + * owf AlgorithmIdentifier, + * -- AlgId for a One-Way Function (SHA-1 recommended) + * iterationCount INTEGER, + * -- number of times the OWF is applied + * mac AlgorithmIdentifier + * -- the MAC AlgId (e.g., DES-MAC, Triple-DES-MAC [PKCS11], + * -- or HMAC [HMAC, RFC2202]) + * } + */ +struct OSSL_crmf_pbmparameter_st { + ASN1_OCTET_STRING *salt; + X509_ALGOR *owf; + ASN1_INTEGER *iterationCount; + X509_ALGOR *mac; +} /* OSSL_CRMF_PBMPARAMETER */; +#define OSSL_CRMF_PBM_MAX_ITERATION_COUNT 100000 /* if too large allows DoS */ + +/*- + * POPOSigningKeyInput ::= SEQUENCE { + * authInfo CHOICE { + * sender [0] GeneralName, + * -- used only if an authenticated identity has been + * -- established for the sender (e.g., a DN from a + * -- previously-issued and currently-valid certificate) + * publicKeyMAC PKMACValue }, + * -- used if no authenticated GeneralName currently exists for + * -- the sender; publicKeyMAC contains a password-based MAC + * -- on the DER-encoded value of publicKey + * publicKey SubjectPublicKeyInfo -- from CertTemplate + * } +*/ +typedef struct OSSL_crmf_poposigningkeyinput_authinfo_st { + int type; + union { + /* 0 */ GENERAL_NAME *sender; + /* 1 */ OSSL_CRMF_PKMACVALUE *publicKeyMAC; + } value; +} OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO) + +typedef struct OSSL_crmf_poposigningkeyinput_st { + OSSL_CRMF_POPOSIGNINGKEYINPUT_AUTHINFO *authInfo; + X509_PUBKEY *publicKey; +} OSSL_CRMF_POPOSIGNINGKEYINPUT; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEYINPUT) + +/*- + * POPOSigningKey ::= SEQUENCE { + * poposkInput [0] POPOSigningKeyInput OPTIONAL, + * algorithmIdentifier AlgorithmIdentifier, + * signature BIT STRING + * } + */ +struct OSSL_crmf_poposigningkey_st { + OSSL_CRMF_POPOSIGNINGKEYINPUT *poposkInput; + X509_ALGOR *algorithmIdentifier; + ASN1_BIT_STRING *signature; +} /* OSSL_CRMF_POPOSIGNINGKEY */; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_POPOSIGNINGKEY) + +/*- + * ProofOfPossession ::= CHOICE { + * raVerified [0] NULL, + * -- used if the RA has already verified that the requester is in + * -- possession of the private key + * signature [1] POPOSigningKey, + * keyEncipherment [2] POPOPrivKey, + * keyAgreement [3] POPOPrivKey + * } + */ +typedef struct OSSL_crmf_popo_st { + int type; + union { + ASN1_NULL *raVerified; /* 0 */ + OSSL_CRMF_POPOSIGNINGKEY *signature; /* 1 */ + OSSL_CRMF_POPOPRIVKEY *keyEncipherment; /* 2 */ + OSSL_CRMF_POPOPRIVKEY *keyAgreement; /* 3 */ + } value; +} OSSL_CRMF_POPO; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_POPO) + +/*- + * OptionalValidity ::= SEQUENCE { + * notBefore [0] Time OPTIONAL, + * notAfter [1] Time OPTIONAL -- at least one MUST be present + * } + */ +struct OSSL_crmf_optionalvalidity_st { + /* 0 */ ASN1_TIME *notBefore; + /* 1 */ ASN1_TIME *notAfter; +} /* OSSL_CRMF_OPTIONALVALIDITY */; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_OPTIONALVALIDITY) + +/*- + * CertTemplate ::= SEQUENCE { + * version [0] Version OPTIONAL, + * serialNumber [1] INTEGER OPTIONAL, + * signingAlg [2] AlgorithmIdentifier OPTIONAL, + * issuer [3] Name OPTIONAL, + * validity [4] OptionalValidity OPTIONAL, + * subject [5] Name OPTIONAL, + * publicKey [6] SubjectPublicKeyInfo OPTIONAL, + * issuerUID [7] UniqueIdentifier OPTIONAL, + * subjectUID [8] UniqueIdentifier OPTIONAL, + * extensions [9] Extensions OPTIONAL + * } + */ +struct OSSL_crmf_certtemplate_st { + ASN1_INTEGER *version; /* 0 */ + ASN1_INTEGER *serialNumber; /* 1 */ /* serialNumber MUST be omitted */ + /* This field is assigned by the CA during certificate creation */ + X509_ALGOR *signingAlg; /* 2 */ /* signingAlg MUST be omitted */ + /* This field is assigned by the CA during certificate creation */ + X509_NAME *issuer; /* 3 */ + OSSL_CRMF_OPTIONALVALIDITY *validity; /* 4 */ + X509_NAME *subject; /* 5 */ + X509_PUBKEY *publicKey; /* 6 */ + ASN1_BIT_STRING *issuerUID; /* 7 */ /* deprecated in version 2 */ + /* According to rfc 3280: UniqueIdentifier ::= BIT STRING */ + ASN1_BIT_STRING *subjectUID; /* 8 */ /* deprecated in version 2 */ + /* Could be X509_EXTENSION*S*, but that's only cosmetic */ + STACK_OF(X509_EXTENSION) *extensions; /* 9 */ +} /* OSSL_CRMF_CERTTEMPLATE */; + +/*- + * CertRequest ::= SEQUENCE { + * certReqId INTEGER, -- ID for matching request and reply + * certTemplate CertTemplate, -- Selected fields of cert to be issued + * controls Controls OPTIONAL -- Attributes affecting issuance + * } + */ +struct OSSL_crmf_certrequest_st { + ASN1_INTEGER *certReqId; + OSSL_CRMF_CERTTEMPLATE *certTemplate; + /* TODO: make OSSL_CRMF_CONTROLS out of that - but only cosmetical */ + STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls; +} /* OSSL_CRMF_CERTREQUEST */; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_CERTREQUEST) +DECLARE_ASN1_DUP_FUNCTION(OSSL_CRMF_CERTREQUEST) + +/* TODO: isn't there a better way to have this for ANY type? */ +struct OSSL_crmf_attributetypeandvalue_st { + ASN1_OBJECT *type; + union { + /* NID_id_regCtrl_regToken */ + ASN1_UTF8STRING *regToken; + + /* NID_id_regCtrl_authenticator */ + ASN1_UTF8STRING *authenticator; + + /* NID_id_regCtrl_pkiPublicationInfo */ + OSSL_CRMF_PKIPUBLICATIONINFO *pkiPublicationInfo; + + /* NID_id_regCtrl_oldCertID */ + OSSL_CRMF_CERTID *oldCertID; + + /* NID_id_regCtrl_protocolEncrKey */ + X509_PUBKEY *protocolEncrKey; + + /* NID_id_regInfo_utf8Pairs */ + ASN1_UTF8STRING *utf8Pairs; + + /* NID_id_regInfo_certReq */ + OSSL_CRMF_CERTREQUEST *certReq; + + ASN1_TYPE *other; + } value; +} /* OSSL_CRMF_ATTRIBUTETYPEANDVALUE */; +DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +DEFINE_STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) +DECLARE_ASN1_DUP_FUNCTION(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) + +/*- + * CertReqMessages ::= SEQUENCE SIZE (1..MAX) OF CertReqMsg + * CertReqMsg ::= SEQUENCE { + * certReq CertRequest, + * popo ProofOfPossession OPTIONAL, + * -- content depends upon key type + * regInfo SEQUENCE SIZE(1..MAX) OF AttributeTypeAndValue OPTIONAL + * } + */ +struct OSSL_crmf_msg_st { + OSSL_CRMF_CERTREQUEST *certReq; + /* 0 */ + OSSL_CRMF_POPO *popo; + /* 1 */ + STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *regInfo; +} /* OSSL_CRMF_MSG */; +/* DEFINE_STACK_OF(OSSL_CRMF_MSG) */ +#endif diff --git a/crypto/crmf/crmf_lib.c b/crypto/crmf/crmf_lib.c new file mode 100644 index 0000000000..49deca75f8 --- /dev/null +++ b/crypto/crmf/crmf_lib.c @@ -0,0 +1,753 @@ +/*- + * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright Nokia 2007-2018 + * Copyright Siemens AG 2015-2018 + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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. + */ + +/* + * This file contains the functions that handle the individual items inside + * the CRMF structures + */ + +/* + * NAMING + * + * The 0 functions use the supplied structure pointer directly in the parent and + * it will be freed up when the parent is freed. + * + * The 1 functions use a copy of the supplied structure pointer (or in some + * cases increases its link count) in the parent and so both should be freed up. + */ + +#include + +#include "crmf_int.h" + +/* explicit #includes not strictly needed since implied by the above: */ +#include +#include +#include + +/*- + * atyp = Attribute Type + * valt = Value Type + * ctrlinf = "regCtrl" or "regInfo" + */ +#define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \ +int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, \ + const valt *in) \ +{ \ + OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ + \ + if (msg == NULL || in == NULL) \ + goto err; \ + if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \ + goto err; \ + if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \ + goto err; \ + if ((atav->value.atyp = valt##_dup(in)) == NULL) \ + goto err; \ + if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \ + goto err; \ + return 1; \ + err: \ + OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \ + return 0; \ +} + + +/*- + * Pushes the given control attribute into the controls stack of a CertRequest + * (section 6) + * returns 1 on success, 0 on error + */ +static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm, + OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl) +{ + int new = 0; + + if (crm == NULL || crm->certReq == NULL || ctrl == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (crm->certReq->controls == NULL) { + crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); + if (crm->certReq->controls == NULL) + goto oom; + new = 1; + } + if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl)) + goto oom; + + return 1; + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, ERR_R_MALLOC_FAILURE); + + if (new != 0) { + sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls); + crm->certReq->controls = NULL; + } + return 0; +} + + /* id-regCtrl-regToken Control (section 6.1) */ +IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl) + + /* id-regCtrl-authenticator Control (section 6.2) */ +#define ASN1_UTF8STRING_dup ASN1_STRING_dup +IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl) + +int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi, + int method, GENERAL_NAME *nm) +{ + if (spi == NULL + || method < OSSL_CRMF_PUB_METHOD_DONTCARE + || method > OSSL_CRMF_PUB_METHOD_LDAP) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO, + ERR_R_PASSED_INVALID_ARGUMENT); + return 0; + } + + if (!ASN1_INTEGER_set(spi->pubMethod, method)) + return 0; + GENERAL_NAME_free(spi->pubLocation); + spi->pubLocation = nm; + return 1; +} + +int OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo( + OSSL_CRMF_PKIPUBLICATIONINFO *pi, + OSSL_CRMF_SINGLEPUBINFO *spi) +{ + if (pi == NULL || spi == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO, + CRMF_R_NULL_ARGUMENT); + return 0; + } + if (pi->pubInfos == NULL) + pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null(); + if (pi->pubInfos == NULL) + goto oom; + + if (!sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi)) + goto oom; + return 1; + + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO, + ERR_R_MALLOC_FAILURE); + return 0; +} + +int OSSL_CRMF_MSG_set_PKIPublicationInfo_action( + OSSL_CRMF_PKIPUBLICATIONINFO *pi, int action) +{ + if (pi == NULL + || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH + || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION, + ERR_R_PASSED_INVALID_ARGUMENT); + return 0; + } + + return ASN1_INTEGER_set(pi->action, action); +} + + /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */ +IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO, + regCtrl) + + /* id-regCtrl-oldCertID Control (section 6.5) from the given */ +IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl) + +OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer, + const ASN1_INTEGER *serial) +{ + OSSL_CRMF_CERTID *cid = NULL; + + if (issuer == NULL || serial == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, CRMF_R_NULL_ARGUMENT); + return NULL; + } + + if ((cid = OSSL_CRMF_CERTID_new()) == NULL) + goto oom; + + if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer)) + goto oom; + cid->issuer->type = GEN_DIRNAME; + + ASN1_INTEGER_free(cid->serialNumber); + if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) + goto oom; + + return cid; + + oom: + CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, ERR_R_MALLOC_FAILURE); + OSSL_CRMF_CERTID_free(cid); + return NULL; +} + + /* + * id-regCtrl-protocolEncrKey Control (section 6.6) + * + * For some reason X509_PUBKEY_dup() is not implemented in OpenSSL X509 + * TODO: check whether that should go elsewhere + */ +static IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY) +IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl) + +/*- + * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack. + * (section 7) + * returns 1 on success, 0 on error + */ +static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm, + OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri) +{ + STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL; + + if (crm == NULL || ri == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (crm->regInfo == NULL) + crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); + if (crm->regInfo == NULL) + goto oom; + if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri)) + goto oom; + return 1; + + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, ERR_R_MALLOC_FAILURE); + if (info != NULL) + crm->regInfo = NULL; + sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info); + return 0; +} + +/* id-regInfo-utf8Pairs to regInfo (section 7.1) */ +IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo) + +/* id-regInfo-certReq to regInfo (section 7.2) */ +IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo) + + +/* retrieves the certificate template of crm */ +OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm) +{ + if (crm == NULL || crm->certReq == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, CRMF_R_NULL_ARGUMENT); + return NULL; + } + return crm->certReq->certTemplate; +} + + +int OSSL_CRMF_MSG_set_validity(OSSL_CRMF_MSG *crm, time_t from, time_t to) +{ + OSSL_CRMF_OPTIONALVALIDITY *vld = NULL; + ASN1_TIME *from_asn = NULL; + ASN1_TIME *to_asn = NULL; + OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); + + if (tmpl == NULL) { /* also crm == NULL implies this */ + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (from != 0 && ((from_asn = ASN1_TIME_set(NULL, from)) == NULL)) + goto oom; + if (to != 0 && ((to_asn = ASN1_TIME_set(NULL, to)) == NULL)) + goto oom; + if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL) + goto oom; + + vld->notBefore = from_asn; + vld->notAfter = to_asn; + + tmpl->validity = vld; + + return 1; + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY, ERR_R_MALLOC_FAILURE); + ASN1_TIME_free(from_asn); + ASN1_TIME_free(to_asn); + return 0; +} + + +int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid) +{ + if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, CRMF_R_NULL_ARGUMENT); + return 0; + } + + return ASN1_INTEGER_set(crm->certReq->certReqId, rid); +} + +/* get ASN.1 encoded integer, return -1 on error */ +static int crmf_asn1_get_int(int func, const ASN1_INTEGER *a) +{ + int64_t res; + + if (!ASN1_INTEGER_get_int64(&res, a)) { + CRMFerr(func, ASN1_R_INVALID_NUMBER); + return -1; + } + if (res < INT_MIN) { + CRMFerr(func, ASN1_R_TOO_SMALL); + return -1; + } + if (res > INT_MAX) { + CRMFerr(func, ASN1_R_TOO_LARGE); + return -1; + } + return (int)res; +} + +int OSSL_CRMF_MSG_get_certReqId(OSSL_CRMF_MSG *crm) +{ + if (crm == NULL || /* not really needed: */ crm->certReq == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, CRMF_R_NULL_ARGUMENT); + return -1; + } + return crmf_asn1_get_int(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, + crm->certReq->certReqId); +} + + +int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm, + X509_EXTENSIONS *exts) +{ + OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); + + if (tmpl == NULL) { /* also crm == NULL implies this */ + CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (sk_X509_EXTENSION_num(exts) == 0) { + sk_X509_EXTENSION_free(exts); + exts = NULL; /* do not include empty extensions list */ + } + + sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free); + tmpl->extensions = exts; + return 1; +} + + +int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm, + const X509_EXTENSION *ext) +{ + int new = 0; + OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); + + if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */ + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (tmpl->extensions == NULL) { + if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL) + goto oom; + new = 1; + } + + if (!sk_X509_EXTENSION_push(tmpl->extensions, (X509_EXTENSION *)ext)) + goto oom; + return 1; + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, ERR_R_MALLOC_FAILURE); + + if (new != 0) { + sk_X509_EXTENSION_free(tmpl->extensions); + tmpl->extensions = NULL; + } + return 0; +} + +/* TODO: support cases 1+2 (besides case 3) defined in RFC 4211, section 4.1. */ +static int CRMF_poposigningkey_init(OSSL_CRMF_POPOSIGNINGKEY *ps, + OSSL_CRMF_CERTREQUEST *cr, + EVP_PKEY *pkey, int dgst) +{ + int len; + size_t crlen; + size_t siglen; + unsigned char *crder = NULL, *sig = NULL; + int alg_nid = 0; + int md_nid = 0; + const EVP_MD *alg = NULL; + EVP_MD_CTX *ctx = NULL; + int ret = 0; + + if (ps == NULL || cr == NULL || pkey == NULL) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_NULL_ARGUMENT); + return 0; + } + + /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */ + ps->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); + ps->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; + + len = i2d_OSSL_CRMF_CERTREQUEST(cr, &crder); + if (len < 0 || crder == NULL) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR); + goto err; + } + crlen = (size_t)len; + + if (!OBJ_find_sigid_by_algs(&alg_nid, dgst, EVP_PKEY_id(pkey))) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, + CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY); + goto err; + } + if (!OBJ_find_sigid_algs(alg_nid, &md_nid, NULL) + || (alg = EVP_get_digestbynid(md_nid)) == NULL) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, + CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY); + goto err; + } + if (!X509_ALGOR_set0(ps->algorithmIdentifier, OBJ_nid2obj(alg_nid), + V_ASN1_NULL, NULL) + || (ctx = EVP_MD_CTX_new()) == NULL + || EVP_DigestSignInit(ctx, NULL, alg, NULL, pkey) <= 0 + || EVP_DigestSignUpdate(ctx, crder, crlen) <= 0 + || EVP_DigestSignFinal(ctx, NULL, &siglen) <= 0) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR); + goto err; + } + if ((sig = OPENSSL_malloc(siglen)) == NULL) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, ERR_R_MALLOC_FAILURE); + goto err; + } + if (EVP_DigestSignFinal(ctx, sig, &siglen) <= 0 + || !ASN1_BIT_STRING_set(ps->signature, sig, siglen)) { + CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR); + goto err; + } + ret = 1; + + err: + OPENSSL_free(crder); + EVP_MD_CTX_free(ctx); + OPENSSL_free(sig); + return ret; +} + + +int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey, + int dgst, int ppmtd) +{ + OSSL_CRMF_POPO *pp = NULL; + ASN1_INTEGER *tag = NULL; + + if (crm == NULL || (ppmtd == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) { + CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT); + return 0; + } + + if (ppmtd == OSSL_CRMF_POPO_NONE) + goto end; + if ((pp = OSSL_CRMF_POPO_new()) == NULL) + goto oom; + pp->type = ppmtd; + + switch (ppmtd) { + case OSSL_CRMF_POPO_RAVERIFIED: + if ((pp->value.raVerified = ASN1_NULL_new()) == NULL) + goto oom; + break; + + case OSSL_CRMF_POPO_SIGNATURE: + { + OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new(); + if (ps == NULL + || !CRMF_poposigningkey_init(ps, crm->certReq, pkey, dgst)){ + OSSL_CRMF_POPOSIGNINGKEY_free(ps); + goto err; + } + pp->value.signature = ps; + } + break; + + case OSSL_CRMF_POPO_KEYENC: + if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL) + goto oom; + tag = ASN1_INTEGER_new(); + pp->value.keyEncipherment->type = + OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE; + pp->value.keyEncipherment->value.subsequentMessage = tag; + if (tag == NULL + || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT)) + goto oom; + break; + + default: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, + CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO); + goto err; + } + + end: + OSSL_CRMF_POPO_free(crm->popo); + crm->popo = pp; + + return 1; + oom: + CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, ERR_R_MALLOC_FAILURE); + err: + OSSL_CRMF_POPO_free(pp); + return 0; +} + +/* returns 0 for equal, -1 for a < b or error on a, 1 for a > b or error on b */ +static int X509_PUBKEY_cmp(X509_PUBKEY *a, X509_PUBKEY *b) +{ + X509_ALGOR *algA = NULL, *algB = NULL; + int res = 0; + + if (a == b) + return 0; + if (a == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) + || algA == NULL) + return -1; + if (b == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) + || algB == NULL) + return 1; + if ((res = X509_ALGOR_cmp(algA, algB)) != 0) + return res; + return EVP_PKEY_cmp(X509_PUBKEY_get0(a), X509_PUBKEY_get0(b)); +} + +/* verifies the Proof-of-Possession of the request with the given rid in reqs */ +int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs, + int rid, int acceptRAVerified) +{ + OSSL_CRMF_MSG *req = NULL; + X509_PUBKEY *pubkey = NULL; + OSSL_CRMF_POPOSIGNINGKEY *sig = NULL; + + if (reqs == NULL + || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL + || req->popo == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, + CRMF_R_NULL_ARGUMENT); + return 0; + } + + switch (req->popo->type) { + case OSSL_CRMF_POPO_RAVERIFIED: + if (acceptRAVerified) + return 1; + break; + case OSSL_CRMF_POPO_SIGNATURE: + pubkey = req->certReq->certTemplate->publicKey; + sig = req->popo->value.signature; + if (sig->poposkInput != NULL) { + /* + * According to RFC 4211: publicKey contains a copy of + * the public key from the certificate template. This MUST be + * exactly the same value as contained in the certificate template. + */ + if (pubkey == NULL + || sig->poposkInput->publicKey == NULL + || X509_PUBKEY_cmp(pubkey, sig->poposkInput->publicKey) + || ASN1_item_verify( + ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT), + sig->algorithmIdentifier, sig->signature, + sig->poposkInput, X509_PUBKEY_get0(pubkey)) < 1) + break; + } else { + if (pubkey == NULL + || req->certReq->certTemplate->subject == NULL + || ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST), + sig->algorithmIdentifier, sig->signature, + req->certReq, + X509_PUBKEY_get0(pubkey)) < 1) + break; + } + return 1; + case OSSL_CRMF_POPO_KEYENC: + /* + * TODO: when OSSL_CMP_certrep_new() supports encrypted certs, + * return 1 if the type of req->popo->value.keyEncipherment + * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and + * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT + */ + case OSSL_CRMF_POPO_KEYAGREE: + default: + CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, + CRMF_R_UNSUPPORTED_POPO_METHOD); + return 0; + } + CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO, + CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED); + return 0; +} + +/* retrieves the serialNumber of the given cert template or NULL on error */ +ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(OSSL_CRMF_CERTTEMPLATE *tmpl) +{ + return tmpl != NULL ? tmpl->serialNumber : NULL; +} + +/* retrieves the issuer name of the given cert template or NULL on error */ +X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(OSSL_CRMF_CERTTEMPLATE *tmpl) +{ + return tmpl != NULL ? tmpl->issuer : NULL; +} + +/* + * fill in certificate template. + * Any value argument that is NULL will leave the respective field unchanged. + */ +int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl, + EVP_PKEY *pubkey, + const X509_NAME *subject, + const X509_NAME *issuer, + const ASN1_INTEGER *serial) +{ + if (tmpl == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT); + return 0; + } + if (subject != NULL && !X509_NAME_set(&tmpl->subject, subject)) + goto oom; + if (issuer != NULL && !X509_NAME_set(&tmpl->issuer, issuer)) + goto oom; + if (serial != NULL) { + ASN1_INTEGER_free(tmpl->serialNumber); + if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) + goto oom; + } + if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey)) + goto oom; + return 1; + + oom: + CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, ERR_R_MALLOC_FAILURE); + return 0; +} + + +/*- + * Decrypts the certificate in the given encryptedValue + * this is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2 + * + * returns a pointer to the decrypted certificate + * returns NULL on error or if no certificate available + */ +X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(OSSL_CRMF_ENCRYPTEDVALUE *ecert, + EVP_PKEY *pkey) +{ + X509 *cert = NULL; /* decrypted certificate */ + EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */ + unsigned char *ek = NULL; /* decrypted symmetric encryption key */ + const EVP_CIPHER *cipher = NULL; /* used cipher */ + unsigned char *iv = NULL; /* initial vector for symmetric encryption */ + unsigned char *outbuf = NULL; /* decryption output buffer */ + const unsigned char *p = NULL; /* needed for decoding ASN1 */ + int symmAlg = 0; /* NIDs for symmetric algorithm */ + int n, outlen = 0; + EVP_PKEY_CTX *pkctx = NULL; /* private key context */ + + if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL + || ecert->encValue == NULL || pkey == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_NULL_ARGUMENT); + return NULL; + } + if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_UNSUPPORTED_CIPHER); + return NULL; + } + + /* first the symmetric key needs to be decrypted */ + pkctx = EVP_PKEY_CTX_new(pkey, NULL); + if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) { + ASN1_BIT_STRING *encKey = ecert->encSymmKey; + size_t eksize = 0; + + if (EVP_PKEY_decrypt(pkctx, NULL, &eksize, encKey->data, encKey->length) + <= 0 + || (ek = OPENSSL_malloc(eksize)) == NULL + || EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data, + encKey->length) <= 0) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY); + goto end; + } + } else { + goto oom; + } + + /* select symmetric cipher based on algorithm given in message */ + if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_UNSUPPORTED_CIPHER); + goto end; + } + if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL) + goto oom; + if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv, + EVP_CIPHER_iv_length(cipher)) + != EVP_CIPHER_iv_length(cipher)) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_MALFORMED_IV); + goto end; + } + + /* + * d2i_X509 changes the given pointer, so use p for decoding the message and + * keep the original pointer in outbuf so the memory can be freed later + */ + if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length + + EVP_CIPHER_block_size(cipher))) == NULL + || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL) + goto oom; + EVP_CIPHER_CTX_set_padding(evp_ctx, 0); + + if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv) + || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen, + ecert->encValue->data, + ecert->encValue->length) + || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_ERROR_DECRYPTING_CERTIFICATE); + goto end; + } + outlen += n; + + /* convert decrypted certificate from DER to internal ASN.1 structure */ + if ((cert = d2i_X509(NULL, &p, outlen)) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, + CRMF_R_ERROR_DECODING_CERTIFICATE); + } + goto end; + + oom: + CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, ERR_R_MALLOC_FAILURE); + end: + EVP_PKEY_CTX_free(pkctx); + OPENSSL_free(outbuf); + EVP_CIPHER_CTX_free(evp_ctx); + OPENSSL_free(ek); + OPENSSL_free(iv); + return cert; +} diff --git a/crypto/crmf/crmf_pbm.c b/crypto/crmf/crmf_pbm.c new file mode 100644 index 0000000000..60d06d4a3c --- /dev/null +++ b/crypto/crmf/crmf_pbm.c @@ -0,0 +1,231 @@ +/*- + * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright Nokia 2007-2018 + * Copyright Siemens AG 2015-2018 + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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. + */ + + +#include +#include + +#include "crmf_int.h" + +/* explicit #includes not strictly needed since implied by the above: */ +#include +#include +#include +#include + +/*- + * creates and initializes OSSL_CRMF_PBMPARAMETER (section 4.4) + * |slen| SHOULD be > 8 (16 is common) + * |owfnid| e.g., NID_sha256 + * |itercnt| MUST be > 100 (500 is common) + * |macnid| e.g., NID_hmac_sha1 + * returns pointer to OSSL_CRMF_PBMPARAMETER on success, NULL on error + */ +OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(size_t slen, int owfnid, + int itercnt, int macnid) +{ + OSSL_CRMF_PBMPARAMETER *pbm = NULL; + unsigned char *salt = NULL; + + if ((pbm = OSSL_CRMF_PBMPARAMETER_new()) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, ERR_R_MALLOC_FAILURE); + 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. + */ + if ((salt = OPENSSL_malloc(slen)) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, ERR_R_MALLOC_FAILURE); + goto err; + } + if (RAND_bytes(salt, (int)slen) <= 0) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_FAILURE_OBTAINING_RANDOM); + 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)) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_OWF_ALGOR_FAILURE); + goto err; + } + + /* + * iterationCount identifies the number of times the hash is applied + * during the key computation process. The iterationCount MUST be a + * minimum of 100. Many people suggest using values as high as 1000 + * 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) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_ITERATIONCOUNT_BELOW_100); + goto err; + } + + if (!ASN1_INTEGER_set(pbm->iterationCount, itercnt)) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_CRMFERROR); + 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)) { + CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_MAC_ALGOR_FAILURE); + 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 + * |mac| pointer to the computed mac, will be set on success + * |maclen| if not NULL, will set variable to the length of the mac on success + * returns 1 on success, 0 on error + */ +int OSSL_CRMF_pbm_new(const OSSL_CRMF_PBMPARAMETER *pbmp, + const unsigned char *msg, size_t msglen, + const unsigned char *sec, size_t seclen, + unsigned char **mac, size_t *maclen) +{ + int mac_nid, hmac_md_nid = NID_undef; + const EVP_MD *m = NULL; + 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; + EVP_MAC_CTX *mctx = NULL; + + if (mac == NULL || pbmp == NULL || pbmp->mac == NULL + || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_NULL_ARGUMENT); + goto err; + } + if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, ERR_R_MALLOC_FAILURE); + 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 ((m = EVP_get_digestbyobj(pbmp->owf->algorithm)) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM); + goto err; + } + + if ((ctx = EVP_MD_CTX_new()) == NULL) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, ERR_R_MALLOC_FAILURE); + goto err; + } + + /* compute the basekey of the salted secret */ + if (!EVP_DigestInit_ex(ctx, m, NULL)) + 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) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_BAD_PBM_ITERATIONCOUNT); + goto err; + } + + /* the first iteration was already done above */ + while (--iterations > 0) { + if (!EVP_DigestInit_ex(ctx, m, NULL)) + 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) + || ((m = EVP_get_digestbynid(hmac_md_nid)) == NULL)) { + CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM); + goto err; + } + + if ((mctx = EVP_MAC_CTX_new(EVP_get_macbyname("HMAC"))) == NULL + || EVP_MAC_ctrl(mctx, EVP_MAC_CTRL_SET_MD, m) <= 0 + || EVP_MAC_ctrl(mctx, EVP_MAC_CTRL_SET_KEY, basekey, bklen) <= 0 + || !EVP_MAC_init(mctx) + || !EVP_MAC_update(mctx, msg, msglen) + || !EVP_MAC_final(mctx, mac_res, maclen)) + goto err; + + ok = 1; + + err: + /* cleanup */ + OPENSSL_cleanse(basekey, bklen); + EVP_MAC_CTX_free(mctx); + EVP_MD_CTX_free(ctx); + + if (ok == 1) { + *mac = mac_res; + 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; +} diff --git a/crypto/err/err.c b/crypto/err/err.c index 3aa3daedfc..63dcfc35e9 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -58,6 +58,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = { {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"}, {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"}, {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"}, + {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"}, {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"}, {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"}, {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"}, diff --git a/crypto/err/err_all.c b/crypto/err/err_all.c index 4bf020c281..1166b01cb5 100644 --- a/crypto/err/err_all.c +++ b/crypto/err/err_all.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -90,6 +91,9 @@ int err_load_crypto_strings_int(void) # ifndef OPENSSL_NO_CMS ERR_load_CMS_strings() == 0 || # endif +# ifndef OPENSSL_NO_CMP + ERR_load_CRMF_strings() == 0 || +# endif # ifndef OPENSSL_NO_CT ERR_load_CT_strings() == 0 || # endif diff --git a/crypto/err/openssl.ec b/crypto/err/openssl.ec index 901a847d29..a204434f8d 100644 --- a/crypto/err/openssl.ec +++ b/crypto/err/openssl.ec @@ -29,6 +29,7 @@ L UI include/openssl/ui.h crypto/ui/ui_err.c L COMP include/openssl/comp.h crypto/comp/comp_err.c L TS include/openssl/ts.h crypto/ts/ts_err.c L CMS include/openssl/cms.h crypto/cms/cms_err.c +L CRMF include/openssl/crmf.h crypto/crmf/crmf_err.c L CT include/openssl/ct.h crypto/ct/ct_err.c L ASYNC include/openssl/async.h crypto/async/async_err.c L KDF include/openssl/kdf.h crypto/kdf/kdf_err.c diff --git a/crypto/err/openssl.txt b/crypto/err/openssl.txt index 8ae11cad30..cb58f429e6 100644 --- a/crypto/err/openssl.txt +++ b/crypto/err/openssl.txt @@ -348,6 +348,28 @@ CONF_F_NCONF_NEW:111:NCONF_new CONF_F_PROCESS_INCLUDE:116:process_include CONF_F_SSL_MODULE_INIT:123:ssl_module_init CONF_F_STR_COPY:101:str_copy +CRMF_F_CRMF_POPOSIGNINGKEY_INIT:100:CRMF_poposigningkey_init +CRMF_F_OSSL_CRMF_CERTID_GEN:101:OSSL_CRMF_CERTID_gen +CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL:102:OSSL_CRMF_CERTTEMPLATE_fill +CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT:103:\ + OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert +CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO:104:OSSL_CRMF_MSGS_verify_popo +CRMF_F_OSSL_CRMF_MSG_CREATE_POPO:105:OSSL_CRMF_MSG_create_popo +CRMF_F_OSSL_CRMF_MSG_GET0_TMPL:106:OSSL_CRMF_MSG_get0_tmpl +CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID:107:OSSL_CRMF_MSG_get_certReqId +CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO:108:\ + OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo +CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION:109:OSSL_CRMF_MSG_push0_extension +CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL:110:OSSL_CRMF_MSG_push0_regCtrl +CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO:111:OSSL_CRMF_MSG_push0_regInfo +CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS:112:OSSL_CRMF_MSG_set0_extensions +CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO:113:OSSL_CRMF_MSG_set0_SinglePubInfo +CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID:114:OSSL_CRMF_MSG_set_certReqId +CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION:115:\ + OSSL_CRMF_MSG_set_PKIPublicationInfo_action +CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY:116:OSSL_CRMF_MSG_set_validity +CRMF_F_OSSL_CRMF_PBMP_NEW:117:OSSL_CRMF_pbmp_new +CRMF_F_OSSL_CRMF_PBM_NEW:118:OSSL_CRMF_pbm_new CRYPTO_F_CMAC_CTX_NEW:120:CMAC_CTX_new CRYPTO_F_CRYPTO_DUP_EX_DATA:110:CRYPTO_dup_ex_data CRYPTO_F_CRYPTO_FREE_EX_DATA:111:CRYPTO_free_ex_data @@ -2103,6 +2125,25 @@ CONF_R_UNABLE_TO_CREATE_NEW_SECTION:103:unable to create new section CONF_R_UNKNOWN_MODULE_NAME:113:unknown module name CONF_R_VARIABLE_EXPANSION_TOO_LONG:116:variable expansion too long CONF_R_VARIABLE_HAS_NO_VALUE:104:variable has no value +CRMF_R_BAD_PBM_ITERATIONCOUNT:100:bad pbm iterationcount +CRMF_R_MALFORMED_IV:101:malformed iv +CRMF_R_CRMFERROR:102:crmferror +CRMF_R_ERROR:103:error +CRMF_R_ERROR_DECODING_CERTIFICATE:104:error decoding certificate +CRMF_R_ERROR_DECRYPTING_CERTIFICATE:105:error decrypting certificate +CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY:106:error decrypting symmetric key +CRMF_R_FAILURE_OBTAINING_RANDOM:107:failure obtaining random +CRMF_R_ITERATIONCOUNT_BELOW_100:108:iterationcount below 100 +CRMF_R_NULL_ARGUMENT:109:null argument +CRMF_R_SETTING_MAC_ALGOR_FAILURE:110:setting mac algor failure +CRMF_R_SETTING_OWF_ALGOR_FAILURE:111:setting owf algor failure +CRMF_R_UNSUPPORTED_ALGORITHM:112:unsupported algorithm +CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY:113:unsupported alg for popsigningkey +CRMF_R_UNSUPPORTED_CIPHER:114:unsupported cipher +CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO:115:\ + unsupported method for creating popo +CRMF_R_UNSUPPORTED_POPO_METHOD:116:unsupported popo method +CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED:117:unsupported popo not accepted CRYPTO_R_FIPS_MODE_NOT_SUPPORTED:101:fips mode not supported CRYPTO_R_ILLEGAL_HEX_DIGIT:102:illegal hex digit CRYPTO_R_ODD_NUMBER_OF_DIGITS:103:odd number of digits diff --git a/doc/man3/OSSL_CRMF_MSG_set1_regCtrl_regToken.pod b/doc/man3/OSSL_CRMF_MSG_set1_regCtrl_regToken.pod index 1b80c72b75..851bd130cc 100644 --- a/doc/man3/OSSL_CRMF_MSG_set1_regCtrl_regToken.pod +++ b/doc/man3/OSSL_CRMF_MSG_set1_regCtrl_regToken.pod @@ -48,9 +48,10 @@ the given B copying the given B as value. See RFC 4211, section 6.2. OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo() pushes the given B to B. Consumes the B pointer. -OSSL_CRMF_MSG_set0_SinglePubInfo() sets given B and PubLoction B to -B. PubLocation is optional, and therefore B may be NULL. Consumes the -B pointer. +OSSL_CRMF_MSG_set0_SinglePubInfo() sets in the given SinglePubInfo B +the B and publication location, in the form of a GeneralName, B. +The publication location is optional, and therefore B may be NULL. +The function consumes the B pointer if present. Available methods are: # define OSSL_CRMF_PUB_METHOD_DONTCARE 0 # define OSSL_CRMF_PUB_METHOD_X500 1 @@ -63,7 +64,7 @@ Available actions are: # define OSSL_CRMF_PUB_ACTION_DONTPUBLISH 0 # define OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH 1 -OSSL_CRMF_MSG_set1_regCtrl_pkiPublicationInfo() sets the pkiPulicationInfo +OSSL_CRMF_MSG_set1_regCtrl_pkiPublicationInfo() sets the pkiPublicationInfo control in the given B copying the given B as value. See RFC 4211, section 6.3. diff --git a/doc/man3/OSSL_CRMF_MSG_set_validity.pod b/doc/man3/OSSL_CRMF_MSG_set_validity.pod index a610337392..b6ea32eab2 100644 --- a/doc/man3/OSSL_CRMF_MSG_set_validity.pod +++ b/doc/man3/OSSL_CRMF_MSG_set_validity.pod @@ -17,10 +17,10 @@ OSSL_CRMF_MSGS_verify_popo int OSSL_CRMF_MSG_set_validity(OSSL_CRMF_MSG *crm, time_t from, time_t to); - int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, const int rid); + int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid); int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl, - const EVP_PKEY *pubkey, + EVP_PKEY *pubkey, const X509_NAME *subject, const X509_NAME *issuer, const ASN1_INTEGER *serial); @@ -31,7 +31,7 @@ OSSL_CRMF_MSGS_verify_popo int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm, const X509_EXTENSION *ext); - int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, const EVP_PKEY *pkey, + int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey, int dgst, int ppmtd); int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs, @@ -46,10 +46,12 @@ OSSL_CRMF_MSG_set_certReqId() sets B as the certReqId of B. OSSL_CRMF_CERTTEMPLATE_fill() sets those fields of the certTemplate B for which non-NULL values are provided: B, B, B, -and/or B. The arguments are not consumed. +and/or B. +On success the reference counter of the B (if given) is incremented, +while the B, B, and B structures (if given) are copied. OSSL_CRMF_MSG_set0_extensions() sets B as the extensions in the -certTemplate of B. Consumes B. +certTemplate of B. Frees any pre-existing ones and consumes B. OSSL_CRMF_MSG_push0_extension() pushes the X509 extension B to the extensions in the certTemplate of B. Consumes B. diff --git a/doc/man3/OSSL_CRMF_pbmp_new.pod b/doc/man3/OSSL_CRMF_pbmp_new.pod index d386d2b264..a83209b51a 100644 --- a/doc/man3/OSSL_CRMF_pbmp_new.pod +++ b/doc/man3/OSSL_CRMF_pbmp_new.pod @@ -13,7 +13,7 @@ OSSL_CRMF_pbmp_new int OSSL_CRMF_pbm_new(const OSSL_CRMF_PBMPARAMETER *pbmp, const unsigned char *msg, size_t msglen, const unsigned char *sec, size_t seclen, - unsigned char **mac, unsigned int *maclen); + unsigned char **mac, size_t *maclen); OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(size_t saltlen, int owfnid, int itercnt, int macnid); @@ -22,23 +22,23 @@ OSSL_CRMF_pbmp_new OSSL_CRMF_pbm_new() generates a PBM (Password-Based MAC) based on given PBM parameters B, message B, and secret B, along with the respective -lengths B and B. Will write the adddress of the newly allocated -MAC via the B reference parameter and the length via the B reference -parameter. Any previous pointer referred to by B will be freed if not NULL. +lengths B and B. On success writes the adddress of the newly +allocated MAC via the B reference parameter and writes the length via the +B reference parameter unless it its NULL. The iteration count must be at least 100, as stipulated by RFC 4211, and is limited to at most 100000 to avoid DoS through manipulated or otherwise malformed input. OSSL_CRMF_pbmp_new() initializes and returns a new PBMParameter -structure with new a random salt of given length B, OWF (one-way +structure with a new random salt of given length B, OWF (one-way function) NID B, iteration count B, and MAC NID B. =head1 NOTES -The OWF (one-way function) and for the MAC (message authentication code) may be -any with a NID defined in B, -which also should include NID_hmac_sha1 which is specified by RFC 4210. +The algorithms for the OWF (one-way function) and for the MAC (message +authentication code) may be any with a NID defined in B. +As specified by RFC 4210, these should include NID_hmac_sha1. RFC 4210 recommends that the salt SHOULD be at least 8 bytes (64 bits) long. @@ -53,9 +53,9 @@ structure, or NULL on error. OSSL_CRMF_PBMPARAMETER *pbm = NULL; unsigned char *msg = "Hello"; - unsigend char *sec = "SeCrEt"; - unsigend char *mac = NULL; - unsigend int maclen; + unsigned char *sec = "SeCrEt"; + unsigned char *mac = NULL; + size_t maclen; if ((pbm = OSSL_CRMF_pbmp_new(16, NID_sha256, 500, NID_hmac_sha1) == NULL)) goto err; diff --git a/include/openssl/crmf.h b/include/openssl/crmf.h index f88dd39830..b4221a9e44 100644 --- a/include/openssl/crmf.h +++ b/include/openssl/crmf.h @@ -1,4 +1,4 @@ -/* +/*- * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved. * Copyright Nokia 2007-2018 * Copyright Siemens AG 2015-2018 @@ -8,7 +8,7 @@ * 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. + * CRMF (RFC 4211) implementation by M. Peylo, M. Viljanen, and D. von Oheimb. */ #ifndef OSSL_HEADER_CRMF_H @@ -16,7 +16,7 @@ # include -# ifndef OPENSSL_NO_CRMF +# ifndef OPENSSL_NO_CMP # include # include # include @@ -39,7 +39,7 @@ extern "C" { # define OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT 0 # define OSSL_CRMF_SUBSEQUENTMESSAGE_CHALLENGERESP 1 -typedef struct OSSL_crmf_encrypetedvalue_st OSSL_CRMF_ENCRYPTEDVALUE; +typedef struct OSSL_crmf_encryptedvalue_st OSSL_CRMF_ENCRYPTEDVALUE; DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_ENCRYPTEDVALUE) typedef struct OSSL_crmf_msg_st OSSL_CRMF_MSG; DECLARE_ASN1_FUNCTIONS(OSSL_CRMF_MSG) @@ -70,7 +70,7 @@ OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(size_t slen, int owfnid, int OSSL_CRMF_pbm_new(const OSSL_CRMF_PBMPARAMETER *pbmp, const unsigned char *msg, size_t msglen, const unsigned char *sec, size_t seclen, - unsigned char **mac, unsigned int *maclen); + unsigned char **mac, size_t *maclen); /* crmf_lib.c */ int OSSL_CRMF_MSG_set1_regCtrl_regToken(OSSL_CRMF_MSG *msg, @@ -115,7 +115,7 @@ int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm, const X509_EXTENSION *ext) # define OSSL_CRMF_POPO_SIGNATURE 1 # define OSSL_CRMF_POPO_KEYENC 2 # define OSSL_CRMF_POPO_KEYAGREE 3 -int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, const EVP_PKEY *pkey, +int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey, int dgst, int ppmtd); int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs, int rid, int acceptRAVerified); @@ -123,7 +123,7 @@ OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm); ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(OSSL_CRMF_CERTTEMPLATE *t); X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(OSSL_CRMF_CERTTEMPLATE *tmpl); int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl, - const EVP_PKEY *pubkey, + EVP_PKEY *pubkey, const X509_NAME *subject, const X509_NAME *issuer, const ASN1_INTEGER *serial); @@ -133,5 +133,5 @@ X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(OSSL_CRMF_ENCRYPTEDVALUE *ecert, # ifdef __cplusplus } # endif -# endif /* !defined OPENSSL_NO_CRMF */ +# endif /* !defined OPENSSL_NO_CMP */ #endif /* !defined OSSL_HEADER_CRMF_H */ diff --git a/include/openssl/crmferr.h b/include/openssl/crmferr.h new file mode 100644 index 0000000000..06f90fb36f --- /dev/null +++ b/include/openssl/crmferr.h @@ -0,0 +1,69 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * 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 + */ + +#ifndef HEADER_CRMFERR_H +# define HEADER_CRMFERR_H + +# include + +# ifndef OPENSSL_NO_CMP + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_CRMF_strings(void); + +/* + * CRMF function codes. + */ +# define CRMF_F_CRMF_POPOSIGNINGKEY_INIT 100 +# define CRMF_F_OSSL_CRMF_CERTID_GEN 101 +# define CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL 102 +# define CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT 103 +# define CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO 104 +# define CRMF_F_OSSL_CRMF_MSG_CREATE_POPO 105 +# define CRMF_F_OSSL_CRMF_MSG_GET0_TMPL 106 +# define CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID 107 +# define CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO 108 +# define CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION 109 +# define CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL 110 +# define CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO 111 +# define CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS 112 +# define CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO 113 +# define CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID 114 +# define CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION 115 +# define CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY 116 +# define CRMF_F_OSSL_CRMF_PBMP_NEW 117 +# define CRMF_F_OSSL_CRMF_PBM_NEW 118 + +/* + * CRMF reason codes. + */ +# define CRMF_R_BAD_PBM_ITERATIONCOUNT 100 +# define CRMF_R_MALFORMED_IV 101 +# define CRMF_R_CRMFERROR 102 +# define CRMF_R_ERROR 103 +# define CRMF_R_ERROR_DECODING_CERTIFICATE 104 +# define CRMF_R_ERROR_DECRYPTING_CERTIFICATE 105 +# define CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY 106 +# define CRMF_R_FAILURE_OBTAINING_RANDOM 107 +# define CRMF_R_ITERATIONCOUNT_BELOW_100 108 +# define CRMF_R_NULL_ARGUMENT 109 +# define CRMF_R_SETTING_MAC_ALGOR_FAILURE 110 +# define CRMF_R_SETTING_OWF_ALGOR_FAILURE 111 +# define CRMF_R_UNSUPPORTED_ALGORITHM 112 +# define CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY 113 +# define CRMF_R_UNSUPPORTED_CIPHER 114 +# define CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO 115 +# define CRMF_R_UNSUPPORTED_POPO_METHOD 116 +# define CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED 117 + +# endif +#endif diff --git a/include/openssl/err.h b/include/openssl/err.h index 0d6956cff3..fded82c481 100644 --- a/include/openssl/err.h +++ b/include/openssl/err.h @@ -96,6 +96,7 @@ typedef struct err_state_st { # define ERR_LIB_SM2 53 # define ERR_LIB_ESS 54 # define ERR_LIB_PROP 55 +# define ERR_LIB_CRMF 56 # define ERR_LIB_USER 128 @@ -129,6 +130,7 @@ typedef struct err_state_st { # define OSSL_STOREerr(f,r) ERR_PUT_error(ERR_LIB_OSSL_STORE,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define FIPSerr(f,r) ERR_PUT_error(ERR_LIB_FIPS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define CMSerr(f,r) ERR_PUT_error(ERR_LIB_CMS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define CRMFerr(f,r) ERR_PUT_error(ERR_LIB_CRMF,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define TSerr(f,r) ERR_PUT_error(ERR_LIB_TS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define CTerr(f,r) ERR_PUT_error(ERR_LIB_CT,(f),(r),OPENSSL_FILE,OPENSSL_LINE) diff --git a/util/libcrypto.num b/util/libcrypto.num index 42435931c7..974d9184ba 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -4660,3 +4660,85 @@ OSSL_PROVIDER_unload 4615 3_0_0 EXIST::FUNCTION: OSSL_PROVIDER_add_builtin 4616 3_0_0 EXIST::FUNCTION: OSSL_PROVIDER_get_param_types 4617 3_0_0 EXIST::FUNCTION: OSSL_PROVIDER_get_params 4618 3_0_0 EXIST::FUNCTION: +d2i_OSSL_CRMF_ENCRYPTEDVALUE 4619 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_ENCRYPTEDVALUE 4620 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_ENCRYPTEDVALUE_free 4621 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_ENCRYPTEDVALUE_new 4622 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_ENCRYPTEDVALUE_it 4623 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_ENCRYPTEDVALUE_it 4624 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_ENCRYPTEDVALUE_it 4624 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_MSG 4625 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_MSG 4626 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_free 4627 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_new 4628 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_it 4629 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_MSG_it 4630 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_MSG_it 4630 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_PBMPARAMETER 4631 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_PBMPARAMETER 4632 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PBMPARAMETER_free 4633 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PBMPARAMETER_new 4634 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PBMPARAMETER_it 4635 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_PBMPARAMETER_it 4636 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_PBMPARAMETER_it 4636 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_CERTID 4637 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_CERTID 4638 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTID_free 4639 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTID_new 4640 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTID_it 4641 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_CERTID_it 4642 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_CERTID_it 4642 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_PKIPUBLICATIONINFO 4643 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_PKIPUBLICATIONINFO 4644 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PKIPUBLICATIONINFO_free 4645 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PKIPUBLICATIONINFO_new 4646 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_PKIPUBLICATIONINFO_it 4647 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_PKIPUBLICATIONINFO_it 4648 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_PKIPUBLICATIONINFO_it 4648 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_SINGLEPUBINFO 4649 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_SINGLEPUBINFO 4650 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_SINGLEPUBINFO_free 4651 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_SINGLEPUBINFO_new 4652 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_SINGLEPUBINFO_it 4653 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_SINGLEPUBINFO_it 4654 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_SINGLEPUBINFO_it 4654 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_CERTTEMPLATE 4655 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_CERTTEMPLATE 4656 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_free 4657 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_new 4658 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_it 4659 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_CERTTEMPLATE_it 4660 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_CERTTEMPLATE_it 4660 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +d2i_OSSL_CRMF_MSGS 4661 3_0_0 EXIST::FUNCTION:CMP +i2d_OSSL_CRMF_MSGS 4662 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSGS_free 4663 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSGS_new 4664 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSGS_it 4665 3_0_0 NOEXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_MSGS_it 4666 3_0_0 EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:CMP +OSSL_CRMF_MSGS_it 4666 3_0_0 EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:CMP +OSSL_CRMF_pbmp_new 4667 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_pbm_new 4668 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regCtrl_regToken 4669 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regCtrl_authenticator 4670 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo 4671 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set0_SinglePubInfo 4672 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set_PKIPublicationInfo_action 4673 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regCtrl_pkiPublicationInfo 4674 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regCtrl_protocolEncrKey 4675 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regCtrl_oldCertID 4676 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTID_gen 4677 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regInfo_utf8Pairs 4678 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set1_regInfo_certReq 4679 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set_validity 4680 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set_certReqId 4681 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_get_certReqId 4682 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_set0_extensions 4683 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_push0_extension 4684 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_create_popo 4685 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSGS_verify_popo 4686 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_MSG_get0_tmpl 4687 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_get0_serialNumber 4688 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_get0_issuer 4689 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_CERTTEMPLATE_fill 4690 3_0_0 EXIST::FUNCTION:CMP +OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert 4691 3_0_0 EXIST::FUNCTION:CMP +ERR_load_CRMF_strings 4692 3_0_0 EXIST::FUNCTION:CMP