/src/openssl30/crypto/ec/eck_prn.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved | 
| 4 |  |  * | 
| 5 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 6 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 7 |  |  * in the file LICENSE in the source distribution or at | 
| 8 |  |  * https://www.openssl.org/source/license.html | 
| 9 |  |  */ | 
| 10 |  |  | 
| 11 |  | #include "internal/deprecated.h" | 
| 12 |  |  | 
| 13 |  | #include <stdio.h> | 
| 14 |  | #include "internal/cryptlib.h" | 
| 15 |  | #include <openssl/evp.h> | 
| 16 |  | #include <openssl/ec.h> | 
| 17 |  | #include <openssl/bn.h> | 
| 18 |  |  | 
| 19 |  | #ifndef OPENSSL_NO_DEPRECATED_3_0 | 
| 20 |  | # ifndef OPENSSL_NO_STDIO | 
| 21 |  | int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) | 
| 22 | 0 | { | 
| 23 | 0 |     BIO *b; | 
| 24 | 0 |     int ret; | 
| 25 |  | 
 | 
| 26 | 0 |     if ((b = BIO_new(BIO_s_file())) == NULL) { | 
| 27 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BUF_LIB); | 
| 28 | 0 |         return 0; | 
| 29 | 0 |     } | 
| 30 | 0 |     BIO_set_fp(b, fp, BIO_NOCLOSE); | 
| 31 | 0 |     ret = ECPKParameters_print(b, x, off); | 
| 32 | 0 |     BIO_free(b); | 
| 33 | 0 |     return ret; | 
| 34 | 0 | } | 
| 35 |  |  | 
| 36 |  | int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) | 
| 37 | 0 | { | 
| 38 | 0 |     BIO *b; | 
| 39 | 0 |     int ret; | 
| 40 |  | 
 | 
| 41 | 0 |     if ((b = BIO_new(BIO_s_file())) == NULL) { | 
| 42 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB); | 
| 43 | 0 |         return 0; | 
| 44 | 0 |     } | 
| 45 | 0 |     BIO_set_fp(b, fp, BIO_NOCLOSE); | 
| 46 | 0 |     ret = EC_KEY_print(b, x, off); | 
| 47 | 0 |     BIO_free(b); | 
| 48 | 0 |     return ret; | 
| 49 | 0 | } | 
| 50 |  |  | 
| 51 |  | int ECParameters_print_fp(FILE *fp, const EC_KEY *x) | 
| 52 | 0 | { | 
| 53 | 0 |     BIO *b; | 
| 54 | 0 |     int ret; | 
| 55 |  | 
 | 
| 56 | 0 |     if ((b = BIO_new(BIO_s_file())) == NULL) { | 
| 57 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB); | 
| 58 | 0 |         return 0; | 
| 59 | 0 |     } | 
| 60 | 0 |     BIO_set_fp(b, fp, BIO_NOCLOSE); | 
| 61 | 0 |     ret = ECParameters_print(b, x); | 
| 62 | 0 |     BIO_free(b); | 
| 63 | 0 |     return ret; | 
| 64 | 0 | } | 
| 65 |  | #endif /* OPENSSL_NO_STDIO */ | 
| 66 |  |  | 
| 67 |  | static int print_bin(BIO *fp, const char *str, const unsigned char *num, | 
| 68 |  |                      size_t len, int off); | 
| 69 |  |  | 
| 70 |  | int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) | 
| 71 | 952 | { | 
| 72 | 952 |     int ret = 0, reason = ERR_R_BIO_LIB; | 
| 73 | 952 |     BN_CTX *ctx = NULL; | 
| 74 | 952 |     const EC_POINT *point = NULL; | 
| 75 | 952 |     BIGNUM *p = NULL, *a = NULL, *b = NULL; | 
| 76 | 952 |     unsigned char *gen_buf = NULL; | 
| 77 | 952 |     const BIGNUM *order = NULL, *cofactor = NULL; | 
| 78 | 952 |     const unsigned char *seed; | 
| 79 | 952 |     size_t seed_len = 0, gen_buf_len = 0; | 
| 80 |  |  | 
| 81 | 952 |     static const char *gen_compressed = "Generator (compressed):"; | 
| 82 | 952 |     static const char *gen_uncompressed = "Generator (uncompressed):"; | 
| 83 | 952 |     static const char *gen_hybrid = "Generator (hybrid):"; | 
| 84 |  |  | 
| 85 | 952 |     if (!x) { | 
| 86 | 0 |         reason = ERR_R_PASSED_NULL_PARAMETER; | 
| 87 | 0 |         goto err; | 
| 88 | 0 |     } | 
| 89 |  |  | 
| 90 | 952 |     ctx = BN_CTX_new(); | 
| 91 | 952 |     if (ctx == NULL) { | 
| 92 | 0 |         reason = ERR_R_MALLOC_FAILURE; | 
| 93 | 0 |         goto err; | 
| 94 | 0 |     } | 
| 95 |  |  | 
| 96 | 952 |     if (EC_GROUP_get_asn1_flag(x)) { | 
| 97 |  |         /* the curve parameter are given by an asn1 OID */ | 
| 98 | 744 |         int nid; | 
| 99 | 744 |         const char *nname; | 
| 100 |  |  | 
| 101 | 744 |         if (!BIO_indent(bp, off, 128)) | 
| 102 | 0 |             goto err; | 
| 103 |  |  | 
| 104 | 744 |         nid = EC_GROUP_get_curve_name(x); | 
| 105 | 744 |         if (nid == 0) | 
| 106 | 0 |             goto err; | 
| 107 | 744 |         if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) | 
| 108 | 0 |             goto err; | 
| 109 | 744 |         if (BIO_printf(bp, "\n") <= 0) | 
| 110 | 0 |             goto err; | 
| 111 | 744 |         nname = EC_curve_nid2nist(nid); | 
| 112 | 744 |         if (nname) { | 
| 113 | 301 |             if (!BIO_indent(bp, off, 128)) | 
| 114 | 0 |                 goto err; | 
| 115 | 301 |             if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0) | 
| 116 | 0 |                 goto err; | 
| 117 | 301 |         } | 
| 118 | 744 |     } else { | 
| 119 | 208 |         const char *form_str; | 
| 120 |  |         /* explicit parameters */ | 
| 121 | 208 |         int is_char_two = 0; | 
| 122 | 208 |         point_conversion_form_t form; | 
| 123 | 208 |         int tmp_nid = EC_GROUP_get_field_type(x); | 
| 124 |  |  | 
| 125 | 208 |         if (tmp_nid == NID_X9_62_characteristic_two_field) | 
| 126 | 0 |             is_char_two = 1; | 
| 127 |  |  | 
| 128 | 208 |         if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || | 
| 129 | 208 |             (b = BN_new()) == NULL) { | 
| 130 | 0 |             reason = ERR_R_MALLOC_FAILURE; | 
| 131 | 0 |             goto err; | 
| 132 | 0 |         } | 
| 133 |  |  | 
| 134 | 208 |         if (!EC_GROUP_get_curve(x, p, a, b, ctx)) { | 
| 135 | 0 |             reason = ERR_R_EC_LIB; | 
| 136 | 0 |             goto err; | 
| 137 | 0 |         } | 
| 138 |  |  | 
| 139 | 208 |         if ((point = EC_GROUP_get0_generator(x)) == NULL) { | 
| 140 | 0 |             reason = ERR_R_EC_LIB; | 
| 141 | 0 |             goto err; | 
| 142 | 0 |         } | 
| 143 | 208 |         order = EC_GROUP_get0_order(x); | 
| 144 | 208 |         cofactor = EC_GROUP_get0_cofactor(x); | 
| 145 | 208 |         if (order == NULL) { | 
| 146 | 0 |             reason = ERR_R_EC_LIB; | 
| 147 | 0 |             goto err; | 
| 148 | 0 |         } | 
| 149 |  |  | 
| 150 | 208 |         form = EC_GROUP_get_point_conversion_form(x); | 
| 151 |  |  | 
| 152 | 208 |         gen_buf_len = EC_POINT_point2buf(x, point, form, &gen_buf, ctx); | 
| 153 | 208 |         if (gen_buf_len == 0) { | 
| 154 | 0 |             reason = ERR_R_EC_LIB; | 
| 155 | 0 |             goto err; | 
| 156 | 0 |         } | 
| 157 |  |  | 
| 158 | 208 |         if ((seed = EC_GROUP_get0_seed(x)) != NULL) | 
| 159 | 52 |             seed_len = EC_GROUP_get_seed_len(x); | 
| 160 |  |  | 
| 161 | 208 |         if (!BIO_indent(bp, off, 128)) | 
| 162 | 0 |             goto err; | 
| 163 |  |  | 
| 164 |  |         /* print the 'short name' of the field type */ | 
| 165 | 208 |         if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) | 
| 166 | 208 |             <= 0) | 
| 167 | 0 |             goto err; | 
| 168 |  |  | 
| 169 | 208 |         if (is_char_two) { | 
| 170 |  |             /* print the 'short name' of the base type OID */ | 
| 171 | 0 |             int basis_type = EC_GROUP_get_basis_type(x); | 
| 172 | 0 |             if (basis_type == 0) | 
| 173 | 0 |                 goto err; | 
| 174 |  |  | 
| 175 | 0 |             if (!BIO_indent(bp, off, 128)) | 
| 176 | 0 |                 goto err; | 
| 177 |  |  | 
| 178 | 0 |             if (BIO_printf(bp, "Basis Type: %s\n", | 
| 179 | 0 |                            OBJ_nid2sn(basis_type)) <= 0) | 
| 180 | 0 |                 goto err; | 
| 181 |  |  | 
| 182 |  |             /* print the polynomial */ | 
| 183 | 0 |             if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL, | 
| 184 | 0 |                                               off)) | 
| 185 | 0 |                 goto err; | 
| 186 | 208 |         } else { | 
| 187 | 208 |             if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off)) | 
| 188 | 0 |                 goto err; | 
| 189 | 208 |         } | 
| 190 | 208 |         if ((a != NULL) && !ASN1_bn_print(bp, "A:   ", a, NULL, off)) | 
| 191 | 0 |             goto err; | 
| 192 | 208 |         if ((b != NULL) && !ASN1_bn_print(bp, "B:   ", b, NULL, off)) | 
| 193 | 0 |             goto err; | 
| 194 |  |  | 
| 195 | 208 |         if (form == POINT_CONVERSION_COMPRESSED) | 
| 196 | 194 |             form_str = gen_compressed; | 
| 197 | 14 |         else if (form == POINT_CONVERSION_UNCOMPRESSED) | 
| 198 | 8 |             form_str = gen_uncompressed; | 
| 199 | 6 |         else | 
| 200 | 6 |             form_str = gen_hybrid; | 
| 201 | 208 |         if (gen_buf != NULL | 
| 202 | 208 |             && !print_bin(bp, form_str, gen_buf, gen_buf_len, off)) | 
| 203 | 0 |             goto err; | 
| 204 |  |  | 
| 205 | 208 |         if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, NULL, off)) | 
| 206 | 0 |             goto err; | 
| 207 | 208 |         if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, | 
| 208 | 208 |                                                  NULL, off)) | 
| 209 | 0 |             goto err; | 
| 210 | 208 |         if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) | 
| 211 | 0 |             goto err; | 
| 212 | 208 |     } | 
| 213 | 952 |     ret = 1; | 
| 214 | 952 |  err: | 
| 215 | 952 |     if (!ret) | 
| 216 | 952 |         ERR_raise(ERR_LIB_EC, reason); | 
| 217 | 952 |     BN_free(p); | 
| 218 | 952 |     BN_free(a); | 
| 219 | 952 |     BN_free(b); | 
| 220 | 952 |     OPENSSL_clear_free(gen_buf, gen_buf_len); | 
| 221 | 952 |     BN_CTX_free(ctx); | 
| 222 | 952 |     return ret; | 
| 223 | 952 | } | 
| 224 |  |  | 
| 225 |  | static int print_bin(BIO *fp, const char *name, const unsigned char *buf, | 
| 226 |  |                      size_t len, int off) | 
| 227 | 294 | { | 
| 228 | 294 |     size_t i; | 
| 229 | 294 |     char str[128 + 1 + 4]; | 
| 230 |  |  | 
| 231 | 294 |     if (buf == NULL) | 
| 232 | 0 |         return 1; | 
| 233 | 294 |     if (off > 0) { | 
| 234 | 62 |         if (off > 128) | 
| 235 | 0 |             off = 128; | 
| 236 | 62 |         memset(str, ' ', off); | 
| 237 | 62 |         if (BIO_write(fp, str, off) <= 0) | 
| 238 | 0 |             return 0; | 
| 239 | 232 |     } else { | 
| 240 | 232 |         off = 0; | 
| 241 | 232 |     } | 
| 242 |  |  | 
| 243 | 294 |     if (BIO_printf(fp, "%s", name) <= 0) | 
| 244 | 0 |         return 0; | 
| 245 |  |  | 
| 246 | 8.20k |     for (i = 0; i < len; i++) { | 
| 247 | 7.91k |         if ((i % 15) == 0) { | 
| 248 | 603 |             str[0] = '\n'; | 
| 249 | 603 |             memset(&(str[1]), ' ', off + 4); | 
| 250 | 603 |             if (BIO_write(fp, str, off + 1 + 4) <= 0) | 
| 251 | 0 |                 return 0; | 
| 252 | 603 |         } | 
| 253 | 7.91k |         if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= | 
| 254 | 7.91k |             0) | 
| 255 | 0 |             return 0; | 
| 256 | 7.91k |     } | 
| 257 | 294 |     if (BIO_write(fp, "\n", 1) <= 0) | 
| 258 | 0 |         return 0; | 
| 259 |  |  | 
| 260 | 294 |     return 1; | 
| 261 | 294 | } | 
| 262 |  | #endif /* OPENSSL_NO_DEPRECATED_3_0 */ |