@@ -2076,7 +2076,8 @@ static int wp_ecc_decode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
20762076 if (ok && (!wp_read_der_bio (ctx -> provCtx , cBio , & data , & len ))) {
20772077 ok = 0 ;
20782078 }
2079- if (ok && (ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC )) {
2079+ if (ok && ((ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC ) ||
2080+ (ctx -> format == WP_ENC_FORMAT_X9_62 ))) {
20802081 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ) != 0 ) {
20812082 if (!wp_ecc_decode_pki (ecc , data , len )) {
20822083 ok = 0 ;
@@ -2439,7 +2440,8 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
24392440 ok = 0 ;
24402441 }
24412442
2442- if (ok && (ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC )) {
2443+ if (ok && ((ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC ) ||
2444+ (ctx -> format == WP_ENC_FORMAT_X9_62 ))) {
24432445 if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS ) {
24442446 if (!wp_ecc_encode_params_size (key , & derLen )) {
24452447 ok = 0 ;
@@ -2480,14 +2482,18 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
24802482 }
24812483 }
24822484
2483- if (ok && (ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC )) {
2485+ if (ok && ((ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC ) ||
2486+ (ctx -> format == WP_ENC_FORMAT_X9_62 ))) {
24842487 if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS ) {
24852488 pemType = DH_PARAM_TYPE ;
24862489 if (!wp_ecc_encode_params (key , derData , & derLen )) {
24872490 ok = 0 ;
24882491 }
24892492 }
24902493 else {
2494+ if (ctx -> format == WP_ENC_FORMAT_X9_62 ) {
2495+ pemType = ECC_PRIVATEKEY_TYPE ;
2496+ }
24912497 private = 1 ;
24922498 if (!wp_ecc_encode_priv (key , derData , & derLen )) {
24932499 ok = 0 ;
@@ -2543,8 +2549,9 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
25432549 keyLen = pemLen = rc ;
25442550 keyData = pemData ;
25452551 }
2546- if (ok && (ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC ) &&
2547- (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS )) {
2552+ if (ok && ((ctx -> format == WP_ENC_FORMAT_TYPE_SPECIFIC ) ||
2553+ (ctx -> format == WP_ENC_FORMAT_X9_62 )) &&
2554+ (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS )) {
25482555 pemData [11 ] = 'E' ;
25492556 pemData [12 ] = 'C' ;
25502557 pemData [pemLen - 19 ] = 'E' ;
@@ -2984,4 +2991,119 @@ const OSSL_DISPATCH wp_ecc_epki_pem_encoder_functions[] = {
29842991 { 0 , NULL }
29852992};
29862993
2994+ /*
2995+ * ECC X9.62
2996+ */
2997+
2998+ /**
2999+ * Create a new ECC encoder/decoder context that handles decoding X9.62.
3000+ *
3001+ * @param [in] provCtx Provider context.
3002+ * @return New ECC encoder/decoder context object on success.
3003+ * @return NULL on failure.
3004+ */
3005+ static wp_EccEncDecCtx * wp_ecc_x9_62_dec_new (WOLFPROV_CTX * provCtx )
3006+ {
3007+ return wp_ecc_enc_dec_new (provCtx , WP_ENC_FORMAT_X9_62 , 0 );
3008+ }
3009+
3010+ /**
3011+ * Return whether the X9.62 decoder/encoder handles the part of the key.
3012+ *
3013+ * @param [in] ctx ECC encoder/decoder context object.
3014+ * @param [in] selection Parts of key to handle.
3015+ * @return 1 when supported.
3016+ * @return 0 when not supported.
3017+ */
3018+ static int wp_ecc_x9_62_does_selection (WOLFPROV_CTX * provCtx ,
3019+ int selection )
3020+ {
3021+ int ok ;
3022+
3023+ (void )provCtx ;
3024+
3025+ if (selection == 0 ) {
3026+ ok = 1 ;
3027+ }
3028+ else {
3029+ ok = (selection & (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS |
3030+ OSSL_KEYMGMT_SELECT_PRIVATE_KEY )) != 0 ;
3031+ }
3032+
3033+ WOLFPROV_LEAVE (WP_LOG_PK , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
3034+ return ok ;
3035+ }
3036+
3037+ /**
3038+ * Dispatch table for x9_62 decoder.
3039+ */
3040+ const OSSL_DISPATCH wp_ecc_x9_62_decoder_functions [] = {
3041+ { OSSL_FUNC_DECODER_NEWCTX , (DFUNC )wp_ecc_x9_62_dec_new },
3042+ { OSSL_FUNC_DECODER_FREECTX , (DFUNC )wp_ecc_enc_dec_free },
3043+ { OSSL_FUNC_DECODER_DOES_SELECTION ,
3044+ (DFUNC )wp_ecc_x9_62_does_selection },
3045+ { OSSL_FUNC_DECODER_DECODE , (DFUNC )wp_ecc_decode },
3046+ { OSSL_FUNC_DECODER_EXPORT_OBJECT , (DFUNC )wp_ecc_export_object },
3047+ { 0 , NULL }
3048+ };
3049+
3050+ /**
3051+ * Create a new ECC encoder/decoder context that handles encoding params in DER.
3052+ *
3053+ * @param [in] provCtx Provider context.
3054+ * @return New ECC encoder/decoder context object on success.
3055+ * @return NULL on failure.
3056+ */
3057+ static wp_EccEncDecCtx * wp_ecc_x9_62_der_enc_new (WOLFPROV_CTX * provCtx )
3058+ {
3059+ return wp_ecc_enc_dec_new (provCtx , WP_ENC_FORMAT_X9_62 , WP_FORMAT_DER );
3060+ }
3061+
3062+ /**
3063+ * Dispatch table for X9.62 to DER encoder.
3064+ */
3065+ const OSSL_DISPATCH wp_ecc_x9_62_der_encoder_functions [] = {
3066+ { OSSL_FUNC_ENCODER_NEWCTX , (DFUNC )wp_ecc_x9_62_der_enc_new },
3067+ { OSSL_FUNC_ENCODER_FREECTX , (DFUNC )wp_ecc_enc_dec_free },
3068+ { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS ,
3069+ (DFUNC )wp_ecc_enc_dec_settable_ctx_params },
3070+ { OSSL_FUNC_ENCODER_SET_CTX_PARAMS , (DFUNC )wp_ecc_enc_dec_set_ctx_params },
3071+ { OSSL_FUNC_ENCODER_DOES_SELECTION ,
3072+ (DFUNC )wp_ecc_x9_62_does_selection },
3073+ { OSSL_FUNC_ENCODER_ENCODE , (DFUNC )wp_ecc_encode },
3074+ { OSSL_FUNC_ENCODER_IMPORT_OBJECT , (DFUNC )wp_ecc_import },
3075+ { OSSL_FUNC_ENCODER_FREE_OBJECT , (DFUNC )wp_ecc_free },
3076+ { 0 , NULL }
3077+ };
3078+
3079+ /**
3080+ * Create a new ECC encoder/decoder context that handles encoding X9.62 in PEM.
3081+ *
3082+ * @param [in] provCtx Provider context.
3083+ * @return New ECC encoder/decoder context object on success.
3084+ * @return NULL on failure.
3085+ */
3086+ static wp_EccEncDecCtx * wp_ecc_x9_62_pem_enc_new (WOLFPROV_CTX * provCtx )
3087+ {
3088+ return wp_ecc_enc_dec_new (provCtx , WP_ENC_FORMAT_X9_62 , WP_FORMAT_PEM );
3089+ }
3090+
3091+ /**
3092+ * Dispatch table for X9.62 to PEM encoder.
3093+ */
3094+ const OSSL_DISPATCH wp_ecc_x9_62_pem_encoder_functions [] = {
3095+ { OSSL_FUNC_ENCODER_NEWCTX ,
3096+ (DFUNC )wp_ecc_x9_62_pem_enc_new },
3097+ { OSSL_FUNC_ENCODER_FREECTX , (DFUNC )wp_ecc_enc_dec_free },
3098+ { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS ,
3099+ (DFUNC )wp_ecc_enc_dec_settable_ctx_params },
3100+ { OSSL_FUNC_ENCODER_SET_CTX_PARAMS , (DFUNC )wp_ecc_enc_dec_set_ctx_params },
3101+ { OSSL_FUNC_ENCODER_DOES_SELECTION ,
3102+ (DFUNC )wp_ecc_x9_62_does_selection },
3103+ { OSSL_FUNC_ENCODER_ENCODE , (DFUNC )wp_ecc_encode },
3104+ { OSSL_FUNC_ENCODER_IMPORT_OBJECT , (DFUNC )wp_ecc_import },
3105+ { OSSL_FUNC_ENCODER_FREE_OBJECT , (DFUNC )wp_ecc_free },
3106+ { 0 , NULL }
3107+ };
3108+
29873109#endif /* WP_HAVE_ECC */
0 commit comments